blob: acd20c549ce20ad05ff07c3e3cf529c71155615f [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;
Trond Myklebust03391692010-01-26 15:42:38 -0500387#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400388 case -NFS4ERR_BADSESSION:
389 case -NFS4ERR_BADSLOT:
390 case -NFS4ERR_BAD_HIGH_SLOT:
391 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
392 case -NFS4ERR_DEADSESSION:
393 case -NFS4ERR_SEQ_FALSE_RETRY:
394 case -NFS4ERR_SEQ_MISORDERED:
395 dprintk("%s ERROR: %d Reset session\n", __func__,
396 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400397 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400398 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500399#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500400 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500401 if (exception->timeout > HZ) {
402 /* We have retried a decent amount, time to
403 * fail
404 */
405 ret = -EBUSY;
406 break;
407 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500408 case -NFS4ERR_GRACE:
409 case -NFS4ERR_DELAY:
410 ret = nfs4_delay(server->client, &exception->timeout);
411 if (ret != 0)
412 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400413 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500414 case -NFS4ERR_OLD_STATEID:
415 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800416 break;
417 case -NFS4ERR_BADOWNER:
418 /* The following works around a Linux server bug! */
419 case -NFS4ERR_BADNAME:
420 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
421 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
422 exception->retry = 1;
423 printk(KERN_WARNING "NFS: v4 server %s "
424 "does not accept raw "
425 "uid/gids. "
426 "Reenabling the idmapper.\n",
427 server->nfs_client->cl_hostname);
428 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500429 }
430 /* We failed to handle the error */
431 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500432wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500433 ret = nfs4_wait_clnt_recover(clp);
434 if (ret == 0)
435 exception->retry = 1;
436 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500437}
438
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400439/*
440 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
441 * or 'false' otherwise.
442 */
443static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
444{
445 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
446
447 if (flavor == RPC_AUTH_GSS_KRB5I ||
448 flavor == RPC_AUTH_GSS_KRB5P)
449 return true;
450
451 return false;
452}
Trond Myklebust65de8722008-12-23 15:21:44 -0500453
Trond Myklebust452e9352010-07-31 14:29:06 -0400454static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 spin_lock(&clp->cl_lock);
457 if (time_before(clp->cl_last_renewal,timestamp))
458 clp->cl_last_renewal = timestamp;
459 spin_unlock(&clp->cl_lock);
460}
461
Trond Myklebust452e9352010-07-31 14:29:06 -0400462static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
463{
464 do_renew_lease(server->nfs_client, timestamp);
465}
466
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400467struct nfs4_call_sync_data {
468 const struct nfs_server *seq_server;
469 struct nfs4_sequence_args *seq_args;
470 struct nfs4_sequence_res *seq_res;
471};
472
Chuck Levera9c92d62013-08-09 12:48:18 -0400473static void nfs4_init_sequence(struct nfs4_sequence_args *args,
474 struct nfs4_sequence_res *res, int cache_reply)
475{
476 args->sa_slot = NULL;
477 args->sa_cache_this = cache_reply;
478 args->sa_privileged = 0;
479
480 res->sr_slot = NULL;
481}
482
483static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
484{
485 args->sa_privileged = 1;
486}
487
Chuck Lever3bd23842013-08-09 12:49:19 -0400488static int nfs40_setup_sequence(const struct nfs_server *server,
489 struct nfs4_sequence_args *args,
490 struct nfs4_sequence_res *res,
491 struct rpc_task *task)
492{
493 struct nfs4_slot_table *tbl = server->nfs_client->cl_slot_tbl;
494 struct nfs4_slot *slot;
495
496 /* slot already allocated? */
497 if (res->sr_slot != NULL)
498 goto out_start;
499
500 spin_lock(&tbl->slot_tbl_lock);
501 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
502 goto out_sleep;
503
504 slot = nfs4_alloc_slot(tbl);
505 if (IS_ERR(slot)) {
506 if (slot == ERR_PTR(-ENOMEM))
507 task->tk_timeout = HZ >> 2;
508 goto out_sleep;
509 }
510 spin_unlock(&tbl->slot_tbl_lock);
511
512 args->sa_slot = slot;
513 res->sr_slot = slot;
514
515out_start:
516 rpc_call_start(task);
517 return 0;
518
519out_sleep:
520 if (args->sa_privileged)
521 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
522 NULL, RPC_PRIORITY_PRIVILEGED);
523 else
524 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
525 spin_unlock(&tbl->slot_tbl_lock);
526 return -EAGAIN;
527}
528
529static int nfs40_sequence_done(struct rpc_task *task,
530 struct nfs4_sequence_res *res)
531{
532 struct nfs4_slot *slot = res->sr_slot;
533 struct nfs4_slot_table *tbl;
534
535 if (!RPC_WAS_SENT(task))
536 goto out;
537
538 tbl = slot->table;
539 spin_lock(&tbl->slot_tbl_lock);
540 if (!nfs41_wake_and_assign_slot(tbl, slot))
541 nfs4_free_slot(tbl, slot);
542 spin_unlock(&tbl->slot_tbl_lock);
543
544 res->sr_slot = NULL;
545out:
546 return 1;
547}
548
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400549#if defined(CONFIG_NFS_V4_1)
550
Trond Myklebustd185a332010-06-16 09:52:25 -0400551static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400552{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500553 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400554 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500555 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400556
Benny Halevydfb4f3092010-09-24 09:17:01 -0400557 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400558 /* just wake up the next guy waiting since
559 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500560 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500561 return;
Andy Adamson13615872009-04-01 09:22:17 -0400562 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500563 tbl = res->sr_slot->table;
564 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500565
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500566 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500567 /* Be nice to the server: try to ensure that the last transmitted
568 * value for highest_user_slotid <= target_highest_slotid
569 */
570 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
571 send_new_highest_used_slotid = true;
572
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500573 if (nfs41_wake_and_assign_slot(tbl, res->sr_slot)) {
574 send_new_highest_used_slotid = false;
575 goto out_unlock;
576 }
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500577 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500578
579 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
580 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500581out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500582 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400583 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500584 if (send_new_highest_used_slotid)
585 nfs41_server_notify_highest_slotid_update(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400586}
587
Trond Myklebust14516c32010-07-31 14:29:06 -0400588static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400589{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500590 struct nfs4_session *session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500591 struct nfs4_slot *slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400592 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500593 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500594 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400595
Bryan Schumaker468f8612011-04-18 15:57:32 -0400596 /* don't increment the sequence number if the task wasn't sent */
597 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400598 goto out;
599
Trond Myklebust933602e2012-11-16 12:12:38 -0500600 slot = res->sr_slot;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500601 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500602
Trond Myklebustac20d162012-12-15 15:36:07 -0500603 if (slot->interrupted) {
604 slot->interrupted = 0;
605 interrupted = true;
606 }
607
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400608 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500609 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400610 switch (res->sr_status) {
611 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400612 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500613 ++slot->seq_nr;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500614 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500615 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500616 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500617 if (res->sr_status_flags != 0)
618 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500619 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400620 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500621 case 1:
622 /*
623 * sr_status remains 1 if an RPC level error occurred.
624 * The server may or may not have processed the sequence
625 * operation..
626 * Mark the slot as having hosted an interrupted RPC call.
627 */
628 slot->interrupted = 1;
629 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400630 case -NFS4ERR_DELAY:
631 /* The server detected a resend of the RPC call and
632 * returned NFS4ERR_DELAY as per Section 2.10.6.2
633 * of RFC5661.
634 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500635 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400636 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500637 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500638 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400639 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500640 case -NFS4ERR_BADSLOT:
641 /*
642 * The slot id we used was probably retired. Try again
643 * using a different slot id.
644 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500645 goto retry_nowait;
646 case -NFS4ERR_SEQ_MISORDERED:
647 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500648 * Was the last operation on this sequence interrupted?
649 * If so, retry after bumping the sequence number.
650 */
651 if (interrupted) {
652 ++slot->seq_nr;
653 goto retry_nowait;
654 }
655 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500656 * Could this slot have been previously retired?
657 * If so, then the server may be expecting seq_nr = 1!
658 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500659 if (slot->seq_nr != 1) {
660 slot->seq_nr = 1;
661 goto retry_nowait;
662 }
663 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500664 case -NFS4ERR_SEQ_FALSE_RETRY:
665 ++slot->seq_nr;
666 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400667 default:
668 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500669 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400670 }
671out:
672 /* The session may be reset by one of the error handlers. */
673 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400674 nfs41_sequence_free_slot(res);
Trond Myklebust85563072012-12-11 10:31:12 -0500675 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500676retry_nowait:
677 if (rpc_restart_call_prepare(task)) {
678 task->tk_status = 0;
679 ret = 0;
680 }
681 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400682out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400683 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400684 goto out;
685 rpc_delay(task, NFS4_POLL_RETRY_MAX);
686 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400687}
688
Trond Myklebust14516c32010-07-31 14:29:06 -0400689static int nfs4_sequence_done(struct rpc_task *task,
690 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400691{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500692 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400693 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400694 if (!res->sr_slot->table->session)
695 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400696 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400697}
698
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000699int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400700 struct nfs4_sequence_args *args,
701 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400702 struct rpc_task *task)
703{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400704 struct nfs4_slot *slot;
705 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400706
707 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400708 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400709 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400710 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400711
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400712 tbl = &session->fc_slot_table;
713
Trond Myklebust69d206b2012-11-22 13:21:02 -0500714 task->tk_timeout = 0;
715
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400716 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400717 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400718 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500719 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500720 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400721 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400722 }
723
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500724 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500725 if (IS_ERR(slot)) {
726 /* If out of memory, try again in 1/4 second */
727 if (slot == ERR_PTR(-ENOMEM))
728 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400729 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400730 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400731 }
732 spin_unlock(&tbl->slot_tbl_lock);
733
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500734 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400735
Chuck Levere8d92382013-08-09 12:47:51 -0400736 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500737 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400738
Benny Halevydfb4f3092010-09-24 09:17:01 -0400739 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500740 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400741 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400742 /*
743 * sr_status is only set in decode_sequence, and so will remain
744 * set to 1 if an rpc level failure occurs.
745 */
746 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400747 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400748out_success:
749 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400750 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400751out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400752 /* Privileged tasks are queued with top priority */
753 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400754 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
755 NULL, RPC_PRIORITY_PRIVILEGED);
756 else
757 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400758 spin_unlock(&tbl->slot_tbl_lock);
759 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400760}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000761EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400762
Chuck Lever5a580e02013-08-09 12:48:09 -0400763static int nfs4_setup_sequence(const struct nfs_server *server,
764 struct nfs4_sequence_args *args,
765 struct nfs4_sequence_res *res,
766 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400767{
Trond Myklebust035168a2010-06-16 09:52:26 -0400768 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400769 int ret = 0;
770
Chuck Lever3bd23842013-08-09 12:49:19 -0400771 if (!session)
772 return nfs40_setup_sequence(server, args, res, task);
Trond Myklebust035168a2010-06-16 09:52:26 -0400773
Chuck Levere8d92382013-08-09 12:47:51 -0400774 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400775 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400776 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168a2010-06-16 09:52:26 -0400777
Trond Myklebust9d12b212012-01-17 22:04:25 -0500778 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400779
Andy Adamsonce5039c2009-04-01 09:22:13 -0400780 dprintk("<-- %s status=%d\n", __func__, ret);
781 return ret;
782}
783
Andy Adamsonce5039c2009-04-01 09:22:13 -0400784static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
785{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400786 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400787 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400788
Trond Myklebust035168a2010-06-16 09:52:26 -0400789 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
790
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400791 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400792}
793
Andy Adamson69ab40c2009-04-01 09:22:19 -0400794static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
795{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400796 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400797
Trond Myklebust14516c32010-07-31 14:29:06 -0400798 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400799}
800
Trond Myklebust17280172012-03-11 13:11:00 -0400801static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400802 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400803 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400804};
805
Chuck Lever3bd23842013-08-09 12:49:19 -0400806#else /* !CONFIG_NFS_V4_1 */
807
Chuck Lever5a580e02013-08-09 12:48:09 -0400808static int nfs4_setup_sequence(const struct nfs_server *server,
809 struct nfs4_sequence_args *args,
810 struct nfs4_sequence_res *res,
811 struct rpc_task *task)
812{
Chuck Lever3bd23842013-08-09 12:49:19 -0400813 return nfs40_setup_sequence(server, args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -0400814}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400815
Trond Myklebust14516c32010-07-31 14:29:06 -0400816static int nfs4_sequence_done(struct rpc_task *task,
817 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400818{
Chuck Lever3bd23842013-08-09 12:49:19 -0400819 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400820}
Chuck Lever3bd23842013-08-09 12:49:19 -0400821
822#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400823
Chuck Lever9915ea72013-08-09 12:48:27 -0400824static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
825{
826 struct nfs4_call_sync_data *data = calldata;
827 nfs4_setup_sequence(data->seq_server,
828 data->seq_args, data->seq_res, task);
829}
830
831static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
832{
833 struct nfs4_call_sync_data *data = calldata;
834 nfs4_sequence_done(task, data->seq_res);
835}
836
837static const struct rpc_call_ops nfs40_call_sync_ops = {
838 .rpc_call_prepare = nfs40_call_sync_prepare,
839 .rpc_call_done = nfs40_call_sync_done,
840};
841
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400842static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -0400843 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500844 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100845 struct nfs4_sequence_args *args,
846 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -0400847{
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100848 int ret;
849 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -0400850 struct nfs_client *clp = server->nfs_client;
851 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100852 .seq_server = server,
853 .seq_args = args,
854 .seq_res = res,
855 };
856 struct rpc_task_setup task_setup = {
857 .rpc_client = clnt,
858 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -0400859 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100860 .callback_data = &data
861 };
862
863 task = rpc_run_task(&task_setup);
864 if (IS_ERR(task))
865 ret = PTR_ERR(task);
866 else {
867 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -0400868 rpc_put_task(task);
869 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100870 return ret;
871}
872
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400873static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000874int nfs4_call_sync(struct rpc_clnt *clnt,
875 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000876 struct rpc_message *msg,
877 struct nfs4_sequence_args *args,
878 struct nfs4_sequence_res *res,
879 int cache_reply)
880{
Chuck Levera9c92d62013-08-09 12:48:18 -0400881 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -0400882 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000883}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
886{
887 struct nfs_inode *nfsi = NFS_I(dir);
888
889 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400890 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700891 if (!cinfo->atomic || cinfo->before != dir->i_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700893 dir->i_version = cinfo->after;
David Howellsde242c02012-12-20 21:52:38 +0000894 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 spin_unlock(&dir->i_lock);
896}
897
898struct nfs4_opendata {
899 struct kref kref;
900 struct nfs_openargs o_arg;
901 struct nfs_openres o_res;
902 struct nfs_open_confirmargs c_arg;
903 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500904 struct nfs4_string owner_name;
905 struct nfs4_string group_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400907 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400909 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 struct nfs4_state_owner *owner;
911 struct nfs4_state *state;
912 struct iattr attrs;
913 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400914 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -0400915 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -0400916 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400917 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 int cancelled;
919};
Trond Myklebustdecf4912005-10-27 22:12:39 -0400920
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400921static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
922 int err, struct nfs4_exception *exception)
923{
924 if (err != -EINVAL)
925 return false;
926 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
927 return false;
928 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
929 exception->retry = 1;
930 return true;
931}
932
933static enum open_claim_type4
934nfs4_map_atomic_open_claim(struct nfs_server *server,
935 enum open_claim_type4 claim)
936{
937 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
938 return claim;
939 switch (claim) {
940 default:
941 return claim;
942 case NFS4_OPEN_CLAIM_FH:
943 return NFS4_OPEN_CLAIM_NULL;
944 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
945 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
946 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
947 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
948 }
949}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100952{
953 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400954 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100955 p->o_res.seqid = p->o_arg.seqid;
956 p->c_res.seqid = p->c_arg.seqid;
957 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400958 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100959 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500960 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100961}
962
Al Viro82a2c1b2011-06-22 18:30:55 -0400963static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100964 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400965 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -0400966 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400967 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400968 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100969{
Al Viro82a2c1b2011-06-22 18:30:55 -0400970 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100971 struct inode *dir = parent->d_inode;
972 struct nfs_server *server = NFS_SERVER(dir);
973 struct nfs4_opendata *p;
974
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400975 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100976 if (p == NULL)
977 goto err;
David Quigley14c43f72013-05-22 12:50:43 -0400978
979 p->f_label = nfs4_label_alloc(server, gfp_mask);
980 if (IS_ERR(p->f_label))
981 goto err_free_p;
982
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400983 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100984 if (p->o_arg.seqid == NULL)
David Quigley14c43f72013-05-22 12:50:43 -0400985 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -0400986 nfs_sb_active(dentry->d_sb);
987 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100988 p->dir = parent;
989 p->owner = sp;
990 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500991 p->o_arg.open_flags = flags;
992 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700993 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
994 * will return permission denied for all bits until close */
995 if (!(flags & O_EXCL)) {
996 /* ask server to check for all possible rights as results
997 * are cached */
998 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
999 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001000 }
David Howells7539bba2006-08-22 20:06:09 -04001001 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001002 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1003 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001004 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001005 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001006 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001007 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
David Quigley1775fd32013-05-22 12:50:42 -04001008 p->o_arg.label = label;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001009 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1010 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001011 case NFS4_OPEN_CLAIM_NULL:
1012 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1013 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1014 p->o_arg.fh = NFS_FH(dir);
1015 break;
1016 case NFS4_OPEN_CLAIM_PREVIOUS:
1017 case NFS4_OPEN_CLAIM_FH:
1018 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1019 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1020 p->o_arg.fh = NFS_FH(dentry->d_inode);
1021 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001022 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001023 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001024
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001025 p->o_arg.u.attrs = &p->attrs;
1026 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001027
1028 verf[0] = jiffies;
1029 verf[1] = current->pid;
1030 memcpy(p->o_arg.u.verifier.data, verf,
1031 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001032 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001033 p->c_arg.fh = &p->o_res.fh;
1034 p->c_arg.stateid = &p->o_res.stateid;
1035 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001036 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001037 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001038 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001039
1040err_free_label:
1041 nfs4_label_free(p->f_label);
1042err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001043 kfree(p);
1044err:
1045 dput(parent);
1046 return NULL;
1047}
1048
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001049static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001050{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001051 struct nfs4_opendata *p = container_of(kref,
1052 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001053 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001054
1055 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001056 if (p->state != NULL)
1057 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001058 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001059
1060 nfs4_label_free(p->f_label);
1061
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001062 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001063 dput(p->dentry);
1064 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001065 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001066 kfree(p);
1067}
1068
1069static void nfs4_opendata_put(struct nfs4_opendata *p)
1070{
1071 if (p != NULL)
1072 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001073}
1074
Trond Myklebust06f814a2006-01-03 09:55:07 +01001075static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1076{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001077 int ret;
1078
Trond Myklebust06f814a2006-01-03 09:55:07 +01001079 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001080 return ret;
1081}
1082
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001083static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001084{
1085 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001086
Trond Myklebust536e43d2012-01-17 22:04:26 -05001087 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001088 goto out;
1089 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001090 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001091 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1092 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001093 break;
1094 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001095 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1096 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001097 break;
1098 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001099 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1100 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001101 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001102out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001103 return ret;
1104}
1105
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001106static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001107{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001108 if (delegation == NULL)
1109 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001110 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001111 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -05001112 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -04001113 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001114 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1115 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -05001116 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001117 return 1;
1118}
1119
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001120static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001121{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001122 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001123 case FMODE_WRITE:
1124 state->n_wronly++;
1125 break;
1126 case FMODE_READ:
1127 state->n_rdonly++;
1128 break;
1129 case FMODE_READ|FMODE_WRITE:
1130 state->n_rdwr++;
1131 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001132 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001133}
1134
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001135static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001136{
1137 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001138 nfs4_stateid_copy(&state->stateid, stateid);
1139 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust92b40e92013-04-20 01:25:45 -04001140 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001141 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001142 case FMODE_READ:
1143 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1144 break;
1145 case FMODE_WRITE:
1146 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1147 break;
1148 case FMODE_READ|FMODE_WRITE:
1149 set_bit(NFS_O_RDWR_STATE, &state->flags);
1150 }
1151}
1152
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001153static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001154{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001155 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001156 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001157 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001158}
1159
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001160static 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 -07001161{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001162 /*
1163 * Protect the call to nfs4_state_set_mode_locked and
1164 * serialise the stateid update
1165 */
1166 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001167 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001168 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001169 set_bit(NFS_DELEGATED_STATE, &state->flags);
1170 }
1171 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001172 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001173 write_sequnlock(&state->seqlock);
1174 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001175 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001176 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177}
1178
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001179static 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 -05001180{
1181 struct nfs_inode *nfsi = NFS_I(state->inode);
1182 struct nfs_delegation *deleg_cur;
1183 int ret = 0;
1184
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001185 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001186
1187 rcu_read_lock();
1188 deleg_cur = rcu_dereference(nfsi->delegation);
1189 if (deleg_cur == NULL)
1190 goto no_delegation;
1191
1192 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001193 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001194 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001195 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001196 goto no_delegation_unlock;
1197
1198 if (delegation == NULL)
1199 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001200 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001201 goto no_delegation_unlock;
1202
Trond Myklebustb7391f42008-12-23 15:21:52 -05001203 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001204 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001205 ret = 1;
1206no_delegation_unlock:
1207 spin_unlock(&deleg_cur->lock);
1208no_delegation:
1209 rcu_read_unlock();
1210
1211 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001212 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001213 ret = 1;
1214 }
1215
1216 return ret;
1217}
1218
1219
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001220static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001221{
1222 struct nfs_delegation *delegation;
1223
1224 rcu_read_lock();
1225 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001226 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001227 rcu_read_unlock();
1228 return;
1229 }
1230 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001231 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001232}
1233
Trond Myklebust6ee41262007-07-08 14:11:36 -04001234static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001235{
1236 struct nfs4_state *state = opendata->state;
1237 struct nfs_inode *nfsi = NFS_I(state->inode);
1238 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001239 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001240 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001241 nfs4_stateid stateid;
1242 int ret = -EAGAIN;
1243
Trond Myklebustaac00a82007-07-05 19:02:21 -04001244 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001245 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001246 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001247 if (can_open_cached(state, fmode, open_mode)) {
1248 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001249 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001250 goto out_return_state;
1251 }
1252 spin_unlock(&state->owner->so_lock);
1253 }
Trond Myklebust34310432008-12-23 15:21:38 -05001254 rcu_read_lock();
1255 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001256 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001257 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001258 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001259 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001260 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001261 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001262 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001263 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001264 if (!opendata->is_recover) {
1265 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1266 if (ret != 0)
1267 goto out;
1268 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001269 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001270
1271 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001272 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001273 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001274 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001275out:
1276 return ERR_PTR(ret);
1277out_return_state:
1278 atomic_inc(&state->count);
1279 return state;
1280}
1281
Andy Adamsone23008e2012-10-02 21:07:32 -04001282static void
1283nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1284{
1285 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1286 struct nfs_delegation *delegation;
1287 int delegation_flags = 0;
1288
1289 rcu_read_lock();
1290 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1291 if (delegation)
1292 delegation_flags = delegation->flags;
1293 rcu_read_unlock();
1294 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1295 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1296 "returning a delegation for "
1297 "OPEN(CLAIM_DELEGATE_CUR)\n",
1298 clp->cl_hostname);
1299 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1300 nfs_inode_set_delegation(state->inode,
1301 data->owner->so_cred,
1302 &data->o_res);
1303 else
1304 nfs_inode_reclaim_delegation(state->inode,
1305 data->owner->so_cred,
1306 &data->o_res);
1307}
1308
1309/*
1310 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1311 * and update the nfs4_state.
1312 */
1313static struct nfs4_state *
1314_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1315{
1316 struct inode *inode = data->state->inode;
1317 struct nfs4_state *state = data->state;
1318 int ret;
1319
Weston Andros Adamsonf494a602013-10-21 13:10:10 -04001320 /* allow cached opens (!rpc_done && !rpc_status) */
1321 if (!data->rpc_done && data->rpc_status) {
Andy Adamsone23008e2012-10-02 21:07:32 -04001322 ret = data->rpc_status;
1323 goto err;
1324 }
1325
1326 ret = -ESTALE;
1327 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1328 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1329 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1330 goto err;
1331
1332 ret = -ENOMEM;
1333 state = nfs4_get_open_state(inode, data->owner);
1334 if (state == NULL)
1335 goto err;
1336
1337 ret = nfs_refresh_inode(inode, &data->f_attr);
1338 if (ret)
1339 goto err;
1340
1341 if (data->o_res.delegation_type != 0)
1342 nfs4_opendata_check_deleg(data, state);
1343 update_open_stateid(state, &data->o_res.stateid, NULL,
1344 data->o_arg.fmode);
1345
1346 return state;
1347err:
1348 return ERR_PTR(ret);
1349
1350}
1351
1352static struct nfs4_state *
1353_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001354{
1355 struct inode *inode;
1356 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001357 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001358
Trond Myklebustaac00a82007-07-05 19:02:21 -04001359 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001360 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001361 goto out;
1362 }
1363
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001364 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001365 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001366 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001367 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001368 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001369 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001370 goto err;
1371 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001372 state = nfs4_get_open_state(inode, data->owner);
1373 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001374 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001375 if (data->o_res.delegation_type != 0)
1376 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001377 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001378 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001379 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001380out:
Trond Myklebust7aa262b2013-02-28 16:19:59 -08001381 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001382 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001383err_put_inode:
1384 iput(inode);
1385err:
1386 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001387}
1388
Andy Adamsone23008e2012-10-02 21:07:32 -04001389static struct nfs4_state *
1390nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1391{
1392 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1393 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1394 return _nfs4_opendata_to_nfs4_state(data);
1395}
1396
Trond Myklebust864472e2006-01-03 09:55:15 +01001397static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1398{
1399 struct nfs_inode *nfsi = NFS_I(state->inode);
1400 struct nfs_open_context *ctx;
1401
1402 spin_lock(&state->inode->i_lock);
1403 list_for_each_entry(ctx, &nfsi->open_files, list) {
1404 if (ctx->state != state)
1405 continue;
1406 get_nfs_open_context(ctx);
1407 spin_unlock(&state->inode->i_lock);
1408 return ctx;
1409 }
1410 spin_unlock(&state->inode->i_lock);
1411 return ERR_PTR(-ENOENT);
1412}
1413
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001414static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1415 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001416{
1417 struct nfs4_opendata *opendata;
1418
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001419 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001420 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001421 if (opendata == NULL)
1422 return ERR_PTR(-ENOMEM);
1423 opendata->state = state;
1424 atomic_inc(&state->count);
1425 return opendata;
1426}
1427
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001428static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001429{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001430 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001431 int ret;
1432
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001433 opendata->o_arg.open_flags = 0;
1434 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001435 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1436 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1437 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001438 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001439 if (ret != 0)
1440 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001441 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001442 if (IS_ERR(newstate))
1443 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001444 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001445 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001446 return 0;
1447}
1448
1449static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1450{
Trond Myklebust864472e2006-01-03 09:55:15 +01001451 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001452 int ret;
1453
1454 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001455 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001456 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001457 smp_rmb();
1458 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001459 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001460 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001461 if (ret != 0)
1462 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001463 if (newstate != state)
1464 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001465 }
1466 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001467 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001468 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001469 if (ret != 0)
1470 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001471 if (newstate != state)
1472 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001473 }
1474 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001475 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001476 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001477 if (ret != 0)
1478 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001479 if (newstate != state)
1480 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001481 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001482 /*
1483 * We may have performed cached opens for all three recoveries.
1484 * Check if we need to update the current stateid.
1485 */
1486 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001487 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001488 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001489 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001490 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001491 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001492 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001493 return 0;
1494}
1495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496/*
1497 * OPEN_RECLAIM:
1498 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001500static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001502 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001503 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001504 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 int status;
1506
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001507 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1508 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001509 if (IS_ERR(opendata))
1510 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001511 rcu_read_lock();
1512 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001513 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001514 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001515 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001516 opendata->o_arg.u.delegation_type = delegation_type;
1517 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001518 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 return status;
1520}
1521
Trond Myklebust539cd032007-06-05 11:46:42 -04001522static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523{
1524 struct nfs_server *server = NFS_SERVER(state->inode);
1525 struct nfs4_exception exception = { };
1526 int err;
1527 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001528 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001529 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001530 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1531 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001532 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001533 break;
1534 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 } while (exception.retry);
1536 return err;
1537}
1538
Trond Myklebust864472e2006-01-03 09:55:15 +01001539static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1540{
1541 struct nfs_open_context *ctx;
1542 int ret;
1543
1544 ctx = nfs4_state_find_open_context(state);
1545 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001546 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001547 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001548 put_nfs_open_context(ctx);
1549 return ret;
1550}
1551
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001552static 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 -07001553{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001554 switch (err) {
1555 default:
1556 printk(KERN_ERR "NFS: %s: unhandled error "
1557 "%d.\n", __func__, err);
1558 case 0:
1559 case -ENOENT:
1560 case -ESTALE:
1561 break;
1562 case -NFS4ERR_BADSESSION:
1563 case -NFS4ERR_BADSLOT:
1564 case -NFS4ERR_BAD_HIGH_SLOT:
1565 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1566 case -NFS4ERR_DEADSESSION:
1567 set_bit(NFS_DELEGATED_STATE, &state->flags);
1568 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1569 return -EAGAIN;
1570 case -NFS4ERR_STALE_CLIENTID:
1571 case -NFS4ERR_STALE_STATEID:
1572 set_bit(NFS_DELEGATED_STATE, &state->flags);
1573 case -NFS4ERR_EXPIRED:
1574 /* Don't recall a delegation if it was lost */
1575 nfs4_schedule_lease_recovery(server->nfs_client);
1576 return -EAGAIN;
1577 case -NFS4ERR_DELEG_REVOKED:
1578 case -NFS4ERR_ADMIN_REVOKED:
1579 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001580 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001581 nfs_inode_find_state_and_recover(state->inode,
1582 stateid);
1583 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001584 return 0;
1585 case -NFS4ERR_DELAY:
1586 case -NFS4ERR_GRACE:
1587 set_bit(NFS_DELEGATED_STATE, &state->flags);
1588 ssleep(1);
1589 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001590 case -ENOMEM:
1591 case -NFS4ERR_DENIED:
1592 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1593 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 return err;
1596}
1597
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001598int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1599{
1600 struct nfs_server *server = NFS_SERVER(state->inode);
1601 struct nfs4_opendata *opendata;
1602 int err;
1603
1604 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1605 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1606 if (IS_ERR(opendata))
1607 return PTR_ERR(opendata);
1608 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1609 err = nfs4_open_recover(opendata, state);
1610 nfs4_opendata_put(opendata);
1611 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1612}
1613
Chuck Leverbe05c862013-08-09 12:49:47 -04001614static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1615{
1616 struct nfs4_opendata *data = calldata;
1617
1618 nfs40_setup_sequence(data->o_arg.server, &data->o_arg.seq_args,
1619 &data->o_res.seq_res, task);
1620}
1621
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001622static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1623{
1624 struct nfs4_opendata *data = calldata;
1625
Chuck Leverbe05c862013-08-09 12:49:47 -04001626 nfs40_sequence_done(task, &data->o_res.seq_res);
1627
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001628 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001629 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001630 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001631 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001632 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001633 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001634 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001635}
1636
1637static void nfs4_open_confirm_release(void *calldata)
1638{
1639 struct nfs4_opendata *data = calldata;
1640 struct nfs4_state *state = NULL;
1641
1642 /* If this request hasn't been cancelled, do nothing */
1643 if (data->cancelled == 0)
1644 goto out_free;
1645 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001646 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001647 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001648 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001649 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001650 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001651out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001652 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001653}
1654
1655static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04001656 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001657 .rpc_call_done = nfs4_open_confirm_done,
1658 .rpc_release = nfs4_open_confirm_release,
1659};
1660
1661/*
1662 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1663 */
1664static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1665{
1666 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1667 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001668 struct rpc_message msg = {
1669 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1670 .rpc_argp = &data->c_arg,
1671 .rpc_resp = &data->c_res,
1672 .rpc_cred = data->owner->so_cred,
1673 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001674 struct rpc_task_setup task_setup_data = {
1675 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001676 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001677 .callback_ops = &nfs4_open_confirm_ops,
1678 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001679 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001680 .flags = RPC_TASK_ASYNC,
1681 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 int status;
1683
Chuck Leverbe05c862013-08-09 12:49:47 -04001684 nfs4_init_sequence(&data->o_arg.seq_args, &data->o_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001685 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001686 data->rpc_done = 0;
1687 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001688 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001689 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001690 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001691 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001692 status = nfs4_wait_for_completion_rpc_task(task);
1693 if (status != 0) {
1694 data->cancelled = 1;
1695 smp_wmb();
1696 } else
1697 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001698 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 return status;
1700}
1701
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001702static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001704 struct nfs4_opendata *data = calldata;
1705 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001706 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001707
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001708 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001709 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001710 /*
1711 * Check if we still need to send an OPEN call, or if we can use
1712 * a delegation instead.
1713 */
1714 if (data->state != NULL) {
1715 struct nfs_delegation *delegation;
1716
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001717 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001718 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001719 rcu_read_lock();
1720 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001721 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
Trond Myklebustcd4c9be2013-04-23 14:46:25 -04001722 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
Trond Myklebust652f89f2011-12-09 19:05:58 -05001723 can_open_delegated(delegation, data->o_arg.fmode))
1724 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001725 rcu_read_unlock();
1726 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001727 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04001728 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust8188df12013-04-23 14:31:19 -04001729 switch (data->o_arg.claim) {
1730 case NFS4_OPEN_CLAIM_PREVIOUS:
1731 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1732 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001733 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04001734 case NFS4_OPEN_CLAIM_FH:
1735 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001736 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1737 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001738 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001739 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001740 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001741 &data->o_res.seq_res,
1742 task) != 0)
1743 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04001744
1745 /* Set the create mode (note dependency on the session type) */
1746 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1747 if (data->o_arg.open_flags & O_EXCL) {
1748 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1749 if (nfs4_has_persistent_session(clp))
1750 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1751 else if (clp->cl_mvops->minor_version > 0)
1752 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1753 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04001754 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001755unlock_no_action:
1756 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001757out_no_action:
1758 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001759out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001760 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001761}
1762
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001763static void nfs4_open_done(struct rpc_task *task, void *calldata)
1764{
1765 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001767 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001768
Trond Myklebust14516c32010-07-31 14:29:06 -04001769 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1770 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001771
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001772 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001773 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1774 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001775 case S_IFREG:
1776 break;
1777 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001778 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001779 break;
1780 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001781 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001782 break;
1783 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001784 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001785 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001786 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001787 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001788 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1789 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001790 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001791 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001792}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001793
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001794static void nfs4_open_release(void *calldata)
1795{
1796 struct nfs4_opendata *data = calldata;
1797 struct nfs4_state *state = NULL;
1798
1799 /* If this request hasn't been cancelled, do nothing */
1800 if (data->cancelled == 0)
1801 goto out_free;
1802 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001803 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001804 goto out_free;
1805 /* In case we need an open_confirm, no cleanup! */
1806 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1807 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001808 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001809 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001810 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001811out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001812 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001813}
1814
1815static const struct rpc_call_ops nfs4_open_ops = {
1816 .rpc_call_prepare = nfs4_open_prepare,
1817 .rpc_call_done = nfs4_open_done,
1818 .rpc_release = nfs4_open_release,
1819};
1820
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001821static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001822{
1823 struct inode *dir = data->dir->d_inode;
1824 struct nfs_server *server = NFS_SERVER(dir);
1825 struct nfs_openargs *o_arg = &data->o_arg;
1826 struct nfs_openres *o_res = &data->o_res;
1827 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001828 struct rpc_message msg = {
1829 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1830 .rpc_argp = o_arg,
1831 .rpc_resp = o_res,
1832 .rpc_cred = data->owner->so_cred,
1833 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001834 struct rpc_task_setup task_setup_data = {
1835 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001836 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001837 .callback_ops = &nfs4_open_ops,
1838 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001839 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001840 .flags = RPC_TASK_ASYNC,
1841 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001842 int status;
1843
Chuck Levera9c92d62013-08-09 12:48:18 -04001844 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001845 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001846 data->rpc_done = 0;
1847 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001848 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001849 data->is_recover = 0;
1850 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001851 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001852 data->is_recover = 1;
1853 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04001854 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001855 if (IS_ERR(task))
1856 return PTR_ERR(task);
1857 status = nfs4_wait_for_completion_rpc_task(task);
1858 if (status != 0) {
1859 data->cancelled = 1;
1860 smp_wmb();
1861 } else
1862 status = data->rpc_status;
1863 rpc_put_task(task);
1864
1865 return status;
1866}
1867
1868static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1869{
1870 struct inode *dir = data->dir->d_inode;
1871 struct nfs_openres *o_res = &data->o_res;
1872 int status;
1873
1874 status = nfs4_run_open_task(data, 1);
1875 if (status != 0 || !data->rpc_done)
1876 return status;
1877
Trond Myklebust6926afd2012-01-07 13:22:46 -05001878 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1879
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001880 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1881 status = _nfs4_proc_open_confirm(data);
1882 if (status != 0)
1883 return status;
1884 }
1885
1886 return status;
1887}
1888
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001889static int nfs4_opendata_access(struct rpc_cred *cred,
1890 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001891 struct nfs4_state *state, fmode_t fmode,
1892 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001893{
1894 struct nfs_access_entry cache;
1895 u32 mask;
1896
1897 /* access call failed or for some reason the server doesn't
1898 * support any access modes -- defer access call until later */
1899 if (opendata->o_res.access_supported == 0)
1900 return 0;
1901
1902 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001903 /* don't check MAY_WRITE - a newly created file may not have
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001904 * write mode bits, but POSIX allows the creating process to write.
1905 * use openflags to check for exec, because fmode won't
1906 * always have FMODE_EXEC set when file open for exec. */
1907 if (openflags & __FMODE_EXEC) {
1908 /* ONLY check for exec rights */
1909 mask = MAY_EXEC;
1910 } else if (fmode & FMODE_READ)
1911 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001912
1913 cache.cred = cred;
1914 cache.jiffies = jiffies;
1915 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1916 nfs_access_add_cache(state->inode, &cache);
1917
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001918 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001919 return 0;
1920
1921 /* even though OPEN succeeded, access is denied. Close the file */
1922 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001923 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001924}
1925
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001926/*
1927 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1928 */
1929static int _nfs4_proc_open(struct nfs4_opendata *data)
1930{
1931 struct inode *dir = data->dir->d_inode;
1932 struct nfs_server *server = NFS_SERVER(dir);
1933 struct nfs_openargs *o_arg = &data->o_arg;
1934 struct nfs_openres *o_res = &data->o_res;
1935 int status;
1936
1937 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001938 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001939 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001940 if (status != 0) {
1941 if (status == -NFS4ERR_BADNAME &&
1942 !(o_arg->open_flags & O_CREAT))
1943 return -ENOENT;
1944 return status;
1945 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001946
Trond Myklebust6926afd2012-01-07 13:22:46 -05001947 nfs_fattr_map_and_free_names(server, &data->f_attr);
1948
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001949 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001950 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001951 if (o_arg->open_flags & O_EXCL)
1952 data->file_created = 1;
1953 else if (o_res->cinfo.before != o_res->cinfo.after)
1954 data->file_created = 1;
1955 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001956 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1957 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001959 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001961 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 }
1963 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
David Quigley1775fd32013-05-22 12:50:42 -04001964 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001965 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966}
1967
Andy Adamsond83217c2011-03-01 01:34:17 +00001968static int nfs4_recover_expired_lease(struct nfs_server *server)
1969{
1970 return nfs4_client_recover_expired_lease(server->nfs_client);
1971}
1972
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973/*
1974 * OPEN_EXPIRED:
1975 * reclaim state on the server after a network partition.
1976 * Assumes caller holds the appropriate lock
1977 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001978static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001980 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001981 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001983 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001984 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001985 if (IS_ERR(opendata))
1986 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001987 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001988 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001989 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001990 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001991 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992}
1993
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001994static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001995{
Trond Myklebust539cd032007-06-05 11:46:42 -04001996 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001997 struct nfs4_exception exception = { };
1998 int err;
1999
2000 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002001 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002002 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002003 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2004 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002005 switch (err) {
2006 default:
2007 goto out;
2008 case -NFS4ERR_GRACE:
2009 case -NFS4ERR_DELAY:
2010 nfs4_handle_exception(server, err, &exception);
2011 err = 0;
2012 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002013 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002014out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002015 return err;
2016}
2017
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2019{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002021 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
Trond Myklebust864472e2006-01-03 09:55:15 +01002023 ctx = nfs4_state_find_open_context(state);
2024 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002025 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002026 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002027 put_nfs_open_context(ctx);
2028 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029}
2030
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002031#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002032static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002033{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002034 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002035 nfs4_stateid *stateid = &state->stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002036 struct nfs_delegation *delegation;
2037 struct rpc_cred *cred = NULL;
2038 int status = -NFS4ERR_BAD_STATEID;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002039
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002040 /* If a state reset has been done, test_stateid is unneeded */
2041 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2042 return;
2043
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002044 /* Get the delegation credential for use by test/free_stateid */
2045 rcu_read_lock();
2046 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2047 if (delegation != NULL &&
2048 nfs4_stateid_match(&delegation->stateid, stateid)) {
2049 cred = get_rpccred(delegation->cred);
2050 rcu_read_unlock();
2051 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002052 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002053 } else
2054 rcu_read_unlock();
2055
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002056 if (status != NFS_OK) {
2057 /* Free the stateid unless the server explicitly
2058 * informs us the stateid is unrecognized. */
2059 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002060 nfs41_free_stateid(server, stateid, cred);
Bryan Schumaker69388672012-09-26 15:25:52 -04002061 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002062
Bryan Schumaker69388672012-09-26 15:25:52 -04002063 write_seqlock(&state->seqlock);
2064 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2065 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002066 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2067 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002068
2069 if (cred != NULL)
2070 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002071}
2072
2073/**
2074 * nfs41_check_open_stateid - possibly free an open stateid
2075 *
2076 * @state: NFSv4 state for an inode
2077 *
2078 * Returns NFS_OK if recovery for this stateid is now finished.
2079 * Otherwise a negative NFS4ERR value is returned.
2080 */
2081static int nfs41_check_open_stateid(struct nfs4_state *state)
2082{
2083 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002084 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002085 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002086 int status;
2087
2088 /* If a state reset has been done, test_stateid is unneeded */
2089 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2090 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2091 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2092 return -NFS4ERR_BAD_STATEID;
2093
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002094 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002095 trace_nfs4_test_open_stateid(state, NULL, status);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002096 if (status != NFS_OK) {
2097 /* Free the stateid unless the server explicitly
2098 * informs us the stateid is unrecognized. */
2099 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002100 nfs41_free_stateid(server, stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002101
2102 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2103 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2104 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002105 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002106 }
2107 return status;
2108}
2109
2110static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2111{
Chuck Levereb64cf92012-07-11 16:30:05 -04002112 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002113
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002114 nfs41_clear_delegation_stateid(state);
2115 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002116 if (status != NFS_OK)
2117 status = nfs4_open_expired(sp, state);
2118 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002119}
2120#endif
2121
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002123 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2124 * fields corresponding to attributes that were used to store the verifier.
2125 * Make sure we clobber those fields in the later setattr call
2126 */
2127static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2128{
2129 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2130 !(sattr->ia_valid & ATTR_ATIME_SET))
2131 sattr->ia_valid |= ATTR_ATIME;
2132
2133 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2134 !(sattr->ia_valid & ATTR_MTIME_SET))
2135 sattr->ia_valid |= ATTR_MTIME;
2136}
2137
Trond Myklebustc21443c2013-02-07 14:26:21 -05002138static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2139 fmode_t fmode,
2140 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002141 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002142{
2143 struct nfs4_state_owner *sp = opendata->owner;
2144 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002145 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002146 struct nfs4_state *state;
2147 unsigned int seq;
2148 int ret;
2149
2150 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2151
2152 ret = _nfs4_proc_open(opendata);
2153 if (ret != 0)
2154 goto out;
2155
2156 state = nfs4_opendata_to_nfs4_state(opendata);
2157 ret = PTR_ERR(state);
2158 if (IS_ERR(state))
2159 goto out;
2160 if (server->caps & NFS_CAP_POSIX_LOCK)
2161 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2162
Trond Myklebust275bb302013-05-29 13:11:28 -04002163 dentry = opendata->dentry;
2164 if (dentry->d_inode == NULL) {
2165 /* FIXME: Is this d_drop() ever needed? */
2166 d_drop(dentry);
2167 dentry = d_add_unique(dentry, igrab(state->inode));
2168 if (dentry == NULL) {
2169 dentry = opendata->dentry;
2170 } else if (dentry != ctx->dentry) {
2171 dput(ctx->dentry);
2172 ctx->dentry = dget(dentry);
2173 }
2174 nfs_set_verifier(dentry,
2175 nfs_save_change_attribute(opendata->dir->d_inode));
2176 }
2177
Trond Myklebustc21443c2013-02-07 14:26:21 -05002178 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2179 if (ret != 0)
2180 goto out;
2181
Trond Myklebust3efb9722013-05-29 13:17:04 -04002182 ctx->state = state;
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002183 if (dentry->d_inode == state->inode) {
2184 nfs_inode_attach_open_context(ctx);
2185 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2186 nfs4_schedule_stateid_recovery(server, state);
2187 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002188out:
2189 return ret;
2190}
2191
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002192/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002193 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 */
Andy Adamson82be4172012-05-23 05:02:35 -04002195static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002196 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002197 int flags,
2198 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002199 struct nfs4_label *label,
2200 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201{
2202 struct nfs4_state_owner *sp;
2203 struct nfs4_state *state = NULL;
2204 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002205 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002206 struct dentry *dentry = ctx->dentry;
2207 struct rpc_cred *cred = ctx->cred;
2208 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2209 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002210 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002211 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002212 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
2214 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002216 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2217 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2219 goto out_err;
2220 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002221 status = nfs4_recover_expired_lease(server);
2222 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002223 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04002224 if (dentry->d_inode != NULL)
2225 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002226 status = -ENOMEM;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002227 if (dentry->d_inode)
2228 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002229 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002230 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002231 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002232 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
David Quigley14c43f72013-05-22 12:50:43 -04002234 if (label) {
2235 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2236 if (IS_ERR(olabel)) {
2237 status = PTR_ERR(olabel);
2238 goto err_opendata_put;
2239 }
2240 }
2241
Andy Adamson82be4172012-05-23 05:02:35 -04002242 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2243 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2244 if (!opendata->f_attr.mdsthreshold)
David Quigley14c43f72013-05-22 12:50:43 -04002245 goto err_free_label;
Trond Myklebust1549210f2012-06-05 09:16:47 -04002246 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002247 }
Al Viro82a2c1b2011-06-22 18:30:55 -04002248 if (dentry->d_inode != NULL)
2249 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002250
Trond Myklebust3efb9722013-05-29 13:17:04 -04002251 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002252 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002253 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002254 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002255
Trond Myklebust549b19c2013-04-16 18:42:34 -04002256 if ((opendata->o_arg.open_flags & O_EXCL) &&
2257 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002258 nfs4_exclusive_attrset(opendata, sattr);
2259
2260 nfs_fattr_init(opendata->o_res.f_attr);
2261 status = nfs4_do_setattr(state->inode, cred,
2262 opendata->o_res.f_attr, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002263 state, label, olabel);
2264 if (status == 0) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002265 nfs_setattr_update_inode(state->inode, sattr);
David Quigley1775fd32013-05-22 12:50:42 -04002266 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
David Quigleyaa9c2662013-05-22 12:50:44 -04002267 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
David Quigley1775fd32013-05-22 12:50:42 -04002268 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002269 }
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002270 if (opendata->file_created)
2271 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002272
2273 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2274 *ctx_th = opendata->f_attr.mdsthreshold;
2275 else
2276 kfree(opendata->f_attr.mdsthreshold);
2277 opendata->f_attr.mdsthreshold = NULL;
2278
David Quigley14c43f72013-05-22 12:50:43 -04002279 nfs4_label_free(olabel);
2280
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002281 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002284err_free_label:
2285 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002286err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002287 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002288 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002289err_put_state_owner:
2290 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 return status;
2293}
2294
2295
Andy Adamson82be4172012-05-23 05:02:35 -04002296static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002297 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002298 int flags,
2299 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002300 struct nfs4_label *label,
2301 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002303 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 struct nfs4_exception exception = { };
2305 struct nfs4_state *res;
2306 int status;
2307
2308 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002309 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002310 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002311 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 if (status == 0)
2313 break;
2314 /* NOTE: BAD_SEQID means the server and client disagree about the
2315 * book-keeping w.r.t. state-changing operations
2316 * (OPEN/CLOSE/LOCK/LOCKU...)
2317 * It is actually a sign of a bug on the client or on the server.
2318 *
2319 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002320 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 * have unhashed the old state_owner for us, and that we can
2322 * therefore safely retry using a new one. We should still warn
2323 * the user though...
2324 */
2325 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002326 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002327 " returned a bad sequence-id error!\n",
2328 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 exception.retry = 1;
2330 continue;
2331 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002332 /*
2333 * BAD_STATEID on OPEN means that the server cancelled our
2334 * state before it received the OPEN_CONFIRM.
2335 * Recover by retrying the request as per the discussion
2336 * on Page 181 of RFC3530.
2337 */
2338 if (status == -NFS4ERR_BAD_STATEID) {
2339 exception.retry = 1;
2340 continue;
2341 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002342 if (status == -EAGAIN) {
2343 /* We must have found a delegation */
2344 exception.retry = 1;
2345 continue;
2346 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002347 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2348 continue;
2349 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 status, &exception));
2351 } while (exception.retry);
2352 return res;
2353}
2354
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002355static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2356 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002357 struct nfs4_state *state, struct nfs4_label *ilabel,
2358 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002360 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002362 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 .iap = sattr,
2364 .server = server,
2365 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002366 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 };
2368 struct nfs_setattrres res = {
2369 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002370 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 .server = server,
2372 };
2373 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002374 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2375 .rpc_argp = &arg,
2376 .rpc_resp = &res,
2377 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002379 unsigned long timestamp = jiffies;
Trond Myklebustee3ae842013-04-29 10:35:36 -04002380 fmode_t fmode;
2381 bool truncate;
Trond Myklebust65e43082005-08-16 11:49:44 -04002382 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
David Quigleyaa9c2662013-05-22 12:50:44 -04002384 arg.bitmask = nfs4_bitmask(server, ilabel);
2385 if (ilabel)
2386 arg.bitmask = nfs4_bitmask(server, olabel);
2387
Trond Myklebust0e574af2005-10-27 22:12:38 -04002388 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
Trond Myklebustee3ae842013-04-29 10:35:36 -04002390 /* Servers should only apply open mode checks for file size changes */
2391 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2392 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2393
2394 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2395 /* Use that stateid */
2396 } else if (truncate && state != NULL && nfs4_valid_open_stateid(state)) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002397 struct nfs_lockowner lockowner = {
2398 .l_owner = current->files,
2399 .l_pid = current->tgid,
2400 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002401 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002402 &lockowner);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002403 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002404 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
Bryan Schumaker7c513052011-03-24 17:12:24 +00002406 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002407 if (status == 0 && state != NULL)
2408 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002409 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410}
2411
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002412static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2413 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002414 struct nfs4_state *state, struct nfs4_label *ilabel,
2415 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002417 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002418 struct nfs4_exception exception = {
2419 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002420 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002421 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 int err;
2423 do {
David Quigley1775fd32013-05-22 12:50:42 -04002424 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
Trond Myklebustc1578b72013-08-12 16:58:42 -04002425 trace_nfs4_setattr(inode, err);
Trond Myklebust451146b2012-04-18 16:29:11 -04002426 switch (err) {
2427 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002428 if (!(sattr->ia_valid & ATTR_SIZE)) {
2429 pr_warn_once("NFSv4: server %s is incorrectly "
2430 "applying open mode checks to "
2431 "a SETATTR that is not "
2432 "changing file size.\n",
2433 server->nfs_client->cl_hostname);
2434 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002435 if (state && !(state->state & FMODE_WRITE)) {
2436 err = -EBADF;
2437 if (sattr->ia_valid & ATTR_OPEN)
2438 err = -EACCES;
2439 goto out;
2440 }
2441 }
2442 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002444out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 return err;
2446}
2447
2448struct nfs4_closedata {
2449 struct inode *inode;
2450 struct nfs4_state *state;
2451 struct nfs_closeargs arg;
2452 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002453 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002454 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002455 bool roc;
2456 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457};
2458
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002459static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002460{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002461 struct nfs4_closedata *calldata = data;
2462 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002463 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002464
Fred Isamanf7e89172011-01-06 11:36:32 +00002465 if (calldata->roc)
2466 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002467 nfs4_put_open_state(calldata->state);
2468 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002469 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002470 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002471 kfree(calldata);
2472}
2473
Trond Myklebust88069f72009-12-08 08:33:16 -05002474static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2475 fmode_t fmode)
2476{
2477 spin_lock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002478 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002479 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
2480 case FMODE_WRITE:
2481 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2482 break;
2483 case FMODE_READ:
2484 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2485 break;
2486 case 0:
2487 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2488 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2489 clear_bit(NFS_OPEN_STATE, &state->flags);
2490 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002491 spin_unlock(&state->owner->so_lock);
2492}
2493
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002494static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002496 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 struct nfs_server *server = NFS_SERVER(calldata->inode);
2499
Chuck Levera3ca5652012-03-01 17:00:40 -05002500 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002501 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2502 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002503 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 /* hmm. we are done with the inode, and in the process of freeing
2505 * the state_owner. we keep this around to process errors
2506 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 switch (task->tk_status) {
2508 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002509 if (calldata->roc)
2510 pnfs_roc_set_barrier(state->inode,
2511 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002512 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002513 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002514 nfs4_close_clear_stateid_flags(state,
2515 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 break;
2517 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002518 case -NFS4ERR_OLD_STATEID:
2519 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002521 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002522 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002524 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2525 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002527 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002528 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002529 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530}
2531
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002532static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002534 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002535 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002536 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002537 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002538
Chuck Levera3ca5652012-03-01 17:00:40 -05002539 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002540 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002541 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002542
Trond Myklebust88069f72009-12-08 08:33:16 -05002543 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2544 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002545 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002546 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002547 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002548 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002549 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2550 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2551 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002552 }
2553 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002554 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2555 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2556 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002557 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002558 }
Trond Myklebust5d422302013-03-14 16:57:48 -04002559 if (!nfs4_valid_open_stateid(state))
2560 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002561 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002562
2563 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002564 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002565 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002566 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002567
Fred Isamanf7e89172011-01-06 11:36:32 +00002568 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002569 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002570 if (calldata->roc &&
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002571 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2572 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002573 goto out_wait;
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002574 }
Fred Isamanf7e89172011-01-06 11:36:32 +00002575 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002576
Trond Myklebust516a6af2005-10-27 22:12:41 -04002577 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002578 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002579 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002580 &calldata->arg.seq_args,
2581 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002582 task) != 0)
2583 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002584 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002585 return;
2586out_no_action:
2587 task->tk_action = NULL;
2588out_wait:
2589 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590}
2591
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002592static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002593 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002594 .rpc_call_done = nfs4_close_done,
2595 .rpc_release = nfs4_free_closedata,
2596};
2597
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598/*
2599 * It is possible for data to be read/written from a mem-mapped file
2600 * after the sys_close call (which hits the vfs layer as a flush).
2601 * This means that we can't safely call nfsv4 close on a file until
2602 * the inode is cleared. This in turn means that we are not good
2603 * NFSv4 citizens - we do not indicate to the server to update the file's
2604 * share state even when we are done with one of the three share
2605 * stateid's in the inode.
2606 *
2607 * NOTE: Caller must be holding the sp->so_owner semaphore!
2608 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002609int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002611 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002613 struct nfs4_state_owner *sp = state->owner;
2614 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002615 struct rpc_message msg = {
2616 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2617 .rpc_cred = state->owner->so_cred,
2618 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002619 struct rpc_task_setup task_setup_data = {
2620 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002621 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002622 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002623 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002624 .flags = RPC_TASK_ASYNC,
2625 };
Trond Myklebust95121352005-10-18 14:20:12 -07002626 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04002628 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
2629 &task_setup_data.rpc_client, &msg);
2630
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002631 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002633 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04002634 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002635 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002637 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002638 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002640 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002641 if (calldata->arg.seqid == NULL)
2642 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002643 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002644 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002645 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002646 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002647 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002648 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002649 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002650
Trond Myklebust1174dd12010-12-21 10:52:24 -05002651 msg.rpc_argp = &calldata->arg;
2652 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002653 task_setup_data.callback_data = calldata;
2654 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002655 if (IS_ERR(task))
2656 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002657 status = 0;
2658 if (wait)
2659 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002660 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002661 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002662out_free_calldata:
2663 kfree(calldata);
2664out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002665 nfs4_put_open_state(state);
2666 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002667 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668}
2669
Trond Myklebust2b484292010-09-17 10:56:51 -04002670static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002671nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
2672 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04002675 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2676
2677 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678
Trond Myklebust565277f2007-10-15 18:17:53 -04002679 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002680 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04002681
2682 nfs4_label_release_security(label);
2683
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002684 if (IS_ERR(state))
2685 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04002686 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687}
2688
Trond Myklebust1185a552009-12-03 15:54:02 -05002689static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002690{
2691 if (ctx->state == NULL)
2692 return;
2693 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002694 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002695 else
Al Viro643168c2011-06-22 18:20:23 -04002696 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002697}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698
2699static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2700{
Benny Halevy43652ad2009-04-01 09:21:54 -04002701 struct nfs4_server_caps_arg args = {
2702 .fhandle = fhandle,
2703 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 struct nfs4_server_caps_res res = {};
2705 struct rpc_message msg = {
2706 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002707 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 .rpc_resp = &res,
2709 };
2710 int status;
2711
Bryan Schumaker7c513052011-03-24 17:12:24 +00002712 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 if (status == 0) {
2714 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002715 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2716 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2717 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2718 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2719 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2721 server->caps |= NFS_CAP_ACLS;
2722 if (res.has_links != 0)
2723 server->caps |= NFS_CAP_HARDLINKS;
2724 if (res.has_symlinks != 0)
2725 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002726 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2727 server->caps |= NFS_CAP_FILEID;
2728 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2729 server->caps |= NFS_CAP_MODE;
2730 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2731 server->caps |= NFS_CAP_NLINK;
2732 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2733 server->caps |= NFS_CAP_OWNER;
2734 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2735 server->caps |= NFS_CAP_OWNER_GROUP;
2736 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2737 server->caps |= NFS_CAP_ATIME;
2738 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2739 server->caps |= NFS_CAP_CTIME;
2740 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2741 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04002742#ifdef CONFIG_NFS_V4_SECURITY_LABEL
2743 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
2744 server->caps |= NFS_CAP_SECURITY_LABEL;
2745#endif
2746 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
2747 sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002748
David Quigleyaa9c2662013-05-22 12:50:44 -04002749 if (server->caps & NFS_CAP_SECURITY_LABEL) {
2750 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2751 res.attr_bitmask[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2752 }
Trond Myklebusta65318b2009-03-11 14:10:28 -04002753 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2754 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2755 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002757 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002759
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 return status;
2761}
2762
Trond Myklebust55a97592006-06-09 09:34:19 -04002763int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764{
2765 struct nfs4_exception exception = { };
2766 int err;
2767 do {
2768 err = nfs4_handle_exception(server,
2769 _nfs4_server_capabilities(server, fhandle),
2770 &exception);
2771 } while (exception.retry);
2772 return err;
2773}
2774
2775static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2776 struct nfs_fsinfo *info)
2777{
David Quigleyaa9c2662013-05-22 12:50:44 -04002778 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04002780 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 };
2782 struct nfs4_lookup_res res = {
2783 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002784 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 .fh = fhandle,
2786 };
2787 struct rpc_message msg = {
2788 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2789 .rpc_argp = &args,
2790 .rpc_resp = &res,
2791 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002792
David Quigleyaa9c2662013-05-22 12:50:44 -04002793 bitmask[0] = nfs4_fattr_bitmap[0];
2794 bitmask[1] = nfs4_fattr_bitmap[1];
2795 /*
2796 * Process the label in the upcoming getfattr
2797 */
2798 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
2799
Trond Myklebust0e574af2005-10-27 22:12:38 -04002800 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002801 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802}
2803
2804static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2805 struct nfs_fsinfo *info)
2806{
2807 struct nfs4_exception exception = { };
2808 int err;
2809 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002810 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04002811 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002812 switch (err) {
2813 case 0:
2814 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002815 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002816 default:
2817 err = nfs4_handle_exception(server, err, &exception);
2818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002820out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 return err;
2822}
2823
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002824static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2825 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2826{
Trond Myklebustc2190662013-08-26 19:23:04 -04002827 struct rpc_auth_create_args auth_args = {
2828 .pseudoflavor = flavor,
2829 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002830 struct rpc_auth *auth;
2831 int ret;
2832
Trond Myklebustc2190662013-08-26 19:23:04 -04002833 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002834 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04002835 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002836 goto out;
2837 }
2838 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002839out:
2840 return ret;
2841}
2842
Chuck Lever9a744ba2013-03-16 15:56:02 -04002843/*
2844 * Retry pseudoroot lookup with various security flavors. We do this when:
2845 *
2846 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2847 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2848 *
2849 * Returns zero on success, or a negative NFS4ERR value, or a
2850 * negative errno value.
2851 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002852static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002853 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854{
Chuck Lever9a744ba2013-03-16 15:56:02 -04002855 /* Per 3530bis 15.33.5 */
2856 static const rpc_authflavor_t flav_array[] = {
2857 RPC_AUTH_GSS_KRB5P,
2858 RPC_AUTH_GSS_KRB5I,
2859 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04002860 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04002861 RPC_AUTH_NULL,
2862 };
2863 int status = -EPERM;
2864 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
Chuck Lever9a744ba2013-03-16 15:56:02 -04002866 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002867 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002868 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002869 continue;
2870 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002871 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04002872
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002873 /*
2874 * -EACCESS could mean that the user doesn't have correct permissions
2875 * to access the mount. It could also mean that we tried to mount
2876 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2877 * existing mount programs don't handle -EACCES very well so it should
2878 * be mapped to -EPERM instead.
2879 */
2880 if (status == -EACCES)
2881 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002882 return status;
2883}
2884
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002885static int nfs4_do_find_root_sec(struct nfs_server *server,
2886 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
2887{
2888 int mv = server->nfs_client->cl_minorversion;
2889 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
2890}
2891
2892/**
2893 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
2894 * @server: initialized nfs_server handle
2895 * @fhandle: we fill in the pseudo-fs root file handle
2896 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002897 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002898 *
2899 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002900 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002901int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002902 struct nfs_fsinfo *info,
2903 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002904{
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002905 int status;
2906
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002907 switch (auth_probe) {
2908 case false:
2909 status = nfs4_lookup_root(server, fhandle, info);
2910 if (status != -NFS4ERR_WRONGSEC)
2911 break;
2912 /* Did user force a 'sec=' mount option? */
2913 if (server->flags & NFS_MOUNT_SECFLAVOUR)
2914 break;
2915 default:
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002916 status = nfs4_do_find_root_sec(server, fhandle, info);
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002917 }
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002918
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 if (status == 0)
2920 status = nfs4_server_capabilities(server, fhandle);
2921 if (status == 0)
2922 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002923
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002924 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925}
2926
Bryan Schumakerbae36242012-05-10 15:07:31 -04002927static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2928 struct nfs_fsinfo *info)
2929{
2930 int error;
2931 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04002932 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04002933
2934 error = nfs4_server_capabilities(server, mntfh);
2935 if (error < 0) {
2936 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2937 return error;
2938 }
2939
David Quigley14c43f72013-05-22 12:50:43 -04002940 label = nfs4_label_alloc(server, GFP_KERNEL);
2941 if (IS_ERR(label))
2942 return PTR_ERR(label);
2943
David Quigley1775fd32013-05-22 12:50:42 -04002944 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04002945 if (error < 0) {
2946 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04002947 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04002948 }
2949
2950 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2951 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2952 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2953
David Quigley14c43f72013-05-22 12:50:43 -04002954err_free_label:
2955 nfs4_label_free(label);
2956
Bryan Schumakerbae36242012-05-10 15:07:31 -04002957 return error;
2958}
2959
Manoj Naik6b97fd32006-06-09 09:34:29 -04002960/*
2961 * Get locations and (maybe) other attributes of a referral.
2962 * Note that we'll actually follow the referral later when
2963 * we detect fsid mismatch in inode revalidation
2964 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002965static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2966 const struct qstr *name, struct nfs_fattr *fattr,
2967 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002968{
2969 int status = -ENOMEM;
2970 struct page *page = NULL;
2971 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002972
2973 page = alloc_page(GFP_KERNEL);
2974 if (page == NULL)
2975 goto out;
2976 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2977 if (locations == NULL)
2978 goto out;
2979
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002980 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002981 if (status != 0)
2982 goto out;
2983 /* Make sure server returned a different fsid for the referral */
2984 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002985 dprintk("%s: server did not return a different fsid for"
2986 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002987 status = -EIO;
2988 goto out;
2989 }
Andy Adamson533eb462011-06-13 18:25:56 -04002990 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2991 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002992
Andy Adamson533eb462011-06-13 18:25:56 -04002993 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002994 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002995 memset(fhandle, 0, sizeof(struct nfs_fh));
2996out:
2997 if (page)
2998 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002999 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003000 return status;
3001}
3002
David Quigley1775fd32013-05-22 12:50:42 -04003003static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3004 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005{
3006 struct nfs4_getattr_arg args = {
3007 .fh = fhandle,
3008 .bitmask = server->attr_bitmask,
3009 };
3010 struct nfs4_getattr_res res = {
3011 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003012 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 .server = server,
3014 };
3015 struct rpc_message msg = {
3016 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3017 .rpc_argp = &args,
3018 .rpc_resp = &res,
3019 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003020
3021 args.bitmask = nfs4_bitmask(server, label);
3022
Trond Myklebust0e574af2005-10-27 22:12:38 -04003023 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003024 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025}
3026
David Quigley1775fd32013-05-22 12:50:42 -04003027static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3028 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029{
3030 struct nfs4_exception exception = { };
3031 int err;
3032 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003033 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3034 trace_nfs4_getattr(server, fhandle, fattr, err);
3035 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 &exception);
3037 } while (exception.retry);
3038 return err;
3039}
3040
3041/*
3042 * The file is not closed if it is opened due to the a request to change
3043 * the size of the file. The open call will not be needed once the
3044 * VFS layer lookup-intents are implemented.
3045 *
3046 * Close is called when the inode is destroyed.
3047 * If we haven't opened the file for O_WRONLY, we
3048 * need to in the size_change case to obtain a stateid.
3049 *
3050 * Got race?
3051 * Because OPEN is always done by name in nfsv4, it is
3052 * possible that we opened a different file by the same
3053 * name. We can recognize this race condition, but we
3054 * can't do anything about it besides returning an error.
3055 *
3056 * This will be fixed with VFS changes (lookup-intent).
3057 */
3058static int
3059nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3060 struct iattr *sattr)
3061{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003062 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003063 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003064 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003065 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 int status;
3067
Benny Halevy8a1636c2010-07-14 15:43:57 -04003068 if (pnfs_ld_layoutret_on_setattr(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003069 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003070
Trond Myklebust0e574af2005-10-27 22:12:38 -04003071 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072
Andy Adamson26699402012-05-30 16:12:24 -04003073 /* Deal with open(O_TRUNC) */
3074 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003075 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003076
3077 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003078 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003079 return 0;
3080
Trond Myklebustd5308382005-11-04 15:33:38 -05003081 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003082 if (sattr->ia_valid & ATTR_FILE) {
3083 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003084
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003085 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003086 if (ctx) {
3087 cred = ctx->cred;
3088 state = ctx->state;
3089 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003090 }
3091
David Quigley14c43f72013-05-22 12:50:43 -04003092 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3093 if (IS_ERR(label))
3094 return PTR_ERR(label);
3095
3096 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003097 if (status == 0) {
Trond Myklebust65e43082005-08-16 11:49:44 -04003098 nfs_setattr_update_inode(inode, sattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003099 nfs_setsecurity(inode, fattr, label);
3100 }
David Quigley14c43f72013-05-22 12:50:43 -04003101 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 return status;
3103}
3104
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003105static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3106 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003107 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003108{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003109 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003110 int status;
3111 struct nfs4_lookup_arg args = {
3112 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003113 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003114 .name = name,
3115 };
3116 struct nfs4_lookup_res res = {
3117 .server = server,
3118 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003119 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003120 .fh = fhandle,
3121 };
3122 struct rpc_message msg = {
3123 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3124 .rpc_argp = &args,
3125 .rpc_resp = &res,
3126 };
3127
David Quigleyaa9c2662013-05-22 12:50:44 -04003128 args.bitmask = nfs4_bitmask(server, label);
3129
David Howells2b3de442006-08-22 20:06:09 -04003130 nfs_fattr_init(fattr);
3131
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003133 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 dprintk("NFS reply lookup: %d\n", status);
3135 return status;
3136}
3137
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003138static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003139{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003140 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003141 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003142 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3143 fattr->nlink = 2;
3144}
3145
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003146static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3147 struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003148 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003149{
3150 struct nfs4_exception exception = { };
3151 struct rpc_clnt *client = *clnt;
3152 int err;
3153 do {
David Quigley1775fd32013-05-22 12:50:42 -04003154 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003155 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003156 switch (err) {
3157 case -NFS4ERR_BADNAME:
3158 err = -ENOENT;
3159 goto out;
3160 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003161 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003162 goto out;
3163 case -NFS4ERR_WRONGSEC:
3164 err = -EPERM;
3165 if (client != *clnt)
3166 goto out;
Trond Myklebust41d058c2013-09-07 17:49:05 -04003167 /* No security negotiation if the user specified 'sec=' */
3168 if (NFS_SERVER(dir)->flags & NFS_MOUNT_SECFLAVOUR)
3169 goto out;
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003170 client = nfs4_create_sec_client(client, dir, name);
3171 if (IS_ERR(client))
3172 return PTR_ERR(client);
3173
3174 exception.retry = 1;
3175 break;
3176 default:
3177 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3178 }
3179 } while (exception.retry);
3180
3181out:
3182 if (err == 0)
3183 *clnt = client;
3184 else if (client != *clnt)
3185 rpc_shutdown_client(client);
3186
3187 return err;
3188}
3189
Bryan Schumaker80a16b22012-04-27 13:27:46 -04003190static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003191 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3192 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003194 int status;
3195 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003196
David Quigley1775fd32013-05-22 12:50:42 -04003197 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003198 if (client != NFS_CLIENT(dir)) {
3199 rpc_shutdown_client(client);
3200 nfs_fixup_secinfo_attributes(fattr);
3201 }
3202 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203}
3204
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003205struct rpc_clnt *
3206nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3207 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3208{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003209 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003210 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003211
David Quigley1775fd32013-05-22 12:50:42 -04003212 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003213 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003214 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003215 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003216}
3217
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3219{
Trond Myklebust76b32992007-08-10 17:45:11 -04003220 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 struct nfs4_accessargs args = {
3222 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003223 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003225 struct nfs4_accessres res = {
3226 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003227 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 struct rpc_message msg = {
3229 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3230 .rpc_argp = &args,
3231 .rpc_resp = &res,
3232 .rpc_cred = entry->cred,
3233 };
3234 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003235 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236
3237 /*
3238 * Determine which access bits we want to ask for...
3239 */
3240 if (mode & MAY_READ)
3241 args.access |= NFS4_ACCESS_READ;
3242 if (S_ISDIR(inode->i_mode)) {
3243 if (mode & MAY_WRITE)
3244 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3245 if (mode & MAY_EXEC)
3246 args.access |= NFS4_ACCESS_LOOKUP;
3247 } else {
3248 if (mode & MAY_WRITE)
3249 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3250 if (mode & MAY_EXEC)
3251 args.access |= NFS4_ACCESS_EXECUTE;
3252 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003253
3254 res.fattr = nfs_alloc_fattr();
3255 if (res.fattr == NULL)
3256 return -ENOMEM;
3257
Bryan Schumaker7c513052011-03-24 17:12:24 +00003258 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003260 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003261 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003263 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 return status;
3265}
3266
3267static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3268{
3269 struct nfs4_exception exception = { };
3270 int err;
3271 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003272 err = _nfs4_proc_access(inode, entry);
3273 trace_nfs4_access(inode, err);
3274 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 &exception);
3276 } while (exception.retry);
3277 return err;
3278}
3279
3280/*
3281 * TODO: For the time being, we don't try to get any attributes
3282 * along with any of the zero-copy operations READ, READDIR,
3283 * READLINK, WRITE.
3284 *
3285 * In the case of the first three, we want to put the GETATTR
3286 * after the read-type operation -- this is because it is hard
3287 * to predict the length of a GETATTR response in v4, and thus
3288 * align the READ data correctly. This means that the GETATTR
3289 * may end up partially falling into the page cache, and we should
3290 * shift it into the 'tail' of the xdr_buf before processing.
3291 * To do this efficiently, we need to know the total length
3292 * of data received, which doesn't seem to be available outside
3293 * of the RPC layer.
3294 *
3295 * In the case of WRITE, we also want to put the GETATTR after
3296 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003297 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 *
3299 * Both of these changes to the XDR layer would in fact be quite
3300 * minor, but I decided to leave them for a subsequent patch.
3301 */
3302static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3303 unsigned int pgbase, unsigned int pglen)
3304{
3305 struct nfs4_readlink args = {
3306 .fh = NFS_FH(inode),
3307 .pgbase = pgbase,
3308 .pglen = pglen,
3309 .pages = &page,
3310 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003311 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 struct rpc_message msg = {
3313 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3314 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003315 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 };
3317
Bryan Schumaker7c513052011-03-24 17:12:24 +00003318 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 -07003319}
3320
3321static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3322 unsigned int pgbase, unsigned int pglen)
3323{
3324 struct nfs4_exception exception = { };
3325 int err;
3326 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003327 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3328 trace_nfs4_readlink(inode, err);
3329 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 &exception);
3331 } while (exception.retry);
3332 return err;
3333}
3334
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003336 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338static int
3339nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003340 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341{
David Quigleyaa9c2662013-05-22 12:50:44 -04003342 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003343 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 struct nfs4_state *state;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003345 int opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 int status = 0;
3347
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003348 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3349 if (IS_ERR(ctx))
3350 return PTR_ERR(ctx);
3351
David Quigleyaa9c2662013-05-22 12:50:44 -04003352 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3353
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003354 sattr->ia_mode &= ~current_umask();
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003355 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, &opened);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 if (IS_ERR(state)) {
3357 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003358 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003361 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003362 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 return status;
3364}
3365
3366static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3367{
Trond Myklebust16e42952005-10-27 22:12:44 -04003368 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003369 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003371 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003373 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003374 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003375 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003377 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3378 .rpc_argp = &args,
3379 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003381 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003382
Bryan Schumaker7c513052011-03-24 17:12:24 +00003383 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003384 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003385 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 return status;
3387}
3388
3389static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3390{
3391 struct nfs4_exception exception = { };
3392 int err;
3393 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003394 err = _nfs4_proc_remove(dir, name);
3395 trace_nfs4_remove(dir, name, err);
3396 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 &exception);
3398 } while (exception.retry);
3399 return err;
3400}
3401
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003402static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003404 struct nfs_server *server = NFS_SERVER(dir);
3405 struct nfs_removeargs *args = msg->rpc_argp;
3406 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003408 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04003410 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003411
3412 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413}
3414
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003415static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3416{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003417 nfs4_setup_sequence(NFS_SERVER(data->dir),
3418 &data->args.seq_args,
3419 &data->res.seq_res,
3420 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421}
3422
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003423static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003425 struct nfs_unlinkdata *data = task->tk_calldata;
3426 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003427
Trond Myklebust14516c32010-07-31 14:29:06 -04003428 if (!nfs4_sequence_done(task, &res->seq_res))
3429 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003430 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003431 return 0;
3432 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003433 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434}
3435
Jeff Laytond3d41522010-09-17 17:31:57 -04003436static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3437{
3438 struct nfs_server *server = NFS_SERVER(dir);
3439 struct nfs_renameargs *arg = msg->rpc_argp;
3440 struct nfs_renameres *res = msg->rpc_resp;
3441
3442 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003443 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04003444 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003445}
3446
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003447static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3448{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003449 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3450 &data->args.seq_args,
3451 &data->res.seq_res,
3452 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003453}
3454
3455static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3456 struct inode *new_dir)
3457{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003458 struct nfs_renamedata *data = task->tk_calldata;
3459 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04003460
3461 if (!nfs4_sequence_done(task, &res->seq_res))
3462 return 0;
3463 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3464 return 0;
3465
3466 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003467 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003468 return 1;
3469}
3470
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3472 struct inode *new_dir, struct qstr *new_name)
3473{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003474 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003475 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 .old_dir = NFS_FH(old_dir),
3477 .new_dir = NFS_FH(new_dir),
3478 .old_name = old_name,
3479 .new_name = new_name,
3480 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003481 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003482 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003483 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 struct rpc_message msg = {
3485 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3486 .rpc_argp = &arg,
3487 .rpc_resp = &res,
3488 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003489 int status = -ENOMEM;
David Quigley14c43f72013-05-22 12:50:43 -04003490
Bryan Schumaker7c513052011-03-24 17:12:24 +00003491 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 if (!status) {
3493 update_changeattr(old_dir, &res.old_cinfo);
3494 update_changeattr(new_dir, &res.new_cinfo);
3495 }
3496 return status;
3497}
3498
3499static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3500 struct inode *new_dir, struct qstr *new_name)
3501{
3502 struct nfs4_exception exception = { };
3503 int err;
3504 do {
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003505 err = _nfs4_proc_rename(old_dir, old_name,
3506 new_dir, new_name);
3507 trace_nfs4_rename(old_dir, old_name, new_dir, new_name, err);
3508 err = nfs4_handle_exception(NFS_SERVER(old_dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 &exception);
3510 } while (exception.retry);
3511 return err;
3512}
3513
3514static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3515{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003516 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 struct nfs4_link_arg arg = {
3518 .fh = NFS_FH(inode),
3519 .dir_fh = NFS_FH(dir),
3520 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003521 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003523 struct nfs4_link_res res = {
3524 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003525 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003526 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 struct rpc_message msg = {
3528 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3529 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003530 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003532 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533
Trond Myklebust136f2622010-04-16 16:22:49 -04003534 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003535 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003536 goto out;
3537
David Quigley14c43f72013-05-22 12:50:43 -04003538 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3539 if (IS_ERR(res.label)) {
3540 status = PTR_ERR(res.label);
3541 goto out;
3542 }
David Quigleyaa9c2662013-05-22 12:50:44 -04003543 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04003544
Bryan Schumaker7c513052011-03-24 17:12:24 +00003545 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003546 if (!status) {
3547 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04003548 status = nfs_post_op_update_inode(inode, res.fattr);
3549 if (!status)
3550 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003551 }
David Quigley14c43f72013-05-22 12:50:43 -04003552
3553
3554 nfs4_label_free(res.label);
3555
Trond Myklebust136f2622010-04-16 16:22:49 -04003556out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003557 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 return status;
3559}
3560
3561static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3562{
3563 struct nfs4_exception exception = { };
3564 int err;
3565 do {
3566 err = nfs4_handle_exception(NFS_SERVER(inode),
3567 _nfs4_proc_link(inode, dir, name),
3568 &exception);
3569 } while (exception.retry);
3570 return err;
3571}
3572
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003573struct nfs4_createdata {
3574 struct rpc_message msg;
3575 struct nfs4_create_arg arg;
3576 struct nfs4_create_res res;
3577 struct nfs_fh fh;
3578 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003579 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003580};
3581
3582static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3583 struct qstr *name, struct iattr *sattr, u32 ftype)
3584{
3585 struct nfs4_createdata *data;
3586
3587 data = kzalloc(sizeof(*data), GFP_KERNEL);
3588 if (data != NULL) {
3589 struct nfs_server *server = NFS_SERVER(dir);
3590
David Quigley14c43f72013-05-22 12:50:43 -04003591 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3592 if (IS_ERR(data->label))
3593 goto out_free;
3594
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003595 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3596 data->msg.rpc_argp = &data->arg;
3597 data->msg.rpc_resp = &data->res;
3598 data->arg.dir_fh = NFS_FH(dir);
3599 data->arg.server = server;
3600 data->arg.name = name;
3601 data->arg.attrs = sattr;
3602 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04003603 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003604 data->res.server = server;
3605 data->res.fh = &data->fh;
3606 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003607 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003608 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003609 }
3610 return data;
David Quigley14c43f72013-05-22 12:50:43 -04003611out_free:
3612 kfree(data);
3613 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003614}
3615
3616static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3617{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003618 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003619 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003620 if (status == 0) {
3621 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04003622 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003623 }
3624 return status;
3625}
3626
3627static void nfs4_free_createdata(struct nfs4_createdata *data)
3628{
David Quigley14c43f72013-05-22 12:50:43 -04003629 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003630 kfree(data);
3631}
3632
Chuck Lever4f390c12006-08-22 20:06:22 -04003633static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003634 struct page *page, unsigned int len, struct iattr *sattr,
3635 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003637 struct nfs4_createdata *data;
3638 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639
Chuck Lever94a6d752006-08-22 20:06:23 -04003640 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003641 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003642
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003643 status = -ENOMEM;
3644 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3645 if (data == NULL)
3646 goto out;
3647
3648 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3649 data->arg.u.symlink.pages = &page;
3650 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04003651 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003653 status = nfs4_do_create(dir, dentry, data);
3654
3655 nfs4_free_createdata(data);
3656out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 return status;
3658}
3659
Chuck Lever4f390c12006-08-22 20:06:22 -04003660static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003661 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662{
3663 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003664 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04003666
3667 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3668
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003670 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3671 trace_nfs4_symlink(dir, &dentry->d_name, err);
3672 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 &exception);
3674 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003675
3676 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 return err;
3678}
3679
3680static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003681 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003683 struct nfs4_createdata *data;
3684 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003686 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3687 if (data == NULL)
3688 goto out;
3689
David Quigley1775fd32013-05-22 12:50:42 -04003690 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003691 status = nfs4_do_create(dir, dentry, data);
3692
3693 nfs4_free_createdata(data);
3694out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 return status;
3696}
3697
3698static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3699 struct iattr *sattr)
3700{
3701 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003702 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003704
David Quigleyaa9c2662013-05-22 12:50:44 -04003705 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3706
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003707 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003709 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3710 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3711 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 &exception);
3713 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003714 nfs4_label_release_security(label);
3715
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 return err;
3717}
3718
3719static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003720 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721{
3722 struct inode *dir = dentry->d_inode;
3723 struct nfs4_readdir_arg args = {
3724 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003725 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 .pgbase = 0,
3727 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003728 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003729 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 };
3731 struct nfs4_readdir_res res;
3732 struct rpc_message msg = {
3733 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3734 .rpc_argp = &args,
3735 .rpc_resp = &res,
3736 .rpc_cred = cred,
3737 };
3738 int status;
3739
Harvey Harrison3110ff82008-05-02 13:42:44 -07003740 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003741 dentry->d_parent->d_name.name,
3742 dentry->d_name.name,
3743 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003744 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003746 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 -05003747 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003748 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003749 status += args.pgbase;
3750 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003751
3752 nfs_invalidate_atime(dir);
3753
Harvey Harrison3110ff82008-05-02 13:42:44 -07003754 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 return status;
3756}
3757
3758static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003759 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760{
3761 struct nfs4_exception exception = { };
3762 int err;
3763 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003764 err = _nfs4_proc_readdir(dentry, cred, cookie,
3765 pages, count, plus);
3766 trace_nfs4_readdir(dentry->d_inode, err);
3767 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 &exception);
3769 } while (exception.retry);
3770 return err;
3771}
3772
3773static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04003774 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003776 struct nfs4_createdata *data;
3777 int mode = sattr->ia_mode;
3778 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003780 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3781 if (data == NULL)
3782 goto out;
3783
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003785 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003787 data->arg.ftype = NF4BLK;
3788 data->arg.u.device.specdata1 = MAJOR(rdev);
3789 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 }
3791 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003792 data->arg.ftype = NF4CHR;
3793 data->arg.u.device.specdata1 = MAJOR(rdev);
3794 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003795 } else if (!S_ISSOCK(mode)) {
3796 status = -EINVAL;
3797 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 }
David Quigley1775fd32013-05-22 12:50:42 -04003799
David Quigleyaa9c2662013-05-22 12:50:44 -04003800 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003801 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003802out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003803 nfs4_free_createdata(data);
3804out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 return status;
3806}
3807
3808static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3809 struct iattr *sattr, dev_t rdev)
3810{
3811 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003812 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003814
David Quigleyaa9c2662013-05-22 12:50:44 -04003815 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3816
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003817 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003819 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
3820 trace_nfs4_mknod(dir, &dentry->d_name, err);
3821 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 &exception);
3823 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003824
3825 nfs4_label_release_security(label);
3826
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 return err;
3828}
3829
3830static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3831 struct nfs_fsstat *fsstat)
3832{
3833 struct nfs4_statfs_arg args = {
3834 .fh = fhandle,
3835 .bitmask = server->attr_bitmask,
3836 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003837 struct nfs4_statfs_res res = {
3838 .fsstat = fsstat,
3839 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840 struct rpc_message msg = {
3841 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3842 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003843 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 };
3845
Trond Myklebust0e574af2005-10-27 22:12:38 -04003846 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003847 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848}
3849
3850static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3851{
3852 struct nfs4_exception exception = { };
3853 int err;
3854 do {
3855 err = nfs4_handle_exception(server,
3856 _nfs4_proc_statfs(server, fhandle, fsstat),
3857 &exception);
3858 } while (exception.retry);
3859 return err;
3860}
3861
3862static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3863 struct nfs_fsinfo *fsinfo)
3864{
3865 struct nfs4_fsinfo_arg args = {
3866 .fh = fhandle,
3867 .bitmask = server->attr_bitmask,
3868 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003869 struct nfs4_fsinfo_res res = {
3870 .fsinfo = fsinfo,
3871 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 struct rpc_message msg = {
3873 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3874 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003875 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 };
3877
Bryan Schumaker7c513052011-03-24 17:12:24 +00003878 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879}
3880
3881static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3882{
3883 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04003884 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 int err;
3886
3887 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04003888 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003889 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04003890 if (err == 0) {
3891 struct nfs_client *clp = server->nfs_client;
3892
3893 spin_lock(&clp->cl_lock);
3894 clp->cl_lease_time = fsinfo->lease_time * HZ;
3895 clp->cl_last_renewal = now;
3896 spin_unlock(&clp->cl_lock);
3897 break;
3898 }
3899 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 } while (exception.retry);
3901 return err;
3902}
3903
3904static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3905{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003906 int error;
3907
Trond Myklebust0e574af2005-10-27 22:12:38 -04003908 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003909 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003910 if (error == 0) {
3911 /* block layout checks this! */
3912 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003913 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003914 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003915
3916 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917}
3918
3919static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3920 struct nfs_pathconf *pathconf)
3921{
3922 struct nfs4_pathconf_arg args = {
3923 .fh = fhandle,
3924 .bitmask = server->attr_bitmask,
3925 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003926 struct nfs4_pathconf_res res = {
3927 .pathconf = pathconf,
3928 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 struct rpc_message msg = {
3930 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3931 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003932 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 };
3934
3935 /* None of the pathconf attributes are mandatory to implement */
3936 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3937 memset(pathconf, 0, sizeof(*pathconf));
3938 return 0;
3939 }
3940
Trond Myklebust0e574af2005-10-27 22:12:38 -04003941 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003942 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943}
3944
3945static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3946 struct nfs_pathconf *pathconf)
3947{
3948 struct nfs4_exception exception = { };
3949 int err;
3950
3951 do {
3952 err = nfs4_handle_exception(server,
3953 _nfs4_proc_pathconf(server, fhandle, pathconf),
3954 &exception);
3955 } while (exception.retry);
3956 return err;
3957}
3958
Trond Myklebust5521abf2013-03-16 20:54:34 -04003959int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04003960 const struct nfs_open_context *ctx,
3961 const struct nfs_lock_context *l_ctx,
3962 fmode_t fmode)
3963{
3964 const struct nfs_lockowner *lockowner = NULL;
3965
3966 if (l_ctx != NULL)
3967 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003968 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04003969}
3970EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
3971
Trond Myklebust5521abf2013-03-16 20:54:34 -04003972static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
3973 const struct nfs_open_context *ctx,
3974 const struct nfs_lock_context *l_ctx,
3975 fmode_t fmode)
3976{
3977 nfs4_stateid current_stateid;
3978
3979 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode))
3980 return false;
3981 return nfs4_stateid_match(stateid, &current_stateid);
3982}
3983
3984static bool nfs4_error_stateid_expired(int err)
3985{
3986 switch (err) {
3987 case -NFS4ERR_DELEG_REVOKED:
3988 case -NFS4ERR_ADMIN_REVOKED:
3989 case -NFS4ERR_BAD_STATEID:
3990 case -NFS4ERR_STALE_STATEID:
3991 case -NFS4ERR_OLD_STATEID:
3992 case -NFS4ERR_OPENMODE:
3993 case -NFS4ERR_EXPIRED:
3994 return true;
3995 }
3996 return false;
3997}
3998
Benny Halevyd20581a2011-05-22 19:52:03 +03003999void __nfs4_read_done_cb(struct nfs_read_data *data)
4000{
Fred Isamancd841602012-04-20 14:47:44 -04004001 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03004002}
4003
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004004static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005{
Fred Isamancd841602012-04-20 14:47:44 -04004006 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004008 trace_nfs4_read(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004009 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004010 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05004011 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004013
Benny Halevyd20581a2011-05-22 19:52:03 +03004014 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05004016 renew_lease(server, data->timestamp);
4017 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018}
4019
Trond Myklebust5521abf2013-03-16 20:54:34 -04004020static bool nfs4_read_stateid_changed(struct rpc_task *task,
4021 struct nfs_readargs *args)
4022{
4023
4024 if (!nfs4_error_stateid_expired(task->tk_status) ||
4025 nfs4_stateid_is_current(&args->stateid,
4026 args->context,
4027 args->lock_context,
4028 FMODE_READ))
4029 return false;
4030 rpc_restart_call_prepare(task);
4031 return true;
4032}
4033
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004034static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
4035{
4036
4037 dprintk("--> %s\n", __func__);
4038
4039 if (!nfs4_sequence_done(task, &data->res.seq_res))
4040 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004041 if (nfs4_read_stateid_changed(task, &data->args))
4042 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03004043 return data->read_done_cb ? data->read_done_cb(task, data) :
4044 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004045}
4046
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004047static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004050 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004051 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Chuck Levera9c92d62013-08-09 12:48:18 -04004052 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053}
4054
NeilBrownef1820f2013-09-04 17:04:49 +10004055static int nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004056{
Trond Myklebust9b206142013-03-17 15:52:00 -04004057 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004058 &data->args.seq_args,
4059 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004060 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004061 return 0;
4062 if (nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4063 data->args.lock_context, FMODE_READ) == -EIO)
4064 return -EIO;
4065 if (unlikely(test_bit(NFS_CONTEXT_BAD, &data->args.context->flags)))
4066 return -EIO;
4067 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068}
4069
Fred Isamanb029bc92011-03-03 15:13:42 +00004070static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071{
Fred Isamancd841602012-04-20 14:47:44 -04004072 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004074 trace_nfs4_write(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004075 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004076 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004077 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004079 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04004081 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004082 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004083 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084}
4085
Trond Myklebust5521abf2013-03-16 20:54:34 -04004086static bool nfs4_write_stateid_changed(struct rpc_task *task,
4087 struct nfs_writeargs *args)
4088{
4089
4090 if (!nfs4_error_stateid_expired(task->tk_status) ||
4091 nfs4_stateid_is_current(&args->stateid,
4092 args->context,
4093 args->lock_context,
4094 FMODE_WRITE))
4095 return false;
4096 rpc_restart_call_prepare(task);
4097 return true;
4098}
4099
Fred Isamanb029bc92011-03-03 15:13:42 +00004100static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
4101{
4102 if (!nfs4_sequence_done(task, &data->res.seq_res))
4103 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004104 if (nfs4_write_stateid_changed(task, &data->args))
4105 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03004106 return data->write_done_cb ? data->write_done_cb(task, data) :
4107 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00004108}
4109
Trond Myklebust5a37f852012-04-28 14:55:16 -04004110static
4111bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00004112{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004113 const struct nfs_pgio_header *hdr = data->header;
4114
4115 /* Don't request attributes for pNFS or O_DIRECT writes */
4116 if (data->ds_clp != NULL || hdr->dreq != NULL)
4117 return false;
4118 /* Otherwise, request attributes if and only if we don't hold
4119 * a delegation
4120 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004121 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004122}
Fred Isamana69aef12011-03-03 15:13:47 +00004123
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004124static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125{
Fred Isamancd841602012-04-20 14:47:44 -04004126 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004127
Trond Myklebust5a37f852012-04-28 14:55:16 -04004128 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00004129 data->args.bitmask = NULL;
4130 data->res.fattr = NULL;
4131 } else
4132 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004133
Fred Isamanb029bc92011-03-03 15:13:42 +00004134 if (!data->write_done_cb)
4135 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004136 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 data->timestamp = jiffies;
4138
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004139 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004140 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141}
4142
NeilBrownef1820f2013-09-04 17:04:49 +10004143static int nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04004144{
Trond Myklebust9b206142013-03-17 15:52:00 -04004145 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004146 &data->args.seq_args,
4147 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004148 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004149 return 0;
4150 if (nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4151 data->args.lock_context, FMODE_WRITE) == -EIO)
4152 return -EIO;
4153 if (unlikely(test_bit(NFS_CONTEXT_BAD, &data->args.context->flags)))
4154 return -EIO;
4155 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156}
4157
Fred Isaman0b7c0152012-04-20 14:47:39 -04004158static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4159{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004160 nfs4_setup_sequence(NFS_SERVER(data->inode),
4161 &data->args.seq_args,
4162 &data->res.seq_res,
4163 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164}
4165
Fred Isaman0b7c0152012-04-20 14:47:39 -04004166static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004169
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004170 trace_nfs4_commit(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004171 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004172 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004173 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004175 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176}
4177
Fred Isaman0b7c0152012-04-20 14:47:39 -04004178static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004179{
4180 if (!nfs4_sequence_done(task, &data->res.seq_res))
4181 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004182 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004183}
4184
Fred Isaman0b7c0152012-04-20 14:47:39 -04004185static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004187 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004188
Fred Isaman0b7c0152012-04-20 14:47:39 -04004189 if (data->commit_done_cb == NULL)
4190 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004191 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004192 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004193 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194}
4195
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004196struct nfs4_renewdata {
4197 struct nfs_client *client;
4198 unsigned long timestamp;
4199};
4200
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201/*
4202 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4203 * standalone procedure for queueing an asynchronous RENEW.
4204 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004205static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004206{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004207 struct nfs4_renewdata *data = calldata;
4208 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004209
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004210 if (atomic_read(&clp->cl_count) > 1)
4211 nfs4_schedule_state_renewal(clp);
4212 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004213 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004214}
4215
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004216static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004218 struct nfs4_renewdata *data = calldata;
4219 struct nfs_client *clp = data->client;
4220 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004222 trace_nfs4_renew_async(clp, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04004224 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004225 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4226 return;
4227 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004228 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004229 return;
4230 }
4231 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004233 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234}
4235
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004236static const struct rpc_call_ops nfs4_renew_ops = {
4237 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004238 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004239};
4240
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004241static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242{
4243 struct rpc_message msg = {
4244 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4245 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004246 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004248 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004250 if (renew_flags == 0)
4251 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004252 if (!atomic_inc_not_zero(&clp->cl_count))
4253 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004254 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004255 if (data == NULL)
4256 return -ENOMEM;
4257 data->client = clp;
4258 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004259 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004260 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261}
4262
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004263static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264{
4265 struct rpc_message msg = {
4266 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4267 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004268 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 };
4270 unsigned long now = jiffies;
4271 int status;
4272
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004273 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 if (status < 0)
4275 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004276 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 return 0;
4278}
4279
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004280static inline int nfs4_server_supports_acls(struct nfs_server *server)
4281{
4282 return (server->caps & NFS_CAP_ACLS)
4283 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
4284 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
4285}
4286
Trond Myklebust21f498c2012-08-24 10:59:25 -04004287/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4288 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004289 * the stack.
4290 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004291#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004292
Neil Hormane9e3d722011-03-04 19:26:03 -05004293static int buf_to_pages_noslab(const void *buf, size_t buflen,
4294 struct page **pages, unsigned int *pgbase)
4295{
4296 struct page *newpage, **spages;
4297 int rc = 0;
4298 size_t len;
4299 spages = pages;
4300
4301 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004302 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004303 newpage = alloc_page(GFP_KERNEL);
4304
4305 if (newpage == NULL)
4306 goto unwind;
4307 memcpy(page_address(newpage), buf, len);
4308 buf += len;
4309 buflen -= len;
4310 *pages++ = newpage;
4311 rc++;
4312 } while (buflen != 0);
4313
4314 return rc;
4315
4316unwind:
4317 for(; rc > 0; rc--)
4318 __free_page(spages[rc-1]);
4319 return -ENOMEM;
4320}
4321
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004322struct nfs4_cached_acl {
4323 int cached;
4324 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004325 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004326};
4327
4328static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004329{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004330 struct nfs_inode *nfsi = NFS_I(inode);
4331
4332 spin_lock(&inode->i_lock);
4333 kfree(nfsi->nfs4_acl);
4334 nfsi->nfs4_acl = acl;
4335 spin_unlock(&inode->i_lock);
4336}
4337
4338static void nfs4_zap_acl_attr(struct inode *inode)
4339{
4340 nfs4_set_cached_acl(inode, NULL);
4341}
4342
4343static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4344{
4345 struct nfs_inode *nfsi = NFS_I(inode);
4346 struct nfs4_cached_acl *acl;
4347 int ret = -ENOENT;
4348
4349 spin_lock(&inode->i_lock);
4350 acl = nfsi->nfs4_acl;
4351 if (acl == NULL)
4352 goto out;
4353 if (buf == NULL) /* user is just asking for length */
4354 goto out_len;
4355 if (acl->cached == 0)
4356 goto out;
4357 ret = -ERANGE; /* see getxattr(2) man page */
4358 if (acl->len > buflen)
4359 goto out;
4360 memcpy(buf, acl->data, acl->len);
4361out_len:
4362 ret = acl->len;
4363out:
4364 spin_unlock(&inode->i_lock);
4365 return ret;
4366}
4367
Sachin Prabhu5794d212012-04-17 14:36:40 +01004368static 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 +00004369{
4370 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004371 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004372
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004373 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004374 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004375 if (acl == NULL)
4376 goto out;
4377 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004378 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004379 } else {
4380 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4381 if (acl == NULL)
4382 goto out;
4383 acl->cached = 0;
4384 }
4385 acl->len = acl_len;
4386out:
4387 nfs4_set_cached_acl(inode, acl);
4388}
4389
Andy Adamsonbf118a32011-12-07 11:55:27 -05004390/*
4391 * The getxattr API returns the required buffer length when called with a
4392 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4393 * the required buf. On a NULL buf, we send a page of data to the server
4394 * guessing that the ACL request can be serviced by a page. If so, we cache
4395 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4396 * the cache. If not so, we throw away the page, and cache the required
4397 * length. The next getxattr call will then produce another round trip to
4398 * the server, this time with the input buf of the required size.
4399 */
Trond Myklebust16b42892006-08-24 12:27:15 -04004400static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004401{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004402 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004403 struct nfs_getaclargs args = {
4404 .fh = NFS_FH(inode),
4405 .acl_pages = pages,
4406 .acl_len = buflen,
4407 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004408 struct nfs_getaclres res = {
4409 .acl_len = buflen,
4410 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004411 struct rpc_message msg = {
4412 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4413 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004414 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004415 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004416 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4417 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004418
Andy Adamsonbf118a32011-12-07 11:55:27 -05004419 /* As long as we're doing a round trip to the server anyway,
4420 * let's be prepared for a page of acl data. */
4421 if (npages == 0)
4422 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004423 if (npages > ARRAY_SIZE(pages))
4424 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004425
Andy Adamsonbf118a32011-12-07 11:55:27 -05004426 for (i = 0; i < npages; i++) {
4427 pages[i] = alloc_page(GFP_KERNEL);
4428 if (!pages[i])
4429 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004430 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004431
4432 /* for decoding across pages */
4433 res.acl_scratch = alloc_page(GFP_KERNEL);
4434 if (!res.acl_scratch)
4435 goto out_free;
4436
Andy Adamsonbf118a32011-12-07 11:55:27 -05004437 args.acl_len = npages * PAGE_SIZE;
4438 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004439
Peng Taode040be2012-01-10 22:42:47 +08004440 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004441 __func__, buf, buflen, npages, args.acl_len);
4442 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4443 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004444 if (ret)
4445 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004446
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004447 /* Handle the case where the passed-in buffer is too short */
4448 if (res.acl_flags & NFS4_ACL_TRUNC) {
4449 /* Did the user only issue a request for the acl length? */
4450 if (buf == NULL)
4451 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004452 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004453 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004454 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004455 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004456 if (buf) {
4457 if (res.acl_len > buflen) {
4458 ret = -ERANGE;
4459 goto out_free;
4460 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004461 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004462 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004463out_ok:
4464 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004465out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004466 for (i = 0; i < npages; i++)
4467 if (pages[i])
4468 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004469 if (res.acl_scratch)
4470 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004471 return ret;
4472}
4473
Trond Myklebust16b42892006-08-24 12:27:15 -04004474static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4475{
4476 struct nfs4_exception exception = { };
4477 ssize_t ret;
4478 do {
4479 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04004480 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04004481 if (ret >= 0)
4482 break;
4483 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4484 } while (exception.retry);
4485 return ret;
4486}
4487
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004488static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4489{
4490 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004491 int ret;
4492
4493 if (!nfs4_server_supports_acls(server))
4494 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004495 ret = nfs_revalidate_inode(server, inode);
4496 if (ret < 0)
4497 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004498 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4499 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004500 ret = nfs4_read_cached_acl(inode, buf, buflen);
4501 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004502 /* -ENOENT is returned if there is no ACL or if there is an ACL
4503 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004504 return ret;
4505 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004506}
4507
Trond Myklebust16b42892006-08-24 12:27:15 -04004508static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004509{
4510 struct nfs_server *server = NFS_SERVER(inode);
4511 struct page *pages[NFS4ACL_MAXPAGES];
4512 struct nfs_setaclargs arg = {
4513 .fh = NFS_FH(inode),
4514 .acl_pages = pages,
4515 .acl_len = buflen,
4516 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004517 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004518 struct rpc_message msg = {
4519 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4520 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004521 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004522 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004523 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004524 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004525
4526 if (!nfs4_server_supports_acls(server))
4527 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004528 if (npages > ARRAY_SIZE(pages))
4529 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004530 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4531 if (i < 0)
4532 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004533 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004534 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004535
4536 /*
4537 * Free each page after tx, so the only ref left is
4538 * held by the network stack
4539 */
4540 for (; i > 0; i--)
4541 put_page(pages[i-1]);
4542
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004543 /*
4544 * Acl update can result in inode attribute update.
4545 * so mark the attribute cache invalid.
4546 */
4547 spin_lock(&inode->i_lock);
4548 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4549 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004550 nfs_access_zap_cache(inode);
4551 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004552 return ret;
4553}
4554
Trond Myklebust16b42892006-08-24 12:27:15 -04004555static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4556{
4557 struct nfs4_exception exception = { };
4558 int err;
4559 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004560 err = __nfs4_proc_set_acl(inode, buf, buflen);
4561 trace_nfs4_set_acl(inode, err);
4562 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04004563 &exception);
4564 } while (exception.retry);
4565 return err;
4566}
4567
David Quigleyaa9c2662013-05-22 12:50:44 -04004568#ifdef CONFIG_NFS_V4_SECURITY_LABEL
4569static int _nfs4_get_security_label(struct inode *inode, void *buf,
4570 size_t buflen)
4571{
4572 struct nfs_server *server = NFS_SERVER(inode);
4573 struct nfs_fattr fattr;
4574 struct nfs4_label label = {0, 0, buflen, buf};
4575
4576 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4577 struct nfs4_getattr_arg args = {
4578 .fh = NFS_FH(inode),
4579 .bitmask = bitmask,
4580 };
4581 struct nfs4_getattr_res res = {
4582 .fattr = &fattr,
4583 .label = &label,
4584 .server = server,
4585 };
4586 struct rpc_message msg = {
4587 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4588 .rpc_argp = &args,
4589 .rpc_resp = &res,
4590 };
4591 int ret;
4592
4593 nfs_fattr_init(&fattr);
4594
4595 ret = rpc_call_sync(server->client, &msg, 0);
4596 if (ret)
4597 return ret;
4598 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4599 return -ENOENT;
4600 if (buflen < label.len)
4601 return -ERANGE;
4602 return 0;
4603}
4604
4605static int nfs4_get_security_label(struct inode *inode, void *buf,
4606 size_t buflen)
4607{
4608 struct nfs4_exception exception = { };
4609 int err;
4610
4611 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4612 return -EOPNOTSUPP;
4613
4614 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004615 err = _nfs4_get_security_label(inode, buf, buflen);
4616 trace_nfs4_get_security_label(inode, err);
4617 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004618 &exception);
4619 } while (exception.retry);
4620 return err;
4621}
4622
4623static int _nfs4_do_set_security_label(struct inode *inode,
4624 struct nfs4_label *ilabel,
4625 struct nfs_fattr *fattr,
4626 struct nfs4_label *olabel)
4627{
4628
4629 struct iattr sattr = {0};
4630 struct nfs_server *server = NFS_SERVER(inode);
4631 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4632 struct nfs_setattrargs args = {
4633 .fh = NFS_FH(inode),
4634 .iap = &sattr,
4635 .server = server,
4636 .bitmask = bitmask,
4637 .label = ilabel,
4638 };
4639 struct nfs_setattrres res = {
4640 .fattr = fattr,
4641 .label = olabel,
4642 .server = server,
4643 };
4644 struct rpc_message msg = {
4645 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
4646 .rpc_argp = &args,
4647 .rpc_resp = &res,
4648 };
4649 int status;
4650
4651 nfs4_stateid_copy(&args.stateid, &zero_stateid);
4652
4653 status = rpc_call_sync(server->client, &msg, 0);
4654 if (status)
4655 dprintk("%s failed: %d\n", __func__, status);
4656
4657 return status;
4658}
4659
4660static int nfs4_do_set_security_label(struct inode *inode,
4661 struct nfs4_label *ilabel,
4662 struct nfs_fattr *fattr,
4663 struct nfs4_label *olabel)
4664{
4665 struct nfs4_exception exception = { };
4666 int err;
4667
4668 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004669 err = _nfs4_do_set_security_label(inode, ilabel,
4670 fattr, olabel);
4671 trace_nfs4_set_security_label(inode, err);
4672 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004673 &exception);
4674 } while (exception.retry);
4675 return err;
4676}
4677
4678static int
4679nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4680{
4681 struct nfs4_label ilabel, *olabel = NULL;
4682 struct nfs_fattr fattr;
4683 struct rpc_cred *cred;
4684 struct inode *inode = dentry->d_inode;
4685 int status;
4686
4687 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4688 return -EOPNOTSUPP;
4689
4690 nfs_fattr_init(&fattr);
4691
4692 ilabel.pi = 0;
4693 ilabel.lfs = 0;
4694 ilabel.label = (char *)buf;
4695 ilabel.len = buflen;
4696
4697 cred = rpc_lookup_cred();
4698 if (IS_ERR(cred))
4699 return PTR_ERR(cred);
4700
4701 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4702 if (IS_ERR(olabel)) {
4703 status = -PTR_ERR(olabel);
4704 goto out;
4705 }
4706
4707 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4708 if (status == 0)
4709 nfs_setsecurity(inode, &fattr, olabel);
4710
4711 nfs4_label_free(olabel);
4712out:
4713 put_rpccred(cred);
4714 return status;
4715}
4716#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4717
4718
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004720nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004722 struct nfs_client *clp = server->nfs_client;
4723
4724 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 return 0;
4726 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004727 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004728 case -NFS4ERR_ADMIN_REVOKED:
4729 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004730 if (state == NULL)
4731 break;
4732 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004733 case -NFS4ERR_OPENMODE:
4734 if (state == NULL)
4735 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004736 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4737 goto stateid_invalid;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004738 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004739 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004740 if (state != NULL) {
4741 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4742 goto stateid_invalid;
4743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004745 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004746 nfs4_schedule_lease_recovery(clp);
4747 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004748#if defined(CONFIG_NFS_V4_1)
4749 case -NFS4ERR_BADSESSION:
4750 case -NFS4ERR_BADSLOT:
4751 case -NFS4ERR_BAD_HIGH_SLOT:
4752 case -NFS4ERR_DEADSESSION:
4753 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4754 case -NFS4ERR_SEQ_FALSE_RETRY:
4755 case -NFS4ERR_SEQ_MISORDERED:
4756 dprintk("%s ERROR %d, Reset session\n", __func__,
4757 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004758 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004759 task->tk_status = 0;
4760 return -EAGAIN;
4761#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004763 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004764 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4766 task->tk_status = 0;
4767 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004768 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 case -NFS4ERR_OLD_STATEID:
4770 task->tk_status = 0;
4771 return -EAGAIN;
4772 }
4773 task->tk_status = nfs4_map_errors(task->tk_status);
4774 return 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004775stateid_invalid:
4776 task->tk_status = -EIO;
4777 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004778wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004779 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004780 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4781 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4782 task->tk_status = 0;
4783 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784}
4785
Chuck Leverf0920752012-05-21 22:45:41 -04004786static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4787 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004788{
4789 __be32 verf[2];
4790
Chuck Lever2c820d92012-05-21 22:45:33 -04004791 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4792 /* An impossible timestamp guarantees this value
4793 * will never match a generated boot time. */
4794 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04004795 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04004796 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004797 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04004798 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
4799 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04004800 }
Chuck Levercd937102012-03-02 17:14:31 -05004801 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4802}
4803
Chuck Levere984a552012-09-14 17:24:21 -04004804static unsigned int
4805nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4806 char *buf, size_t len)
4807{
4808 unsigned int result;
4809
4810 rcu_read_lock();
4811 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4812 clp->cl_ipaddr,
4813 rpc_peeraddr2str(clp->cl_rpcclient,
4814 RPC_DISPLAY_ADDR),
4815 rpc_peeraddr2str(clp->cl_rpcclient,
4816 RPC_DISPLAY_PROTO));
4817 rcu_read_unlock();
4818 return result;
4819}
4820
4821static unsigned int
4822nfs4_init_uniform_client_string(const struct nfs_client *clp,
4823 char *buf, size_t len)
4824{
Trond Myklebust55b59292013-07-24 16:36:35 -04004825 const char *nodename = clp->cl_rpcclient->cl_nodename;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004826
4827 if (nfs4_client_id_uniquifier[0] != '\0')
Trond Myklebust55b59292013-07-24 16:36:35 -04004828 return scnprintf(buf, len, "Linux NFSv%u.%u %s/%s",
4829 clp->rpc_ops->version,
4830 clp->cl_minorversion,
4831 nfs4_client_id_uniquifier,
4832 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004833 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4834 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004835 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004836}
4837
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004838/**
4839 * nfs4_proc_setclientid - Negotiate client ID
4840 * @clp: state data structure
4841 * @program: RPC program for NFSv4 callback service
4842 * @port: IP port number for NFS4 callback service
4843 * @cred: RPC credential to use for this call
4844 * @res: where to place the result
4845 *
4846 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4847 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004848int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4849 unsigned short port, struct rpc_cred *cred,
4850 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851{
4852 nfs4_verifier sc_verifier;
4853 struct nfs4_setclientid setclientid = {
4854 .sc_verifier = &sc_verifier,
4855 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004856 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 };
4858 struct rpc_message msg = {
4859 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4860 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004861 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004862 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004864 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865
Chuck Leverde734832012-07-11 16:30:50 -04004866 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004867 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004868 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4869 setclientid.sc_name_len =
4870 nfs4_init_uniform_client_string(clp,
4871 setclientid.sc_name,
4872 sizeof(setclientid.sc_name));
4873 else
4874 setclientid.sc_name_len =
4875 nfs4_init_nonuniform_client_string(clp,
4876 setclientid.sc_name,
4877 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004878 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004879 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004880 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Djalal Harounife2d5392013-06-18 17:58:12 +01004881 sizeof(setclientid.sc_netid), "%s",
Chuck Leverd4d3c502007-12-10 14:57:09 -05004882 rpc_peeraddr2str(clp->cl_rpcclient,
4883 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004884 rcu_read_unlock();
4885 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004886 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 clp->cl_ipaddr, port >> 8, port & 255);
4888
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004889 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4890 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4891 setclientid.sc_name_len, setclientid.sc_name);
4892 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004893 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004894 dprintk("NFS reply setclientid: %d\n", status);
4895 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896}
4897
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004898/**
4899 * nfs4_proc_setclientid_confirm - Confirm client ID
4900 * @clp: state data structure
4901 * @res: result of a previous SETCLIENTID
4902 * @cred: RPC credential to use for this call
4903 *
4904 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4905 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004906int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004907 struct nfs4_setclientid_res *arg,
4908 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910 struct rpc_message msg = {
4911 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004912 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004913 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 int status;
4916
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004917 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4918 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4919 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004920 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004921 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004922 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 return status;
4924}
4925
Trond Myklebustfe650402006-01-03 09:55:18 +01004926struct nfs4_delegreturndata {
4927 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004928 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004929 struct nfs_fh fh;
4930 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004931 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004932 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004933 int rpc_status;
4934};
4935
Trond Myklebustfe650402006-01-03 09:55:18 +01004936static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4937{
4938 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004939
Trond Myklebust14516c32010-07-31 14:29:06 -04004940 if (!nfs4_sequence_done(task, &data->res.seq_res))
4941 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004942
Trond Myklebustca8acf82013-08-13 10:36:56 -04004943 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004944 switch (task->tk_status) {
4945 case -NFS4ERR_STALE_STATEID:
4946 case -NFS4ERR_EXPIRED:
4947 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004948 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004949 break;
4950 default:
4951 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4952 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004953 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004954 return;
4955 }
4956 }
4957 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004958}
4959
4960static void nfs4_delegreturn_release(void *calldata)
4961{
Trond Myklebustfe650402006-01-03 09:55:18 +01004962 kfree(calldata);
4963}
4964
Andy Adamson938e1012009-04-01 09:22:28 -04004965static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4966{
4967 struct nfs4_delegreturndata *d_data;
4968
4969 d_data = (struct nfs4_delegreturndata *)data;
4970
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004971 nfs4_setup_sequence(d_data->res.server,
4972 &d_data->args.seq_args,
4973 &d_data->res.seq_res,
4974 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004975}
Andy Adamson938e1012009-04-01 09:22:28 -04004976
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004977static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004978 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01004979 .rpc_call_done = nfs4_delegreturn_done,
4980 .rpc_release = nfs4_delegreturn_release,
4981};
4982
Trond Myklebuste6f81072008-01-24 18:14:34 -05004983static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004984{
4985 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004986 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004987 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004988 struct rpc_message msg = {
4989 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4990 .rpc_cred = cred,
4991 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004992 struct rpc_task_setup task_setup_data = {
4993 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004994 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004995 .callback_ops = &nfs4_delegreturn_ops,
4996 .flags = RPC_TASK_ASYNC,
4997 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004998 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004999
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005000 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005001 if (data == NULL)
5002 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005003 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01005004 data->args.fhandle = &data->fh;
5005 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005006 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005007 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005008 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005009 data->res.fattr = &data->fattr;
5010 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005011 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005012 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005013 data->rpc_status = 0;
5014
Trond Myklebustc970aa82007-07-14 15:39:59 -04005015 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005016 msg.rpc_argp = &data->args;
5017 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005018 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005019 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005020 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005021 if (!issync)
5022 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005023 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005024 if (status != 0)
5025 goto out;
5026 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005027 if (status == 0)
5028 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5029 else
5030 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005031out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005032 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005033 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034}
5035
Trond Myklebuste6f81072008-01-24 18:14:34 -05005036int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037{
5038 struct nfs_server *server = NFS_SERVER(inode);
5039 struct nfs4_exception exception = { };
5040 int err;
5041 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005042 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Trond Myklebustca8acf82013-08-13 10:36:56 -04005043 trace_nfs4_delegreturn(inode, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044 switch (err) {
5045 case -NFS4ERR_STALE_STATEID:
5046 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047 case 0:
5048 return 0;
5049 }
5050 err = nfs4_handle_exception(server, err, &exception);
5051 } while (exception.retry);
5052 return err;
5053}
5054
5055#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5056#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5057
5058/*
5059 * sleep, with exponential backoff, and retry the LOCK operation.
5060 */
5061static unsigned long
5062nfs4_set_lock_task_retry(unsigned long timeout)
5063{
Colin Cross416ad3c2013-05-06 23:50:06 +00005064 freezable_schedule_timeout_killable_unsafe(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065 timeout <<= 1;
5066 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5067 return NFS4_LOCK_MAXTIMEOUT;
5068 return timeout;
5069}
5070
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5072{
5073 struct inode *inode = state->inode;
5074 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005075 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005076 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005078 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005080 struct nfs_lockt_res res = {
5081 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082 };
5083 struct rpc_message msg = {
5084 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5085 .rpc_argp = &arg,
5086 .rpc_resp = &res,
5087 .rpc_cred = state->owner->so_cred,
5088 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089 struct nfs4_lock_state *lsp;
5090 int status;
5091
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005092 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005093 status = nfs4_set_lock_state(state, request);
5094 if (status != 0)
5095 goto out;
5096 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005097 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005098 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005099 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005100 switch (status) {
5101 case 0:
5102 request->fl_type = F_UNLCK;
5103 break;
5104 case -NFS4ERR_DENIED:
5105 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005107 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005108 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005109out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110 return status;
5111}
5112
5113static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5114{
5115 struct nfs4_exception exception = { };
5116 int err;
5117
5118 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005119 err = _nfs4_proc_getlk(state, cmd, request);
5120 trace_nfs4_get_lock(request, state, cmd, err);
5121 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122 &exception);
5123 } while (exception.retry);
5124 return err;
5125}
5126
5127static int do_vfs_lock(struct file *file, struct file_lock *fl)
5128{
5129 int res = 0;
5130 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5131 case FL_POSIX:
5132 res = posix_lock_file_wait(file, fl);
5133 break;
5134 case FL_FLOCK:
5135 res = flock_lock_file_wait(file, fl);
5136 break;
5137 default:
5138 BUG();
5139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140 return res;
5141}
5142
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005143struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005144 struct nfs_locku_args arg;
5145 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005146 struct nfs4_lock_state *lsp;
5147 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005148 struct file_lock fl;
5149 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005150 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005151};
5152
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005153static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5154 struct nfs_open_context *ctx,
5155 struct nfs4_lock_state *lsp,
5156 struct nfs_seqid *seqid)
5157{
5158 struct nfs4_unlockdata *p;
5159 struct inode *inode = lsp->ls_state->inode;
5160
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005161 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005162 if (p == NULL)
5163 return NULL;
5164 p->arg.fh = NFS_FH(inode);
5165 p->arg.fl = &p->fl;
5166 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005167 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005168 p->arg.stateid = &lsp->ls_stateid;
5169 p->lsp = lsp;
5170 atomic_inc(&lsp->ls_count);
5171 /* Ensure we don't close file until we're done freeing locks! */
5172 p->ctx = get_nfs_open_context(ctx);
5173 memcpy(&p->fl, fl, sizeof(p->fl));
5174 p->server = NFS_SERVER(inode);
5175 return p;
5176}
5177
Trond Myklebust06f814a2006-01-03 09:55:07 +01005178static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005179{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005180 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005181 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005182 nfs4_put_lock_state(calldata->lsp);
5183 put_nfs_open_context(calldata->ctx);
5184 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005185}
5186
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005187static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005188{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005189 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005190
Trond Myklebust14516c32010-07-31 14:29:06 -04005191 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5192 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005193 switch (task->tk_status) {
5194 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05005195 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
5196 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005197 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005198 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005199 case -NFS4ERR_BAD_STATEID:
5200 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005201 case -NFS4ERR_STALE_STATEID:
5202 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005203 break;
5204 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005205 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005206 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005207 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005208 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005209}
5210
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005211static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005212{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005213 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005215 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005216 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04005217 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005218 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005219 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005220 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005221 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005222 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005223 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005224 &calldata->res.seq_res,
5225 task) != 0)
5226 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005227 return;
5228out_no_action:
5229 task->tk_action = NULL;
5230out_wait:
5231 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232}
5233
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005234static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005235 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005236 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005237 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005238};
5239
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005240static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5241 struct nfs_open_context *ctx,
5242 struct nfs4_lock_state *lsp,
5243 struct nfs_seqid *seqid)
5244{
5245 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005246 struct rpc_message msg = {
5247 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5248 .rpc_cred = ctx->cred,
5249 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005250 struct rpc_task_setup task_setup_data = {
5251 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005252 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005253 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005254 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005255 .flags = RPC_TASK_ASYNC,
5256 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005257
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005258 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5259 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5260
Frank Filz137d6ac2007-07-09 15:32:29 -07005261 /* Ensure this is an unlock - when canceling a lock, the
5262 * canceled lock is passed in, and it won't be an unlock.
5263 */
5264 fl->fl_type = F_UNLCK;
5265
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005266 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5267 if (data == NULL) {
5268 nfs_free_seqid(seqid);
5269 return ERR_PTR(-ENOMEM);
5270 }
5271
Chuck Levera9c92d62013-08-09 12:48:18 -04005272 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005273 msg.rpc_argp = &data->arg;
5274 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005275 task_setup_data.callback_data = data;
5276 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005277}
5278
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5280{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005281 struct inode *inode = state->inode;
5282 struct nfs4_state_owner *sp = state->owner;
5283 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005284 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005285 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005286 struct rpc_task *task;
5287 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005288 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289
Trond Myklebust9b073572006-06-29 16:38:34 -04005290 status = nfs4_set_lock_state(state, request);
5291 /* Unlock _before_ we do the RPC call */
5292 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005293 /* Exclude nfs_delegation_claim_locks() */
5294 mutex_lock(&sp->so_delegreturn_mutex);
5295 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005296 down_read(&nfsi->rwsem);
5297 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
5298 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005299 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005300 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005301 }
5302 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005303 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005304 if (status != 0)
5305 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005306 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005307 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005308 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5309 goto out;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005310 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005311 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005312 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04005313 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005314 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005315 status = PTR_ERR(task);
5316 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005317 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005318 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005319 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005320out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005321 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005322 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005323 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324}
5325
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005326struct nfs4_lockdata {
5327 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005328 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005329 struct nfs4_lock_state *lsp;
5330 struct nfs_open_context *ctx;
5331 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005332 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005333 int rpc_status;
5334 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005335 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005336};
5337
5338static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005339 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5340 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005341{
5342 struct nfs4_lockdata *p;
5343 struct inode *inode = lsp->ls_state->inode;
5344 struct nfs_server *server = NFS_SERVER(inode);
5345
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005346 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005347 if (p == NULL)
5348 return NULL;
5349
5350 p->arg.fh = NFS_FH(inode);
5351 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005352 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005353 if (p->arg.open_seqid == NULL)
5354 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005355 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005356 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005357 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005358 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04005359 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005360 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005361 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005362 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005363 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005364 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005365 atomic_inc(&lsp->ls_count);
5366 p->ctx = get_nfs_open_context(ctx);
5367 memcpy(&p->fl, fl, sizeof(p->fl));
5368 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005369out_free_seqid:
5370 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005371out_free:
5372 kfree(p);
5373 return NULL;
5374}
5375
5376static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5377{
5378 struct nfs4_lockdata *data = calldata;
5379 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380
Harvey Harrison3110ff82008-05-02 13:42:44 -07005381 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005382 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005383 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005384 /* Do we need to do an open_to_lock_owner? */
5385 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005386 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005387 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005388 }
Trond Myklebust8e472f32013-04-20 01:30:53 -04005389 data->arg.open_stateid = &state->open_stateid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005390 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005391 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005392 } else
5393 data->arg.new_lock_owner = 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04005394 if (!nfs4_valid_open_stateid(state)) {
5395 data->rpc_status = -EBADF;
5396 task->tk_action = NULL;
5397 goto out_release_open_seqid;
5398 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005399 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005400 if (nfs4_setup_sequence(data->server,
5401 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005402 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005403 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04005404 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04005405out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005406 nfs_release_seqid(data->arg.open_seqid);
5407out_release_lock_seqid:
5408 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005409out_wait:
5410 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005411 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005412}
5413
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005414static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5415{
5416 struct nfs4_lockdata *data = calldata;
5417
Harvey Harrison3110ff82008-05-02 13:42:44 -07005418 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005419
Trond Myklebust14516c32010-07-31 14:29:06 -04005420 if (!nfs4_sequence_done(task, &data->res.seq_res))
5421 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04005422
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005423 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005424 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005425 if (data->rpc_status == 0)
5426 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
5427 else
5428 goto out;
5429 }
5430 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05005431 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005432 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04005433 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005434 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005435out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07005436 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005437}
5438
5439static void nfs4_lock_release(void *calldata)
5440{
5441 struct nfs4_lockdata *data = calldata;
5442
Harvey Harrison3110ff82008-05-02 13:42:44 -07005443 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005444 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005445 if (data->cancelled != 0) {
5446 struct rpc_task *task;
5447 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5448 data->arg.lock_seqid);
5449 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08005450 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005451 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005452 } else
5453 nfs_free_seqid(data->arg.lock_seqid);
5454 nfs4_put_lock_state(data->lsp);
5455 put_nfs_open_context(data->ctx);
5456 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005457 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005458}
5459
5460static const struct rpc_call_ops nfs4_lock_ops = {
5461 .rpc_call_prepare = nfs4_lock_prepare,
5462 .rpc_call_done = nfs4_lock_done,
5463 .rpc_release = nfs4_lock_release,
5464};
5465
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005466static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5467{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005468 switch (error) {
5469 case -NFS4ERR_ADMIN_REVOKED:
5470 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05005471 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005472 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04005473 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05005474 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005475 break;
5476 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005477 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05005478 case -NFS4ERR_EXPIRED:
5479 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005480 };
5481}
5482
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005483static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005484{
5485 struct nfs4_lockdata *data;
5486 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005487 struct rpc_message msg = {
5488 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5489 .rpc_cred = state->owner->so_cred,
5490 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005491 struct rpc_task_setup task_setup_data = {
5492 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005493 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005494 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005495 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005496 .flags = RPC_TASK_ASYNC,
5497 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005498 int ret;
5499
Harvey Harrison3110ff82008-05-02 13:42:44 -07005500 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005501 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005502 fl->fl_u.nfs4_fl.owner,
5503 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005504 if (data == NULL)
5505 return -ENOMEM;
5506 if (IS_SETLKW(cmd))
5507 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04005508 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005509 msg.rpc_argp = &data->arg;
5510 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005511 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005512 if (recovery_type > NFS_LOCK_NEW) {
5513 if (recovery_type == NFS_LOCK_RECLAIM)
5514 data->arg.reclaim = NFS_LOCK_RECLAIM;
5515 nfs4_set_sequence_privileged(&data->arg.seq_args);
5516 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04005517 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005518 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005519 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005520 ret = nfs4_wait_for_completion_rpc_task(task);
5521 if (ret == 0) {
5522 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005523 if (ret)
5524 nfs4_handle_setlk_error(data->server, data->lsp,
5525 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005526 } else
5527 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005528 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005529 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005530 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531}
5532
5533static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5534{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005535 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005536 struct nfs4_exception exception = {
5537 .inode = state->inode,
5538 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005539 int err;
5540
5541 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005542 /* Cache the lock if possible... */
5543 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5544 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005545 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005546 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
Trond Myklebust168667c2010-10-19 19:47:49 -04005547 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00005548 break;
5549 nfs4_handle_exception(server, err, &exception);
5550 } while (exception.retry);
5551 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552}
5553
5554static int nfs4_lock_expired(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
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005562 err = nfs4_set_lock_state(state, request);
5563 if (err != 0)
5564 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04005565 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10005566 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
5567 return 0;
5568 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005569 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005570 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5571 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005572 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005573 trace_nfs4_lock_expired(request, state, F_SETLK, err);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005574 switch (err) {
5575 default:
5576 goto out;
5577 case -NFS4ERR_GRACE:
5578 case -NFS4ERR_DELAY:
5579 nfs4_handle_exception(server, err, &exception);
5580 err = 0;
5581 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005582 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005583out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00005584 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585}
5586
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005587#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005588/**
5589 * nfs41_check_expired_locks - possibly free a lock stateid
5590 *
5591 * @state: NFSv4 state for an inode
5592 *
5593 * Returns NFS_OK if recovery for this stateid is now finished.
5594 * Otherwise a negative NFS4ERR value is returned.
5595 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005596static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005597{
Chuck Levereb64cf92012-07-11 16:30:05 -04005598 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005599 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005600 struct nfs_server *server = NFS_SERVER(state->inode);
5601
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005602 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04005603 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005604 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
5605
5606 status = nfs41_test_stateid(server,
5607 &lsp->ls_stateid,
5608 cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04005609 trace_nfs4_test_lock_stateid(state, lsp, status);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005610 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005611 /* Free the stateid unless the server
5612 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04005613 if (status != -NFS4ERR_BAD_STATEID)
5614 nfs41_free_stateid(server,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005615 &lsp->ls_stateid,
5616 cred);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005617 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005618 ret = status;
5619 }
5620 }
5621 };
5622
5623 return ret;
5624}
5625
5626static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5627{
5628 int status = NFS_OK;
5629
5630 if (test_bit(LK_STATE_IN_USE, &state->flags))
5631 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005632 if (status != NFS_OK)
5633 status = nfs4_lock_expired(state, request);
5634 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005635}
5636#endif
5637
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5639{
Trond Myklebust9a99af492013-02-04 20:17:49 -05005640 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005641 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005642 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005643 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005644 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005646 if ((fl_flags & FL_POSIX) &&
5647 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5648 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005649 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005650 status = nfs4_set_lock_state(state, request);
5651 if (status != 0)
5652 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005653 request->fl_flags |= FL_ACCESS;
5654 status = do_vfs_lock(request->fl_file, request);
5655 if (status < 0)
5656 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005657 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005658 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005659 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005660 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005661 request->fl_flags = fl_flags & ~FL_SLEEP;
5662 status = do_vfs_lock(request->fl_file, request);
5663 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005664 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05005665 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5666 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005667 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005668 if (status != 0)
Trond Myklebust9a99af492013-02-04 20:17:49 -05005669 goto out;
5670 down_read(&nfsi->rwsem);
5671 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5672 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005673 goto out_unlock;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005674 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005675 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005676 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005677 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005678 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5679 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005680out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005681 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005682out:
5683 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684 return status;
5685}
5686
5687static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5688{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005689 struct nfs4_exception exception = {
5690 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005691 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005692 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693 int err;
5694
5695 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005696 err = _nfs4_proc_setlk(state, cmd, request);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005697 trace_nfs4_set_lock(request, state, cmd, err);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005698 if (err == -NFS4ERR_DENIED)
5699 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005701 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702 } while (exception.retry);
5703 return err;
5704}
5705
5706static int
5707nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5708{
5709 struct nfs_open_context *ctx;
5710 struct nfs4_state *state;
5711 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5712 int status;
5713
5714 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005715 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716 state = ctx->state;
5717
5718 if (request->fl_start < 0 || request->fl_end < 0)
5719 return -EINVAL;
5720
Trond Myklebustd9531262009-07-21 19:22:38 -04005721 if (IS_GETLK(cmd)) {
5722 if (state != NULL)
5723 return nfs4_proc_getlk(state, F_GETLK, request);
5724 return 0;
5725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726
5727 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5728 return -EINVAL;
5729
Trond Myklebustd9531262009-07-21 19:22:38 -04005730 if (request->fl_type == F_UNLCK) {
5731 if (state != NULL)
5732 return nfs4_proc_unlck(state, cmd, request);
5733 return 0;
5734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735
Trond Myklebustd9531262009-07-21 19:22:38 -04005736 if (state == NULL)
5737 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005738 /*
5739 * Don't rely on the VFS having checked the file open mode,
5740 * since it won't do this for flock() locks.
5741 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005742 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005743 case F_RDLCK:
5744 if (!(filp->f_mode & FMODE_READ))
5745 return -EBADF;
5746 break;
5747 case F_WRLCK:
5748 if (!(filp->f_mode & FMODE_WRITE))
5749 return -EBADF;
5750 }
5751
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752 do {
5753 status = nfs4_proc_setlk(state, cmd, request);
5754 if ((status != -EAGAIN) || IS_SETLK(cmd))
5755 break;
5756 timeout = nfs4_set_lock_task_retry(timeout);
5757 status = -ERESTARTSYS;
5758 if (signalled())
5759 break;
5760 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761 return status;
5762}
5763
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005764int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05005765{
5766 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05005767 int err;
5768
5769 err = nfs4_set_lock_state(state, fl);
5770 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005771 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04005772 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005773 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05005774}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005775
Trond Myklebustcf470c32012-03-07 13:49:12 -05005776struct nfs_release_lockowner_data {
5777 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005778 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005779 struct nfs_release_lockowner_args args;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005780 struct nfs4_sequence_args seq_args;
5781 struct nfs4_sequence_res seq_res;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005782};
5783
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005784static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
5785{
5786 struct nfs_release_lockowner_data *data = calldata;
5787 nfs40_setup_sequence(data->server,
5788 &data->seq_args, &data->seq_res, task);
5789}
5790
5791static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
5792{
5793 struct nfs_release_lockowner_data *data = calldata;
5794 nfs40_sequence_done(task, &data->seq_res);
5795}
5796
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005797static void nfs4_release_lockowner_release(void *calldata)
5798{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005799 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005800 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005801 kfree(calldata);
5802}
5803
Trond Myklebust17280172012-03-11 13:11:00 -04005804static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005805 .rpc_call_prepare = nfs4_release_lockowner_prepare,
5806 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005807 .rpc_release = nfs4_release_lockowner_release,
5808};
5809
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04005810static int nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005811{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005812 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005813 struct rpc_message msg = {
5814 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5815 };
5816
5817 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005818 return -EINVAL;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005819
Trond Myklebustcf470c32012-03-07 13:49:12 -05005820 data = kmalloc(sizeof(*data), GFP_NOFS);
5821 if (!data)
5822 return -ENOMEM;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005823 nfs4_init_sequence(&data->seq_args, &data->seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05005824 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005825 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005826 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5827 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5828 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005829
Trond Myklebustcf470c32012-03-07 13:49:12 -05005830 msg.rpc_argp = &data->args;
5831 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5832 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005833}
5834
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005835#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5836
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005837static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5838 const void *buf, size_t buflen,
5839 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005840{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005841 if (strcmp(key, "") != 0)
5842 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005843
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005844 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005845}
5846
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005847static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5848 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005849{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005850 if (strcmp(key, "") != 0)
5851 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005852
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005853 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005854}
5855
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005856static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5857 size_t list_len, const char *name,
5858 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005859{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005860 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005861
J. Bruce Fields096455a2006-03-20 23:23:42 -05005862 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5863 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005864
5865 if (list && len <= list_len)
5866 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005867 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005868}
5869
David Quigleyc9bccef2013-05-22 12:50:45 -04005870#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5871static inline int nfs4_server_supports_labels(struct nfs_server *server)
5872{
5873 return server->caps & NFS_CAP_SECURITY_LABEL;
5874}
5875
5876static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
5877 const void *buf, size_t buflen,
5878 int flags, int type)
5879{
5880 if (security_ismaclabel(key))
5881 return nfs4_set_security_label(dentry, buf, buflen);
5882
5883 return -EOPNOTSUPP;
5884}
5885
5886static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
5887 void *buf, size_t buflen, int type)
5888{
5889 if (security_ismaclabel(key))
5890 return nfs4_get_security_label(dentry->d_inode, buf, buflen);
5891 return -EOPNOTSUPP;
5892}
5893
5894static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
5895 size_t list_len, const char *name,
5896 size_t name_len, int type)
5897{
5898 size_t len = 0;
5899
5900 if (nfs_server_capable(dentry->d_inode, NFS_CAP_SECURITY_LABEL)) {
5901 len = security_inode_listsecurity(dentry->d_inode, NULL, 0);
5902 if (list && len <= list_len)
5903 security_inode_listsecurity(dentry->d_inode, list, len);
5904 }
5905 return len;
5906}
5907
5908static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
5909 .prefix = XATTR_SECURITY_PREFIX,
5910 .list = nfs4_xattr_list_nfs4_label,
5911 .get = nfs4_xattr_get_nfs4_label,
5912 .set = nfs4_xattr_set_nfs4_label,
5913};
5914#endif
5915
5916
Andy Adamson533eb462011-06-13 18:25:56 -04005917/*
5918 * nfs_fhget will use either the mounted_on_fileid or the fileid
5919 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005920static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5921{
Andy Adamson533eb462011-06-13 18:25:56 -04005922 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5923 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5924 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005925 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005926 return;
5927
5928 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005929 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005930 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5931 fattr->nlink = 2;
5932}
5933
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005934static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5935 const struct qstr *name,
5936 struct nfs4_fs_locations *fs_locations,
5937 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005938{
5939 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04005940 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005941 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005942 };
5943 struct nfs4_fs_locations_arg args = {
5944 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005945 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005946 .page = page,
5947 .bitmask = bitmask,
5948 };
Benny Halevy22958462009-04-01 09:22:02 -04005949 struct nfs4_fs_locations_res res = {
5950 .fs_locations = fs_locations,
5951 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005952 struct rpc_message msg = {
5953 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5954 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005955 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005956 };
5957 int status;
5958
Harvey Harrison3110ff82008-05-02 13:42:44 -07005959 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005960
5961 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5962 * is not supported */
5963 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5964 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5965 else
5966 bitmask[0] |= FATTR4_WORD0_FILEID;
5967
Trond Myklebustc228fd32007-01-13 02:28:11 -05005968 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005969 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005970 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005971 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005972 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005973 return status;
5974}
5975
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005976int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5977 const struct qstr *name,
5978 struct nfs4_fs_locations *fs_locations,
5979 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005980{
5981 struct nfs4_exception exception = { };
5982 int err;
5983 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04005984 err = _nfs4_proc_fs_locations(client, dir, name,
5985 fs_locations, page);
5986 trace_nfs4_get_fs_locations(dir, name, err);
5987 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005988 &exception);
5989 } while (exception.retry);
5990 return err;
5991}
5992
Andy Adamson5ec16a82013-08-08 10:57:55 -04005993/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04005994 * If 'use_integrity' is true and the state managment nfs_client
5995 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
5996 * and the machine credential as per RFC3530bis and RFC5661 Security
5997 * Considerations sections. Otherwise, just use the user cred with the
5998 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04005999 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006000static 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 +00006001{
6002 int status;
6003 struct nfs4_secinfo_arg args = {
6004 .dir_fh = NFS_FH(dir),
6005 .name = name,
6006 };
6007 struct nfs4_secinfo_res res = {
6008 .flavors = flavors,
6009 };
6010 struct rpc_message msg = {
6011 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6012 .rpc_argp = &args,
6013 .rpc_resp = &res,
6014 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006015 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006016 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006017
6018 if (use_integrity) {
6019 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006020 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6021 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006022 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006023
6024 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006025
6026 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6027 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6028
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006029 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6030 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006031 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006032
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006033 if (cred)
6034 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006035
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006036 return status;
6037}
6038
Bryan Schumaker72de53e2012-04-27 13:27:40 -04006039int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6040 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006041{
6042 struct nfs4_exception exception = { };
6043 int err;
6044 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006045 err = -NFS4ERR_WRONGSEC;
6046
6047 /* try to use integrity protection with machine cred */
6048 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6049 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6050
6051 /*
6052 * if unable to use integrity protection, or SECINFO with
6053 * integrity protection returns NFS4ERR_WRONGSEC (which is
6054 * disallowed by spec, but exists in deployed servers) use
6055 * the current filesystem's rpc_client and the user cred.
6056 */
6057 if (err == -NFS4ERR_WRONGSEC)
6058 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6059
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006060 trace_nfs4_secinfo(dir, name, err);
6061 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006062 &exception);
6063 } while (exception.retry);
6064 return err;
6065}
6066
Andy Adamson557134a2009-04-01 09:21:53 -04006067#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04006068/*
Andy Adamson357f54d2010-12-14 10:11:57 -05006069 * Check the exchange flags returned by the server for invalid flags, having
6070 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6071 * DS flags set.
6072 */
6073static int nfs4_check_cl_exchange_flags(u32 flags)
6074{
6075 if (flags & ~EXCHGID4_FLAG_MASK_R)
6076 goto out_inval;
6077 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6078 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6079 goto out_inval;
6080 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6081 goto out_inval;
6082 return NFS_OK;
6083out_inval:
6084 return -NFS4ERR_INVAL;
6085}
6086
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006087static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006088nfs41_same_server_scope(struct nfs41_server_scope *a,
6089 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006090{
6091 if (a->server_scope_sz == b->server_scope_sz &&
6092 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6093 return true;
6094
6095 return false;
6096}
6097
Andy Adamson357f54d2010-12-14 10:11:57 -05006098/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006099 * nfs4_proc_bind_conn_to_session()
6100 *
6101 * The 4.1 client currently uses the same TCP connection for the
6102 * fore and backchannel.
6103 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006104int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006105{
6106 int status;
6107 struct nfs41_bind_conn_to_session_res res;
6108 struct rpc_message msg = {
6109 .rpc_proc =
6110 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
6111 .rpc_argp = clp,
6112 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006113 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006114 };
6115
6116 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006117
6118 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
6119 if (unlikely(res.session == NULL)) {
6120 status = -ENOMEM;
6121 goto out;
6122 }
6123
6124 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006125 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006126 if (status == 0) {
6127 if (memcmp(res.session->sess_id.data,
6128 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6129 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6130 status = -EIO;
6131 goto out_session;
6132 }
6133 if (res.dir != NFS4_CDFS4_BOTH) {
6134 dprintk("NFS: %s: Unexpected direction from server\n",
6135 __func__);
6136 status = -EIO;
6137 goto out_session;
6138 }
6139 if (res.use_conn_in_rdma_mode) {
6140 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6141 __func__);
6142 status = -EIO;
6143 goto out_session;
6144 }
6145 }
6146out_session:
6147 kfree(res.session);
6148out:
6149 dprintk("<-- %s status= %d\n", __func__, status);
6150 return status;
6151}
6152
6153/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006154 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6155 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04006156 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006157static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
6158 .how = SP4_MACH_CRED,
6159 .enforce.u.words = {
6160 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6161 1 << (OP_EXCHANGE_ID - 32) |
6162 1 << (OP_CREATE_SESSION - 32) |
6163 1 << (OP_DESTROY_SESSION - 32) |
6164 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006165 },
6166 .allow.u.words = {
6167 [0] = 1 << (OP_CLOSE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006168 1 << (OP_LOCKU) |
6169 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006170 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006171 1 << (OP_SECINFO_NO_NAME - 32) |
6172 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006173 1 << (OP_FREE_STATEID - 32) |
6174 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006175 }
6176};
6177
6178/*
6179 * Select the state protection mode for client `clp' given the server results
6180 * from exchange_id in `sp'.
6181 *
6182 * Returns 0 on success, negative errno otherwise.
6183 */
6184static int nfs4_sp4_select_mode(struct nfs_client *clp,
6185 struct nfs41_state_protection *sp)
6186{
6187 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
6188 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6189 1 << (OP_EXCHANGE_ID - 32) |
6190 1 << (OP_CREATE_SESSION - 32) |
6191 1 << (OP_DESTROY_SESSION - 32) |
6192 1 << (OP_DESTROY_CLIENTID - 32)
6193 };
6194 unsigned int i;
6195
6196 if (sp->how == SP4_MACH_CRED) {
6197 /* Print state protect result */
6198 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
6199 for (i = 0; i <= LAST_NFS4_OP; i++) {
6200 if (test_bit(i, sp->enforce.u.longs))
6201 dfprintk(MOUNT, " enforce op %d\n", i);
6202 if (test_bit(i, sp->allow.u.longs))
6203 dfprintk(MOUNT, " allow op %d\n", i);
6204 }
6205
6206 /* make sure nothing is on enforce list that isn't supported */
6207 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
6208 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
6209 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6210 return -EINVAL;
6211 }
6212 }
6213
6214 /*
6215 * Minimal mode - state operations are allowed to use machine
6216 * credential. Note this already happens by default, so the
6217 * client doesn't have to do anything more than the negotiation.
6218 *
6219 * NOTE: we don't care if EXCHANGE_ID is in the list -
6220 * we're already using the machine cred for exchange_id
6221 * and will never use a different cred.
6222 */
6223 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
6224 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
6225 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
6226 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
6227 dfprintk(MOUNT, "sp4_mach_cred:\n");
6228 dfprintk(MOUNT, " minimal mode enabled\n");
6229 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
6230 } else {
6231 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6232 return -EINVAL;
6233 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006234
6235 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
6236 test_bit(OP_LOCKU, sp->allow.u.longs)) {
6237 dfprintk(MOUNT, " cleanup mode enabled\n");
6238 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
6239 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006240
6241 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
6242 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
6243 dfprintk(MOUNT, " secinfo mode enabled\n");
6244 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
6245 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006246
6247 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
6248 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
6249 dfprintk(MOUNT, " stateid mode enabled\n");
6250 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
6251 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04006252
6253 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
6254 dfprintk(MOUNT, " write mode enabled\n");
6255 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
6256 }
6257
6258 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
6259 dfprintk(MOUNT, " commit mode enabled\n");
6260 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
6261 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006262 }
6263
6264 return 0;
6265}
6266
6267/*
6268 * _nfs4_proc_exchange_id()
6269 *
6270 * Wrapper for EXCHANGE_ID operation.
6271 */
6272static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
6273 u32 sp4_how)
Benny Halevy99fe60d2009-04-01 09:22:29 -04006274{
6275 nfs4_verifier verifier;
6276 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05006277 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04006278 .client = clp,
Trond Myklebust4f0b4292013-05-20 12:24:03 -04006279 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6280 EXCHGID4_FLAG_BIND_PRINC_STATEID,
Benny Halevy99fe60d2009-04-01 09:22:29 -04006281 };
6282 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04006283 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04006284 };
6285 int status;
6286 struct rpc_message msg = {
6287 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
6288 .rpc_argp = &args,
6289 .rpc_resp = &res,
6290 .rpc_cred = cred,
6291 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04006292
Chuck Leverf0920752012-05-21 22:45:41 -04006293 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04006294 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
6295 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006296 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
6297 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6298 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006299
Chuck Leveracdeb692012-05-21 22:46:16 -04006300 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006301 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04006302 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006303 status = -ENOMEM;
6304 goto out;
6305 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006306
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006307 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006308 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006309 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04006310 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04006311 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04006312 }
6313
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006314 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006315 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006316 status = -ENOMEM;
6317 goto out_server_scope;
6318 }
6319
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006320 switch (sp4_how) {
6321 case SP4_NONE:
6322 args.state_protect.how = SP4_NONE;
6323 break;
6324
6325 case SP4_MACH_CRED:
6326 args.state_protect = nfs4_sp4_mach_cred_request;
6327 break;
6328
6329 default:
6330 /* unsupported! */
6331 WARN_ON_ONCE(1);
6332 status = -EINVAL;
6333 goto out_server_scope;
6334 }
6335
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006336 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006337 trace_nfs4_exchange_id(clp, status);
Chuck Lever177313f2012-05-21 22:44:58 -04006338 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04006339 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006340
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006341 if (status == 0)
6342 status = nfs4_sp4_select_mode(clp, &res.state_protect);
6343
Chuck Lever177313f2012-05-21 22:44:58 -04006344 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04006345 clp->cl_clientid = res.clientid;
6346 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
6347 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
6348 clp->cl_seqid = res.seqid;
6349
Chuck Leveracdeb692012-05-21 22:46:16 -04006350 kfree(clp->cl_serverowner);
6351 clp->cl_serverowner = res.server_owner;
6352 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04006353
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006354 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04006355 kfree(clp->cl_implid);
6356 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006357
Chuck Lever177313f2012-05-21 22:44:58 -04006358 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006359 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006360 res.server_scope)) {
6361 dprintk("%s: server_scope mismatch detected\n",
6362 __func__);
6363 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006364 kfree(clp->cl_serverscope);
6365 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006366 }
6367
Chuck Lever177313f2012-05-21 22:44:58 -04006368 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006369 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006370 goto out;
6371 }
Trond Myklebust32b01312012-05-26 13:41:04 -04006372 } else
6373 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006374
Chuck Leveracdeb692012-05-21 22:46:16 -04006375out_server_owner:
6376 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006377out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006378 kfree(res.server_scope);
6379out:
Chuck Lever177313f2012-05-21 22:44:58 -04006380 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006381 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006382 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006383 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04006384 clp->cl_implid->date.seconds,
6385 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006386 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006387 return status;
6388}
6389
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006390/*
6391 * nfs4_proc_exchange_id()
6392 *
6393 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6394 *
6395 * Since the clientid has expired, all compounds using sessions
6396 * associated with the stale clientid will be returning
6397 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
6398 * be in some phase of session reset.
6399 *
6400 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
6401 */
6402int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
6403{
6404 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
6405 int status;
6406
6407 /* try SP4_MACH_CRED if krb5i/p */
6408 if (authflavor == RPC_AUTH_GSS_KRB5I ||
6409 authflavor == RPC_AUTH_GSS_KRB5P) {
6410 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
6411 if (!status)
6412 return 0;
6413 }
6414
6415 /* try SP4_NONE */
6416 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
6417}
6418
Trond Myklebust66245532012-05-25 17:18:09 -04006419static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
6420 struct rpc_cred *cred)
6421{
6422 struct rpc_message msg = {
6423 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
6424 .rpc_argp = clp,
6425 .rpc_cred = cred,
6426 };
6427 int status;
6428
6429 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006430 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04006431 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04006432 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04006433 "DESTROY_CLIENTID.", status, clp->cl_hostname);
6434 return status;
6435}
6436
6437static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
6438 struct rpc_cred *cred)
6439{
6440 unsigned int loop;
6441 int ret;
6442
6443 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
6444 ret = _nfs4_proc_destroy_clientid(clp, cred);
6445 switch (ret) {
6446 case -NFS4ERR_DELAY:
6447 case -NFS4ERR_CLIENTID_BUSY:
6448 ssleep(1);
6449 break;
6450 default:
6451 return ret;
6452 }
6453 }
6454 return 0;
6455}
6456
6457int nfs4_destroy_clientid(struct nfs_client *clp)
6458{
6459 struct rpc_cred *cred;
6460 int ret = 0;
6461
6462 if (clp->cl_mvops->minor_version < 1)
6463 goto out;
6464 if (clp->cl_exchange_flags == 0)
6465 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04006466 if (clp->cl_preserve_clid)
6467 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04006468 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04006469 ret = nfs4_proc_destroy_clientid(clp, cred);
6470 if (cred)
6471 put_rpccred(cred);
6472 switch (ret) {
6473 case 0:
6474 case -NFS4ERR_STALE_CLIENTID:
6475 clp->cl_exchange_flags = 0;
6476 }
6477out:
6478 return ret;
6479}
6480
Andy Adamson2050f0c2009-04-01 09:22:30 -04006481struct nfs4_get_lease_time_data {
6482 struct nfs4_get_lease_time_args *args;
6483 struct nfs4_get_lease_time_res *res;
6484 struct nfs_client *clp;
6485};
6486
6487static void nfs4_get_lease_time_prepare(struct rpc_task *task,
6488 void *calldata)
6489{
Andy Adamson2050f0c2009-04-01 09:22:30 -04006490 struct nfs4_get_lease_time_data *data =
6491 (struct nfs4_get_lease_time_data *)calldata;
6492
6493 dprintk("--> %s\n", __func__);
6494 /* just setup sequence, do not trigger session recovery
6495 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006496 nfs41_setup_sequence(data->clp->cl_session,
6497 &data->args->la_seq_args,
6498 &data->res->lr_seq_res,
6499 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006500 dprintk("<-- %s\n", __func__);
6501}
6502
6503/*
6504 * Called from nfs4_state_manager thread for session setup, so don't recover
6505 * from sequence operation or clientid errors.
6506 */
6507static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
6508{
6509 struct nfs4_get_lease_time_data *data =
6510 (struct nfs4_get_lease_time_data *)calldata;
6511
6512 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006513 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
6514 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04006515 switch (task->tk_status) {
6516 case -NFS4ERR_DELAY:
6517 case -NFS4ERR_GRACE:
6518 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
6519 rpc_delay(task, NFS4_POLL_RETRY_MIN);
6520 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006521 /* fall through */
6522 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006523 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006524 return;
6525 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04006526 dprintk("<-- %s\n", __func__);
6527}
6528
Trond Myklebust17280172012-03-11 13:11:00 -04006529static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04006530 .rpc_call_prepare = nfs4_get_lease_time_prepare,
6531 .rpc_call_done = nfs4_get_lease_time_done,
6532};
6533
6534int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
6535{
6536 struct rpc_task *task;
6537 struct nfs4_get_lease_time_args args;
6538 struct nfs4_get_lease_time_res res = {
6539 .lr_fsinfo = fsinfo,
6540 };
6541 struct nfs4_get_lease_time_data data = {
6542 .args = &args,
6543 .res = &res,
6544 .clp = clp,
6545 };
6546 struct rpc_message msg = {
6547 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
6548 .rpc_argp = &args,
6549 .rpc_resp = &res,
6550 };
6551 struct rpc_task_setup task_setup = {
6552 .rpc_client = clp->cl_rpcclient,
6553 .rpc_message = &msg,
6554 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006555 .callback_data = &data,
6556 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04006557 };
6558 int status;
6559
Chuck Levera9c92d62013-08-09 12:48:18 -04006560 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006561 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006562 dprintk("--> %s\n", __func__);
6563 task = rpc_run_task(&task_setup);
6564
6565 if (IS_ERR(task))
6566 status = PTR_ERR(task);
6567 else {
6568 status = task->tk_status;
6569 rpc_put_task(task);
6570 }
6571 dprintk("<-- %s return %d\n", __func__, status);
6572
6573 return status;
6574}
6575
Andy Adamsonfc931582009-04-01 09:22:31 -04006576/*
6577 * Initialize the values to be used by the client in CREATE_SESSION
6578 * If nfs4_init_session set the fore channel request and response sizes,
6579 * use them.
6580 *
6581 * Set the back channel max_resp_sz_cached to zero to force the client to
6582 * always set csa_cachethis to FALSE because the current implementation
6583 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
6584 */
6585static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
6586{
Andy Adamson18aad3d2013-06-26 12:21:49 -04006587 unsigned int max_rqst_sz, max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006588
Andy Adamson18aad3d2013-06-26 12:21:49 -04006589 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
6590 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
6591
Andy Adamsonfc931582009-04-01 09:22:31 -04006592 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04006593 args->fc_attrs.max_rqst_sz = max_rqst_sz;
6594 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006595 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05006596 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04006597
6598 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05006599 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04006600 __func__,
6601 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05006602 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04006603
6604 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04006605 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
6606 args->bc_attrs.max_resp_sz = PAGE_SIZE;
6607 args->bc_attrs.max_resp_sz_cached = 0;
6608 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
6609 args->bc_attrs.max_reqs = 1;
6610
6611 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
6612 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
6613 __func__,
6614 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
6615 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
6616 args->bc_attrs.max_reqs);
6617}
6618
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006619static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04006620{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006621 struct nfs4_channel_attrs *sent = &args->fc_attrs;
6622 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
6623
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006624 if (rcvd->max_resp_sz > sent->max_resp_sz)
6625 return -EINVAL;
6626 /*
6627 * Our requested max_ops is the minimum we need; we're not
6628 * prepared to break up compounds into smaller pieces than that.
6629 * So, no point even trying to continue if the server won't
6630 * cooperate:
6631 */
6632 if (rcvd->max_ops < sent->max_ops)
6633 return -EINVAL;
6634 if (rcvd->max_reqs == 0)
6635 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006636 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
6637 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006638 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04006639}
6640
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006641static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
6642{
6643 struct nfs4_channel_attrs *sent = &args->bc_attrs;
6644 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04006645
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006646 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
6647 return -EINVAL;
6648 if (rcvd->max_resp_sz < sent->max_resp_sz)
6649 return -EINVAL;
6650 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
6651 return -EINVAL;
6652 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006653 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006654 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006655 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006656 return -EINVAL;
6657 return 0;
6658}
Andy Adamson8d353012009-04-01 09:22:32 -04006659
Andy Adamson8d353012009-04-01 09:22:32 -04006660static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
6661 struct nfs4_session *session)
6662{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006663 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04006664
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006665 ret = nfs4_verify_fore_channel_attrs(args, session);
6666 if (ret)
6667 return ret;
6668 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04006669}
6670
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006671static int _nfs4_proc_create_session(struct nfs_client *clp,
6672 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04006673{
6674 struct nfs4_session *session = clp->cl_session;
6675 struct nfs41_create_session_args args = {
6676 .client = clp,
6677 .cb_program = NFS4_CALLBACK,
6678 };
6679 struct nfs41_create_session_res res = {
6680 .client = clp,
6681 };
6682 struct rpc_message msg = {
6683 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
6684 .rpc_argp = &args,
6685 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006686 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04006687 };
6688 int status;
6689
6690 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04006691 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04006692
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006693 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006694 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04006695
Trond Myklebust43095d32012-11-20 11:13:12 -05006696 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04006697 /* Verify the session's negotiated channel_attrs values */
6698 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04006699 /* Increment the clientid slot sequence id */
6700 clp->cl_seqid++;
6701 }
6702
6703 return status;
6704}
6705
6706/*
6707 * Issues a CREATE_SESSION operation to the server.
6708 * It is the responsibility of the caller to verify the session is
6709 * expired before calling this routine.
6710 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006711int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04006712{
6713 int status;
6714 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04006715 struct nfs4_session *session = clp->cl_session;
6716
6717 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
6718
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006719 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04006720 if (status)
6721 goto out;
6722
Andy Adamsonaacd5532011-11-09 13:58:21 -05006723 /* Init or reset the session slot tables */
6724 status = nfs4_setup_session_slot_tables(session);
6725 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04006726 if (status)
6727 goto out;
6728
6729 ptr = (unsigned *)&session->sess_id.data[0];
6730 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
6731 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04006732out:
6733 dprintk("<-- %s\n", __func__);
6734 return status;
6735}
6736
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006737/*
6738 * Issue the over-the-wire RPC DESTROY_SESSION.
6739 * The caller must serialize access to this routine.
6740 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006741int nfs4_proc_destroy_session(struct nfs4_session *session,
6742 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006743{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006744 struct rpc_message msg = {
6745 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
6746 .rpc_argp = session,
6747 .rpc_cred = cred,
6748 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006749 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006750
6751 dprintk("--> nfs4_proc_destroy_session\n");
6752
6753 /* session is still being setup */
6754 if (session->clp->cl_cons_state != NFS_CS_READY)
6755 return status;
6756
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006757 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006758 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006759
6760 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04006761 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006762 "Session has been destroyed regardless...\n", status);
6763
6764 dprintk("<-- nfs4_proc_destroy_session\n");
6765 return status;
6766}
6767
Trond Myklebust7b38c362012-05-23 13:23:31 -04006768/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006769 * Renew the cl_session lease.
6770 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006771struct nfs4_sequence_data {
6772 struct nfs_client *clp;
6773 struct nfs4_sequence_args args;
6774 struct nfs4_sequence_res res;
6775};
6776
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006777static void nfs41_sequence_release(void *data)
6778{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006779 struct nfs4_sequence_data *calldata = data;
6780 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006781
Alexandros Batsakis71358402010-02-05 03:45:05 -08006782 if (atomic_read(&clp->cl_count) > 1)
6783 nfs4_schedule_state_renewal(clp);
6784 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006785 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006786}
6787
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006788static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6789{
6790 switch(task->tk_status) {
6791 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006792 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6793 return -EAGAIN;
6794 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006795 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006796 }
6797 return 0;
6798}
6799
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006800static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006801{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006802 struct nfs4_sequence_data *calldata = data;
6803 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006804
Trond Myklebust14516c32010-07-31 14:29:06 -04006805 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6806 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006807
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006808 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006809 if (task->tk_status < 0) {
6810 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006811 if (atomic_read(&clp->cl_count) == 1)
6812 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006813
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006814 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6815 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006816 return;
6817 }
6818 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006819 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006820out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006821 dprintk("<-- %s\n", __func__);
6822}
6823
6824static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6825{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006826 struct nfs4_sequence_data *calldata = data;
6827 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006828 struct nfs4_sequence_args *args;
6829 struct nfs4_sequence_res *res;
6830
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006831 args = task->tk_msg.rpc_argp;
6832 res = task->tk_msg.rpc_resp;
6833
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006834 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006835}
6836
6837static const struct rpc_call_ops nfs41_sequence_ops = {
6838 .rpc_call_done = nfs41_sequence_call_done,
6839 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006840 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006841};
6842
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006843static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
6844 struct rpc_cred *cred,
6845 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006846{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006847 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006848 struct rpc_message msg = {
6849 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6850 .rpc_cred = cred,
6851 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006852 struct rpc_task_setup task_setup_data = {
6853 .rpc_client = clp->cl_rpcclient,
6854 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006855 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04006856 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006857 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006858
Alexandros Batsakis71358402010-02-05 03:45:05 -08006859 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006860 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006861 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006862 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006863 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006864 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006865 }
Chuck Levera9c92d62013-08-09 12:48:18 -04006866 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006867 if (is_privileged)
6868 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006869 msg.rpc_argp = &calldata->args;
6870 msg.rpc_resp = &calldata->res;
6871 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006872 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006873
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006874 return rpc_run_task(&task_setup_data);
6875}
6876
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006877static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006878{
6879 struct rpc_task *task;
6880 int ret = 0;
6881
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006882 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6883 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006884 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006885 if (IS_ERR(task))
6886 ret = PTR_ERR(task);
6887 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006888 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006889 dprintk("<-- %s status=%d\n", __func__, ret);
6890 return ret;
6891}
6892
6893static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6894{
6895 struct rpc_task *task;
6896 int ret;
6897
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006898 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006899 if (IS_ERR(task)) {
6900 ret = PTR_ERR(task);
6901 goto out;
6902 }
6903 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006904 if (!ret) {
6905 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6906
6907 if (task->tk_status == 0)
6908 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006909 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006910 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006911 rpc_put_task(task);
6912out:
6913 dprintk("<-- %s status=%d\n", __func__, ret);
6914 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006915}
6916
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006917struct nfs4_reclaim_complete_data {
6918 struct nfs_client *clp;
6919 struct nfs41_reclaim_complete_args arg;
6920 struct nfs41_reclaim_complete_res res;
6921};
6922
6923static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6924{
6925 struct nfs4_reclaim_complete_data *calldata = data;
6926
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006927 nfs41_setup_sequence(calldata->clp->cl_session,
6928 &calldata->arg.seq_args,
6929 &calldata->res.seq_res,
6930 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006931}
6932
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006933static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6934{
6935 switch(task->tk_status) {
6936 case 0:
6937 case -NFS4ERR_COMPLETE_ALREADY:
6938 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6939 break;
6940 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006941 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006942 /* fall through */
6943 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006944 return -EAGAIN;
6945 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006946 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006947 }
6948 return 0;
6949}
6950
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006951static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6952{
6953 struct nfs4_reclaim_complete_data *calldata = data;
6954 struct nfs_client *clp = calldata->clp;
6955 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6956
6957 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006958 if (!nfs41_sequence_done(task, res))
6959 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006960
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006961 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006962 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6963 rpc_restart_call_prepare(task);
6964 return;
6965 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006966 dprintk("<-- %s\n", __func__);
6967}
6968
6969static void nfs4_free_reclaim_complete_data(void *data)
6970{
6971 struct nfs4_reclaim_complete_data *calldata = data;
6972
6973 kfree(calldata);
6974}
6975
6976static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6977 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6978 .rpc_call_done = nfs4_reclaim_complete_done,
6979 .rpc_release = nfs4_free_reclaim_complete_data,
6980};
6981
6982/*
6983 * Issue a global reclaim complete.
6984 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04006985static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
6986 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006987{
6988 struct nfs4_reclaim_complete_data *calldata;
6989 struct rpc_task *task;
6990 struct rpc_message msg = {
6991 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04006992 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006993 };
6994 struct rpc_task_setup task_setup_data = {
6995 .rpc_client = clp->cl_rpcclient,
6996 .rpc_message = &msg,
6997 .callback_ops = &nfs4_reclaim_complete_call_ops,
6998 .flags = RPC_TASK_ASYNC,
6999 };
7000 int status = -ENOMEM;
7001
7002 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04007003 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007004 if (calldata == NULL)
7005 goto out;
7006 calldata->clp = clp;
7007 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007008
Chuck Levera9c92d62013-08-09 12:48:18 -04007009 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007010 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007011 msg.rpc_argp = &calldata->arg;
7012 msg.rpc_resp = &calldata->res;
7013 task_setup_data.callback_data = calldata;
7014 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007015 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007016 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007017 goto out;
7018 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05007019 status = nfs4_wait_for_completion_rpc_task(task);
7020 if (status == 0)
7021 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007022 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007023 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007024out:
7025 dprintk("<-- %s status=%d\n", __func__, status);
7026 return status;
7027}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007028
7029static void
7030nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
7031{
7032 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00007033 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007034 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007035
7036 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00007037 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
7038 * right now covering the LAYOUTGET we are about to send.
7039 * However, that is not so catastrophic, and there seems
7040 * to be no way to prevent it completely.
7041 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007042 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05007043 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007044 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00007045 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
7046 NFS_I(lgp->args.inode)->layout,
7047 lgp->args.ctx->state)) {
7048 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00007049 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007050}
7051
7052static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
7053{
7054 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07007055 struct inode *inode = lgp->args.inode;
7056 struct nfs_server *server = NFS_SERVER(inode);
7057 struct pnfs_layout_hdr *lo;
7058 struct nfs4_state *state = NULL;
Weston Andros Adamson30005122013-02-28 20:30:10 -05007059 unsigned long timeo, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007060
7061 dprintk("--> %s\n", __func__);
7062
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007063 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07007064 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007065
7066 switch (task->tk_status) {
7067 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07007068 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007069 case -NFS4ERR_LAYOUTTRYLATER:
7070 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05007071 timeo = rpc_get_timeout(task->tk_client);
7072 giveup = lgp->args.timestamp + timeo;
7073 if (time_after(giveup, jiffies))
7074 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07007075 break;
7076 case -NFS4ERR_EXPIRED:
7077 case -NFS4ERR_BAD_STATEID:
7078 spin_lock(&inode->i_lock);
7079 lo = NFS_I(inode)->layout;
7080 if (!lo || list_empty(&lo->plh_segs)) {
7081 spin_unlock(&inode->i_lock);
7082 /* If the open stateid was bad, then recover it. */
7083 state = lgp->args.ctx->state;
7084 } else {
7085 LIST_HEAD(head);
7086
7087 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
7088 spin_unlock(&inode->i_lock);
7089 /* Mark the bad layout state as invalid, then
7090 * retry using the open stateid. */
7091 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007092 }
7093 }
Trond Myklebustee314c22012-10-01 17:25:48 -07007094 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
7095 rpc_restart_call_prepare(task);
7096out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007097 dprintk("<-- %s\n", __func__);
7098}
7099
Idan Kedar85541162012-08-02 11:47:10 +03007100static size_t max_response_pages(struct nfs_server *server)
7101{
7102 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
7103 return nfs_page_array_len(0, max_resp_sz);
7104}
7105
7106static void nfs4_free_pages(struct page **pages, size_t size)
7107{
7108 int i;
7109
7110 if (!pages)
7111 return;
7112
7113 for (i = 0; i < size; i++) {
7114 if (!pages[i])
7115 break;
7116 __free_page(pages[i]);
7117 }
7118 kfree(pages);
7119}
7120
7121static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
7122{
7123 struct page **pages;
7124 int i;
7125
7126 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
7127 if (!pages) {
7128 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
7129 return NULL;
7130 }
7131
7132 for (i = 0; i < size; i++) {
7133 pages[i] = alloc_page(gfp_flags);
7134 if (!pages[i]) {
7135 dprintk("%s: failed to allocate page\n", __func__);
7136 nfs4_free_pages(pages, size);
7137 return NULL;
7138 }
7139 }
7140
7141 return pages;
7142}
7143
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007144static void nfs4_layoutget_release(void *calldata)
7145{
7146 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007147 struct inode *inode = lgp->args.inode;
7148 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007149 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007150
7151 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03007152 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007153 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007154 put_nfs_open_context(lgp->args.ctx);
7155 kfree(calldata);
7156 dprintk("<-- %s\n", __func__);
7157}
7158
7159static const struct rpc_call_ops nfs4_layoutget_call_ops = {
7160 .rpc_call_prepare = nfs4_layoutget_prepare,
7161 .rpc_call_done = nfs4_layoutget_done,
7162 .rpc_release = nfs4_layoutget_release,
7163};
7164
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007165struct pnfs_layout_segment *
7166nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007167{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007168 struct inode *inode = lgp->args.inode;
7169 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007170 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007171 struct rpc_task *task;
7172 struct rpc_message msg = {
7173 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
7174 .rpc_argp = &lgp->args,
7175 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04007176 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007177 };
7178 struct rpc_task_setup task_setup_data = {
7179 .rpc_client = server->client,
7180 .rpc_message = &msg,
7181 .callback_ops = &nfs4_layoutget_call_ops,
7182 .callback_data = lgp,
7183 .flags = RPC_TASK_ASYNC,
7184 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007185 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007186 int status = 0;
7187
7188 dprintk("--> %s\n", __func__);
7189
Idan Kedar85541162012-08-02 11:47:10 +03007190 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
7191 if (!lgp->args.layout.pages) {
7192 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007193 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03007194 }
7195 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05007196 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03007197
Weston Andros Adamson35124a02011-03-24 16:48:21 -04007198 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007199 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04007200 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007201
7202 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
7203 pnfs_get_layout_hdr(NFS_I(inode)->layout);
7204
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007205 task = rpc_run_task(&task_setup_data);
7206 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007207 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007208 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00007209 if (status == 0)
7210 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007211 trace_nfs4_layoutget(lgp->args.ctx,
7212 &lgp->args.range,
7213 &lgp->res.range,
7214 status);
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05007215 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
7216 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007217 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007218 rpc_put_task(task);
7219 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007220 if (status)
7221 return ERR_PTR(status);
7222 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007223}
7224
Benny Halevycbe82602011-05-22 19:52:37 +03007225static void
7226nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
7227{
7228 struct nfs4_layoutreturn *lrp = calldata;
7229
7230 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007231 nfs41_setup_sequence(lrp->clp->cl_session,
7232 &lrp->args.seq_args,
7233 &lrp->res.seq_res,
7234 task);
Benny Halevycbe82602011-05-22 19:52:37 +03007235}
7236
7237static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
7238{
7239 struct nfs4_layoutreturn *lrp = calldata;
7240 struct nfs_server *server;
7241
7242 dprintk("--> %s\n", __func__);
7243
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007244 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03007245 return;
7246
7247 server = NFS_SERVER(lrp->args.inode);
7248 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007249 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03007250 return;
7251 }
Benny Halevycbe82602011-05-22 19:52:37 +03007252 dprintk("<-- %s\n", __func__);
7253}
7254
7255static void nfs4_layoutreturn_release(void *calldata)
7256{
7257 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04007258 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03007259
7260 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04007261 spin_lock(&lo->plh_inode->i_lock);
7262 if (lrp->res.lrs_present)
7263 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
7264 lo->plh_block_lgets--;
7265 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04007266 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03007267 kfree(calldata);
7268 dprintk("<-- %s\n", __func__);
7269}
7270
7271static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
7272 .rpc_call_prepare = nfs4_layoutreturn_prepare,
7273 .rpc_call_done = nfs4_layoutreturn_done,
7274 .rpc_release = nfs4_layoutreturn_release,
7275};
7276
7277int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
7278{
7279 struct rpc_task *task;
7280 struct rpc_message msg = {
7281 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
7282 .rpc_argp = &lrp->args,
7283 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04007284 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03007285 };
7286 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04007287 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03007288 .rpc_message = &msg,
7289 .callback_ops = &nfs4_layoutreturn_call_ops,
7290 .callback_data = lrp,
7291 };
7292 int status;
7293
7294 dprintk("--> %s\n", __func__);
Chuck Levera9c92d62013-08-09 12:48:18 -04007295 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03007296 task = rpc_run_task(&task_setup_data);
7297 if (IS_ERR(task))
7298 return PTR_ERR(task);
7299 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007300 trace_nfs4_layoutreturn(lrp->args.inode, status);
Benny Halevycbe82602011-05-22 19:52:37 +03007301 dprintk("<-- %s status=%d\n", __func__, status);
7302 rpc_put_task(task);
7303 return status;
7304}
7305
Andy Adamson7f11d8d2011-07-30 20:52:35 -04007306/*
7307 * Retrieve the list of Data Server devices from the MDS.
7308 */
7309static int _nfs4_getdevicelist(struct nfs_server *server,
7310 const struct nfs_fh *fh,
7311 struct pnfs_devicelist *devlist)
7312{
7313 struct nfs4_getdevicelist_args args = {
7314 .fh = fh,
7315 .layoutclass = server->pnfs_curr_ld->id,
7316 };
7317 struct nfs4_getdevicelist_res res = {
7318 .devlist = devlist,
7319 };
7320 struct rpc_message msg = {
7321 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
7322 .rpc_argp = &args,
7323 .rpc_resp = &res,
7324 };
7325 int status;
7326
7327 dprintk("--> %s\n", __func__);
7328 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
7329 &res.seq_res, 0);
7330 dprintk("<-- %s status=%d\n", __func__, status);
7331 return status;
7332}
7333
7334int nfs4_proc_getdevicelist(struct nfs_server *server,
7335 const struct nfs_fh *fh,
7336 struct pnfs_devicelist *devlist)
7337{
7338 struct nfs4_exception exception = { };
7339 int err;
7340
7341 do {
7342 err = nfs4_handle_exception(server,
7343 _nfs4_getdevicelist(server, fh, devlist),
7344 &exception);
7345 } while (exception.retry);
7346
7347 dprintk("%s: err=%d, num_devs=%u\n", __func__,
7348 err, devlist->num_devs);
7349
7350 return err;
7351}
7352EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
7353
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007354static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007355_nfs4_proc_getdeviceinfo(struct nfs_server *server,
7356 struct pnfs_device *pdev,
7357 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007358{
7359 struct nfs4_getdeviceinfo_args args = {
7360 .pdev = pdev,
7361 };
7362 struct nfs4_getdeviceinfo_res res = {
7363 .pdev = pdev,
7364 };
7365 struct rpc_message msg = {
7366 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
7367 .rpc_argp = &args,
7368 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007369 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007370 };
7371 int status;
7372
7373 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00007374 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007375 dprintk("<-- %s status=%d\n", __func__, status);
7376
7377 return status;
7378}
7379
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007380int nfs4_proc_getdeviceinfo(struct nfs_server *server,
7381 struct pnfs_device *pdev,
7382 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007383{
7384 struct nfs4_exception exception = { };
7385 int err;
7386
7387 do {
7388 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007389 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007390 &exception);
7391 } while (exception.retry);
7392 return err;
7393}
7394EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
7395
Andy Adamson863a3c62011-03-23 13:27:54 +00007396static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
7397{
7398 struct nfs4_layoutcommit_data *data = calldata;
7399 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007400 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00007401
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007402 nfs41_setup_sequence(session,
7403 &data->args.seq_args,
7404 &data->res.seq_res,
7405 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00007406}
7407
7408static void
7409nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
7410{
7411 struct nfs4_layoutcommit_data *data = calldata;
7412 struct nfs_server *server = NFS_SERVER(data->args.inode);
7413
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007414 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00007415 return;
7416
7417 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007418 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
7419 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
7420 case -NFS4ERR_BADLAYOUT: /* no layout */
7421 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00007422 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007423 break;
7424 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00007425 nfs_post_op_update_inode_force_wcc(data->args.inode,
7426 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007427 break;
7428 default:
7429 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
7430 rpc_restart_call_prepare(task);
7431 return;
7432 }
7433 }
Andy Adamson863a3c62011-03-23 13:27:54 +00007434}
7435
7436static void nfs4_layoutcommit_release(void *calldata)
7437{
7438 struct nfs4_layoutcommit_data *data = calldata;
7439
Andy Adamsondb29c082011-07-30 20:52:38 -04007440 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00007441 put_rpccred(data->cred);
7442 kfree(data);
7443}
7444
7445static const struct rpc_call_ops nfs4_layoutcommit_ops = {
7446 .rpc_call_prepare = nfs4_layoutcommit_prepare,
7447 .rpc_call_done = nfs4_layoutcommit_done,
7448 .rpc_release = nfs4_layoutcommit_release,
7449};
7450
7451int
Andy Adamsonef311532011-03-12 02:58:10 -05007452nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00007453{
7454 struct rpc_message msg = {
7455 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
7456 .rpc_argp = &data->args,
7457 .rpc_resp = &data->res,
7458 .rpc_cred = data->cred,
7459 };
7460 struct rpc_task_setup task_setup_data = {
7461 .task = &data->task,
7462 .rpc_client = NFS_CLIENT(data->args.inode),
7463 .rpc_message = &msg,
7464 .callback_ops = &nfs4_layoutcommit_ops,
7465 .callback_data = data,
7466 .flags = RPC_TASK_ASYNC,
7467 };
7468 struct rpc_task *task;
7469 int status = 0;
7470
7471 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
7472 "lbw: %llu inode %lu\n",
7473 data->task.tk_pid, sync,
7474 data->args.lastbytewritten,
7475 data->args.inode->i_ino);
7476
Chuck Levera9c92d62013-08-09 12:48:18 -04007477 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00007478 task = rpc_run_task(&task_setup_data);
7479 if (IS_ERR(task))
7480 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05007481 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00007482 goto out;
7483 status = nfs4_wait_for_completion_rpc_task(task);
7484 if (status != 0)
7485 goto out;
7486 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007487 trace_nfs4_layoutcommit(data->args.inode, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00007488out:
7489 dprintk("%s: status %d\n", __func__, status);
7490 rpc_put_task(task);
7491 return status;
7492}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007493
Andy Adamson97431202013-08-08 10:57:56 -04007494/**
7495 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
7496 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
7497 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007498static int
7499_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007500 struct nfs_fsinfo *info,
7501 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007502{
7503 struct nfs41_secinfo_no_name_args args = {
7504 .style = SECINFO_STYLE_CURRENT_FH,
7505 };
7506 struct nfs4_secinfo_res res = {
7507 .flavors = flavors,
7508 };
7509 struct rpc_message msg = {
7510 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
7511 .rpc_argp = &args,
7512 .rpc_resp = &res,
7513 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007514 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007515 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007516 int status;
7517
7518 if (use_integrity) {
7519 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007520 cred = nfs4_get_clid_cred(server->nfs_client);
7521 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007522 }
7523
7524 dprintk("--> %s\n", __func__);
7525 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
7526 &res.seq_res, 0);
7527 dprintk("<-- %s status=%d\n", __func__, status);
7528
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007529 if (cred)
7530 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007531
7532 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007533}
7534
7535static int
7536nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7537 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
7538{
7539 struct nfs4_exception exception = { };
7540 int err;
7541 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007542 /* first try using integrity protection */
7543 err = -NFS4ERR_WRONGSEC;
7544
7545 /* try to use integrity protection with machine cred */
7546 if (_nfs4_is_integrity_protected(server->nfs_client))
7547 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
7548 flavors, true);
7549
7550 /*
7551 * if unable to use integrity protection, or SECINFO with
7552 * integrity protection returns NFS4ERR_WRONGSEC (which is
7553 * disallowed by spec, but exists in deployed servers) use
7554 * the current filesystem's rpc_client and the user cred.
7555 */
7556 if (err == -NFS4ERR_WRONGSEC)
7557 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
7558 flavors, false);
7559
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007560 switch (err) {
7561 case 0:
7562 case -NFS4ERR_WRONGSEC:
7563 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007564 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007565 default:
7566 err = nfs4_handle_exception(server, err, &exception);
7567 }
7568 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007569out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007570 return err;
7571}
7572
7573static int
7574nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
7575 struct nfs_fsinfo *info)
7576{
7577 int err;
7578 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04007579 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007580 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04007581 struct nfs4_secinfo4 *secinfo;
7582 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007583
7584 page = alloc_page(GFP_KERNEL);
7585 if (!page) {
7586 err = -ENOMEM;
7587 goto out;
7588 }
7589
7590 flavors = page_address(page);
7591 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
7592
7593 /*
7594 * Fall back on "guess and check" method if
7595 * the server doesn't support SECINFO_NO_NAME
7596 */
7597 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
7598 err = nfs4_find_root_sec(server, fhandle, info);
7599 goto out_freepage;
7600 }
7601 if (err)
7602 goto out_freepage;
7603
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04007604 for (i = 0; i < flavors->num_flavors; i++) {
7605 secinfo = &flavors->flavors[i];
7606
7607 switch (secinfo->flavor) {
7608 case RPC_AUTH_NULL:
7609 case RPC_AUTH_UNIX:
7610 case RPC_AUTH_GSS:
7611 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
7612 &secinfo->flavor_info);
7613 break;
7614 default:
7615 flavor = RPC_AUTH_MAXFLAVOR;
7616 break;
7617 }
7618
7619 if (flavor != RPC_AUTH_MAXFLAVOR) {
7620 err = nfs4_lookup_root_sec(server, fhandle,
7621 info, flavor);
7622 if (!err)
7623 break;
7624 }
7625 }
7626
7627 if (flavor == RPC_AUTH_MAXFLAVOR)
7628 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007629
7630out_freepage:
7631 put_page(page);
7632 if (err == -EACCES)
7633 return -EPERM;
7634out:
7635 return err;
7636}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007637
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007638static int _nfs41_test_stateid(struct nfs_server *server,
7639 nfs4_stateid *stateid,
7640 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04007641{
7642 int status;
7643 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007644 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04007645 };
7646 struct nfs41_test_stateid_res res;
7647 struct rpc_message msg = {
7648 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
7649 .rpc_argp = &args,
7650 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007651 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04007652 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007653 struct rpc_clnt *rpc_client = server->client;
7654
7655 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
7656 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007657
Chuck Lever38527b12012-07-11 16:30:23 -04007658 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04007659 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007660 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007661 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007662 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04007663 if (status != NFS_OK) {
7664 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04007665 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04007666 }
7667 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04007668 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04007669}
7670
Chuck Lever38527b12012-07-11 16:30:23 -04007671/**
7672 * nfs41_test_stateid - perform a TEST_STATEID operation
7673 *
7674 * @server: server / transport on which to perform the operation
7675 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007676 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04007677 *
7678 * Returns NFS_OK if the server recognizes that "stateid" is valid.
7679 * Otherwise a negative NFS4ERR value is returned if the operation
7680 * failed or the state ID is not currently valid.
7681 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007682static int nfs41_test_stateid(struct nfs_server *server,
7683 nfs4_stateid *stateid,
7684 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04007685{
7686 struct nfs4_exception exception = { };
7687 int err;
7688 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007689 err = _nfs41_test_stateid(server, stateid, cred);
Chuck Lever377e5072012-07-11 16:29:45 -04007690 if (err != -NFS4ERR_DELAY)
7691 break;
7692 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04007693 } while (exception.retry);
7694 return err;
7695}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007696
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007697struct nfs_free_stateid_data {
7698 struct nfs_server *server;
7699 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007700 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007701};
7702
7703static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
7704{
7705 struct nfs_free_stateid_data *data = calldata;
7706 nfs41_setup_sequence(nfs4_get_session(data->server),
7707 &data->args.seq_args,
7708 &data->res.seq_res,
7709 task);
7710}
7711
7712static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
7713{
7714 struct nfs_free_stateid_data *data = calldata;
7715
7716 nfs41_sequence_done(task, &data->res.seq_res);
7717
7718 switch (task->tk_status) {
7719 case -NFS4ERR_DELAY:
7720 if (nfs4_async_handle_error(task, data->server, NULL) == -EAGAIN)
7721 rpc_restart_call_prepare(task);
7722 }
7723}
7724
7725static void nfs41_free_stateid_release(void *calldata)
7726{
7727 kfree(calldata);
7728}
7729
Trond Myklebust17f26b12013-08-21 15:48:42 -04007730static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007731 .rpc_call_prepare = nfs41_free_stateid_prepare,
7732 .rpc_call_done = nfs41_free_stateid_done,
7733 .rpc_release = nfs41_free_stateid_release,
7734};
7735
7736static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
7737 nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007738 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007739 bool privileged)
7740{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007741 struct rpc_message msg = {
7742 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007743 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007744 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007745 struct rpc_task_setup task_setup = {
7746 .rpc_client = server->client,
7747 .rpc_message = &msg,
7748 .callback_ops = &nfs41_free_stateid_ops,
7749 .flags = RPC_TASK_ASYNC,
7750 };
7751 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007752
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007753 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
7754 &task_setup.rpc_client, &msg);
7755
Chuck Lever38527b12012-07-11 16:30:23 -04007756 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007757 data = kmalloc(sizeof(*data), GFP_NOFS);
7758 if (!data)
7759 return ERR_PTR(-ENOMEM);
7760 data->server = server;
7761 nfs4_stateid_copy(&data->args.stateid, stateid);
7762
7763 task_setup.callback_data = data;
7764
7765 msg.rpc_argp = &data->args;
7766 msg.rpc_resp = &data->res;
Chuck Levera9c92d62013-08-09 12:48:18 -04007767 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007768 if (privileged)
7769 nfs4_set_sequence_privileged(&data->args.seq_args);
7770
7771 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007772}
7773
Chuck Lever38527b12012-07-11 16:30:23 -04007774/**
7775 * nfs41_free_stateid - perform a FREE_STATEID operation
7776 *
7777 * @server: server / transport on which to perform the operation
7778 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007779 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04007780 *
7781 * Returns NFS_OK if the server freed "stateid". Otherwise a
7782 * negative NFS4ERR value is returned.
7783 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007784static int nfs41_free_stateid(struct nfs_server *server,
7785 nfs4_stateid *stateid,
7786 struct rpc_cred *cred)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007787{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007788 struct rpc_task *task;
7789 int ret;
7790
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007791 task = _nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007792 if (IS_ERR(task))
7793 return PTR_ERR(task);
7794 ret = rpc_wait_for_completion_task(task);
7795 if (!ret)
7796 ret = task->tk_status;
7797 rpc_put_task(task);
7798 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007799}
Trond Myklebust36281ca2012-03-04 18:13:56 -05007800
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007801static int nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
7802{
7803 struct rpc_task *task;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007804 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007805
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007806 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007807 nfs4_free_lock_state(server, lsp);
7808 if (IS_ERR(task))
7809 return PTR_ERR(task);
7810 rpc_put_task(task);
7811 return 0;
7812}
7813
Trond Myklebust36281ca2012-03-04 18:13:56 -05007814static bool nfs41_match_stateid(const nfs4_stateid *s1,
7815 const nfs4_stateid *s2)
7816{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007817 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007818 return false;
7819
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007820 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007821 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007822 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007823 return true;
7824
7825 return false;
7826}
7827
Andy Adamson557134a2009-04-01 09:21:53 -04007828#endif /* CONFIG_NFS_V4_1 */
7829
Trond Myklebust36281ca2012-03-04 18:13:56 -05007830static bool nfs4_match_stateid(const nfs4_stateid *s1,
7831 const nfs4_stateid *s2)
7832{
Trond Myklebustf597c532012-03-04 18:13:56 -05007833 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05007834}
7835
7836
Trond Myklebust17280172012-03-11 13:11:00 -04007837static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007838 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007839 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007840 .recover_open = nfs4_open_reclaim,
7841 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04007842 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04007843 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007844};
7845
Andy Adamson591d71c2009-04-01 09:22:47 -04007846#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007847static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007848 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
7849 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
7850 .recover_open = nfs4_open_reclaim,
7851 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05007852 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007853 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04007854 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04007855};
7856#endif /* CONFIG_NFS_V4_1 */
7857
Trond Myklebust17280172012-03-11 13:11:00 -04007858static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007859 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007860 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007861 .recover_open = nfs4_open_expired,
7862 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04007863 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007864};
7865
Andy Adamson591d71c2009-04-01 09:22:47 -04007866#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007867static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007868 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
7869 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007870 .recover_open = nfs41_open_expired,
7871 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05007872 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04007873};
7874#endif /* CONFIG_NFS_V4_1 */
7875
Trond Myklebust17280172012-03-11 13:11:00 -04007876static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007877 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04007878 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007879 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04007880};
7881
7882#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007883static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007884 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04007885 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007886 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04007887};
7888#endif
7889
Trond Myklebust97dc1352010-06-16 09:52:26 -04007890static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
7891 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04007892 .init_caps = NFS_CAP_READDIRPLUS
7893 | NFS_CAP_ATOMIC_OPEN
7894 | NFS_CAP_CHANGE_ATTR
7895 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04007896 .init_client = nfs40_init_client,
7897 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007898 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007899 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007900 .free_lock_state = nfs4_release_lockowner,
Chuck Lever9915ea72013-08-09 12:48:27 -04007901 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007902 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
7903 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
7904 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007905};
7906
7907#if defined(CONFIG_NFS_V4_1)
7908static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
7909 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04007910 .init_caps = NFS_CAP_READDIRPLUS
7911 | NFS_CAP_ATOMIC_OPEN
7912 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04007913 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04007914 | NFS_CAP_STATEID_NFSV41
7915 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04007916 .init_client = nfs41_init_client,
7917 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007918 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007919 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007920 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04007921 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007922 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7923 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7924 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007925};
7926#endif
7927
Steve Dickson42c2c422013-05-22 12:50:38 -04007928#if defined(CONFIG_NFS_V4_2)
7929static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
7930 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04007931 .init_caps = NFS_CAP_READDIRPLUS
7932 | NFS_CAP_ATOMIC_OPEN
7933 | NFS_CAP_CHANGE_ATTR
7934 | NFS_CAP_POSIX_LOCK
7935 | NFS_CAP_STATEID_NFSV41
7936 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04007937 .init_client = nfs41_init_client,
7938 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04007939 .match_stateid = nfs41_match_stateid,
7940 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04007941 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04007942 .call_sync_ops = &nfs41_call_sync_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04007943 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7944 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7945 .state_renewal_ops = &nfs41_state_renewal_ops,
7946};
7947#endif
7948
Trond Myklebust97dc1352010-06-16 09:52:26 -04007949const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
7950 [0] = &nfs_v4_0_minor_ops,
7951#if defined(CONFIG_NFS_V4_1)
7952 [1] = &nfs_v4_1_minor_ops,
7953#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04007954#if defined(CONFIG_NFS_V4_2)
7955 [2] = &nfs_v4_2_minor_ops,
7956#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04007957};
7958
Trond Myklebust17f26b12013-08-21 15:48:42 -04007959static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04007960 .create = nfs_create,
7961 .lookup = nfs_lookup,
7962 .atomic_open = nfs_atomic_open,
7963 .link = nfs_link,
7964 .unlink = nfs_unlink,
7965 .symlink = nfs_symlink,
7966 .mkdir = nfs_mkdir,
7967 .rmdir = nfs_rmdir,
7968 .mknod = nfs_mknod,
7969 .rename = nfs_rename,
7970 .permission = nfs_permission,
7971 .getattr = nfs_getattr,
7972 .setattr = nfs_setattr,
7973 .getxattr = generic_getxattr,
7974 .setxattr = generic_setxattr,
7975 .listxattr = generic_listxattr,
7976 .removexattr = generic_removexattr,
7977};
7978
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08007979static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007980 .permission = nfs_permission,
7981 .getattr = nfs_getattr,
7982 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007983 .getxattr = generic_getxattr,
7984 .setxattr = generic_setxattr,
7985 .listxattr = generic_listxattr,
7986 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007987};
7988
David Howells509de812006-08-22 20:06:11 -04007989const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007990 .version = 4, /* protocol version */
7991 .dentry_ops = &nfs4_dentry_operations,
7992 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007993 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04007994 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007995 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04007996 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04007997 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007998 .getattr = nfs4_proc_getattr,
7999 .setattr = nfs4_proc_setattr,
8000 .lookup = nfs4_proc_lookup,
8001 .access = nfs4_proc_access,
8002 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008003 .create = nfs4_proc_create,
8004 .remove = nfs4_proc_remove,
8005 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04008006 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008007 .unlink_done = nfs4_proc_unlink_done,
8008 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04008009 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04008010 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04008011 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008012 .link = nfs4_proc_link,
8013 .symlink = nfs4_proc_symlink,
8014 .mkdir = nfs4_proc_mkdir,
8015 .rmdir = nfs4_proc_remove,
8016 .readdir = nfs4_proc_readdir,
8017 .mknod = nfs4_proc_mknod,
8018 .statfs = nfs4_proc_statfs,
8019 .fsinfo = nfs4_proc_fsinfo,
8020 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04008021 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008022 .decode_dirent = nfs4_decode_dirent,
8023 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04008024 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04008025 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05008026 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008027 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04008028 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04008029 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008030 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008031 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04008032 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008033 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008034 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00008035 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04008036 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04008037 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04008038 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04008039 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04008040 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00008041 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04008042 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04008043 .create_server = nfs4_create_server,
8044 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008045};
8046
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008047static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
8048 .prefix = XATTR_NAME_NFSV4_ACL,
8049 .list = nfs4_xattr_list_nfs4_acl,
8050 .get = nfs4_xattr_get_nfs4_acl,
8051 .set = nfs4_xattr_set_nfs4_acl,
8052};
8053
8054const struct xattr_handler *nfs4_xattr_handlers[] = {
8055 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04008056#ifdef CONFIG_NFS_V4_SECURITY_LABEL
8057 &nfs4_xattr_nfs4_label_handler,
8058#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008059 NULL
8060};
8061
Linus Torvalds1da177e2005-04-16 15:20:36 -07008062/*
8063 * Local variables:
8064 * c-basic-offset: 8
8065 * End:
8066 */