blob: d2b4845ecb4d338aa7dee616a302c2aa7aacbd1d [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 Myklebust6926afd12012-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 Myklebust035168ab2010-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 Myklebust035168ab2010-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 Myklebust035168ab2010-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 Myklebust035168ab2010-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 Myklebust6926afd12012-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 Myklebust6926afd12012-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 Myklebust6926afd12012-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
1320 if (!data->rpc_done) {
1321 ret = data->rpc_status;
1322 goto err;
1323 }
1324
1325 ret = -ESTALE;
1326 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1327 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1328 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1329 goto err;
1330
1331 ret = -ENOMEM;
1332 state = nfs4_get_open_state(inode, data->owner);
1333 if (state == NULL)
1334 goto err;
1335
1336 ret = nfs_refresh_inode(inode, &data->f_attr);
1337 if (ret)
1338 goto err;
1339
David Quigleyaa9c2662013-05-22 12:50:44 -04001340 nfs_setsecurity(inode, &data->f_attr, data->f_label);
1341
Andy Adamsone23008e2012-10-02 21:07:32 -04001342 if (data->o_res.delegation_type != 0)
1343 nfs4_opendata_check_deleg(data, state);
1344 update_open_stateid(state, &data->o_res.stateid, NULL,
1345 data->o_arg.fmode);
1346
1347 return state;
1348err:
1349 return ERR_PTR(ret);
1350
1351}
1352
1353static struct nfs4_state *
1354_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001355{
1356 struct inode *inode;
1357 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001358 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001359
Trond Myklebustaac00a82007-07-05 19:02:21 -04001360 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001361 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001362 goto out;
1363 }
1364
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001365 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001366 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001367 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001368 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001369 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001370 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001371 goto err;
1372 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001373 state = nfs4_get_open_state(inode, data->owner);
1374 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001375 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001376 if (data->o_res.delegation_type != 0)
1377 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001378 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001379 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001380 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001381out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001382 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001383 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001384err_put_inode:
1385 iput(inode);
1386err:
1387 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001388}
1389
Andy Adamsone23008e2012-10-02 21:07:32 -04001390static struct nfs4_state *
1391nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1392{
1393 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1394 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1395 return _nfs4_opendata_to_nfs4_state(data);
1396}
1397
Trond Myklebust864472e2006-01-03 09:55:15 +01001398static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1399{
1400 struct nfs_inode *nfsi = NFS_I(state->inode);
1401 struct nfs_open_context *ctx;
1402
1403 spin_lock(&state->inode->i_lock);
1404 list_for_each_entry(ctx, &nfsi->open_files, list) {
1405 if (ctx->state != state)
1406 continue;
1407 get_nfs_open_context(ctx);
1408 spin_unlock(&state->inode->i_lock);
1409 return ctx;
1410 }
1411 spin_unlock(&state->inode->i_lock);
1412 return ERR_PTR(-ENOENT);
1413}
1414
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001415static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1416 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001417{
1418 struct nfs4_opendata *opendata;
1419
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001420 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001421 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001422 if (opendata == NULL)
1423 return ERR_PTR(-ENOMEM);
1424 opendata->state = state;
1425 atomic_inc(&state->count);
1426 return opendata;
1427}
1428
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001429static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001430{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001431 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001432 int ret;
1433
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001434 opendata->o_arg.open_flags = 0;
1435 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001436 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1437 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1438 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001439 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001440 if (ret != 0)
1441 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001442 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001443 if (IS_ERR(newstate))
1444 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001445 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001446 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001447 return 0;
1448}
1449
1450static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1451{
Trond Myklebust864472e2006-01-03 09:55:15 +01001452 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001453 int ret;
1454
1455 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001456 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001457 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001458 smp_rmb();
1459 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001460 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001461 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001462 if (ret != 0)
1463 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001464 if (newstate != state)
1465 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001466 }
1467 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001468 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001469 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001470 if (ret != 0)
1471 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001472 if (newstate != state)
1473 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001474 }
1475 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001476 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001477 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001478 if (ret != 0)
1479 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001480 if (newstate != state)
1481 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001482 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001483 /*
1484 * We may have performed cached opens for all three recoveries.
1485 * Check if we need to update the current stateid.
1486 */
1487 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001488 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001489 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001490 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001491 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001492 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001493 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001494 return 0;
1495}
1496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497/*
1498 * OPEN_RECLAIM:
1499 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001501static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001503 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001504 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001505 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 int status;
1507
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001508 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1509 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001510 if (IS_ERR(opendata))
1511 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001512 rcu_read_lock();
1513 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001514 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001515 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001516 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001517 opendata->o_arg.u.delegation_type = delegation_type;
1518 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001519 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 return status;
1521}
1522
Trond Myklebust539cd032007-06-05 11:46:42 -04001523static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524{
1525 struct nfs_server *server = NFS_SERVER(state->inode);
1526 struct nfs4_exception exception = { };
1527 int err;
1528 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001529 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001530 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001531 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1532 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001533 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001534 break;
1535 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 } while (exception.retry);
1537 return err;
1538}
1539
Trond Myklebust864472e2006-01-03 09:55:15 +01001540static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1541{
1542 struct nfs_open_context *ctx;
1543 int ret;
1544
1545 ctx = nfs4_state_find_open_context(state);
1546 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001547 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001548 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001549 put_nfs_open_context(ctx);
1550 return ret;
1551}
1552
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001553static 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 -07001554{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001555 switch (err) {
1556 default:
1557 printk(KERN_ERR "NFS: %s: unhandled error "
1558 "%d.\n", __func__, err);
1559 case 0:
1560 case -ENOENT:
1561 case -ESTALE:
1562 break;
1563 case -NFS4ERR_BADSESSION:
1564 case -NFS4ERR_BADSLOT:
1565 case -NFS4ERR_BAD_HIGH_SLOT:
1566 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1567 case -NFS4ERR_DEADSESSION:
1568 set_bit(NFS_DELEGATED_STATE, &state->flags);
1569 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1570 return -EAGAIN;
1571 case -NFS4ERR_STALE_CLIENTID:
1572 case -NFS4ERR_STALE_STATEID:
1573 set_bit(NFS_DELEGATED_STATE, &state->flags);
1574 case -NFS4ERR_EXPIRED:
1575 /* Don't recall a delegation if it was lost */
1576 nfs4_schedule_lease_recovery(server->nfs_client);
1577 return -EAGAIN;
1578 case -NFS4ERR_DELEG_REVOKED:
1579 case -NFS4ERR_ADMIN_REVOKED:
1580 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001581 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001582 nfs_inode_find_state_and_recover(state->inode,
1583 stateid);
1584 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001585 return 0;
1586 case -NFS4ERR_DELAY:
1587 case -NFS4ERR_GRACE:
1588 set_bit(NFS_DELEGATED_STATE, &state->flags);
1589 ssleep(1);
1590 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001591 case -ENOMEM:
1592 case -NFS4ERR_DENIED:
1593 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1594 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 return err;
1597}
1598
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001599int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1600{
1601 struct nfs_server *server = NFS_SERVER(state->inode);
1602 struct nfs4_opendata *opendata;
1603 int err;
1604
1605 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1606 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1607 if (IS_ERR(opendata))
1608 return PTR_ERR(opendata);
1609 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1610 err = nfs4_open_recover(opendata, state);
1611 nfs4_opendata_put(opendata);
1612 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1613}
1614
Chuck Leverbe05c862013-08-09 12:49:47 -04001615static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1616{
1617 struct nfs4_opendata *data = calldata;
1618
1619 nfs40_setup_sequence(data->o_arg.server, &data->o_arg.seq_args,
1620 &data->o_res.seq_res, task);
1621}
1622
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001623static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1624{
1625 struct nfs4_opendata *data = calldata;
1626
Chuck Leverbe05c862013-08-09 12:49:47 -04001627 nfs40_sequence_done(task, &data->o_res.seq_res);
1628
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001629 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001630 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001631 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001632 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001633 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001634 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001635 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001636}
1637
1638static void nfs4_open_confirm_release(void *calldata)
1639{
1640 struct nfs4_opendata *data = calldata;
1641 struct nfs4_state *state = NULL;
1642
1643 /* If this request hasn't been cancelled, do nothing */
1644 if (data->cancelled == 0)
1645 goto out_free;
1646 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001647 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001648 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001649 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001650 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001651 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001652out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001653 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001654}
1655
1656static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04001657 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001658 .rpc_call_done = nfs4_open_confirm_done,
1659 .rpc_release = nfs4_open_confirm_release,
1660};
1661
1662/*
1663 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1664 */
1665static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1666{
1667 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1668 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001669 struct rpc_message msg = {
1670 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1671 .rpc_argp = &data->c_arg,
1672 .rpc_resp = &data->c_res,
1673 .rpc_cred = data->owner->so_cred,
1674 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001675 struct rpc_task_setup task_setup_data = {
1676 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001677 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001678 .callback_ops = &nfs4_open_confirm_ops,
1679 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001680 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001681 .flags = RPC_TASK_ASYNC,
1682 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 int status;
1684
Chuck Leverbe05c862013-08-09 12:49:47 -04001685 nfs4_init_sequence(&data->o_arg.seq_args, &data->o_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001686 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001687 data->rpc_done = 0;
1688 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001689 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001690 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001691 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001692 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001693 status = nfs4_wait_for_completion_rpc_task(task);
1694 if (status != 0) {
1695 data->cancelled = 1;
1696 smp_wmb();
1697 } else
1698 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001699 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 return status;
1701}
1702
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001703static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001705 struct nfs4_opendata *data = calldata;
1706 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001707 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001708
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001709 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001710 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001711 /*
1712 * Check if we still need to send an OPEN call, or if we can use
1713 * a delegation instead.
1714 */
1715 if (data->state != NULL) {
1716 struct nfs_delegation *delegation;
1717
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001718 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001719 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001720 rcu_read_lock();
1721 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001722 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
Trond Myklebustcd4c9be2013-04-23 14:46:25 -04001723 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
Trond Myklebust652f89f2011-12-09 19:05:58 -05001724 can_open_delegated(delegation, data->o_arg.fmode))
1725 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001726 rcu_read_unlock();
1727 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001728 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04001729 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust8188df12013-04-23 14:31:19 -04001730 switch (data->o_arg.claim) {
1731 case NFS4_OPEN_CLAIM_PREVIOUS:
1732 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1733 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001734 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04001735 case NFS4_OPEN_CLAIM_FH:
1736 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001737 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1738 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001739 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04001740 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001741 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001742 &data->o_res.seq_res,
1743 task) != 0)
1744 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04001745
1746 /* Set the create mode (note dependency on the session type) */
1747 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1748 if (data->o_arg.open_flags & O_EXCL) {
1749 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1750 if (nfs4_has_persistent_session(clp))
1751 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1752 else if (clp->cl_mvops->minor_version > 0)
1753 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1754 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04001755 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001756unlock_no_action:
1757 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001758out_no_action:
1759 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001760out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001761 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001762}
1763
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001764static void nfs4_open_done(struct rpc_task *task, void *calldata)
1765{
1766 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001768 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001769
Trond Myklebust14516c32010-07-31 14:29:06 -04001770 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1771 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001772
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001773 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001774 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1775 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001776 case S_IFREG:
1777 break;
1778 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001779 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001780 break;
1781 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001782 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001783 break;
1784 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001785 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001786 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001787 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001788 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001789 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1790 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001791 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001792 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001793}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001794
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001795static void nfs4_open_release(void *calldata)
1796{
1797 struct nfs4_opendata *data = calldata;
1798 struct nfs4_state *state = NULL;
1799
1800 /* If this request hasn't been cancelled, do nothing */
1801 if (data->cancelled == 0)
1802 goto out_free;
1803 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001804 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001805 goto out_free;
1806 /* In case we need an open_confirm, no cleanup! */
1807 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1808 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001809 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001810 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001811 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001812out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001813 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001814}
1815
1816static const struct rpc_call_ops nfs4_open_ops = {
1817 .rpc_call_prepare = nfs4_open_prepare,
1818 .rpc_call_done = nfs4_open_done,
1819 .rpc_release = nfs4_open_release,
1820};
1821
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001822static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001823{
1824 struct inode *dir = data->dir->d_inode;
1825 struct nfs_server *server = NFS_SERVER(dir);
1826 struct nfs_openargs *o_arg = &data->o_arg;
1827 struct nfs_openres *o_res = &data->o_res;
1828 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001829 struct rpc_message msg = {
1830 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1831 .rpc_argp = o_arg,
1832 .rpc_resp = o_res,
1833 .rpc_cred = data->owner->so_cred,
1834 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001835 struct rpc_task_setup task_setup_data = {
1836 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001837 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001838 .callback_ops = &nfs4_open_ops,
1839 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001840 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001841 .flags = RPC_TASK_ASYNC,
1842 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001843 int status;
1844
Chuck Levera9c92d62013-08-09 12:48:18 -04001845 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001846 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001847 data->rpc_done = 0;
1848 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001849 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001850 data->is_recover = 0;
1851 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001852 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001853 data->is_recover = 1;
1854 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04001855 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001856 if (IS_ERR(task))
1857 return PTR_ERR(task);
1858 status = nfs4_wait_for_completion_rpc_task(task);
1859 if (status != 0) {
1860 data->cancelled = 1;
1861 smp_wmb();
1862 } else
1863 status = data->rpc_status;
1864 rpc_put_task(task);
1865
1866 return status;
1867}
1868
1869static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1870{
1871 struct inode *dir = data->dir->d_inode;
1872 struct nfs_openres *o_res = &data->o_res;
1873 int status;
1874
1875 status = nfs4_run_open_task(data, 1);
1876 if (status != 0 || !data->rpc_done)
1877 return status;
1878
Trond Myklebust6926afd12012-01-07 13:22:46 -05001879 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1880
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001881 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1882 status = _nfs4_proc_open_confirm(data);
1883 if (status != 0)
1884 return status;
1885 }
1886
1887 return status;
1888}
1889
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001890static int nfs4_opendata_access(struct rpc_cred *cred,
1891 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001892 struct nfs4_state *state, fmode_t fmode,
1893 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001894{
1895 struct nfs_access_entry cache;
1896 u32 mask;
1897
1898 /* access call failed or for some reason the server doesn't
1899 * support any access modes -- defer access call until later */
1900 if (opendata->o_res.access_supported == 0)
1901 return 0;
1902
1903 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001904 /* don't check MAY_WRITE - a newly created file may not have
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001905 * write mode bits, but POSIX allows the creating process to write.
1906 * use openflags to check for exec, because fmode won't
1907 * always have FMODE_EXEC set when file open for exec. */
1908 if (openflags & __FMODE_EXEC) {
1909 /* ONLY check for exec rights */
1910 mask = MAY_EXEC;
1911 } else if (fmode & FMODE_READ)
1912 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001913
1914 cache.cred = cred;
1915 cache.jiffies = jiffies;
1916 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1917 nfs_access_add_cache(state->inode, &cache);
1918
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001919 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001920 return 0;
1921
1922 /* even though OPEN succeeded, access is denied. Close the file */
1923 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001924 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001925}
1926
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001927/*
1928 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1929 */
1930static int _nfs4_proc_open(struct nfs4_opendata *data)
1931{
1932 struct inode *dir = data->dir->d_inode;
1933 struct nfs_server *server = NFS_SERVER(dir);
1934 struct nfs_openargs *o_arg = &data->o_arg;
1935 struct nfs_openres *o_res = &data->o_res;
1936 int status;
1937
1938 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001939 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001940 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001941 if (status != 0) {
1942 if (status == -NFS4ERR_BADNAME &&
1943 !(o_arg->open_flags & O_CREAT))
1944 return -ENOENT;
1945 return status;
1946 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001947
Trond Myklebust6926afd12012-01-07 13:22:46 -05001948 nfs_fattr_map_and_free_names(server, &data->f_attr);
1949
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001950 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001951 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001952 if (o_arg->open_flags & O_EXCL)
1953 data->file_created = 1;
1954 else if (o_res->cinfo.before != o_res->cinfo.after)
1955 data->file_created = 1;
1956 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001957 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1958 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001960 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001962 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 }
1964 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
David Quigley1775fd32013-05-22 12:50:42 -04001965 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001966 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967}
1968
Andy Adamsond83217c2011-03-01 01:34:17 +00001969static int nfs4_recover_expired_lease(struct nfs_server *server)
1970{
1971 return nfs4_client_recover_expired_lease(server->nfs_client);
1972}
1973
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974/*
1975 * OPEN_EXPIRED:
1976 * reclaim state on the server after a network partition.
1977 * Assumes caller holds the appropriate lock
1978 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001979static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001981 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001982 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001984 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001985 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001986 if (IS_ERR(opendata))
1987 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001988 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001989 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001990 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001991 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001992 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993}
1994
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001995static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001996{
Trond Myklebust539cd032007-06-05 11:46:42 -04001997 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001998 struct nfs4_exception exception = { };
1999 int err;
2000
2001 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002002 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002003 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002004 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2005 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002006 switch (err) {
2007 default:
2008 goto out;
2009 case -NFS4ERR_GRACE:
2010 case -NFS4ERR_DELAY:
2011 nfs4_handle_exception(server, err, &exception);
2012 err = 0;
2013 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002014 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002015out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002016 return err;
2017}
2018
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2020{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002022 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
Trond Myklebust864472e2006-01-03 09:55:15 +01002024 ctx = nfs4_state_find_open_context(state);
2025 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002026 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002027 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002028 put_nfs_open_context(ctx);
2029 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030}
2031
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002032#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002033static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002034{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002035 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002036 nfs4_stateid *stateid = &state->stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002037 struct nfs_delegation *delegation;
2038 struct rpc_cred *cred = NULL;
2039 int status = -NFS4ERR_BAD_STATEID;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002040
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002041 /* If a state reset has been done, test_stateid is unneeded */
2042 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2043 return;
2044
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002045 /* Get the delegation credential for use by test/free_stateid */
2046 rcu_read_lock();
2047 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2048 if (delegation != NULL &&
2049 nfs4_stateid_match(&delegation->stateid, stateid)) {
2050 cred = get_rpccred(delegation->cred);
2051 rcu_read_unlock();
2052 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002053 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002054 } else
2055 rcu_read_unlock();
2056
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002057 if (status != NFS_OK) {
2058 /* Free the stateid unless the server explicitly
2059 * informs us the stateid is unrecognized. */
2060 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002061 nfs41_free_stateid(server, stateid, cred);
Bryan Schumaker69388672012-09-26 15:25:52 -04002062 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002063
Bryan Schumaker69388672012-09-26 15:25:52 -04002064 write_seqlock(&state->seqlock);
2065 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2066 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002067 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2068 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002069
2070 if (cred != NULL)
2071 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002072}
2073
2074/**
2075 * nfs41_check_open_stateid - possibly free an open stateid
2076 *
2077 * @state: NFSv4 state for an inode
2078 *
2079 * Returns NFS_OK if recovery for this stateid is now finished.
2080 * Otherwise a negative NFS4ERR value is returned.
2081 */
2082static int nfs41_check_open_stateid(struct nfs4_state *state)
2083{
2084 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002085 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002086 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002087 int status;
2088
2089 /* If a state reset has been done, test_stateid is unneeded */
2090 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2091 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2092 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2093 return -NFS4ERR_BAD_STATEID;
2094
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002095 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002096 trace_nfs4_test_open_stateid(state, NULL, status);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002097 if (status != NFS_OK) {
2098 /* Free the stateid unless the server explicitly
2099 * informs us the stateid is unrecognized. */
2100 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002101 nfs41_free_stateid(server, stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002102
2103 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2104 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2105 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002106 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002107 }
2108 return status;
2109}
2110
2111static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2112{
Chuck Levereb64cf92012-07-11 16:30:05 -04002113 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002114
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002115 nfs41_clear_delegation_stateid(state);
2116 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002117 if (status != NFS_OK)
2118 status = nfs4_open_expired(sp, state);
2119 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002120}
2121#endif
2122
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002124 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2125 * fields corresponding to attributes that were used to store the verifier.
2126 * Make sure we clobber those fields in the later setattr call
2127 */
2128static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2129{
2130 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2131 !(sattr->ia_valid & ATTR_ATIME_SET))
2132 sattr->ia_valid |= ATTR_ATIME;
2133
2134 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2135 !(sattr->ia_valid & ATTR_MTIME_SET))
2136 sattr->ia_valid |= ATTR_MTIME;
2137}
2138
Trond Myklebustc21443c2013-02-07 14:26:21 -05002139static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2140 fmode_t fmode,
2141 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002142 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002143{
2144 struct nfs4_state_owner *sp = opendata->owner;
2145 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002146 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002147 struct nfs4_state *state;
2148 unsigned int seq;
2149 int ret;
2150
2151 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2152
2153 ret = _nfs4_proc_open(opendata);
2154 if (ret != 0)
2155 goto out;
2156
2157 state = nfs4_opendata_to_nfs4_state(opendata);
2158 ret = PTR_ERR(state);
2159 if (IS_ERR(state))
2160 goto out;
2161 if (server->caps & NFS_CAP_POSIX_LOCK)
2162 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2163
Trond Myklebust275bb302013-05-29 13:11:28 -04002164 dentry = opendata->dentry;
2165 if (dentry->d_inode == NULL) {
2166 /* FIXME: Is this d_drop() ever needed? */
2167 d_drop(dentry);
2168 dentry = d_add_unique(dentry, igrab(state->inode));
2169 if (dentry == NULL) {
2170 dentry = opendata->dentry;
2171 } else if (dentry != ctx->dentry) {
2172 dput(ctx->dentry);
2173 ctx->dentry = dget(dentry);
2174 }
2175 nfs_set_verifier(dentry,
2176 nfs_save_change_attribute(opendata->dir->d_inode));
2177 }
2178
Trond Myklebustc21443c2013-02-07 14:26:21 -05002179 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2180 if (ret != 0)
2181 goto out;
2182
Trond Myklebust3efb9722013-05-29 13:17:04 -04002183 ctx->state = state;
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002184 if (dentry->d_inode == state->inode) {
2185 nfs_inode_attach_open_context(ctx);
2186 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2187 nfs4_schedule_stateid_recovery(server, state);
2188 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002189out:
2190 return ret;
2191}
2192
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002193/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002194 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 */
Andy Adamson82be4172012-05-23 05:02:35 -04002196static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002197 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002198 int flags,
2199 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002200 struct nfs4_label *label,
2201 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202{
2203 struct nfs4_state_owner *sp;
2204 struct nfs4_state *state = NULL;
2205 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002206 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002207 struct dentry *dentry = ctx->dentry;
2208 struct rpc_cred *cred = ctx->cred;
2209 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2210 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002211 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002212 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002213 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
2215 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002217 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2218 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2220 goto out_err;
2221 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002222 status = nfs4_recover_expired_lease(server);
2223 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002224 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04002225 if (dentry->d_inode != NULL)
2226 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002227 status = -ENOMEM;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002228 if (dentry->d_inode)
2229 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002230 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002231 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002232 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002233 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
David Quigley14c43f72013-05-22 12:50:43 -04002235 if (label) {
2236 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2237 if (IS_ERR(olabel)) {
2238 status = PTR_ERR(olabel);
2239 goto err_opendata_put;
2240 }
2241 }
2242
Andy Adamson82be4172012-05-23 05:02:35 -04002243 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2244 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2245 if (!opendata->f_attr.mdsthreshold)
David Quigley14c43f72013-05-22 12:50:43 -04002246 goto err_free_label;
Trond Myklebust1549210f2012-06-05 09:16:47 -04002247 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002248 }
Al Viro82a2c1b2011-06-22 18:30:55 -04002249 if (dentry->d_inode != NULL)
2250 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002251
Trond Myklebust3efb9722013-05-29 13:17:04 -04002252 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002253 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002254 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002255 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002256
Trond Myklebust549b19c2013-04-16 18:42:34 -04002257 if ((opendata->o_arg.open_flags & O_EXCL) &&
2258 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002259 nfs4_exclusive_attrset(opendata, sattr);
2260
2261 nfs_fattr_init(opendata->o_res.f_attr);
2262 status = nfs4_do_setattr(state->inode, cred,
2263 opendata->o_res.f_attr, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002264 state, label, olabel);
2265 if (status == 0) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002266 nfs_setattr_update_inode(state->inode, sattr);
David Quigley1775fd32013-05-22 12:50:42 -04002267 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
David Quigleyaa9c2662013-05-22 12:50:44 -04002268 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
David Quigley1775fd32013-05-22 12:50:42 -04002269 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002270 }
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002271 if (opendata->file_created)
2272 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002273
2274 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2275 *ctx_th = opendata->f_attr.mdsthreshold;
2276 else
2277 kfree(opendata->f_attr.mdsthreshold);
2278 opendata->f_attr.mdsthreshold = NULL;
2279
David Quigley14c43f72013-05-22 12:50:43 -04002280 nfs4_label_free(olabel);
2281
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002282 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002285err_free_label:
2286 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002287err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002288 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002289 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002290err_put_state_owner:
2291 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 return status;
2294}
2295
2296
Andy Adamson82be4172012-05-23 05:02:35 -04002297static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002298 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002299 int flags,
2300 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002301 struct nfs4_label *label,
2302 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002304 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 struct nfs4_exception exception = { };
2306 struct nfs4_state *res;
2307 int status;
2308
2309 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002310 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002311 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002312 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 if (status == 0)
2314 break;
2315 /* NOTE: BAD_SEQID means the server and client disagree about the
2316 * book-keeping w.r.t. state-changing operations
2317 * (OPEN/CLOSE/LOCK/LOCKU...)
2318 * It is actually a sign of a bug on the client or on the server.
2319 *
2320 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002321 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 * have unhashed the old state_owner for us, and that we can
2323 * therefore safely retry using a new one. We should still warn
2324 * the user though...
2325 */
2326 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002327 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002328 " returned a bad sequence-id error!\n",
2329 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 exception.retry = 1;
2331 continue;
2332 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002333 /*
2334 * BAD_STATEID on OPEN means that the server cancelled our
2335 * state before it received the OPEN_CONFIRM.
2336 * Recover by retrying the request as per the discussion
2337 * on Page 181 of RFC3530.
2338 */
2339 if (status == -NFS4ERR_BAD_STATEID) {
2340 exception.retry = 1;
2341 continue;
2342 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002343 if (status == -EAGAIN) {
2344 /* We must have found a delegation */
2345 exception.retry = 1;
2346 continue;
2347 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002348 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2349 continue;
2350 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 status, &exception));
2352 } while (exception.retry);
2353 return res;
2354}
2355
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002356static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2357 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002358 struct nfs4_state *state, struct nfs4_label *ilabel,
2359 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002361 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002363 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 .iap = sattr,
2365 .server = server,
2366 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002367 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 };
2369 struct nfs_setattrres res = {
2370 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002371 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 .server = server,
2373 };
2374 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002375 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2376 .rpc_argp = &arg,
2377 .rpc_resp = &res,
2378 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002380 unsigned long timestamp = jiffies;
Trond Myklebustee3ae842013-04-29 10:35:36 -04002381 fmode_t fmode;
2382 bool truncate;
Trond Myklebust65e43082005-08-16 11:49:44 -04002383 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
David Quigleyaa9c2662013-05-22 12:50:44 -04002385 arg.bitmask = nfs4_bitmask(server, ilabel);
2386 if (ilabel)
2387 arg.bitmask = nfs4_bitmask(server, olabel);
2388
Trond Myklebust0e574af2005-10-27 22:12:38 -04002389 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
Trond Myklebustee3ae842013-04-29 10:35:36 -04002391 /* Servers should only apply open mode checks for file size changes */
2392 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2393 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2394
2395 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2396 /* Use that stateid */
2397 } else if (truncate && state != NULL && nfs4_valid_open_stateid(state)) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002398 struct nfs_lockowner lockowner = {
2399 .l_owner = current->files,
2400 .l_pid = current->tgid,
2401 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002402 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002403 &lockowner);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002404 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002405 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
Bryan Schumaker7c513052011-03-24 17:12:24 +00002407 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002408 if (status == 0 && state != NULL)
2409 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002410 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411}
2412
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002413static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2414 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002415 struct nfs4_state *state, struct nfs4_label *ilabel,
2416 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002418 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002419 struct nfs4_exception exception = {
2420 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002421 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002422 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 int err;
2424 do {
David Quigley1775fd32013-05-22 12:50:42 -04002425 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
Trond Myklebustc1578b72013-08-12 16:58:42 -04002426 trace_nfs4_setattr(inode, err);
Trond Myklebust451146b2012-04-18 16:29:11 -04002427 switch (err) {
2428 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002429 if (!(sattr->ia_valid & ATTR_SIZE)) {
2430 pr_warn_once("NFSv4: server %s is incorrectly "
2431 "applying open mode checks to "
2432 "a SETATTR that is not "
2433 "changing file size.\n",
2434 server->nfs_client->cl_hostname);
2435 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002436 if (state && !(state->state & FMODE_WRITE)) {
2437 err = -EBADF;
2438 if (sattr->ia_valid & ATTR_OPEN)
2439 err = -EACCES;
2440 goto out;
2441 }
2442 }
2443 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002445out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 return err;
2447}
2448
2449struct nfs4_closedata {
2450 struct inode *inode;
2451 struct nfs4_state *state;
2452 struct nfs_closeargs arg;
2453 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002454 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002455 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002456 bool roc;
2457 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458};
2459
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002460static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002461{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002462 struct nfs4_closedata *calldata = data;
2463 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002464 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002465
Fred Isamanf7e89172011-01-06 11:36:32 +00002466 if (calldata->roc)
2467 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002468 nfs4_put_open_state(calldata->state);
2469 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002470 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002471 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002472 kfree(calldata);
2473}
2474
Trond Myklebust88069f72009-12-08 08:33:16 -05002475static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2476 fmode_t fmode)
2477{
2478 spin_lock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002479 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002480 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
2481 case FMODE_WRITE:
2482 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2483 break;
2484 case FMODE_READ:
2485 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2486 break;
2487 case 0:
2488 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2489 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2490 clear_bit(NFS_OPEN_STATE, &state->flags);
2491 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002492 spin_unlock(&state->owner->so_lock);
2493}
2494
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002495static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002497 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 struct nfs_server *server = NFS_SERVER(calldata->inode);
2500
Chuck Levera3ca5652012-03-01 17:00:40 -05002501 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002502 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2503 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002504 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 /* hmm. we are done with the inode, and in the process of freeing
2506 * the state_owner. we keep this around to process errors
2507 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 switch (task->tk_status) {
2509 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002510 if (calldata->roc)
2511 pnfs_roc_set_barrier(state->inode,
2512 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002513 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002514 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002515 nfs4_close_clear_stateid_flags(state,
2516 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 break;
2518 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002519 case -NFS4ERR_OLD_STATEID:
2520 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002522 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002523 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002525 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2526 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002528 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002529 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002530 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531}
2532
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002533static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002535 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002536 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002537 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002538 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002539
Chuck Levera3ca5652012-03-01 17:00:40 -05002540 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002541 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002542 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002543
Trond Myklebust88069f72009-12-08 08:33:16 -05002544 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2545 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002546 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002547 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002548 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002549 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002550 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2551 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2552 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002553 }
2554 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002555 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2556 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2557 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002558 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002559 }
Trond Myklebust5d422302013-03-14 16:57:48 -04002560 if (!nfs4_valid_open_stateid(state))
2561 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002562 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002563
2564 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002565 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002566 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002567 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002568
Fred Isamanf7e89172011-01-06 11:36:32 +00002569 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002570 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002571 if (calldata->roc &&
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002572 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2573 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002574 goto out_wait;
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002575 }
Fred Isamanf7e89172011-01-06 11:36:32 +00002576 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002577
Trond Myklebust516a6af2005-10-27 22:12:41 -04002578 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002579 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002580 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002581 &calldata->arg.seq_args,
2582 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002583 task) != 0)
2584 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002585 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002586 return;
2587out_no_action:
2588 task->tk_action = NULL;
2589out_wait:
2590 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591}
2592
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002593static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002594 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002595 .rpc_call_done = nfs4_close_done,
2596 .rpc_release = nfs4_free_closedata,
2597};
2598
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599/*
2600 * It is possible for data to be read/written from a mem-mapped file
2601 * after the sys_close call (which hits the vfs layer as a flush).
2602 * This means that we can't safely call nfsv4 close on a file until
2603 * the inode is cleared. This in turn means that we are not good
2604 * NFSv4 citizens - we do not indicate to the server to update the file's
2605 * share state even when we are done with one of the three share
2606 * stateid's in the inode.
2607 *
2608 * NOTE: Caller must be holding the sp->so_owner semaphore!
2609 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002610int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002612 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002614 struct nfs4_state_owner *sp = state->owner;
2615 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002616 struct rpc_message msg = {
2617 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2618 .rpc_cred = state->owner->so_cred,
2619 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002620 struct rpc_task_setup task_setup_data = {
2621 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002622 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002623 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002624 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002625 .flags = RPC_TASK_ASYNC,
2626 };
Trond Myklebust95121352005-10-18 14:20:12 -07002627 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04002629 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
2630 &task_setup_data.rpc_client, &msg);
2631
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002632 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002634 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04002635 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002636 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002638 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002639 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002641 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002642 if (calldata->arg.seqid == NULL)
2643 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002644 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002645 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002646 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002647 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002648 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002649 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002650 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002651
Trond Myklebust1174dd12010-12-21 10:52:24 -05002652 msg.rpc_argp = &calldata->arg;
2653 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002654 task_setup_data.callback_data = calldata;
2655 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002656 if (IS_ERR(task))
2657 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002658 status = 0;
2659 if (wait)
2660 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002661 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002662 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002663out_free_calldata:
2664 kfree(calldata);
2665out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002666 nfs4_put_open_state(state);
2667 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002668 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669}
2670
Trond Myklebust2b484292010-09-17 10:56:51 -04002671static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002672nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
2673 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04002676 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2677
2678 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679
Trond Myklebust565277f2007-10-15 18:17:53 -04002680 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002681 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04002682
2683 nfs4_label_release_security(label);
2684
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002685 if (IS_ERR(state))
2686 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04002687 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688}
2689
Trond Myklebust1185a552009-12-03 15:54:02 -05002690static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002691{
2692 if (ctx->state == NULL)
2693 return;
2694 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002695 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002696 else
Al Viro643168c2011-06-22 18:20:23 -04002697 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002698}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699
2700static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2701{
Benny Halevy43652ad2009-04-01 09:21:54 -04002702 struct nfs4_server_caps_arg args = {
2703 .fhandle = fhandle,
2704 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 struct nfs4_server_caps_res res = {};
2706 struct rpc_message msg = {
2707 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002708 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 .rpc_resp = &res,
2710 };
2711 int status;
2712
Bryan Schumaker7c513052011-03-24 17:12:24 +00002713 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 if (status == 0) {
2715 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002716 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2717 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2718 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2719 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2720 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2722 server->caps |= NFS_CAP_ACLS;
2723 if (res.has_links != 0)
2724 server->caps |= NFS_CAP_HARDLINKS;
2725 if (res.has_symlinks != 0)
2726 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002727 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2728 server->caps |= NFS_CAP_FILEID;
2729 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2730 server->caps |= NFS_CAP_MODE;
2731 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2732 server->caps |= NFS_CAP_NLINK;
2733 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2734 server->caps |= NFS_CAP_OWNER;
2735 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2736 server->caps |= NFS_CAP_OWNER_GROUP;
2737 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2738 server->caps |= NFS_CAP_ATIME;
2739 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2740 server->caps |= NFS_CAP_CTIME;
2741 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2742 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04002743#ifdef CONFIG_NFS_V4_SECURITY_LABEL
2744 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
2745 server->caps |= NFS_CAP_SECURITY_LABEL;
2746#endif
2747 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
2748 sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002749
David Quigleyaa9c2662013-05-22 12:50:44 -04002750 if (server->caps & NFS_CAP_SECURITY_LABEL) {
2751 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2752 res.attr_bitmask[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2753 }
Trond Myklebusta65318b2009-03-11 14:10:28 -04002754 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2755 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2756 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002758 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002760
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 return status;
2762}
2763
Trond Myklebust55a97592006-06-09 09:34:19 -04002764int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765{
2766 struct nfs4_exception exception = { };
2767 int err;
2768 do {
2769 err = nfs4_handle_exception(server,
2770 _nfs4_server_capabilities(server, fhandle),
2771 &exception);
2772 } while (exception.retry);
2773 return err;
2774}
2775
2776static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2777 struct nfs_fsinfo *info)
2778{
David Quigleyaa9c2662013-05-22 12:50:44 -04002779 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04002781 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 };
2783 struct nfs4_lookup_res res = {
2784 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002785 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 .fh = fhandle,
2787 };
2788 struct rpc_message msg = {
2789 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2790 .rpc_argp = &args,
2791 .rpc_resp = &res,
2792 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002793
David Quigleyaa9c2662013-05-22 12:50:44 -04002794 bitmask[0] = nfs4_fattr_bitmap[0];
2795 bitmask[1] = nfs4_fattr_bitmap[1];
2796 /*
2797 * Process the label in the upcoming getfattr
2798 */
2799 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
2800
Trond Myklebust0e574af2005-10-27 22:12:38 -04002801 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002802 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803}
2804
2805static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2806 struct nfs_fsinfo *info)
2807{
2808 struct nfs4_exception exception = { };
2809 int err;
2810 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002811 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04002812 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002813 switch (err) {
2814 case 0:
2815 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002816 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002817 default:
2818 err = nfs4_handle_exception(server, err, &exception);
2819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002821out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 return err;
2823}
2824
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002825static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2826 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2827{
Trond Myklebustc2190662013-08-26 19:23:04 -04002828 struct rpc_auth_create_args auth_args = {
2829 .pseudoflavor = flavor,
2830 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002831 struct rpc_auth *auth;
2832 int ret;
2833
Trond Myklebustc2190662013-08-26 19:23:04 -04002834 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002835 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04002836 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002837 goto out;
2838 }
2839 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002840out:
2841 return ret;
2842}
2843
Chuck Lever9a744ba2013-03-16 15:56:02 -04002844/*
2845 * Retry pseudoroot lookup with various security flavors. We do this when:
2846 *
2847 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2848 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2849 *
2850 * Returns zero on success, or a negative NFS4ERR value, or a
2851 * negative errno value.
2852 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002853static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002854 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855{
Chuck Lever9a744ba2013-03-16 15:56:02 -04002856 /* Per 3530bis 15.33.5 */
2857 static const rpc_authflavor_t flav_array[] = {
2858 RPC_AUTH_GSS_KRB5P,
2859 RPC_AUTH_GSS_KRB5I,
2860 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04002861 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04002862 RPC_AUTH_NULL,
2863 };
2864 int status = -EPERM;
2865 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866
Chuck Lever9a744ba2013-03-16 15:56:02 -04002867 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002868 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002869 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002870 continue;
2871 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002872 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04002873
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002874 /*
2875 * -EACCESS could mean that the user doesn't have correct permissions
2876 * to access the mount. It could also mean that we tried to mount
2877 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2878 * existing mount programs don't handle -EACCES very well so it should
2879 * be mapped to -EPERM instead.
2880 */
2881 if (status == -EACCES)
2882 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002883 return status;
2884}
2885
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002886static int nfs4_do_find_root_sec(struct nfs_server *server,
2887 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
2888{
2889 int mv = server->nfs_client->cl_minorversion;
2890 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
2891}
2892
2893/**
2894 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
2895 * @server: initialized nfs_server handle
2896 * @fhandle: we fill in the pseudo-fs root file handle
2897 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002898 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002899 *
2900 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002901 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002902int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002903 struct nfs_fsinfo *info,
2904 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002905{
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002906 int status;
2907
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002908 switch (auth_probe) {
2909 case false:
2910 status = nfs4_lookup_root(server, fhandle, info);
2911 if (status != -NFS4ERR_WRONGSEC)
2912 break;
2913 /* Did user force a 'sec=' mount option? */
2914 if (server->flags & NFS_MOUNT_SECFLAVOUR)
2915 break;
2916 default:
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002917 status = nfs4_do_find_root_sec(server, fhandle, info);
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002918 }
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002919
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 if (status == 0)
2921 status = nfs4_server_capabilities(server, fhandle);
2922 if (status == 0)
2923 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002924
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002925 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926}
2927
Bryan Schumakerbae36242012-05-10 15:07:31 -04002928static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2929 struct nfs_fsinfo *info)
2930{
2931 int error;
2932 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04002933 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04002934
2935 error = nfs4_server_capabilities(server, mntfh);
2936 if (error < 0) {
2937 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2938 return error;
2939 }
2940
David Quigley14c43f72013-05-22 12:50:43 -04002941 label = nfs4_label_alloc(server, GFP_KERNEL);
2942 if (IS_ERR(label))
2943 return PTR_ERR(label);
2944
David Quigley1775fd32013-05-22 12:50:42 -04002945 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04002946 if (error < 0) {
2947 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04002948 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04002949 }
2950
2951 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2952 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2953 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2954
David Quigley14c43f72013-05-22 12:50:43 -04002955err_free_label:
2956 nfs4_label_free(label);
2957
Bryan Schumakerbae36242012-05-10 15:07:31 -04002958 return error;
2959}
2960
Manoj Naik6b97fd32006-06-09 09:34:29 -04002961/*
2962 * Get locations and (maybe) other attributes of a referral.
2963 * Note that we'll actually follow the referral later when
2964 * we detect fsid mismatch in inode revalidation
2965 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002966static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2967 const struct qstr *name, struct nfs_fattr *fattr,
2968 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002969{
2970 int status = -ENOMEM;
2971 struct page *page = NULL;
2972 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002973
2974 page = alloc_page(GFP_KERNEL);
2975 if (page == NULL)
2976 goto out;
2977 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2978 if (locations == NULL)
2979 goto out;
2980
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002981 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002982 if (status != 0)
2983 goto out;
2984 /* Make sure server returned a different fsid for the referral */
2985 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002986 dprintk("%s: server did not return a different fsid for"
2987 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002988 status = -EIO;
2989 goto out;
2990 }
Andy Adamson533eb462011-06-13 18:25:56 -04002991 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2992 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002993
Andy Adamson533eb462011-06-13 18:25:56 -04002994 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002995 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002996 memset(fhandle, 0, sizeof(struct nfs_fh));
2997out:
2998 if (page)
2999 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003000 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003001 return status;
3002}
3003
David Quigley1775fd32013-05-22 12:50:42 -04003004static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3005 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006{
3007 struct nfs4_getattr_arg args = {
3008 .fh = fhandle,
3009 .bitmask = server->attr_bitmask,
3010 };
3011 struct nfs4_getattr_res res = {
3012 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003013 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 .server = server,
3015 };
3016 struct rpc_message msg = {
3017 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3018 .rpc_argp = &args,
3019 .rpc_resp = &res,
3020 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003021
3022 args.bitmask = nfs4_bitmask(server, label);
3023
Trond Myklebust0e574af2005-10-27 22:12:38 -04003024 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003025 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026}
3027
David Quigley1775fd32013-05-22 12:50:42 -04003028static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3029 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030{
3031 struct nfs4_exception exception = { };
3032 int err;
3033 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003034 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3035 trace_nfs4_getattr(server, fhandle, fattr, err);
3036 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 &exception);
3038 } while (exception.retry);
3039 return err;
3040}
3041
3042/*
3043 * The file is not closed if it is opened due to the a request to change
3044 * the size of the file. The open call will not be needed once the
3045 * VFS layer lookup-intents are implemented.
3046 *
3047 * Close is called when the inode is destroyed.
3048 * If we haven't opened the file for O_WRONLY, we
3049 * need to in the size_change case to obtain a stateid.
3050 *
3051 * Got race?
3052 * Because OPEN is always done by name in nfsv4, it is
3053 * possible that we opened a different file by the same
3054 * name. We can recognize this race condition, but we
3055 * can't do anything about it besides returning an error.
3056 *
3057 * This will be fixed with VFS changes (lookup-intent).
3058 */
3059static int
3060nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3061 struct iattr *sattr)
3062{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003063 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003064 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003065 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003066 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 int status;
3068
Benny Halevy8a1636c2010-07-14 15:43:57 -04003069 if (pnfs_ld_layoutret_on_setattr(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003070 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003071
Trond Myklebust0e574af2005-10-27 22:12:38 -04003072 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073
Andy Adamson26699402012-05-30 16:12:24 -04003074 /* Deal with open(O_TRUNC) */
3075 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003076 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003077
3078 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003079 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003080 return 0;
3081
Trond Myklebustd5308382005-11-04 15:33:38 -05003082 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003083 if (sattr->ia_valid & ATTR_FILE) {
3084 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003085
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003086 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003087 if (ctx) {
3088 cred = ctx->cred;
3089 state = ctx->state;
3090 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003091 }
3092
David Quigley14c43f72013-05-22 12:50:43 -04003093 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3094 if (IS_ERR(label))
3095 return PTR_ERR(label);
3096
3097 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003098 if (status == 0) {
Trond Myklebust65e43082005-08-16 11:49:44 -04003099 nfs_setattr_update_inode(inode, sattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003100 nfs_setsecurity(inode, fattr, label);
3101 }
David Quigley14c43f72013-05-22 12:50:43 -04003102 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 return status;
3104}
3105
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003106static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3107 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003108 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003109{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003110 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003111 int status;
3112 struct nfs4_lookup_arg args = {
3113 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003114 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003115 .name = name,
3116 };
3117 struct nfs4_lookup_res res = {
3118 .server = server,
3119 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003120 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003121 .fh = fhandle,
3122 };
3123 struct rpc_message msg = {
3124 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3125 .rpc_argp = &args,
3126 .rpc_resp = &res,
3127 };
3128
David Quigleyaa9c2662013-05-22 12:50:44 -04003129 args.bitmask = nfs4_bitmask(server, label);
3130
David Howells2b3de442006-08-22 20:06:09 -04003131 nfs_fattr_init(fattr);
3132
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003134 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 dprintk("NFS reply lookup: %d\n", status);
3136 return status;
3137}
3138
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003139static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003140{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003141 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003142 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003143 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3144 fattr->nlink = 2;
3145}
3146
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003147static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3148 struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003149 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003150{
3151 struct nfs4_exception exception = { };
3152 struct rpc_clnt *client = *clnt;
3153 int err;
3154 do {
David Quigley1775fd32013-05-22 12:50:42 -04003155 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003156 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003157 switch (err) {
3158 case -NFS4ERR_BADNAME:
3159 err = -ENOENT;
3160 goto out;
3161 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003162 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003163 goto out;
3164 case -NFS4ERR_WRONGSEC:
3165 err = -EPERM;
3166 if (client != *clnt)
3167 goto out;
Trond Myklebust41d058c2013-09-07 17:49:05 -04003168 /* No security negotiation if the user specified 'sec=' */
3169 if (NFS_SERVER(dir)->flags & NFS_MOUNT_SECFLAVOUR)
3170 goto out;
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003171 client = nfs4_create_sec_client(client, dir, name);
3172 if (IS_ERR(client))
3173 return PTR_ERR(client);
3174
3175 exception.retry = 1;
3176 break;
3177 default:
3178 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3179 }
3180 } while (exception.retry);
3181
3182out:
3183 if (err == 0)
3184 *clnt = client;
3185 else if (client != *clnt)
3186 rpc_shutdown_client(client);
3187
3188 return err;
3189}
3190
Bryan Schumaker80a16b22012-04-27 13:27:46 -04003191static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003192 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3193 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003195 int status;
3196 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003197
David Quigley1775fd32013-05-22 12:50:42 -04003198 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003199 if (client != NFS_CLIENT(dir)) {
3200 rpc_shutdown_client(client);
3201 nfs_fixup_secinfo_attributes(fattr);
3202 }
3203 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204}
3205
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003206struct rpc_clnt *
3207nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3208 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3209{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003210 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003211 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003212
David Quigley1775fd32013-05-22 12:50:42 -04003213 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003214 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003215 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003216 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003217}
3218
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3220{
Trond Myklebust76b32992007-08-10 17:45:11 -04003221 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 struct nfs4_accessargs args = {
3223 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003224 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003226 struct nfs4_accessres res = {
3227 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003228 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 struct rpc_message msg = {
3230 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3231 .rpc_argp = &args,
3232 .rpc_resp = &res,
3233 .rpc_cred = entry->cred,
3234 };
3235 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003236 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237
3238 /*
3239 * Determine which access bits we want to ask for...
3240 */
3241 if (mode & MAY_READ)
3242 args.access |= NFS4_ACCESS_READ;
3243 if (S_ISDIR(inode->i_mode)) {
3244 if (mode & MAY_WRITE)
3245 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3246 if (mode & MAY_EXEC)
3247 args.access |= NFS4_ACCESS_LOOKUP;
3248 } else {
3249 if (mode & MAY_WRITE)
3250 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3251 if (mode & MAY_EXEC)
3252 args.access |= NFS4_ACCESS_EXECUTE;
3253 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003254
3255 res.fattr = nfs_alloc_fattr();
3256 if (res.fattr == NULL)
3257 return -ENOMEM;
3258
Bryan Schumaker7c513052011-03-24 17:12:24 +00003259 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003261 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003262 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003264 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 return status;
3266}
3267
3268static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3269{
3270 struct nfs4_exception exception = { };
3271 int err;
3272 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003273 err = _nfs4_proc_access(inode, entry);
3274 trace_nfs4_access(inode, err);
3275 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 &exception);
3277 } while (exception.retry);
3278 return err;
3279}
3280
3281/*
3282 * TODO: For the time being, we don't try to get any attributes
3283 * along with any of the zero-copy operations READ, READDIR,
3284 * READLINK, WRITE.
3285 *
3286 * In the case of the first three, we want to put the GETATTR
3287 * after the read-type operation -- this is because it is hard
3288 * to predict the length of a GETATTR response in v4, and thus
3289 * align the READ data correctly. This means that the GETATTR
3290 * may end up partially falling into the page cache, and we should
3291 * shift it into the 'tail' of the xdr_buf before processing.
3292 * To do this efficiently, we need to know the total length
3293 * of data received, which doesn't seem to be available outside
3294 * of the RPC layer.
3295 *
3296 * In the case of WRITE, we also want to put the GETATTR after
3297 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003298 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 *
3300 * Both of these changes to the XDR layer would in fact be quite
3301 * minor, but I decided to leave them for a subsequent patch.
3302 */
3303static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3304 unsigned int pgbase, unsigned int pglen)
3305{
3306 struct nfs4_readlink args = {
3307 .fh = NFS_FH(inode),
3308 .pgbase = pgbase,
3309 .pglen = pglen,
3310 .pages = &page,
3311 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003312 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 struct rpc_message msg = {
3314 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3315 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003316 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 };
3318
Bryan Schumaker7c513052011-03-24 17:12:24 +00003319 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 -07003320}
3321
3322static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3323 unsigned int pgbase, unsigned int pglen)
3324{
3325 struct nfs4_exception exception = { };
3326 int err;
3327 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003328 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3329 trace_nfs4_readlink(inode, err);
3330 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 &exception);
3332 } while (exception.retry);
3333 return err;
3334}
3335
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003337 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339static int
3340nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003341 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342{
David Quigleyaa9c2662013-05-22 12:50:44 -04003343 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003344 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 struct nfs4_state *state;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003346 int opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 int status = 0;
3348
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003349 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3350 if (IS_ERR(ctx))
3351 return PTR_ERR(ctx);
3352
David Quigleyaa9c2662013-05-22 12:50:44 -04003353 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3354
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003355 sattr->ia_mode &= ~current_umask();
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003356 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, &opened);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 if (IS_ERR(state)) {
3358 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003359 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003362 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003363 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 return status;
3365}
3366
3367static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3368{
Trond Myklebust16e42952005-10-27 22:12:44 -04003369 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003370 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003372 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003374 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003375 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003376 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003378 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3379 .rpc_argp = &args,
3380 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003382 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003383
Bryan Schumaker7c513052011-03-24 17:12:24 +00003384 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003385 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003386 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 return status;
3388}
3389
3390static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3391{
3392 struct nfs4_exception exception = { };
3393 int err;
3394 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003395 err = _nfs4_proc_remove(dir, name);
3396 trace_nfs4_remove(dir, name, err);
3397 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 &exception);
3399 } while (exception.retry);
3400 return err;
3401}
3402
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003403static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003405 struct nfs_server *server = NFS_SERVER(dir);
3406 struct nfs_removeargs *args = msg->rpc_argp;
3407 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003409 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04003411 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003412
3413 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414}
3415
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003416static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3417{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003418 nfs4_setup_sequence(NFS_SERVER(data->dir),
3419 &data->args.seq_args,
3420 &data->res.seq_res,
3421 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422}
3423
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003424static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003426 struct nfs_unlinkdata *data = task->tk_calldata;
3427 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003428
Trond Myklebust14516c32010-07-31 14:29:06 -04003429 if (!nfs4_sequence_done(task, &res->seq_res))
3430 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003431 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003432 return 0;
3433 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003434 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435}
3436
Jeff Laytond3d41522010-09-17 17:31:57 -04003437static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3438{
3439 struct nfs_server *server = NFS_SERVER(dir);
3440 struct nfs_renameargs *arg = msg->rpc_argp;
3441 struct nfs_renameres *res = msg->rpc_resp;
3442
3443 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003444 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04003445 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003446}
3447
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003448static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3449{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003450 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3451 &data->args.seq_args,
3452 &data->res.seq_res,
3453 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003454}
3455
3456static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3457 struct inode *new_dir)
3458{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003459 struct nfs_renamedata *data = task->tk_calldata;
3460 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04003461
3462 if (!nfs4_sequence_done(task, &res->seq_res))
3463 return 0;
3464 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3465 return 0;
3466
3467 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003468 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003469 return 1;
3470}
3471
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3473 struct inode *new_dir, struct qstr *new_name)
3474{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003475 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003476 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 .old_dir = NFS_FH(old_dir),
3478 .new_dir = NFS_FH(new_dir),
3479 .old_name = old_name,
3480 .new_name = new_name,
3481 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003482 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003483 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003484 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 struct rpc_message msg = {
3486 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3487 .rpc_argp = &arg,
3488 .rpc_resp = &res,
3489 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003490 int status = -ENOMEM;
David Quigley14c43f72013-05-22 12:50:43 -04003491
Bryan Schumaker7c513052011-03-24 17:12:24 +00003492 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 if (!status) {
3494 update_changeattr(old_dir, &res.old_cinfo);
3495 update_changeattr(new_dir, &res.new_cinfo);
3496 }
3497 return status;
3498}
3499
3500static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3501 struct inode *new_dir, struct qstr *new_name)
3502{
3503 struct nfs4_exception exception = { };
3504 int err;
3505 do {
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003506 err = _nfs4_proc_rename(old_dir, old_name,
3507 new_dir, new_name);
3508 trace_nfs4_rename(old_dir, old_name, new_dir, new_name, err);
3509 err = nfs4_handle_exception(NFS_SERVER(old_dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 &exception);
3511 } while (exception.retry);
3512 return err;
3513}
3514
3515static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3516{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003517 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 struct nfs4_link_arg arg = {
3519 .fh = NFS_FH(inode),
3520 .dir_fh = NFS_FH(dir),
3521 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003522 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003524 struct nfs4_link_res res = {
3525 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003526 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003527 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 struct rpc_message msg = {
3529 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3530 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003531 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003533 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534
Trond Myklebust136f2622010-04-16 16:22:49 -04003535 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003536 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003537 goto out;
3538
David Quigley14c43f72013-05-22 12:50:43 -04003539 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3540 if (IS_ERR(res.label)) {
3541 status = PTR_ERR(res.label);
3542 goto out;
3543 }
David Quigleyaa9c2662013-05-22 12:50:44 -04003544 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04003545
Bryan Schumaker7c513052011-03-24 17:12:24 +00003546 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003547 if (!status) {
3548 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04003549 status = nfs_post_op_update_inode(inode, res.fattr);
3550 if (!status)
3551 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003552 }
David Quigley14c43f72013-05-22 12:50:43 -04003553
3554
3555 nfs4_label_free(res.label);
3556
Trond Myklebust136f2622010-04-16 16:22:49 -04003557out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003558 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 return status;
3560}
3561
3562static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3563{
3564 struct nfs4_exception exception = { };
3565 int err;
3566 do {
3567 err = nfs4_handle_exception(NFS_SERVER(inode),
3568 _nfs4_proc_link(inode, dir, name),
3569 &exception);
3570 } while (exception.retry);
3571 return err;
3572}
3573
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003574struct nfs4_createdata {
3575 struct rpc_message msg;
3576 struct nfs4_create_arg arg;
3577 struct nfs4_create_res res;
3578 struct nfs_fh fh;
3579 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003580 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003581};
3582
3583static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3584 struct qstr *name, struct iattr *sattr, u32 ftype)
3585{
3586 struct nfs4_createdata *data;
3587
3588 data = kzalloc(sizeof(*data), GFP_KERNEL);
3589 if (data != NULL) {
3590 struct nfs_server *server = NFS_SERVER(dir);
3591
David Quigley14c43f72013-05-22 12:50:43 -04003592 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3593 if (IS_ERR(data->label))
3594 goto out_free;
3595
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003596 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3597 data->msg.rpc_argp = &data->arg;
3598 data->msg.rpc_resp = &data->res;
3599 data->arg.dir_fh = NFS_FH(dir);
3600 data->arg.server = server;
3601 data->arg.name = name;
3602 data->arg.attrs = sattr;
3603 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04003604 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003605 data->res.server = server;
3606 data->res.fh = &data->fh;
3607 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003608 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003609 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003610 }
3611 return data;
David Quigley14c43f72013-05-22 12:50:43 -04003612out_free:
3613 kfree(data);
3614 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003615}
3616
3617static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3618{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003619 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003620 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003621 if (status == 0) {
3622 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04003623 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003624 }
3625 return status;
3626}
3627
3628static void nfs4_free_createdata(struct nfs4_createdata *data)
3629{
David Quigley14c43f72013-05-22 12:50:43 -04003630 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003631 kfree(data);
3632}
3633
Chuck Lever4f390c12006-08-22 20:06:22 -04003634static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003635 struct page *page, unsigned int len, struct iattr *sattr,
3636 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003638 struct nfs4_createdata *data;
3639 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640
Chuck Lever94a6d752006-08-22 20:06:23 -04003641 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003642 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003643
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003644 status = -ENOMEM;
3645 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3646 if (data == NULL)
3647 goto out;
3648
3649 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3650 data->arg.u.symlink.pages = &page;
3651 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04003652 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003654 status = nfs4_do_create(dir, dentry, data);
3655
3656 nfs4_free_createdata(data);
3657out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 return status;
3659}
3660
Chuck Lever4f390c12006-08-22 20:06:22 -04003661static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003662 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663{
3664 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003665 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04003667
3668 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3669
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003671 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3672 trace_nfs4_symlink(dir, &dentry->d_name, err);
3673 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 &exception);
3675 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003676
3677 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 return err;
3679}
3680
3681static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003682 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003684 struct nfs4_createdata *data;
3685 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003687 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3688 if (data == NULL)
3689 goto out;
3690
David Quigley1775fd32013-05-22 12:50:42 -04003691 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003692 status = nfs4_do_create(dir, dentry, data);
3693
3694 nfs4_free_createdata(data);
3695out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 return status;
3697}
3698
3699static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3700 struct iattr *sattr)
3701{
3702 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003703 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003705
David Quigleyaa9c2662013-05-22 12:50:44 -04003706 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3707
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003708 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003710 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3711 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3712 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 &exception);
3714 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003715 nfs4_label_release_security(label);
3716
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 return err;
3718}
3719
3720static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003721 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722{
3723 struct inode *dir = dentry->d_inode;
3724 struct nfs4_readdir_arg args = {
3725 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003726 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 .pgbase = 0,
3728 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003729 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003730 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 };
3732 struct nfs4_readdir_res res;
3733 struct rpc_message msg = {
3734 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3735 .rpc_argp = &args,
3736 .rpc_resp = &res,
3737 .rpc_cred = cred,
3738 };
3739 int status;
3740
Harvey Harrison3110ff82008-05-02 13:42:44 -07003741 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003742 dentry->d_parent->d_name.name,
3743 dentry->d_name.name,
3744 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003745 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003747 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 -05003748 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003749 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003750 status += args.pgbase;
3751 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003752
3753 nfs_invalidate_atime(dir);
3754
Harvey Harrison3110ff82008-05-02 13:42:44 -07003755 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 return status;
3757}
3758
3759static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003760 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761{
3762 struct nfs4_exception exception = { };
3763 int err;
3764 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003765 err = _nfs4_proc_readdir(dentry, cred, cookie,
3766 pages, count, plus);
3767 trace_nfs4_readdir(dentry->d_inode, err);
3768 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 &exception);
3770 } while (exception.retry);
3771 return err;
3772}
3773
3774static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04003775 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003777 struct nfs4_createdata *data;
3778 int mode = sattr->ia_mode;
3779 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003781 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3782 if (data == NULL)
3783 goto out;
3784
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003786 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003788 data->arg.ftype = NF4BLK;
3789 data->arg.u.device.specdata1 = MAJOR(rdev);
3790 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 }
3792 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003793 data->arg.ftype = NF4CHR;
3794 data->arg.u.device.specdata1 = MAJOR(rdev);
3795 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003796 } else if (!S_ISSOCK(mode)) {
3797 status = -EINVAL;
3798 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 }
David Quigley1775fd32013-05-22 12:50:42 -04003800
David Quigleyaa9c2662013-05-22 12:50:44 -04003801 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003802 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003803out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003804 nfs4_free_createdata(data);
3805out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 return status;
3807}
3808
3809static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3810 struct iattr *sattr, dev_t rdev)
3811{
3812 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003813 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003815
David Quigleyaa9c2662013-05-22 12:50:44 -04003816 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3817
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003818 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003820 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
3821 trace_nfs4_mknod(dir, &dentry->d_name, err);
3822 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 &exception);
3824 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003825
3826 nfs4_label_release_security(label);
3827
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 return err;
3829}
3830
3831static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3832 struct nfs_fsstat *fsstat)
3833{
3834 struct nfs4_statfs_arg args = {
3835 .fh = fhandle,
3836 .bitmask = server->attr_bitmask,
3837 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003838 struct nfs4_statfs_res res = {
3839 .fsstat = fsstat,
3840 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 struct rpc_message msg = {
3842 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3843 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003844 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 };
3846
Trond Myklebust0e574af2005-10-27 22:12:38 -04003847 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003848 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849}
3850
3851static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3852{
3853 struct nfs4_exception exception = { };
3854 int err;
3855 do {
3856 err = nfs4_handle_exception(server,
3857 _nfs4_proc_statfs(server, fhandle, fsstat),
3858 &exception);
3859 } while (exception.retry);
3860 return err;
3861}
3862
3863static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3864 struct nfs_fsinfo *fsinfo)
3865{
3866 struct nfs4_fsinfo_arg args = {
3867 .fh = fhandle,
3868 .bitmask = server->attr_bitmask,
3869 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003870 struct nfs4_fsinfo_res res = {
3871 .fsinfo = fsinfo,
3872 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 struct rpc_message msg = {
3874 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3875 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003876 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 };
3878
Bryan Schumaker7c513052011-03-24 17:12:24 +00003879 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880}
3881
3882static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3883{
3884 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04003885 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 int err;
3887
3888 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04003889 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003890 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04003891 if (err == 0) {
3892 struct nfs_client *clp = server->nfs_client;
3893
3894 spin_lock(&clp->cl_lock);
3895 clp->cl_lease_time = fsinfo->lease_time * HZ;
3896 clp->cl_last_renewal = now;
3897 spin_unlock(&clp->cl_lock);
3898 break;
3899 }
3900 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 } while (exception.retry);
3902 return err;
3903}
3904
3905static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3906{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003907 int error;
3908
Trond Myklebust0e574af2005-10-27 22:12:38 -04003909 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003910 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003911 if (error == 0) {
3912 /* block layout checks this! */
3913 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003914 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003915 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003916
3917 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918}
3919
3920static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3921 struct nfs_pathconf *pathconf)
3922{
3923 struct nfs4_pathconf_arg args = {
3924 .fh = fhandle,
3925 .bitmask = server->attr_bitmask,
3926 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003927 struct nfs4_pathconf_res res = {
3928 .pathconf = pathconf,
3929 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 struct rpc_message msg = {
3931 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3932 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003933 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 };
3935
3936 /* None of the pathconf attributes are mandatory to implement */
3937 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3938 memset(pathconf, 0, sizeof(*pathconf));
3939 return 0;
3940 }
3941
Trond Myklebust0e574af2005-10-27 22:12:38 -04003942 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003943 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944}
3945
3946static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3947 struct nfs_pathconf *pathconf)
3948{
3949 struct nfs4_exception exception = { };
3950 int err;
3951
3952 do {
3953 err = nfs4_handle_exception(server,
3954 _nfs4_proc_pathconf(server, fhandle, pathconf),
3955 &exception);
3956 } while (exception.retry);
3957 return err;
3958}
3959
Trond Myklebust5521abf2013-03-16 20:54:34 -04003960int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04003961 const struct nfs_open_context *ctx,
3962 const struct nfs_lock_context *l_ctx,
3963 fmode_t fmode)
3964{
3965 const struct nfs_lockowner *lockowner = NULL;
3966
3967 if (l_ctx != NULL)
3968 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003969 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04003970}
3971EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
3972
Trond Myklebust5521abf2013-03-16 20:54:34 -04003973static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
3974 const struct nfs_open_context *ctx,
3975 const struct nfs_lock_context *l_ctx,
3976 fmode_t fmode)
3977{
3978 nfs4_stateid current_stateid;
3979
3980 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode))
3981 return false;
3982 return nfs4_stateid_match(stateid, &current_stateid);
3983}
3984
3985static bool nfs4_error_stateid_expired(int err)
3986{
3987 switch (err) {
3988 case -NFS4ERR_DELEG_REVOKED:
3989 case -NFS4ERR_ADMIN_REVOKED:
3990 case -NFS4ERR_BAD_STATEID:
3991 case -NFS4ERR_STALE_STATEID:
3992 case -NFS4ERR_OLD_STATEID:
3993 case -NFS4ERR_OPENMODE:
3994 case -NFS4ERR_EXPIRED:
3995 return true;
3996 }
3997 return false;
3998}
3999
Benny Halevyd20581a2011-05-22 19:52:03 +03004000void __nfs4_read_done_cb(struct nfs_read_data *data)
4001{
Fred Isamancd841602012-04-20 14:47:44 -04004002 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03004003}
4004
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004005static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006{
Fred Isamancd841602012-04-20 14:47:44 -04004007 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004009 trace_nfs4_read(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004010 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004011 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05004012 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004014
Benny Halevyd20581a2011-05-22 19:52:03 +03004015 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05004017 renew_lease(server, data->timestamp);
4018 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019}
4020
Trond Myklebust5521abf2013-03-16 20:54:34 -04004021static bool nfs4_read_stateid_changed(struct rpc_task *task,
4022 struct nfs_readargs *args)
4023{
4024
4025 if (!nfs4_error_stateid_expired(task->tk_status) ||
4026 nfs4_stateid_is_current(&args->stateid,
4027 args->context,
4028 args->lock_context,
4029 FMODE_READ))
4030 return false;
4031 rpc_restart_call_prepare(task);
4032 return true;
4033}
4034
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004035static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
4036{
4037
4038 dprintk("--> %s\n", __func__);
4039
4040 if (!nfs4_sequence_done(task, &data->res.seq_res))
4041 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004042 if (nfs4_read_stateid_changed(task, &data->args))
4043 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03004044 return data->read_done_cb ? data->read_done_cb(task, data) :
4045 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004046}
4047
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004048static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 data->timestamp = jiffies;
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004051 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004052 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Chuck Levera9c92d62013-08-09 12:48:18 -04004053 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054}
4055
NeilBrownef1820f2013-09-04 17:04:49 +10004056static int nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004057{
Trond Myklebust9b206142013-03-17 15:52:00 -04004058 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004059 &data->args.seq_args,
4060 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004061 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004062 return 0;
4063 if (nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4064 data->args.lock_context, FMODE_READ) == -EIO)
4065 return -EIO;
4066 if (unlikely(test_bit(NFS_CONTEXT_BAD, &data->args.context->flags)))
4067 return -EIO;
4068 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069}
4070
Fred Isamanb029bc92011-03-03 15:13:42 +00004071static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072{
Fred Isamancd841602012-04-20 14:47:44 -04004073 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004075 trace_nfs4_write(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004076 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004077 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004078 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004080 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04004082 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004083 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004084 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085}
4086
Trond Myklebust5521abf2013-03-16 20:54:34 -04004087static bool nfs4_write_stateid_changed(struct rpc_task *task,
4088 struct nfs_writeargs *args)
4089{
4090
4091 if (!nfs4_error_stateid_expired(task->tk_status) ||
4092 nfs4_stateid_is_current(&args->stateid,
4093 args->context,
4094 args->lock_context,
4095 FMODE_WRITE))
4096 return false;
4097 rpc_restart_call_prepare(task);
4098 return true;
4099}
4100
Fred Isamanb029bc92011-03-03 15:13:42 +00004101static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
4102{
4103 if (!nfs4_sequence_done(task, &data->res.seq_res))
4104 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004105 if (nfs4_write_stateid_changed(task, &data->args))
4106 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03004107 return data->write_done_cb ? data->write_done_cb(task, data) :
4108 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00004109}
4110
Trond Myklebust5a37f852012-04-28 14:55:16 -04004111static
4112bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00004113{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004114 const struct nfs_pgio_header *hdr = data->header;
4115
4116 /* Don't request attributes for pNFS or O_DIRECT writes */
4117 if (data->ds_clp != NULL || hdr->dreq != NULL)
4118 return false;
4119 /* Otherwise, request attributes if and only if we don't hold
4120 * a delegation
4121 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004122 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004123}
Fred Isamana69aef12011-03-03 15:13:47 +00004124
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004125static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126{
Fred Isamancd841602012-04-20 14:47:44 -04004127 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004128
Trond Myklebust5a37f852012-04-28 14:55:16 -04004129 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00004130 data->args.bitmask = NULL;
4131 data->res.fattr = NULL;
4132 } else
4133 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004134
Fred Isamanb029bc92011-03-03 15:13:42 +00004135 if (!data->write_done_cb)
4136 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004137 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 data->timestamp = jiffies;
4139
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004140 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004141 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142}
4143
NeilBrownef1820f2013-09-04 17:04:49 +10004144static int nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04004145{
Trond Myklebust9b206142013-03-17 15:52:00 -04004146 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004147 &data->args.seq_args,
4148 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004149 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004150 return 0;
4151 if (nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4152 data->args.lock_context, FMODE_WRITE) == -EIO)
4153 return -EIO;
4154 if (unlikely(test_bit(NFS_CONTEXT_BAD, &data->args.context->flags)))
4155 return -EIO;
4156 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157}
4158
Fred Isaman0b7c0152012-04-20 14:47:39 -04004159static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4160{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004161 nfs4_setup_sequence(NFS_SERVER(data->inode),
4162 &data->args.seq_args,
4163 &data->res.seq_res,
4164 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165}
4166
Fred Isaman0b7c0152012-04-20 14:47:39 -04004167static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004170
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004171 trace_nfs4_commit(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004172 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004173 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004174 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004176 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177}
4178
Fred Isaman0b7c0152012-04-20 14:47:39 -04004179static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004180{
4181 if (!nfs4_sequence_done(task, &data->res.seq_res))
4182 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004183 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004184}
4185
Fred Isaman0b7c0152012-04-20 14:47:39 -04004186static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004188 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004189
Fred Isaman0b7c0152012-04-20 14:47:39 -04004190 if (data->commit_done_cb == NULL)
4191 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004192 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004193 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004194 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195}
4196
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004197struct nfs4_renewdata {
4198 struct nfs_client *client;
4199 unsigned long timestamp;
4200};
4201
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202/*
4203 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4204 * standalone procedure for queueing an asynchronous RENEW.
4205 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004206static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004207{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004208 struct nfs4_renewdata *data = calldata;
4209 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004210
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004211 if (atomic_read(&clp->cl_count) > 1)
4212 nfs4_schedule_state_renewal(clp);
4213 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004214 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004215}
4216
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004217static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004219 struct nfs4_renewdata *data = calldata;
4220 struct nfs_client *clp = data->client;
4221 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004223 trace_nfs4_renew_async(clp, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04004225 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004226 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4227 return;
4228 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004229 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004230 return;
4231 }
4232 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004234 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235}
4236
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004237static const struct rpc_call_ops nfs4_renew_ops = {
4238 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004239 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004240};
4241
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004242static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243{
4244 struct rpc_message msg = {
4245 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4246 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004247 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004249 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004251 if (renew_flags == 0)
4252 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004253 if (!atomic_inc_not_zero(&clp->cl_count))
4254 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004255 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004256 if (data == NULL)
4257 return -ENOMEM;
4258 data->client = clp;
4259 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004260 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004261 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262}
4263
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004264static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265{
4266 struct rpc_message msg = {
4267 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4268 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004269 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 };
4271 unsigned long now = jiffies;
4272 int status;
4273
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004274 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 if (status < 0)
4276 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004277 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 return 0;
4279}
4280
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004281static inline int nfs4_server_supports_acls(struct nfs_server *server)
4282{
4283 return (server->caps & NFS_CAP_ACLS)
4284 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
4285 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
4286}
4287
Trond Myklebust21f498c2012-08-24 10:59:25 -04004288/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4289 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004290 * the stack.
4291 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004292#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004293
Neil Hormane9e3d722011-03-04 19:26:03 -05004294static int buf_to_pages_noslab(const void *buf, size_t buflen,
4295 struct page **pages, unsigned int *pgbase)
4296{
4297 struct page *newpage, **spages;
4298 int rc = 0;
4299 size_t len;
4300 spages = pages;
4301
4302 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004303 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004304 newpage = alloc_page(GFP_KERNEL);
4305
4306 if (newpage == NULL)
4307 goto unwind;
4308 memcpy(page_address(newpage), buf, len);
4309 buf += len;
4310 buflen -= len;
4311 *pages++ = newpage;
4312 rc++;
4313 } while (buflen != 0);
4314
4315 return rc;
4316
4317unwind:
4318 for(; rc > 0; rc--)
4319 __free_page(spages[rc-1]);
4320 return -ENOMEM;
4321}
4322
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004323struct nfs4_cached_acl {
4324 int cached;
4325 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004326 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004327};
4328
4329static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004330{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004331 struct nfs_inode *nfsi = NFS_I(inode);
4332
4333 spin_lock(&inode->i_lock);
4334 kfree(nfsi->nfs4_acl);
4335 nfsi->nfs4_acl = acl;
4336 spin_unlock(&inode->i_lock);
4337}
4338
4339static void nfs4_zap_acl_attr(struct inode *inode)
4340{
4341 nfs4_set_cached_acl(inode, NULL);
4342}
4343
4344static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4345{
4346 struct nfs_inode *nfsi = NFS_I(inode);
4347 struct nfs4_cached_acl *acl;
4348 int ret = -ENOENT;
4349
4350 spin_lock(&inode->i_lock);
4351 acl = nfsi->nfs4_acl;
4352 if (acl == NULL)
4353 goto out;
4354 if (buf == NULL) /* user is just asking for length */
4355 goto out_len;
4356 if (acl->cached == 0)
4357 goto out;
4358 ret = -ERANGE; /* see getxattr(2) man page */
4359 if (acl->len > buflen)
4360 goto out;
4361 memcpy(buf, acl->data, acl->len);
4362out_len:
4363 ret = acl->len;
4364out:
4365 spin_unlock(&inode->i_lock);
4366 return ret;
4367}
4368
Sachin Prabhu5794d212012-04-17 14:36:40 +01004369static 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 +00004370{
4371 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004372 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004373
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004374 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004375 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004376 if (acl == NULL)
4377 goto out;
4378 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004379 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004380 } else {
4381 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4382 if (acl == NULL)
4383 goto out;
4384 acl->cached = 0;
4385 }
4386 acl->len = acl_len;
4387out:
4388 nfs4_set_cached_acl(inode, acl);
4389}
4390
Andy Adamsonbf118a32011-12-07 11:55:27 -05004391/*
4392 * The getxattr API returns the required buffer length when called with a
4393 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4394 * the required buf. On a NULL buf, we send a page of data to the server
4395 * guessing that the ACL request can be serviced by a page. If so, we cache
4396 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4397 * the cache. If not so, we throw away the page, and cache the required
4398 * length. The next getxattr call will then produce another round trip to
4399 * the server, this time with the input buf of the required size.
4400 */
Trond Myklebust16b42892006-08-24 12:27:15 -04004401static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004402{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004403 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004404 struct nfs_getaclargs args = {
4405 .fh = NFS_FH(inode),
4406 .acl_pages = pages,
4407 .acl_len = buflen,
4408 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004409 struct nfs_getaclres res = {
4410 .acl_len = buflen,
4411 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004412 struct rpc_message msg = {
4413 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4414 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004415 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004416 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004417 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4418 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004419
Andy Adamsonbf118a32011-12-07 11:55:27 -05004420 /* As long as we're doing a round trip to the server anyway,
4421 * let's be prepared for a page of acl data. */
4422 if (npages == 0)
4423 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004424 if (npages > ARRAY_SIZE(pages))
4425 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004426
Andy Adamsonbf118a32011-12-07 11:55:27 -05004427 for (i = 0; i < npages; i++) {
4428 pages[i] = alloc_page(GFP_KERNEL);
4429 if (!pages[i])
4430 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004431 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004432
4433 /* for decoding across pages */
4434 res.acl_scratch = alloc_page(GFP_KERNEL);
4435 if (!res.acl_scratch)
4436 goto out_free;
4437
Andy Adamsonbf118a32011-12-07 11:55:27 -05004438 args.acl_len = npages * PAGE_SIZE;
4439 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004440
Peng Taode040be2012-01-10 22:42:47 +08004441 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004442 __func__, buf, buflen, npages, args.acl_len);
4443 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4444 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004445 if (ret)
4446 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004447
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004448 /* Handle the case where the passed-in buffer is too short */
4449 if (res.acl_flags & NFS4_ACL_TRUNC) {
4450 /* Did the user only issue a request for the acl length? */
4451 if (buf == NULL)
4452 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004453 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004454 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004455 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004456 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004457 if (buf) {
4458 if (res.acl_len > buflen) {
4459 ret = -ERANGE;
4460 goto out_free;
4461 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004462 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004463 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004464out_ok:
4465 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004466out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004467 for (i = 0; i < npages; i++)
4468 if (pages[i])
4469 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004470 if (res.acl_scratch)
4471 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004472 return ret;
4473}
4474
Trond Myklebust16b42892006-08-24 12:27:15 -04004475static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4476{
4477 struct nfs4_exception exception = { };
4478 ssize_t ret;
4479 do {
4480 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04004481 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04004482 if (ret >= 0)
4483 break;
4484 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4485 } while (exception.retry);
4486 return ret;
4487}
4488
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004489static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4490{
4491 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004492 int ret;
4493
4494 if (!nfs4_server_supports_acls(server))
4495 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004496 ret = nfs_revalidate_inode(server, inode);
4497 if (ret < 0)
4498 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004499 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4500 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004501 ret = nfs4_read_cached_acl(inode, buf, buflen);
4502 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004503 /* -ENOENT is returned if there is no ACL or if there is an ACL
4504 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004505 return ret;
4506 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004507}
4508
Trond Myklebust16b42892006-08-24 12:27:15 -04004509static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004510{
4511 struct nfs_server *server = NFS_SERVER(inode);
4512 struct page *pages[NFS4ACL_MAXPAGES];
4513 struct nfs_setaclargs arg = {
4514 .fh = NFS_FH(inode),
4515 .acl_pages = pages,
4516 .acl_len = buflen,
4517 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004518 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004519 struct rpc_message msg = {
4520 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4521 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004522 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004523 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004524 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004525 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004526
4527 if (!nfs4_server_supports_acls(server))
4528 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004529 if (npages > ARRAY_SIZE(pages))
4530 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004531 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4532 if (i < 0)
4533 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004534 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004535 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004536
4537 /*
4538 * Free each page after tx, so the only ref left is
4539 * held by the network stack
4540 */
4541 for (; i > 0; i--)
4542 put_page(pages[i-1]);
4543
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004544 /*
4545 * Acl update can result in inode attribute update.
4546 * so mark the attribute cache invalid.
4547 */
4548 spin_lock(&inode->i_lock);
4549 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4550 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004551 nfs_access_zap_cache(inode);
4552 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004553 return ret;
4554}
4555
Trond Myklebust16b42892006-08-24 12:27:15 -04004556static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4557{
4558 struct nfs4_exception exception = { };
4559 int err;
4560 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004561 err = __nfs4_proc_set_acl(inode, buf, buflen);
4562 trace_nfs4_set_acl(inode, err);
4563 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04004564 &exception);
4565 } while (exception.retry);
4566 return err;
4567}
4568
David Quigleyaa9c2662013-05-22 12:50:44 -04004569#ifdef CONFIG_NFS_V4_SECURITY_LABEL
4570static int _nfs4_get_security_label(struct inode *inode, void *buf,
4571 size_t buflen)
4572{
4573 struct nfs_server *server = NFS_SERVER(inode);
4574 struct nfs_fattr fattr;
4575 struct nfs4_label label = {0, 0, buflen, buf};
4576
4577 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4578 struct nfs4_getattr_arg args = {
4579 .fh = NFS_FH(inode),
4580 .bitmask = bitmask,
4581 };
4582 struct nfs4_getattr_res res = {
4583 .fattr = &fattr,
4584 .label = &label,
4585 .server = server,
4586 };
4587 struct rpc_message msg = {
4588 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4589 .rpc_argp = &args,
4590 .rpc_resp = &res,
4591 };
4592 int ret;
4593
4594 nfs_fattr_init(&fattr);
4595
4596 ret = rpc_call_sync(server->client, &msg, 0);
4597 if (ret)
4598 return ret;
4599 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4600 return -ENOENT;
4601 if (buflen < label.len)
4602 return -ERANGE;
4603 return 0;
4604}
4605
4606static int nfs4_get_security_label(struct inode *inode, void *buf,
4607 size_t buflen)
4608{
4609 struct nfs4_exception exception = { };
4610 int err;
4611
4612 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4613 return -EOPNOTSUPP;
4614
4615 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004616 err = _nfs4_get_security_label(inode, buf, buflen);
4617 trace_nfs4_get_security_label(inode, err);
4618 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004619 &exception);
4620 } while (exception.retry);
4621 return err;
4622}
4623
4624static int _nfs4_do_set_security_label(struct inode *inode,
4625 struct nfs4_label *ilabel,
4626 struct nfs_fattr *fattr,
4627 struct nfs4_label *olabel)
4628{
4629
4630 struct iattr sattr = {0};
4631 struct nfs_server *server = NFS_SERVER(inode);
4632 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4633 struct nfs_setattrargs args = {
4634 .fh = NFS_FH(inode),
4635 .iap = &sattr,
4636 .server = server,
4637 .bitmask = bitmask,
4638 .label = ilabel,
4639 };
4640 struct nfs_setattrres res = {
4641 .fattr = fattr,
4642 .label = olabel,
4643 .server = server,
4644 };
4645 struct rpc_message msg = {
4646 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
4647 .rpc_argp = &args,
4648 .rpc_resp = &res,
4649 };
4650 int status;
4651
4652 nfs4_stateid_copy(&args.stateid, &zero_stateid);
4653
4654 status = rpc_call_sync(server->client, &msg, 0);
4655 if (status)
4656 dprintk("%s failed: %d\n", __func__, status);
4657
4658 return status;
4659}
4660
4661static int nfs4_do_set_security_label(struct inode *inode,
4662 struct nfs4_label *ilabel,
4663 struct nfs_fattr *fattr,
4664 struct nfs4_label *olabel)
4665{
4666 struct nfs4_exception exception = { };
4667 int err;
4668
4669 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004670 err = _nfs4_do_set_security_label(inode, ilabel,
4671 fattr, olabel);
4672 trace_nfs4_set_security_label(inode, err);
4673 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004674 &exception);
4675 } while (exception.retry);
4676 return err;
4677}
4678
4679static int
4680nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4681{
4682 struct nfs4_label ilabel, *olabel = NULL;
4683 struct nfs_fattr fattr;
4684 struct rpc_cred *cred;
4685 struct inode *inode = dentry->d_inode;
4686 int status;
4687
4688 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4689 return -EOPNOTSUPP;
4690
4691 nfs_fattr_init(&fattr);
4692
4693 ilabel.pi = 0;
4694 ilabel.lfs = 0;
4695 ilabel.label = (char *)buf;
4696 ilabel.len = buflen;
4697
4698 cred = rpc_lookup_cred();
4699 if (IS_ERR(cred))
4700 return PTR_ERR(cred);
4701
4702 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4703 if (IS_ERR(olabel)) {
4704 status = -PTR_ERR(olabel);
4705 goto out;
4706 }
4707
4708 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4709 if (status == 0)
4710 nfs_setsecurity(inode, &fattr, olabel);
4711
4712 nfs4_label_free(olabel);
4713out:
4714 put_rpccred(cred);
4715 return status;
4716}
4717#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4718
4719
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004721nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004723 struct nfs_client *clp = server->nfs_client;
4724
4725 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726 return 0;
4727 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004728 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004729 case -NFS4ERR_ADMIN_REVOKED:
4730 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004731 if (state == NULL)
4732 break;
4733 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004734 case -NFS4ERR_OPENMODE:
4735 if (state == NULL)
4736 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004737 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4738 goto stateid_invalid;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004739 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004740 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004741 if (state != NULL) {
4742 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4743 goto stateid_invalid;
4744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004746 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004747 nfs4_schedule_lease_recovery(clp);
4748 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004749#if defined(CONFIG_NFS_V4_1)
4750 case -NFS4ERR_BADSESSION:
4751 case -NFS4ERR_BADSLOT:
4752 case -NFS4ERR_BAD_HIGH_SLOT:
4753 case -NFS4ERR_DEADSESSION:
4754 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4755 case -NFS4ERR_SEQ_FALSE_RETRY:
4756 case -NFS4ERR_SEQ_MISORDERED:
4757 dprintk("%s ERROR %d, Reset session\n", __func__,
4758 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004759 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004760 task->tk_status = 0;
4761 return -EAGAIN;
4762#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004764 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004765 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4767 task->tk_status = 0;
4768 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004769 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770 case -NFS4ERR_OLD_STATEID:
4771 task->tk_status = 0;
4772 return -EAGAIN;
4773 }
4774 task->tk_status = nfs4_map_errors(task->tk_status);
4775 return 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004776stateid_invalid:
4777 task->tk_status = -EIO;
4778 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004779wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004780 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004781 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4782 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4783 task->tk_status = 0;
4784 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785}
4786
Chuck Leverf0920752012-05-21 22:45:41 -04004787static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4788 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004789{
4790 __be32 verf[2];
4791
Chuck Lever2c820d92012-05-21 22:45:33 -04004792 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4793 /* An impossible timestamp guarantees this value
4794 * will never match a generated boot time. */
4795 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04004796 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04004797 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004798 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04004799 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
4800 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04004801 }
Chuck Levercd937102012-03-02 17:14:31 -05004802 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4803}
4804
Chuck Levere984a552012-09-14 17:24:21 -04004805static unsigned int
4806nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4807 char *buf, size_t len)
4808{
4809 unsigned int result;
4810
4811 rcu_read_lock();
4812 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4813 clp->cl_ipaddr,
4814 rpc_peeraddr2str(clp->cl_rpcclient,
4815 RPC_DISPLAY_ADDR),
4816 rpc_peeraddr2str(clp->cl_rpcclient,
4817 RPC_DISPLAY_PROTO));
4818 rcu_read_unlock();
4819 return result;
4820}
4821
4822static unsigned int
4823nfs4_init_uniform_client_string(const struct nfs_client *clp,
4824 char *buf, size_t len)
4825{
Trond Myklebust55b59292013-07-24 16:36:35 -04004826 const char *nodename = clp->cl_rpcclient->cl_nodename;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004827
4828 if (nfs4_client_id_uniquifier[0] != '\0')
Trond Myklebust55b59292013-07-24 16:36:35 -04004829 return scnprintf(buf, len, "Linux NFSv%u.%u %s/%s",
4830 clp->rpc_ops->version,
4831 clp->cl_minorversion,
4832 nfs4_client_id_uniquifier,
4833 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004834 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4835 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004836 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004837}
4838
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004839/**
4840 * nfs4_proc_setclientid - Negotiate client ID
4841 * @clp: state data structure
4842 * @program: RPC program for NFSv4 callback service
4843 * @port: IP port number for NFS4 callback service
4844 * @cred: RPC credential to use for this call
4845 * @res: where to place the result
4846 *
4847 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4848 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004849int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4850 unsigned short port, struct rpc_cred *cred,
4851 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852{
4853 nfs4_verifier sc_verifier;
4854 struct nfs4_setclientid setclientid = {
4855 .sc_verifier = &sc_verifier,
4856 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004857 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 };
4859 struct rpc_message msg = {
4860 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4861 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004862 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004863 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004865 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866
Chuck Leverde734832012-07-11 16:30:50 -04004867 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004868 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004869 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4870 setclientid.sc_name_len =
4871 nfs4_init_uniform_client_string(clp,
4872 setclientid.sc_name,
4873 sizeof(setclientid.sc_name));
4874 else
4875 setclientid.sc_name_len =
4876 nfs4_init_nonuniform_client_string(clp,
4877 setclientid.sc_name,
4878 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004879 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004880 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004881 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Djalal Harounife2d5392013-06-18 17:58:12 +01004882 sizeof(setclientid.sc_netid), "%s",
Chuck Leverd4d3c502007-12-10 14:57:09 -05004883 rpc_peeraddr2str(clp->cl_rpcclient,
4884 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004885 rcu_read_unlock();
4886 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004887 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 clp->cl_ipaddr, port >> 8, port & 255);
4889
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004890 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4891 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4892 setclientid.sc_name_len, setclientid.sc_name);
4893 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004894 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004895 dprintk("NFS reply setclientid: %d\n", status);
4896 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897}
4898
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004899/**
4900 * nfs4_proc_setclientid_confirm - Confirm client ID
4901 * @clp: state data structure
4902 * @res: result of a previous SETCLIENTID
4903 * @cred: RPC credential to use for this call
4904 *
4905 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4906 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004907int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004908 struct nfs4_setclientid_res *arg,
4909 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 struct rpc_message msg = {
4912 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004913 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004914 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 int status;
4917
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004918 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4919 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4920 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004921 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004922 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004923 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 return status;
4925}
4926
Trond Myklebustfe650402006-01-03 09:55:18 +01004927struct nfs4_delegreturndata {
4928 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004929 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004930 struct nfs_fh fh;
4931 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004932 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004933 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004934 int rpc_status;
4935};
4936
Trond Myklebustfe650402006-01-03 09:55:18 +01004937static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4938{
4939 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004940
Trond Myklebust14516c32010-07-31 14:29:06 -04004941 if (!nfs4_sequence_done(task, &data->res.seq_res))
4942 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004943
Trond Myklebustca8acf82013-08-13 10:36:56 -04004944 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004945 switch (task->tk_status) {
4946 case -NFS4ERR_STALE_STATEID:
4947 case -NFS4ERR_EXPIRED:
4948 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004949 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004950 break;
4951 default:
4952 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4953 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004954 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004955 return;
4956 }
4957 }
4958 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004959}
4960
4961static void nfs4_delegreturn_release(void *calldata)
4962{
Trond Myklebustfe650402006-01-03 09:55:18 +01004963 kfree(calldata);
4964}
4965
Andy Adamson938e1012009-04-01 09:22:28 -04004966static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4967{
4968 struct nfs4_delegreturndata *d_data;
4969
4970 d_data = (struct nfs4_delegreturndata *)data;
4971
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004972 nfs4_setup_sequence(d_data->res.server,
4973 &d_data->args.seq_args,
4974 &d_data->res.seq_res,
4975 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004976}
Andy Adamson938e1012009-04-01 09:22:28 -04004977
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004978static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004979 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01004980 .rpc_call_done = nfs4_delegreturn_done,
4981 .rpc_release = nfs4_delegreturn_release,
4982};
4983
Trond Myklebuste6f81072008-01-24 18:14:34 -05004984static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004985{
4986 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004987 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004988 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004989 struct rpc_message msg = {
4990 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4991 .rpc_cred = cred,
4992 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004993 struct rpc_task_setup task_setup_data = {
4994 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004995 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004996 .callback_ops = &nfs4_delegreturn_ops,
4997 .flags = RPC_TASK_ASYNC,
4998 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004999 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005000
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005001 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005002 if (data == NULL)
5003 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005004 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01005005 data->args.fhandle = &data->fh;
5006 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005007 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005008 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005009 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005010 data->res.fattr = &data->fattr;
5011 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005012 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005013 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005014 data->rpc_status = 0;
5015
Trond Myklebustc970aa82007-07-14 15:39:59 -04005016 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005017 msg.rpc_argp = &data->args;
5018 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005019 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005020 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005021 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005022 if (!issync)
5023 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005024 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005025 if (status != 0)
5026 goto out;
5027 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005028 if (status == 0)
5029 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5030 else
5031 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005032out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005033 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005034 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035}
5036
Trond Myklebuste6f81072008-01-24 18:14:34 -05005037int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038{
5039 struct nfs_server *server = NFS_SERVER(inode);
5040 struct nfs4_exception exception = { };
5041 int err;
5042 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005043 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Trond Myklebustca8acf82013-08-13 10:36:56 -04005044 trace_nfs4_delegreturn(inode, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045 switch (err) {
5046 case -NFS4ERR_STALE_STATEID:
5047 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048 case 0:
5049 return 0;
5050 }
5051 err = nfs4_handle_exception(server, err, &exception);
5052 } while (exception.retry);
5053 return err;
5054}
5055
5056#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5057#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5058
5059/*
5060 * sleep, with exponential backoff, and retry the LOCK operation.
5061 */
5062static unsigned long
5063nfs4_set_lock_task_retry(unsigned long timeout)
5064{
Colin Cross416ad3c2013-05-06 23:50:06 +00005065 freezable_schedule_timeout_killable_unsafe(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066 timeout <<= 1;
5067 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5068 return NFS4_LOCK_MAXTIMEOUT;
5069 return timeout;
5070}
5071
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5073{
5074 struct inode *inode = state->inode;
5075 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005076 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005077 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005079 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005081 struct nfs_lockt_res res = {
5082 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 };
5084 struct rpc_message msg = {
5085 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5086 .rpc_argp = &arg,
5087 .rpc_resp = &res,
5088 .rpc_cred = state->owner->so_cred,
5089 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090 struct nfs4_lock_state *lsp;
5091 int status;
5092
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005093 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005094 status = nfs4_set_lock_state(state, request);
5095 if (status != 0)
5096 goto out;
5097 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005098 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005099 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005100 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005101 switch (status) {
5102 case 0:
5103 request->fl_type = F_UNLCK;
5104 break;
5105 case -NFS4ERR_DENIED:
5106 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005108 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005109 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005110out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 return status;
5112}
5113
5114static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5115{
5116 struct nfs4_exception exception = { };
5117 int err;
5118
5119 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005120 err = _nfs4_proc_getlk(state, cmd, request);
5121 trace_nfs4_get_lock(request, state, cmd, err);
5122 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 &exception);
5124 } while (exception.retry);
5125 return err;
5126}
5127
5128static int do_vfs_lock(struct file *file, struct file_lock *fl)
5129{
5130 int res = 0;
5131 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5132 case FL_POSIX:
5133 res = posix_lock_file_wait(file, fl);
5134 break;
5135 case FL_FLOCK:
5136 res = flock_lock_file_wait(file, fl);
5137 break;
5138 default:
5139 BUG();
5140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 return res;
5142}
5143
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005144struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005145 struct nfs_locku_args arg;
5146 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005147 struct nfs4_lock_state *lsp;
5148 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005149 struct file_lock fl;
5150 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005151 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005152};
5153
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005154static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5155 struct nfs_open_context *ctx,
5156 struct nfs4_lock_state *lsp,
5157 struct nfs_seqid *seqid)
5158{
5159 struct nfs4_unlockdata *p;
5160 struct inode *inode = lsp->ls_state->inode;
5161
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005162 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005163 if (p == NULL)
5164 return NULL;
5165 p->arg.fh = NFS_FH(inode);
5166 p->arg.fl = &p->fl;
5167 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005168 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005169 p->arg.stateid = &lsp->ls_stateid;
5170 p->lsp = lsp;
5171 atomic_inc(&lsp->ls_count);
5172 /* Ensure we don't close file until we're done freeing locks! */
5173 p->ctx = get_nfs_open_context(ctx);
5174 memcpy(&p->fl, fl, sizeof(p->fl));
5175 p->server = NFS_SERVER(inode);
5176 return p;
5177}
5178
Trond Myklebust06f814a2006-01-03 09:55:07 +01005179static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005180{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005181 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005182 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005183 nfs4_put_lock_state(calldata->lsp);
5184 put_nfs_open_context(calldata->ctx);
5185 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005186}
5187
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005188static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005189{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005190 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005191
Trond Myklebust14516c32010-07-31 14:29:06 -04005192 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5193 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005194 switch (task->tk_status) {
5195 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05005196 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
5197 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005198 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005199 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005200 case -NFS4ERR_BAD_STATEID:
5201 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005202 case -NFS4ERR_STALE_STATEID:
5203 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005204 break;
5205 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005206 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005207 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005208 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005209 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005210}
5211
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005212static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005213{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005214 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005216 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005217 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04005218 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005219 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005220 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005221 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005222 calldata->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04005223 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005224 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005225 &calldata->res.seq_res,
5226 task) != 0)
5227 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005228 return;
5229out_no_action:
5230 task->tk_action = NULL;
5231out_wait:
5232 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233}
5234
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005235static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005236 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005237 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005238 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005239};
5240
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005241static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5242 struct nfs_open_context *ctx,
5243 struct nfs4_lock_state *lsp,
5244 struct nfs_seqid *seqid)
5245{
5246 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005247 struct rpc_message msg = {
5248 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5249 .rpc_cred = ctx->cred,
5250 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005251 struct rpc_task_setup task_setup_data = {
5252 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005253 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005254 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005255 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005256 .flags = RPC_TASK_ASYNC,
5257 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005258
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005259 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5260 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5261
Frank Filz137d6ac2007-07-09 15:32:29 -07005262 /* Ensure this is an unlock - when canceling a lock, the
5263 * canceled lock is passed in, and it won't be an unlock.
5264 */
5265 fl->fl_type = F_UNLCK;
5266
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005267 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5268 if (data == NULL) {
5269 nfs_free_seqid(seqid);
5270 return ERR_PTR(-ENOMEM);
5271 }
5272
Chuck Levera9c92d62013-08-09 12:48:18 -04005273 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005274 msg.rpc_argp = &data->arg;
5275 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005276 task_setup_data.callback_data = data;
5277 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005278}
5279
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5281{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005282 struct inode *inode = state->inode;
5283 struct nfs4_state_owner *sp = state->owner;
5284 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005285 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005286 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005287 struct rpc_task *task;
5288 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005289 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290
Trond Myklebust9b073572006-06-29 16:38:34 -04005291 status = nfs4_set_lock_state(state, request);
5292 /* Unlock _before_ we do the RPC call */
5293 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005294 /* Exclude nfs_delegation_claim_locks() */
5295 mutex_lock(&sp->so_delegreturn_mutex);
5296 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005297 down_read(&nfsi->rwsem);
5298 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
5299 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005300 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005301 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005302 }
5303 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005304 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005305 if (status != 0)
5306 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005307 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005308 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005309 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5310 goto out;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005311 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005312 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005313 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04005314 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005315 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005316 status = PTR_ERR(task);
5317 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005318 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005319 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005320 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005321out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005322 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005323 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005324 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325}
5326
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005327struct nfs4_lockdata {
5328 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005329 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005330 struct nfs4_lock_state *lsp;
5331 struct nfs_open_context *ctx;
5332 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005333 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005334 int rpc_status;
5335 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005336 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005337};
5338
5339static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005340 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5341 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005342{
5343 struct nfs4_lockdata *p;
5344 struct inode *inode = lsp->ls_state->inode;
5345 struct nfs_server *server = NFS_SERVER(inode);
5346
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005347 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005348 if (p == NULL)
5349 return NULL;
5350
5351 p->arg.fh = NFS_FH(inode);
5352 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005353 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005354 if (p->arg.open_seqid == NULL)
5355 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005356 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005357 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005358 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005359 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04005360 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005361 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005362 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005363 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005364 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005365 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005366 atomic_inc(&lsp->ls_count);
5367 p->ctx = get_nfs_open_context(ctx);
5368 memcpy(&p->fl, fl, sizeof(p->fl));
5369 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005370out_free_seqid:
5371 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005372out_free:
5373 kfree(p);
5374 return NULL;
5375}
5376
5377static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5378{
5379 struct nfs4_lockdata *data = calldata;
5380 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381
Harvey Harrison3110ff82008-05-02 13:42:44 -07005382 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005383 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005384 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005385 /* Do we need to do an open_to_lock_owner? */
5386 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005387 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005388 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005389 }
Trond Myklebust8e472f32013-04-20 01:30:53 -04005390 data->arg.open_stateid = &state->open_stateid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005391 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005392 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005393 } else
5394 data->arg.new_lock_owner = 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04005395 if (!nfs4_valid_open_stateid(state)) {
5396 data->rpc_status = -EBADF;
5397 task->tk_action = NULL;
5398 goto out_release_open_seqid;
5399 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005400 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04005401 if (nfs4_setup_sequence(data->server,
5402 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005403 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005404 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04005405 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04005406out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005407 nfs_release_seqid(data->arg.open_seqid);
5408out_release_lock_seqid:
5409 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005410out_wait:
5411 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005412 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005413}
5414
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005415static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5416{
5417 struct nfs4_lockdata *data = calldata;
5418
Harvey Harrison3110ff82008-05-02 13:42:44 -07005419 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005420
Trond Myklebust14516c32010-07-31 14:29:06 -04005421 if (!nfs4_sequence_done(task, &data->res.seq_res))
5422 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04005423
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005424 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005425 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005426 if (data->rpc_status == 0)
5427 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
5428 else
5429 goto out;
5430 }
5431 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05005432 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005433 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04005434 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005435 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005436out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07005437 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005438}
5439
5440static void nfs4_lock_release(void *calldata)
5441{
5442 struct nfs4_lockdata *data = calldata;
5443
Harvey Harrison3110ff82008-05-02 13:42:44 -07005444 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005445 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005446 if (data->cancelled != 0) {
5447 struct rpc_task *task;
5448 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5449 data->arg.lock_seqid);
5450 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08005451 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005452 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005453 } else
5454 nfs_free_seqid(data->arg.lock_seqid);
5455 nfs4_put_lock_state(data->lsp);
5456 put_nfs_open_context(data->ctx);
5457 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005458 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005459}
5460
5461static const struct rpc_call_ops nfs4_lock_ops = {
5462 .rpc_call_prepare = nfs4_lock_prepare,
5463 .rpc_call_done = nfs4_lock_done,
5464 .rpc_release = nfs4_lock_release,
5465};
5466
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005467static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5468{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005469 switch (error) {
5470 case -NFS4ERR_ADMIN_REVOKED:
5471 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05005472 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005473 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04005474 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05005475 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005476 break;
5477 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005478 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05005479 case -NFS4ERR_EXPIRED:
5480 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005481 };
5482}
5483
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005484static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005485{
5486 struct nfs4_lockdata *data;
5487 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005488 struct rpc_message msg = {
5489 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5490 .rpc_cred = state->owner->so_cred,
5491 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005492 struct rpc_task_setup task_setup_data = {
5493 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005494 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005495 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005496 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005497 .flags = RPC_TASK_ASYNC,
5498 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005499 int ret;
5500
Harvey Harrison3110ff82008-05-02 13:42:44 -07005501 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005502 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005503 fl->fl_u.nfs4_fl.owner,
5504 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005505 if (data == NULL)
5506 return -ENOMEM;
5507 if (IS_SETLKW(cmd))
5508 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04005509 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005510 msg.rpc_argp = &data->arg;
5511 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005512 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005513 if (recovery_type > NFS_LOCK_NEW) {
5514 if (recovery_type == NFS_LOCK_RECLAIM)
5515 data->arg.reclaim = NFS_LOCK_RECLAIM;
5516 nfs4_set_sequence_privileged(&data->arg.seq_args);
5517 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04005518 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005519 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005520 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005521 ret = nfs4_wait_for_completion_rpc_task(task);
5522 if (ret == 0) {
5523 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005524 if (ret)
5525 nfs4_handle_setlk_error(data->server, data->lsp,
5526 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005527 } else
5528 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005529 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005530 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005531 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532}
5533
5534static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5535{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005536 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005537 struct nfs4_exception exception = {
5538 .inode = state->inode,
5539 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005540 int err;
5541
5542 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005543 /* Cache the lock if possible... */
5544 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5545 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005546 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005547 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
Trond Myklebust168667c2010-10-19 19:47:49 -04005548 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00005549 break;
5550 nfs4_handle_exception(server, err, &exception);
5551 } while (exception.retry);
5552 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553}
5554
5555static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5556{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005557 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005558 struct nfs4_exception exception = {
5559 .inode = state->inode,
5560 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005561 int err;
5562
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005563 err = nfs4_set_lock_state(state, request);
5564 if (err != 0)
5565 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04005566 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10005567 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
5568 return 0;
5569 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005570 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005571 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5572 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005573 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005574 trace_nfs4_lock_expired(request, state, F_SETLK, err);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005575 switch (err) {
5576 default:
5577 goto out;
5578 case -NFS4ERR_GRACE:
5579 case -NFS4ERR_DELAY:
5580 nfs4_handle_exception(server, err, &exception);
5581 err = 0;
5582 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005583 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005584out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00005585 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586}
5587
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005588#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005589/**
5590 * nfs41_check_expired_locks - possibly free a lock stateid
5591 *
5592 * @state: NFSv4 state for an inode
5593 *
5594 * Returns NFS_OK if recovery for this stateid is now finished.
5595 * Otherwise a negative NFS4ERR value is returned.
5596 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005597static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005598{
Chuck Levereb64cf92012-07-11 16:30:05 -04005599 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005600 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005601 struct nfs_server *server = NFS_SERVER(state->inode);
5602
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005603 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04005604 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005605 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
5606
5607 status = nfs41_test_stateid(server,
5608 &lsp->ls_stateid,
5609 cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04005610 trace_nfs4_test_lock_stateid(state, lsp, status);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005611 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005612 /* Free the stateid unless the server
5613 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04005614 if (status != -NFS4ERR_BAD_STATEID)
5615 nfs41_free_stateid(server,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005616 &lsp->ls_stateid,
5617 cred);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005618 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005619 ret = status;
5620 }
5621 }
5622 };
5623
5624 return ret;
5625}
5626
5627static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5628{
5629 int status = NFS_OK;
5630
5631 if (test_bit(LK_STATE_IN_USE, &state->flags))
5632 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005633 if (status != NFS_OK)
5634 status = nfs4_lock_expired(state, request);
5635 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005636}
5637#endif
5638
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5640{
Trond Myklebust9a99af42013-02-04 20:17:49 -05005641 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005642 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005643 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af42013-02-04 20:17:49 -05005644 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005645 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005647 if ((fl_flags & FL_POSIX) &&
5648 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5649 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005650 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005651 status = nfs4_set_lock_state(state, request);
5652 if (status != 0)
5653 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005654 request->fl_flags |= FL_ACCESS;
5655 status = do_vfs_lock(request->fl_file, request);
5656 if (status < 0)
5657 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005658 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005659 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005660 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005661 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005662 request->fl_flags = fl_flags & ~FL_SLEEP;
5663 status = do_vfs_lock(request->fl_file, request);
5664 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005665 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05005666 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5667 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005668 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005669 if (status != 0)
Trond Myklebust9a99af42013-02-04 20:17:49 -05005670 goto out;
5671 down_read(&nfsi->rwsem);
5672 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5673 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005674 goto out_unlock;
Trond Myklebust9a99af42013-02-04 20:17:49 -05005675 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005676 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005677 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005678 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005679 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5680 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005681out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005682 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005683out:
5684 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685 return status;
5686}
5687
5688static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5689{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005690 struct nfs4_exception exception = {
5691 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005692 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005693 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694 int err;
5695
5696 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005697 err = _nfs4_proc_setlk(state, cmd, request);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005698 trace_nfs4_set_lock(request, state, cmd, err);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005699 if (err == -NFS4ERR_DENIED)
5700 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005702 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703 } while (exception.retry);
5704 return err;
5705}
5706
5707static int
5708nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5709{
5710 struct nfs_open_context *ctx;
5711 struct nfs4_state *state;
5712 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5713 int status;
5714
5715 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005716 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717 state = ctx->state;
5718
5719 if (request->fl_start < 0 || request->fl_end < 0)
5720 return -EINVAL;
5721
Trond Myklebustd9531262009-07-21 19:22:38 -04005722 if (IS_GETLK(cmd)) {
5723 if (state != NULL)
5724 return nfs4_proc_getlk(state, F_GETLK, request);
5725 return 0;
5726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727
5728 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5729 return -EINVAL;
5730
Trond Myklebustd9531262009-07-21 19:22:38 -04005731 if (request->fl_type == F_UNLCK) {
5732 if (state != NULL)
5733 return nfs4_proc_unlck(state, cmd, request);
5734 return 0;
5735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736
Trond Myklebustd9531262009-07-21 19:22:38 -04005737 if (state == NULL)
5738 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005739 /*
5740 * Don't rely on the VFS having checked the file open mode,
5741 * since it won't do this for flock() locks.
5742 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005743 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005744 case F_RDLCK:
5745 if (!(filp->f_mode & FMODE_READ))
5746 return -EBADF;
5747 break;
5748 case F_WRLCK:
5749 if (!(filp->f_mode & FMODE_WRITE))
5750 return -EBADF;
5751 }
5752
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753 do {
5754 status = nfs4_proc_setlk(state, cmd, request);
5755 if ((status != -EAGAIN) || IS_SETLK(cmd))
5756 break;
5757 timeout = nfs4_set_lock_task_retry(timeout);
5758 status = -ERESTARTSYS;
5759 if (signalled())
5760 break;
5761 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762 return status;
5763}
5764
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04005765int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05005766{
5767 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05005768 int err;
5769
5770 err = nfs4_set_lock_state(state, fl);
5771 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04005772 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04005773 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04005774 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05005775}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005776
Trond Myklebustcf470c32012-03-07 13:49:12 -05005777struct nfs_release_lockowner_data {
5778 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005779 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005780 struct nfs_release_lockowner_args args;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005781 struct nfs4_sequence_args seq_args;
5782 struct nfs4_sequence_res seq_res;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005783};
5784
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005785static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
5786{
5787 struct nfs_release_lockowner_data *data = calldata;
5788 nfs40_setup_sequence(data->server,
5789 &data->seq_args, &data->seq_res, task);
5790}
5791
5792static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
5793{
5794 struct nfs_release_lockowner_data *data = calldata;
5795 nfs40_sequence_done(task, &data->seq_res);
5796}
5797
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005798static void nfs4_release_lockowner_release(void *calldata)
5799{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005800 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005801 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005802 kfree(calldata);
5803}
5804
Trond Myklebust17280172012-03-11 13:11:00 -04005805static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005806 .rpc_call_prepare = nfs4_release_lockowner_prepare,
5807 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005808 .rpc_release = nfs4_release_lockowner_release,
5809};
5810
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04005811static int nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005812{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005813 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005814 struct rpc_message msg = {
5815 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5816 };
5817
5818 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005819 return -EINVAL;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005820
Trond Myklebustcf470c32012-03-07 13:49:12 -05005821 data = kmalloc(sizeof(*data), GFP_NOFS);
5822 if (!data)
5823 return -ENOMEM;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005824 nfs4_init_sequence(&data->seq_args, &data->seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05005825 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005826 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005827 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5828 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5829 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005830
Trond Myklebustcf470c32012-03-07 13:49:12 -05005831 msg.rpc_argp = &data->args;
5832 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5833 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005834}
5835
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005836#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5837
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005838static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5839 const void *buf, size_t buflen,
5840 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005841{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005842 if (strcmp(key, "") != 0)
5843 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005844
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005845 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005846}
5847
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005848static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5849 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005850{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005851 if (strcmp(key, "") != 0)
5852 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005853
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005854 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005855}
5856
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005857static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5858 size_t list_len, const char *name,
5859 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005860{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005861 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005862
J. Bruce Fields096455a2006-03-20 23:23:42 -05005863 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5864 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005865
5866 if (list && len <= list_len)
5867 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005868 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005869}
5870
David Quigleyc9bccef2013-05-22 12:50:45 -04005871#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5872static inline int nfs4_server_supports_labels(struct nfs_server *server)
5873{
5874 return server->caps & NFS_CAP_SECURITY_LABEL;
5875}
5876
5877static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
5878 const void *buf, size_t buflen,
5879 int flags, int type)
5880{
5881 if (security_ismaclabel(key))
5882 return nfs4_set_security_label(dentry, buf, buflen);
5883
5884 return -EOPNOTSUPP;
5885}
5886
5887static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
5888 void *buf, size_t buflen, int type)
5889{
5890 if (security_ismaclabel(key))
5891 return nfs4_get_security_label(dentry->d_inode, buf, buflen);
5892 return -EOPNOTSUPP;
5893}
5894
5895static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
5896 size_t list_len, const char *name,
5897 size_t name_len, int type)
5898{
5899 size_t len = 0;
5900
5901 if (nfs_server_capable(dentry->d_inode, NFS_CAP_SECURITY_LABEL)) {
5902 len = security_inode_listsecurity(dentry->d_inode, NULL, 0);
5903 if (list && len <= list_len)
5904 security_inode_listsecurity(dentry->d_inode, list, len);
5905 }
5906 return len;
5907}
5908
5909static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
5910 .prefix = XATTR_SECURITY_PREFIX,
5911 .list = nfs4_xattr_list_nfs4_label,
5912 .get = nfs4_xattr_get_nfs4_label,
5913 .set = nfs4_xattr_set_nfs4_label,
5914};
5915#endif
5916
5917
Andy Adamson533eb462011-06-13 18:25:56 -04005918/*
5919 * nfs_fhget will use either the mounted_on_fileid or the fileid
5920 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005921static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5922{
Andy Adamson533eb462011-06-13 18:25:56 -04005923 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5924 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5925 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005926 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005927 return;
5928
5929 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005930 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005931 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5932 fattr->nlink = 2;
5933}
5934
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005935static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5936 const struct qstr *name,
5937 struct nfs4_fs_locations *fs_locations,
5938 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005939{
5940 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04005941 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005942 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005943 };
5944 struct nfs4_fs_locations_arg args = {
5945 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005946 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005947 .page = page,
5948 .bitmask = bitmask,
5949 };
Benny Halevy22958462009-04-01 09:22:02 -04005950 struct nfs4_fs_locations_res res = {
5951 .fs_locations = fs_locations,
5952 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005953 struct rpc_message msg = {
5954 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5955 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005956 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005957 };
5958 int status;
5959
Harvey Harrison3110ff82008-05-02 13:42:44 -07005960 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005961
5962 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5963 * is not supported */
5964 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5965 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5966 else
5967 bitmask[0] |= FATTR4_WORD0_FILEID;
5968
Trond Myklebustc228fd32007-01-13 02:28:11 -05005969 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005970 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005971 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005972 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005973 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005974 return status;
5975}
5976
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005977int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5978 const struct qstr *name,
5979 struct nfs4_fs_locations *fs_locations,
5980 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005981{
5982 struct nfs4_exception exception = { };
5983 int err;
5984 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04005985 err = _nfs4_proc_fs_locations(client, dir, name,
5986 fs_locations, page);
5987 trace_nfs4_get_fs_locations(dir, name, err);
5988 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005989 &exception);
5990 } while (exception.retry);
5991 return err;
5992}
5993
Andy Adamson5ec16a82013-08-08 10:57:55 -04005994/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04005995 * If 'use_integrity' is true and the state managment nfs_client
5996 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
5997 * and the machine credential as per RFC3530bis and RFC5661 Security
5998 * Considerations sections. Otherwise, just use the user cred with the
5999 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04006000 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006001static 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 +00006002{
6003 int status;
6004 struct nfs4_secinfo_arg args = {
6005 .dir_fh = NFS_FH(dir),
6006 .name = name,
6007 };
6008 struct nfs4_secinfo_res res = {
6009 .flavors = flavors,
6010 };
6011 struct rpc_message msg = {
6012 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6013 .rpc_argp = &args,
6014 .rpc_resp = &res,
6015 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006016 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006017 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006018
6019 if (use_integrity) {
6020 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006021 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6022 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006023 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006024
6025 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006026
6027 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6028 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6029
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006030 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6031 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006032 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006033
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006034 if (cred)
6035 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006036
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006037 return status;
6038}
6039
Bryan Schumaker72de53e2012-04-27 13:27:40 -04006040int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6041 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006042{
6043 struct nfs4_exception exception = { };
6044 int err;
6045 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006046 err = -NFS4ERR_WRONGSEC;
6047
6048 /* try to use integrity protection with machine cred */
6049 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6050 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6051
6052 /*
6053 * if unable to use integrity protection, or SECINFO with
6054 * integrity protection returns NFS4ERR_WRONGSEC (which is
6055 * disallowed by spec, but exists in deployed servers) use
6056 * the current filesystem's rpc_client and the user cred.
6057 */
6058 if (err == -NFS4ERR_WRONGSEC)
6059 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6060
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006061 trace_nfs4_secinfo(dir, name, err);
6062 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006063 &exception);
6064 } while (exception.retry);
6065 return err;
6066}
6067
Andy Adamson557134a2009-04-01 09:21:53 -04006068#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04006069/*
Andy Adamson357f54d2010-12-14 10:11:57 -05006070 * Check the exchange flags returned by the server for invalid flags, having
6071 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6072 * DS flags set.
6073 */
6074static int nfs4_check_cl_exchange_flags(u32 flags)
6075{
6076 if (flags & ~EXCHGID4_FLAG_MASK_R)
6077 goto out_inval;
6078 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6079 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6080 goto out_inval;
6081 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6082 goto out_inval;
6083 return NFS_OK;
6084out_inval:
6085 return -NFS4ERR_INVAL;
6086}
6087
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006088static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006089nfs41_same_server_scope(struct nfs41_server_scope *a,
6090 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006091{
6092 if (a->server_scope_sz == b->server_scope_sz &&
6093 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6094 return true;
6095
6096 return false;
6097}
6098
Andy Adamson357f54d2010-12-14 10:11:57 -05006099/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006100 * nfs4_proc_bind_conn_to_session()
6101 *
6102 * The 4.1 client currently uses the same TCP connection for the
6103 * fore and backchannel.
6104 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006105int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006106{
6107 int status;
6108 struct nfs41_bind_conn_to_session_res res;
6109 struct rpc_message msg = {
6110 .rpc_proc =
6111 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
6112 .rpc_argp = clp,
6113 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006114 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006115 };
6116
6117 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006118
6119 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
6120 if (unlikely(res.session == NULL)) {
6121 status = -ENOMEM;
6122 goto out;
6123 }
6124
6125 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006126 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006127 if (status == 0) {
6128 if (memcmp(res.session->sess_id.data,
6129 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6130 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6131 status = -EIO;
6132 goto out_session;
6133 }
6134 if (res.dir != NFS4_CDFS4_BOTH) {
6135 dprintk("NFS: %s: Unexpected direction from server\n",
6136 __func__);
6137 status = -EIO;
6138 goto out_session;
6139 }
6140 if (res.use_conn_in_rdma_mode) {
6141 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6142 __func__);
6143 status = -EIO;
6144 goto out_session;
6145 }
6146 }
6147out_session:
6148 kfree(res.session);
6149out:
6150 dprintk("<-- %s status= %d\n", __func__, status);
6151 return status;
6152}
6153
6154/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006155 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6156 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04006157 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006158static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
6159 .how = SP4_MACH_CRED,
6160 .enforce.u.words = {
6161 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6162 1 << (OP_EXCHANGE_ID - 32) |
6163 1 << (OP_CREATE_SESSION - 32) |
6164 1 << (OP_DESTROY_SESSION - 32) |
6165 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006166 },
6167 .allow.u.words = {
6168 [0] = 1 << (OP_CLOSE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006169 1 << (OP_LOCKU) |
6170 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006171 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006172 1 << (OP_SECINFO_NO_NAME - 32) |
6173 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006174 1 << (OP_FREE_STATEID - 32) |
6175 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006176 }
6177};
6178
6179/*
6180 * Select the state protection mode for client `clp' given the server results
6181 * from exchange_id in `sp'.
6182 *
6183 * Returns 0 on success, negative errno otherwise.
6184 */
6185static int nfs4_sp4_select_mode(struct nfs_client *clp,
6186 struct nfs41_state_protection *sp)
6187{
6188 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
6189 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6190 1 << (OP_EXCHANGE_ID - 32) |
6191 1 << (OP_CREATE_SESSION - 32) |
6192 1 << (OP_DESTROY_SESSION - 32) |
6193 1 << (OP_DESTROY_CLIENTID - 32)
6194 };
6195 unsigned int i;
6196
6197 if (sp->how == SP4_MACH_CRED) {
6198 /* Print state protect result */
6199 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
6200 for (i = 0; i <= LAST_NFS4_OP; i++) {
6201 if (test_bit(i, sp->enforce.u.longs))
6202 dfprintk(MOUNT, " enforce op %d\n", i);
6203 if (test_bit(i, sp->allow.u.longs))
6204 dfprintk(MOUNT, " allow op %d\n", i);
6205 }
6206
6207 /* make sure nothing is on enforce list that isn't supported */
6208 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
6209 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
6210 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6211 return -EINVAL;
6212 }
6213 }
6214
6215 /*
6216 * Minimal mode - state operations are allowed to use machine
6217 * credential. Note this already happens by default, so the
6218 * client doesn't have to do anything more than the negotiation.
6219 *
6220 * NOTE: we don't care if EXCHANGE_ID is in the list -
6221 * we're already using the machine cred for exchange_id
6222 * and will never use a different cred.
6223 */
6224 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
6225 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
6226 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
6227 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
6228 dfprintk(MOUNT, "sp4_mach_cred:\n");
6229 dfprintk(MOUNT, " minimal mode enabled\n");
6230 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
6231 } else {
6232 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6233 return -EINVAL;
6234 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006235
6236 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
6237 test_bit(OP_LOCKU, sp->allow.u.longs)) {
6238 dfprintk(MOUNT, " cleanup mode enabled\n");
6239 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
6240 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006241
6242 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
6243 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
6244 dfprintk(MOUNT, " secinfo mode enabled\n");
6245 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
6246 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006247
6248 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
6249 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
6250 dfprintk(MOUNT, " stateid mode enabled\n");
6251 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
6252 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04006253
6254 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
6255 dfprintk(MOUNT, " write mode enabled\n");
6256 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
6257 }
6258
6259 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
6260 dfprintk(MOUNT, " commit mode enabled\n");
6261 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
6262 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006263 }
6264
6265 return 0;
6266}
6267
6268/*
6269 * _nfs4_proc_exchange_id()
6270 *
6271 * Wrapper for EXCHANGE_ID operation.
6272 */
6273static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
6274 u32 sp4_how)
Benny Halevy99fe60d2009-04-01 09:22:29 -04006275{
6276 nfs4_verifier verifier;
6277 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05006278 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04006279 .client = clp,
Trond Myklebust4f0b4292013-05-20 12:24:03 -04006280 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6281 EXCHGID4_FLAG_BIND_PRINC_STATEID,
Benny Halevy99fe60d2009-04-01 09:22:29 -04006282 };
6283 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04006284 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04006285 };
6286 int status;
6287 struct rpc_message msg = {
6288 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
6289 .rpc_argp = &args,
6290 .rpc_resp = &res,
6291 .rpc_cred = cred,
6292 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04006293
Chuck Leverf0920752012-05-21 22:45:41 -04006294 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04006295 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
6296 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006297 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
6298 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6299 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006300
Chuck Leveracdeb692012-05-21 22:46:16 -04006301 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006302 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04006303 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006304 status = -ENOMEM;
6305 goto out;
6306 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006307
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006308 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006309 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006310 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04006311 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04006312 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04006313 }
6314
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006315 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006316 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006317 status = -ENOMEM;
6318 goto out_server_scope;
6319 }
6320
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006321 switch (sp4_how) {
6322 case SP4_NONE:
6323 args.state_protect.how = SP4_NONE;
6324 break;
6325
6326 case SP4_MACH_CRED:
6327 args.state_protect = nfs4_sp4_mach_cred_request;
6328 break;
6329
6330 default:
6331 /* unsupported! */
6332 WARN_ON_ONCE(1);
6333 status = -EINVAL;
6334 goto out_server_scope;
6335 }
6336
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006337 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006338 trace_nfs4_exchange_id(clp, status);
Chuck Lever177313f2012-05-21 22:44:58 -04006339 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04006340 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006341
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006342 if (status == 0)
6343 status = nfs4_sp4_select_mode(clp, &res.state_protect);
6344
Chuck Lever177313f2012-05-21 22:44:58 -04006345 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04006346 clp->cl_clientid = res.clientid;
6347 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
6348 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
6349 clp->cl_seqid = res.seqid;
6350
Chuck Leveracdeb692012-05-21 22:46:16 -04006351 kfree(clp->cl_serverowner);
6352 clp->cl_serverowner = res.server_owner;
6353 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04006354
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006355 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04006356 kfree(clp->cl_implid);
6357 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006358
Chuck Lever177313f2012-05-21 22:44:58 -04006359 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006360 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006361 res.server_scope)) {
6362 dprintk("%s: server_scope mismatch detected\n",
6363 __func__);
6364 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006365 kfree(clp->cl_serverscope);
6366 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006367 }
6368
Chuck Lever177313f2012-05-21 22:44:58 -04006369 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006370 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006371 goto out;
6372 }
Trond Myklebust32b01312012-05-26 13:41:04 -04006373 } else
6374 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006375
Chuck Leveracdeb692012-05-21 22:46:16 -04006376out_server_owner:
6377 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006378out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006379 kfree(res.server_scope);
6380out:
Chuck Lever177313f2012-05-21 22:44:58 -04006381 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006382 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006383 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006384 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04006385 clp->cl_implid->date.seconds,
6386 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006387 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006388 return status;
6389}
6390
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006391/*
6392 * nfs4_proc_exchange_id()
6393 *
6394 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6395 *
6396 * Since the clientid has expired, all compounds using sessions
6397 * associated with the stale clientid will be returning
6398 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
6399 * be in some phase of session reset.
6400 *
6401 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
6402 */
6403int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
6404{
6405 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
6406 int status;
6407
6408 /* try SP4_MACH_CRED if krb5i/p */
6409 if (authflavor == RPC_AUTH_GSS_KRB5I ||
6410 authflavor == RPC_AUTH_GSS_KRB5P) {
6411 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
6412 if (!status)
6413 return 0;
6414 }
6415
6416 /* try SP4_NONE */
6417 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
6418}
6419
Trond Myklebust66245532012-05-25 17:18:09 -04006420static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
6421 struct rpc_cred *cred)
6422{
6423 struct rpc_message msg = {
6424 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
6425 .rpc_argp = clp,
6426 .rpc_cred = cred,
6427 };
6428 int status;
6429
6430 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006431 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04006432 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04006433 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04006434 "DESTROY_CLIENTID.", status, clp->cl_hostname);
6435 return status;
6436}
6437
6438static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
6439 struct rpc_cred *cred)
6440{
6441 unsigned int loop;
6442 int ret;
6443
6444 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
6445 ret = _nfs4_proc_destroy_clientid(clp, cred);
6446 switch (ret) {
6447 case -NFS4ERR_DELAY:
6448 case -NFS4ERR_CLIENTID_BUSY:
6449 ssleep(1);
6450 break;
6451 default:
6452 return ret;
6453 }
6454 }
6455 return 0;
6456}
6457
6458int nfs4_destroy_clientid(struct nfs_client *clp)
6459{
6460 struct rpc_cred *cred;
6461 int ret = 0;
6462
6463 if (clp->cl_mvops->minor_version < 1)
6464 goto out;
6465 if (clp->cl_exchange_flags == 0)
6466 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04006467 if (clp->cl_preserve_clid)
6468 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04006469 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04006470 ret = nfs4_proc_destroy_clientid(clp, cred);
6471 if (cred)
6472 put_rpccred(cred);
6473 switch (ret) {
6474 case 0:
6475 case -NFS4ERR_STALE_CLIENTID:
6476 clp->cl_exchange_flags = 0;
6477 }
6478out:
6479 return ret;
6480}
6481
Andy Adamson2050f0c2009-04-01 09:22:30 -04006482struct nfs4_get_lease_time_data {
6483 struct nfs4_get_lease_time_args *args;
6484 struct nfs4_get_lease_time_res *res;
6485 struct nfs_client *clp;
6486};
6487
6488static void nfs4_get_lease_time_prepare(struct rpc_task *task,
6489 void *calldata)
6490{
Andy Adamson2050f0c2009-04-01 09:22:30 -04006491 struct nfs4_get_lease_time_data *data =
6492 (struct nfs4_get_lease_time_data *)calldata;
6493
6494 dprintk("--> %s\n", __func__);
6495 /* just setup sequence, do not trigger session recovery
6496 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006497 nfs41_setup_sequence(data->clp->cl_session,
6498 &data->args->la_seq_args,
6499 &data->res->lr_seq_res,
6500 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006501 dprintk("<-- %s\n", __func__);
6502}
6503
6504/*
6505 * Called from nfs4_state_manager thread for session setup, so don't recover
6506 * from sequence operation or clientid errors.
6507 */
6508static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
6509{
6510 struct nfs4_get_lease_time_data *data =
6511 (struct nfs4_get_lease_time_data *)calldata;
6512
6513 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006514 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
6515 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04006516 switch (task->tk_status) {
6517 case -NFS4ERR_DELAY:
6518 case -NFS4ERR_GRACE:
6519 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
6520 rpc_delay(task, NFS4_POLL_RETRY_MIN);
6521 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006522 /* fall through */
6523 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006524 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006525 return;
6526 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04006527 dprintk("<-- %s\n", __func__);
6528}
6529
Trond Myklebust17280172012-03-11 13:11:00 -04006530static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04006531 .rpc_call_prepare = nfs4_get_lease_time_prepare,
6532 .rpc_call_done = nfs4_get_lease_time_done,
6533};
6534
6535int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
6536{
6537 struct rpc_task *task;
6538 struct nfs4_get_lease_time_args args;
6539 struct nfs4_get_lease_time_res res = {
6540 .lr_fsinfo = fsinfo,
6541 };
6542 struct nfs4_get_lease_time_data data = {
6543 .args = &args,
6544 .res = &res,
6545 .clp = clp,
6546 };
6547 struct rpc_message msg = {
6548 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
6549 .rpc_argp = &args,
6550 .rpc_resp = &res,
6551 };
6552 struct rpc_task_setup task_setup = {
6553 .rpc_client = clp->cl_rpcclient,
6554 .rpc_message = &msg,
6555 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006556 .callback_data = &data,
6557 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04006558 };
6559 int status;
6560
Chuck Levera9c92d62013-08-09 12:48:18 -04006561 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006562 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006563 dprintk("--> %s\n", __func__);
6564 task = rpc_run_task(&task_setup);
6565
6566 if (IS_ERR(task))
6567 status = PTR_ERR(task);
6568 else {
6569 status = task->tk_status;
6570 rpc_put_task(task);
6571 }
6572 dprintk("<-- %s return %d\n", __func__, status);
6573
6574 return status;
6575}
6576
Andy Adamsonfc931582009-04-01 09:22:31 -04006577/*
6578 * Initialize the values to be used by the client in CREATE_SESSION
6579 * If nfs4_init_session set the fore channel request and response sizes,
6580 * use them.
6581 *
6582 * Set the back channel max_resp_sz_cached to zero to force the client to
6583 * always set csa_cachethis to FALSE because the current implementation
6584 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
6585 */
6586static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
6587{
Andy Adamson18aad3d2013-06-26 12:21:49 -04006588 unsigned int max_rqst_sz, max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006589
Andy Adamson18aad3d2013-06-26 12:21:49 -04006590 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
6591 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
6592
Andy Adamsonfc931582009-04-01 09:22:31 -04006593 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04006594 args->fc_attrs.max_rqst_sz = max_rqst_sz;
6595 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006596 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05006597 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04006598
6599 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05006600 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04006601 __func__,
6602 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05006603 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04006604
6605 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04006606 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
6607 args->bc_attrs.max_resp_sz = PAGE_SIZE;
6608 args->bc_attrs.max_resp_sz_cached = 0;
6609 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
6610 args->bc_attrs.max_reqs = 1;
6611
6612 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
6613 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
6614 __func__,
6615 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
6616 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
6617 args->bc_attrs.max_reqs);
6618}
6619
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006620static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04006621{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006622 struct nfs4_channel_attrs *sent = &args->fc_attrs;
6623 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
6624
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006625 if (rcvd->max_resp_sz > sent->max_resp_sz)
6626 return -EINVAL;
6627 /*
6628 * Our requested max_ops is the minimum we need; we're not
6629 * prepared to break up compounds into smaller pieces than that.
6630 * So, no point even trying to continue if the server won't
6631 * cooperate:
6632 */
6633 if (rcvd->max_ops < sent->max_ops)
6634 return -EINVAL;
6635 if (rcvd->max_reqs == 0)
6636 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006637 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
6638 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006639 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04006640}
6641
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006642static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
6643{
6644 struct nfs4_channel_attrs *sent = &args->bc_attrs;
6645 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04006646
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006647 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
6648 return -EINVAL;
6649 if (rcvd->max_resp_sz < sent->max_resp_sz)
6650 return -EINVAL;
6651 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
6652 return -EINVAL;
6653 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006654 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006655 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006656 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006657 return -EINVAL;
6658 return 0;
6659}
Andy Adamson8d353012009-04-01 09:22:32 -04006660
Andy Adamson8d353012009-04-01 09:22:32 -04006661static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
6662 struct nfs4_session *session)
6663{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006664 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04006665
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006666 ret = nfs4_verify_fore_channel_attrs(args, session);
6667 if (ret)
6668 return ret;
6669 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04006670}
6671
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006672static int _nfs4_proc_create_session(struct nfs_client *clp,
6673 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04006674{
6675 struct nfs4_session *session = clp->cl_session;
6676 struct nfs41_create_session_args args = {
6677 .client = clp,
6678 .cb_program = NFS4_CALLBACK,
6679 };
6680 struct nfs41_create_session_res res = {
6681 .client = clp,
6682 };
6683 struct rpc_message msg = {
6684 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
6685 .rpc_argp = &args,
6686 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006687 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04006688 };
6689 int status;
6690
6691 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04006692 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04006693
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006694 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006695 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04006696
Trond Myklebust43095d32012-11-20 11:13:12 -05006697 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04006698 /* Verify the session's negotiated channel_attrs values */
6699 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04006700 /* Increment the clientid slot sequence id */
6701 clp->cl_seqid++;
6702 }
6703
6704 return status;
6705}
6706
6707/*
6708 * Issues a CREATE_SESSION operation to the server.
6709 * It is the responsibility of the caller to verify the session is
6710 * expired before calling this routine.
6711 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006712int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04006713{
6714 int status;
6715 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04006716 struct nfs4_session *session = clp->cl_session;
6717
6718 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
6719
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006720 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04006721 if (status)
6722 goto out;
6723
Andy Adamsonaacd5532011-11-09 13:58:21 -05006724 /* Init or reset the session slot tables */
6725 status = nfs4_setup_session_slot_tables(session);
6726 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04006727 if (status)
6728 goto out;
6729
6730 ptr = (unsigned *)&session->sess_id.data[0];
6731 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
6732 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04006733out:
6734 dprintk("<-- %s\n", __func__);
6735 return status;
6736}
6737
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006738/*
6739 * Issue the over-the-wire RPC DESTROY_SESSION.
6740 * The caller must serialize access to this routine.
6741 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006742int nfs4_proc_destroy_session(struct nfs4_session *session,
6743 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006744{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006745 struct rpc_message msg = {
6746 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
6747 .rpc_argp = session,
6748 .rpc_cred = cred,
6749 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006750 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006751
6752 dprintk("--> nfs4_proc_destroy_session\n");
6753
6754 /* session is still being setup */
6755 if (session->clp->cl_cons_state != NFS_CS_READY)
6756 return status;
6757
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006758 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006759 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006760
6761 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04006762 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006763 "Session has been destroyed regardless...\n", status);
6764
6765 dprintk("<-- nfs4_proc_destroy_session\n");
6766 return status;
6767}
6768
Trond Myklebust7b38c362012-05-23 13:23:31 -04006769/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006770 * Renew the cl_session lease.
6771 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006772struct nfs4_sequence_data {
6773 struct nfs_client *clp;
6774 struct nfs4_sequence_args args;
6775 struct nfs4_sequence_res res;
6776};
6777
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006778static void nfs41_sequence_release(void *data)
6779{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006780 struct nfs4_sequence_data *calldata = data;
6781 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006782
Alexandros Batsakis71358402010-02-05 03:45:05 -08006783 if (atomic_read(&clp->cl_count) > 1)
6784 nfs4_schedule_state_renewal(clp);
6785 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006786 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006787}
6788
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006789static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6790{
6791 switch(task->tk_status) {
6792 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006793 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6794 return -EAGAIN;
6795 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006796 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006797 }
6798 return 0;
6799}
6800
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006801static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006802{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006803 struct nfs4_sequence_data *calldata = data;
6804 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006805
Trond Myklebust14516c32010-07-31 14:29:06 -04006806 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6807 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006808
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006809 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006810 if (task->tk_status < 0) {
6811 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006812 if (atomic_read(&clp->cl_count) == 1)
6813 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006814
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006815 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6816 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006817 return;
6818 }
6819 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006820 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006821out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006822 dprintk("<-- %s\n", __func__);
6823}
6824
6825static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6826{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006827 struct nfs4_sequence_data *calldata = data;
6828 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006829 struct nfs4_sequence_args *args;
6830 struct nfs4_sequence_res *res;
6831
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006832 args = task->tk_msg.rpc_argp;
6833 res = task->tk_msg.rpc_resp;
6834
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006835 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006836}
6837
6838static const struct rpc_call_ops nfs41_sequence_ops = {
6839 .rpc_call_done = nfs41_sequence_call_done,
6840 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006841 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006842};
6843
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006844static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
6845 struct rpc_cred *cred,
6846 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006847{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006848 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006849 struct rpc_message msg = {
6850 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6851 .rpc_cred = cred,
6852 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006853 struct rpc_task_setup task_setup_data = {
6854 .rpc_client = clp->cl_rpcclient,
6855 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006856 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04006857 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006858 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006859
Alexandros Batsakis71358402010-02-05 03:45:05 -08006860 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006861 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006862 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006863 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006864 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006865 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006866 }
Chuck Levera9c92d62013-08-09 12:48:18 -04006867 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006868 if (is_privileged)
6869 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006870 msg.rpc_argp = &calldata->args;
6871 msg.rpc_resp = &calldata->res;
6872 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006873 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006874
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006875 return rpc_run_task(&task_setup_data);
6876}
6877
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006878static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006879{
6880 struct rpc_task *task;
6881 int ret = 0;
6882
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006883 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6884 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006885 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006886 if (IS_ERR(task))
6887 ret = PTR_ERR(task);
6888 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006889 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006890 dprintk("<-- %s status=%d\n", __func__, ret);
6891 return ret;
6892}
6893
6894static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6895{
6896 struct rpc_task *task;
6897 int ret;
6898
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006899 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006900 if (IS_ERR(task)) {
6901 ret = PTR_ERR(task);
6902 goto out;
6903 }
6904 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006905 if (!ret) {
6906 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6907
6908 if (task->tk_status == 0)
6909 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006910 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006911 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006912 rpc_put_task(task);
6913out:
6914 dprintk("<-- %s status=%d\n", __func__, ret);
6915 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006916}
6917
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006918struct nfs4_reclaim_complete_data {
6919 struct nfs_client *clp;
6920 struct nfs41_reclaim_complete_args arg;
6921 struct nfs41_reclaim_complete_res res;
6922};
6923
6924static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6925{
6926 struct nfs4_reclaim_complete_data *calldata = data;
6927
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006928 nfs41_setup_sequence(calldata->clp->cl_session,
6929 &calldata->arg.seq_args,
6930 &calldata->res.seq_res,
6931 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006932}
6933
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006934static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6935{
6936 switch(task->tk_status) {
6937 case 0:
6938 case -NFS4ERR_COMPLETE_ALREADY:
6939 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6940 break;
6941 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006942 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006943 /* fall through */
6944 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006945 return -EAGAIN;
6946 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006947 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006948 }
6949 return 0;
6950}
6951
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006952static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6953{
6954 struct nfs4_reclaim_complete_data *calldata = data;
6955 struct nfs_client *clp = calldata->clp;
6956 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6957
6958 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006959 if (!nfs41_sequence_done(task, res))
6960 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006961
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006962 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006963 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6964 rpc_restart_call_prepare(task);
6965 return;
6966 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006967 dprintk("<-- %s\n", __func__);
6968}
6969
6970static void nfs4_free_reclaim_complete_data(void *data)
6971{
6972 struct nfs4_reclaim_complete_data *calldata = data;
6973
6974 kfree(calldata);
6975}
6976
6977static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6978 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6979 .rpc_call_done = nfs4_reclaim_complete_done,
6980 .rpc_release = nfs4_free_reclaim_complete_data,
6981};
6982
6983/*
6984 * Issue a global reclaim complete.
6985 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04006986static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
6987 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006988{
6989 struct nfs4_reclaim_complete_data *calldata;
6990 struct rpc_task *task;
6991 struct rpc_message msg = {
6992 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04006993 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006994 };
6995 struct rpc_task_setup task_setup_data = {
6996 .rpc_client = clp->cl_rpcclient,
6997 .rpc_message = &msg,
6998 .callback_ops = &nfs4_reclaim_complete_call_ops,
6999 .flags = RPC_TASK_ASYNC,
7000 };
7001 int status = -ENOMEM;
7002
7003 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04007004 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007005 if (calldata == NULL)
7006 goto out;
7007 calldata->clp = clp;
7008 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007009
Chuck Levera9c92d62013-08-09 12:48:18 -04007010 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007011 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007012 msg.rpc_argp = &calldata->arg;
7013 msg.rpc_resp = &calldata->res;
7014 task_setup_data.callback_data = calldata;
7015 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007016 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007017 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007018 goto out;
7019 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05007020 status = nfs4_wait_for_completion_rpc_task(task);
7021 if (status == 0)
7022 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007023 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007024 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007025out:
7026 dprintk("<-- %s status=%d\n", __func__, status);
7027 return status;
7028}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007029
7030static void
7031nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
7032{
7033 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00007034 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007035 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007036
7037 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00007038 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
7039 * right now covering the LAYOUTGET we are about to send.
7040 * However, that is not so catastrophic, and there seems
7041 * to be no way to prevent it completely.
7042 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007043 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05007044 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007045 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00007046 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
7047 NFS_I(lgp->args.inode)->layout,
7048 lgp->args.ctx->state)) {
7049 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00007050 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007051}
7052
7053static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
7054{
7055 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07007056 struct inode *inode = lgp->args.inode;
7057 struct nfs_server *server = NFS_SERVER(inode);
7058 struct pnfs_layout_hdr *lo;
7059 struct nfs4_state *state = NULL;
Weston Andros Adamson30005122013-02-28 20:30:10 -05007060 unsigned long timeo, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007061
7062 dprintk("--> %s\n", __func__);
7063
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007064 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07007065 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007066
7067 switch (task->tk_status) {
7068 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07007069 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007070 case -NFS4ERR_LAYOUTTRYLATER:
7071 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05007072 timeo = rpc_get_timeout(task->tk_client);
7073 giveup = lgp->args.timestamp + timeo;
7074 if (time_after(giveup, jiffies))
7075 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07007076 break;
7077 case -NFS4ERR_EXPIRED:
7078 case -NFS4ERR_BAD_STATEID:
7079 spin_lock(&inode->i_lock);
7080 lo = NFS_I(inode)->layout;
7081 if (!lo || list_empty(&lo->plh_segs)) {
7082 spin_unlock(&inode->i_lock);
7083 /* If the open stateid was bad, then recover it. */
7084 state = lgp->args.ctx->state;
7085 } else {
7086 LIST_HEAD(head);
7087
7088 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
7089 spin_unlock(&inode->i_lock);
7090 /* Mark the bad layout state as invalid, then
7091 * retry using the open stateid. */
7092 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007093 }
7094 }
Trond Myklebustee314c22012-10-01 17:25:48 -07007095 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
7096 rpc_restart_call_prepare(task);
7097out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007098 dprintk("<-- %s\n", __func__);
7099}
7100
Idan Kedar85541162012-08-02 11:47:10 +03007101static size_t max_response_pages(struct nfs_server *server)
7102{
7103 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
7104 return nfs_page_array_len(0, max_resp_sz);
7105}
7106
7107static void nfs4_free_pages(struct page **pages, size_t size)
7108{
7109 int i;
7110
7111 if (!pages)
7112 return;
7113
7114 for (i = 0; i < size; i++) {
7115 if (!pages[i])
7116 break;
7117 __free_page(pages[i]);
7118 }
7119 kfree(pages);
7120}
7121
7122static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
7123{
7124 struct page **pages;
7125 int i;
7126
7127 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
7128 if (!pages) {
7129 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
7130 return NULL;
7131 }
7132
7133 for (i = 0; i < size; i++) {
7134 pages[i] = alloc_page(gfp_flags);
7135 if (!pages[i]) {
7136 dprintk("%s: failed to allocate page\n", __func__);
7137 nfs4_free_pages(pages, size);
7138 return NULL;
7139 }
7140 }
7141
7142 return pages;
7143}
7144
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007145static void nfs4_layoutget_release(void *calldata)
7146{
7147 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007148 struct inode *inode = lgp->args.inode;
7149 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007150 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007151
7152 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03007153 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007154 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007155 put_nfs_open_context(lgp->args.ctx);
7156 kfree(calldata);
7157 dprintk("<-- %s\n", __func__);
7158}
7159
7160static const struct rpc_call_ops nfs4_layoutget_call_ops = {
7161 .rpc_call_prepare = nfs4_layoutget_prepare,
7162 .rpc_call_done = nfs4_layoutget_done,
7163 .rpc_release = nfs4_layoutget_release,
7164};
7165
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007166struct pnfs_layout_segment *
7167nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007168{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007169 struct inode *inode = lgp->args.inode;
7170 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007171 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007172 struct rpc_task *task;
7173 struct rpc_message msg = {
7174 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
7175 .rpc_argp = &lgp->args,
7176 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04007177 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007178 };
7179 struct rpc_task_setup task_setup_data = {
7180 .rpc_client = server->client,
7181 .rpc_message = &msg,
7182 .callback_ops = &nfs4_layoutget_call_ops,
7183 .callback_data = lgp,
7184 .flags = RPC_TASK_ASYNC,
7185 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007186 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007187 int status = 0;
7188
7189 dprintk("--> %s\n", __func__);
7190
Idan Kedar85541162012-08-02 11:47:10 +03007191 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
7192 if (!lgp->args.layout.pages) {
7193 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007194 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03007195 }
7196 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05007197 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03007198
Weston Andros Adamson35124a02011-03-24 16:48:21 -04007199 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007200 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04007201 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007202
7203 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
7204 pnfs_get_layout_hdr(NFS_I(inode)->layout);
7205
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007206 task = rpc_run_task(&task_setup_data);
7207 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007208 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007209 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00007210 if (status == 0)
7211 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007212 trace_nfs4_layoutget(lgp->args.ctx,
7213 &lgp->args.range,
7214 &lgp->res.range,
7215 status);
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05007216 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
7217 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007218 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007219 rpc_put_task(task);
7220 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007221 if (status)
7222 return ERR_PTR(status);
7223 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007224}
7225
Benny Halevycbe82602011-05-22 19:52:37 +03007226static void
7227nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
7228{
7229 struct nfs4_layoutreturn *lrp = calldata;
7230
7231 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007232 nfs41_setup_sequence(lrp->clp->cl_session,
7233 &lrp->args.seq_args,
7234 &lrp->res.seq_res,
7235 task);
Benny Halevycbe82602011-05-22 19:52:37 +03007236}
7237
7238static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
7239{
7240 struct nfs4_layoutreturn *lrp = calldata;
7241 struct nfs_server *server;
7242
7243 dprintk("--> %s\n", __func__);
7244
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007245 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03007246 return;
7247
7248 server = NFS_SERVER(lrp->args.inode);
7249 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007250 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03007251 return;
7252 }
Benny Halevycbe82602011-05-22 19:52:37 +03007253 dprintk("<-- %s\n", __func__);
7254}
7255
7256static void nfs4_layoutreturn_release(void *calldata)
7257{
7258 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04007259 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03007260
7261 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04007262 spin_lock(&lo->plh_inode->i_lock);
7263 if (lrp->res.lrs_present)
7264 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
7265 lo->plh_block_lgets--;
7266 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04007267 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03007268 kfree(calldata);
7269 dprintk("<-- %s\n", __func__);
7270}
7271
7272static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
7273 .rpc_call_prepare = nfs4_layoutreturn_prepare,
7274 .rpc_call_done = nfs4_layoutreturn_done,
7275 .rpc_release = nfs4_layoutreturn_release,
7276};
7277
7278int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
7279{
7280 struct rpc_task *task;
7281 struct rpc_message msg = {
7282 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
7283 .rpc_argp = &lrp->args,
7284 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04007285 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03007286 };
7287 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04007288 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03007289 .rpc_message = &msg,
7290 .callback_ops = &nfs4_layoutreturn_call_ops,
7291 .callback_data = lrp,
7292 };
7293 int status;
7294
7295 dprintk("--> %s\n", __func__);
Chuck Levera9c92d62013-08-09 12:48:18 -04007296 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03007297 task = rpc_run_task(&task_setup_data);
7298 if (IS_ERR(task))
7299 return PTR_ERR(task);
7300 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007301 trace_nfs4_layoutreturn(lrp->args.inode, status);
Benny Halevycbe82602011-05-22 19:52:37 +03007302 dprintk("<-- %s status=%d\n", __func__, status);
7303 rpc_put_task(task);
7304 return status;
7305}
7306
Andy Adamson7f11d8d2011-07-30 20:52:35 -04007307/*
7308 * Retrieve the list of Data Server devices from the MDS.
7309 */
7310static int _nfs4_getdevicelist(struct nfs_server *server,
7311 const struct nfs_fh *fh,
7312 struct pnfs_devicelist *devlist)
7313{
7314 struct nfs4_getdevicelist_args args = {
7315 .fh = fh,
7316 .layoutclass = server->pnfs_curr_ld->id,
7317 };
7318 struct nfs4_getdevicelist_res res = {
7319 .devlist = devlist,
7320 };
7321 struct rpc_message msg = {
7322 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
7323 .rpc_argp = &args,
7324 .rpc_resp = &res,
7325 };
7326 int status;
7327
7328 dprintk("--> %s\n", __func__);
7329 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
7330 &res.seq_res, 0);
7331 dprintk("<-- %s status=%d\n", __func__, status);
7332 return status;
7333}
7334
7335int nfs4_proc_getdevicelist(struct nfs_server *server,
7336 const struct nfs_fh *fh,
7337 struct pnfs_devicelist *devlist)
7338{
7339 struct nfs4_exception exception = { };
7340 int err;
7341
7342 do {
7343 err = nfs4_handle_exception(server,
7344 _nfs4_getdevicelist(server, fh, devlist),
7345 &exception);
7346 } while (exception.retry);
7347
7348 dprintk("%s: err=%d, num_devs=%u\n", __func__,
7349 err, devlist->num_devs);
7350
7351 return err;
7352}
7353EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
7354
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007355static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007356_nfs4_proc_getdeviceinfo(struct nfs_server *server,
7357 struct pnfs_device *pdev,
7358 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007359{
7360 struct nfs4_getdeviceinfo_args args = {
7361 .pdev = pdev,
7362 };
7363 struct nfs4_getdeviceinfo_res res = {
7364 .pdev = pdev,
7365 };
7366 struct rpc_message msg = {
7367 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
7368 .rpc_argp = &args,
7369 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007370 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007371 };
7372 int status;
7373
7374 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00007375 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007376 dprintk("<-- %s status=%d\n", __func__, status);
7377
7378 return status;
7379}
7380
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007381int nfs4_proc_getdeviceinfo(struct nfs_server *server,
7382 struct pnfs_device *pdev,
7383 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007384{
7385 struct nfs4_exception exception = { };
7386 int err;
7387
7388 do {
7389 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007390 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007391 &exception);
7392 } while (exception.retry);
7393 return err;
7394}
7395EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
7396
Andy Adamson863a3c62011-03-23 13:27:54 +00007397static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
7398{
7399 struct nfs4_layoutcommit_data *data = calldata;
7400 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007401 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00007402
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007403 nfs41_setup_sequence(session,
7404 &data->args.seq_args,
7405 &data->res.seq_res,
7406 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00007407}
7408
7409static void
7410nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
7411{
7412 struct nfs4_layoutcommit_data *data = calldata;
7413 struct nfs_server *server = NFS_SERVER(data->args.inode);
7414
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007415 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00007416 return;
7417
7418 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007419 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
7420 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
7421 case -NFS4ERR_BADLAYOUT: /* no layout */
7422 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00007423 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007424 break;
7425 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00007426 nfs_post_op_update_inode_force_wcc(data->args.inode,
7427 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007428 break;
7429 default:
7430 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
7431 rpc_restart_call_prepare(task);
7432 return;
7433 }
7434 }
Andy Adamson863a3c62011-03-23 13:27:54 +00007435}
7436
7437static void nfs4_layoutcommit_release(void *calldata)
7438{
7439 struct nfs4_layoutcommit_data *data = calldata;
7440
Andy Adamsondb29c082011-07-30 20:52:38 -04007441 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00007442 put_rpccred(data->cred);
7443 kfree(data);
7444}
7445
7446static const struct rpc_call_ops nfs4_layoutcommit_ops = {
7447 .rpc_call_prepare = nfs4_layoutcommit_prepare,
7448 .rpc_call_done = nfs4_layoutcommit_done,
7449 .rpc_release = nfs4_layoutcommit_release,
7450};
7451
7452int
Andy Adamsonef311532011-03-12 02:58:10 -05007453nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00007454{
7455 struct rpc_message msg = {
7456 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
7457 .rpc_argp = &data->args,
7458 .rpc_resp = &data->res,
7459 .rpc_cred = data->cred,
7460 };
7461 struct rpc_task_setup task_setup_data = {
7462 .task = &data->task,
7463 .rpc_client = NFS_CLIENT(data->args.inode),
7464 .rpc_message = &msg,
7465 .callback_ops = &nfs4_layoutcommit_ops,
7466 .callback_data = data,
7467 .flags = RPC_TASK_ASYNC,
7468 };
7469 struct rpc_task *task;
7470 int status = 0;
7471
7472 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
7473 "lbw: %llu inode %lu\n",
7474 data->task.tk_pid, sync,
7475 data->args.lastbytewritten,
7476 data->args.inode->i_ino);
7477
Chuck Levera9c92d62013-08-09 12:48:18 -04007478 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00007479 task = rpc_run_task(&task_setup_data);
7480 if (IS_ERR(task))
7481 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05007482 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00007483 goto out;
7484 status = nfs4_wait_for_completion_rpc_task(task);
7485 if (status != 0)
7486 goto out;
7487 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007488 trace_nfs4_layoutcommit(data->args.inode, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00007489out:
7490 dprintk("%s: status %d\n", __func__, status);
7491 rpc_put_task(task);
7492 return status;
7493}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007494
Andy Adamson97431202013-08-08 10:57:56 -04007495/**
7496 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
7497 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
7498 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007499static int
7500_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007501 struct nfs_fsinfo *info,
7502 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007503{
7504 struct nfs41_secinfo_no_name_args args = {
7505 .style = SECINFO_STYLE_CURRENT_FH,
7506 };
7507 struct nfs4_secinfo_res res = {
7508 .flavors = flavors,
7509 };
7510 struct rpc_message msg = {
7511 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
7512 .rpc_argp = &args,
7513 .rpc_resp = &res,
7514 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007515 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007516 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007517 int status;
7518
7519 if (use_integrity) {
7520 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007521 cred = nfs4_get_clid_cred(server->nfs_client);
7522 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007523 }
7524
7525 dprintk("--> %s\n", __func__);
7526 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
7527 &res.seq_res, 0);
7528 dprintk("<-- %s status=%d\n", __func__, status);
7529
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007530 if (cred)
7531 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007532
7533 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007534}
7535
7536static int
7537nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7538 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
7539{
7540 struct nfs4_exception exception = { };
7541 int err;
7542 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007543 /* first try using integrity protection */
7544 err = -NFS4ERR_WRONGSEC;
7545
7546 /* try to use integrity protection with machine cred */
7547 if (_nfs4_is_integrity_protected(server->nfs_client))
7548 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
7549 flavors, true);
7550
7551 /*
7552 * if unable to use integrity protection, or SECINFO with
7553 * integrity protection returns NFS4ERR_WRONGSEC (which is
7554 * disallowed by spec, but exists in deployed servers) use
7555 * the current filesystem's rpc_client and the user cred.
7556 */
7557 if (err == -NFS4ERR_WRONGSEC)
7558 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
7559 flavors, false);
7560
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007561 switch (err) {
7562 case 0:
7563 case -NFS4ERR_WRONGSEC:
7564 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007565 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007566 default:
7567 err = nfs4_handle_exception(server, err, &exception);
7568 }
7569 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007570out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007571 return err;
7572}
7573
7574static int
7575nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
7576 struct nfs_fsinfo *info)
7577{
7578 int err;
7579 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04007580 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007581 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04007582 struct nfs4_secinfo4 *secinfo;
7583 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007584
7585 page = alloc_page(GFP_KERNEL);
7586 if (!page) {
7587 err = -ENOMEM;
7588 goto out;
7589 }
7590
7591 flavors = page_address(page);
7592 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
7593
7594 /*
7595 * Fall back on "guess and check" method if
7596 * the server doesn't support SECINFO_NO_NAME
7597 */
7598 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
7599 err = nfs4_find_root_sec(server, fhandle, info);
7600 goto out_freepage;
7601 }
7602 if (err)
7603 goto out_freepage;
7604
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04007605 for (i = 0; i < flavors->num_flavors; i++) {
7606 secinfo = &flavors->flavors[i];
7607
7608 switch (secinfo->flavor) {
7609 case RPC_AUTH_NULL:
7610 case RPC_AUTH_UNIX:
7611 case RPC_AUTH_GSS:
7612 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
7613 &secinfo->flavor_info);
7614 break;
7615 default:
7616 flavor = RPC_AUTH_MAXFLAVOR;
7617 break;
7618 }
7619
7620 if (flavor != RPC_AUTH_MAXFLAVOR) {
7621 err = nfs4_lookup_root_sec(server, fhandle,
7622 info, flavor);
7623 if (!err)
7624 break;
7625 }
7626 }
7627
7628 if (flavor == RPC_AUTH_MAXFLAVOR)
7629 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007630
7631out_freepage:
7632 put_page(page);
7633 if (err == -EACCES)
7634 return -EPERM;
7635out:
7636 return err;
7637}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007638
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007639static int _nfs41_test_stateid(struct nfs_server *server,
7640 nfs4_stateid *stateid,
7641 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04007642{
7643 int status;
7644 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007645 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04007646 };
7647 struct nfs41_test_stateid_res res;
7648 struct rpc_message msg = {
7649 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
7650 .rpc_argp = &args,
7651 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007652 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04007653 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007654 struct rpc_clnt *rpc_client = server->client;
7655
7656 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
7657 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007658
Chuck Lever38527b12012-07-11 16:30:23 -04007659 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04007660 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007661 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007662 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007663 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04007664 if (status != NFS_OK) {
7665 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04007666 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04007667 }
7668 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04007669 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04007670}
7671
Chuck Lever38527b12012-07-11 16:30:23 -04007672/**
7673 * nfs41_test_stateid - perform a TEST_STATEID operation
7674 *
7675 * @server: server / transport on which to perform the operation
7676 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007677 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04007678 *
7679 * Returns NFS_OK if the server recognizes that "stateid" is valid.
7680 * Otherwise a negative NFS4ERR value is returned if the operation
7681 * failed or the state ID is not currently valid.
7682 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007683static int nfs41_test_stateid(struct nfs_server *server,
7684 nfs4_stateid *stateid,
7685 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04007686{
7687 struct nfs4_exception exception = { };
7688 int err;
7689 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007690 err = _nfs41_test_stateid(server, stateid, cred);
Chuck Lever377e5072012-07-11 16:29:45 -04007691 if (err != -NFS4ERR_DELAY)
7692 break;
7693 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04007694 } while (exception.retry);
7695 return err;
7696}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007697
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007698struct nfs_free_stateid_data {
7699 struct nfs_server *server;
7700 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007701 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007702};
7703
7704static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
7705{
7706 struct nfs_free_stateid_data *data = calldata;
7707 nfs41_setup_sequence(nfs4_get_session(data->server),
7708 &data->args.seq_args,
7709 &data->res.seq_res,
7710 task);
7711}
7712
7713static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
7714{
7715 struct nfs_free_stateid_data *data = calldata;
7716
7717 nfs41_sequence_done(task, &data->res.seq_res);
7718
7719 switch (task->tk_status) {
7720 case -NFS4ERR_DELAY:
7721 if (nfs4_async_handle_error(task, data->server, NULL) == -EAGAIN)
7722 rpc_restart_call_prepare(task);
7723 }
7724}
7725
7726static void nfs41_free_stateid_release(void *calldata)
7727{
7728 kfree(calldata);
7729}
7730
Trond Myklebust17f26b12013-08-21 15:48:42 -04007731static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007732 .rpc_call_prepare = nfs41_free_stateid_prepare,
7733 .rpc_call_done = nfs41_free_stateid_done,
7734 .rpc_release = nfs41_free_stateid_release,
7735};
7736
7737static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
7738 nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007739 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007740 bool privileged)
7741{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007742 struct rpc_message msg = {
7743 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007744 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007745 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007746 struct rpc_task_setup task_setup = {
7747 .rpc_client = server->client,
7748 .rpc_message = &msg,
7749 .callback_ops = &nfs41_free_stateid_ops,
7750 .flags = RPC_TASK_ASYNC,
7751 };
7752 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007753
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007754 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
7755 &task_setup.rpc_client, &msg);
7756
Chuck Lever38527b12012-07-11 16:30:23 -04007757 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007758 data = kmalloc(sizeof(*data), GFP_NOFS);
7759 if (!data)
7760 return ERR_PTR(-ENOMEM);
7761 data->server = server;
7762 nfs4_stateid_copy(&data->args.stateid, stateid);
7763
7764 task_setup.callback_data = data;
7765
7766 msg.rpc_argp = &data->args;
7767 msg.rpc_resp = &data->res;
Chuck Levera9c92d62013-08-09 12:48:18 -04007768 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007769 if (privileged)
7770 nfs4_set_sequence_privileged(&data->args.seq_args);
7771
7772 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007773}
7774
Chuck Lever38527b12012-07-11 16:30:23 -04007775/**
7776 * nfs41_free_stateid - perform a FREE_STATEID operation
7777 *
7778 * @server: server / transport on which to perform the operation
7779 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007780 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04007781 *
7782 * Returns NFS_OK if the server freed "stateid". Otherwise a
7783 * negative NFS4ERR value is returned.
7784 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007785static int nfs41_free_stateid(struct nfs_server *server,
7786 nfs4_stateid *stateid,
7787 struct rpc_cred *cred)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007788{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007789 struct rpc_task *task;
7790 int ret;
7791
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007792 task = _nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007793 if (IS_ERR(task))
7794 return PTR_ERR(task);
7795 ret = rpc_wait_for_completion_task(task);
7796 if (!ret)
7797 ret = task->tk_status;
7798 rpc_put_task(task);
7799 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007800}
Trond Myklebust36281ca2012-03-04 18:13:56 -05007801
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007802static int nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
7803{
7804 struct rpc_task *task;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007805 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007806
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007807 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007808 nfs4_free_lock_state(server, lsp);
7809 if (IS_ERR(task))
7810 return PTR_ERR(task);
7811 rpc_put_task(task);
7812 return 0;
7813}
7814
Trond Myklebust36281ca2012-03-04 18:13:56 -05007815static bool nfs41_match_stateid(const nfs4_stateid *s1,
7816 const nfs4_stateid *s2)
7817{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007818 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007819 return false;
7820
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007821 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007822 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007823 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007824 return true;
7825
7826 return false;
7827}
7828
Andy Adamson557134a2009-04-01 09:21:53 -04007829#endif /* CONFIG_NFS_V4_1 */
7830
Trond Myklebust36281ca2012-03-04 18:13:56 -05007831static bool nfs4_match_stateid(const nfs4_stateid *s1,
7832 const nfs4_stateid *s2)
7833{
Trond Myklebustf597c532012-03-04 18:13:56 -05007834 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05007835}
7836
7837
Trond Myklebust17280172012-03-11 13:11:00 -04007838static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007839 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007840 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007841 .recover_open = nfs4_open_reclaim,
7842 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04007843 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04007844 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007845};
7846
Andy Adamson591d71c2009-04-01 09:22:47 -04007847#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007848static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007849 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
7850 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
7851 .recover_open = nfs4_open_reclaim,
7852 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05007853 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007854 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04007855 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04007856};
7857#endif /* CONFIG_NFS_V4_1 */
7858
Trond Myklebust17280172012-03-11 13:11:00 -04007859static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007860 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007861 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007862 .recover_open = nfs4_open_expired,
7863 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04007864 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007865};
7866
Andy Adamson591d71c2009-04-01 09:22:47 -04007867#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007868static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007869 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
7870 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007871 .recover_open = nfs41_open_expired,
7872 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05007873 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04007874};
7875#endif /* CONFIG_NFS_V4_1 */
7876
Trond Myklebust17280172012-03-11 13:11:00 -04007877static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007878 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04007879 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007880 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04007881};
7882
7883#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007884static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007885 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04007886 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007887 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04007888};
7889#endif
7890
Trond Myklebust97dc1352010-06-16 09:52:26 -04007891static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
7892 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04007893 .init_caps = NFS_CAP_READDIRPLUS
7894 | NFS_CAP_ATOMIC_OPEN
7895 | NFS_CAP_CHANGE_ATTR
7896 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04007897 .init_client = nfs40_init_client,
7898 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007899 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007900 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007901 .free_lock_state = nfs4_release_lockowner,
Chuck Lever9915ea72013-08-09 12:48:27 -04007902 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007903 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
7904 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
7905 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007906};
7907
7908#if defined(CONFIG_NFS_V4_1)
7909static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
7910 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04007911 .init_caps = NFS_CAP_READDIRPLUS
7912 | NFS_CAP_ATOMIC_OPEN
7913 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04007914 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04007915 | NFS_CAP_STATEID_NFSV41
7916 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04007917 .init_client = nfs41_init_client,
7918 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007919 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007920 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007921 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04007922 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007923 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7924 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7925 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007926};
7927#endif
7928
Steve Dickson42c2c422013-05-22 12:50:38 -04007929#if defined(CONFIG_NFS_V4_2)
7930static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
7931 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04007932 .init_caps = NFS_CAP_READDIRPLUS
7933 | NFS_CAP_ATOMIC_OPEN
7934 | NFS_CAP_CHANGE_ATTR
7935 | NFS_CAP_POSIX_LOCK
7936 | NFS_CAP_STATEID_NFSV41
7937 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04007938 .init_client = nfs41_init_client,
7939 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04007940 .match_stateid = nfs41_match_stateid,
7941 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04007942 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04007943 .call_sync_ops = &nfs41_call_sync_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04007944 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7945 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7946 .state_renewal_ops = &nfs41_state_renewal_ops,
7947};
7948#endif
7949
Trond Myklebust97dc1352010-06-16 09:52:26 -04007950const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
7951 [0] = &nfs_v4_0_minor_ops,
7952#if defined(CONFIG_NFS_V4_1)
7953 [1] = &nfs_v4_1_minor_ops,
7954#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04007955#if defined(CONFIG_NFS_V4_2)
7956 [2] = &nfs_v4_2_minor_ops,
7957#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04007958};
7959
Trond Myklebust17f26b12013-08-21 15:48:42 -04007960static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04007961 .create = nfs_create,
7962 .lookup = nfs_lookup,
7963 .atomic_open = nfs_atomic_open,
7964 .link = nfs_link,
7965 .unlink = nfs_unlink,
7966 .symlink = nfs_symlink,
7967 .mkdir = nfs_mkdir,
7968 .rmdir = nfs_rmdir,
7969 .mknod = nfs_mknod,
7970 .rename = nfs_rename,
7971 .permission = nfs_permission,
7972 .getattr = nfs_getattr,
7973 .setattr = nfs_setattr,
7974 .getxattr = generic_getxattr,
7975 .setxattr = generic_setxattr,
7976 .listxattr = generic_listxattr,
7977 .removexattr = generic_removexattr,
7978};
7979
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08007980static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007981 .permission = nfs_permission,
7982 .getattr = nfs_getattr,
7983 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007984 .getxattr = generic_getxattr,
7985 .setxattr = generic_setxattr,
7986 .listxattr = generic_listxattr,
7987 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007988};
7989
David Howells509de812006-08-22 20:06:11 -04007990const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007991 .version = 4, /* protocol version */
7992 .dentry_ops = &nfs4_dentry_operations,
7993 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007994 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04007995 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007996 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04007997 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04007998 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007999 .getattr = nfs4_proc_getattr,
8000 .setattr = nfs4_proc_setattr,
8001 .lookup = nfs4_proc_lookup,
8002 .access = nfs4_proc_access,
8003 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008004 .create = nfs4_proc_create,
8005 .remove = nfs4_proc_remove,
8006 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04008007 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008008 .unlink_done = nfs4_proc_unlink_done,
8009 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04008010 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04008011 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04008012 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008013 .link = nfs4_proc_link,
8014 .symlink = nfs4_proc_symlink,
8015 .mkdir = nfs4_proc_mkdir,
8016 .rmdir = nfs4_proc_remove,
8017 .readdir = nfs4_proc_readdir,
8018 .mknod = nfs4_proc_mknod,
8019 .statfs = nfs4_proc_statfs,
8020 .fsinfo = nfs4_proc_fsinfo,
8021 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04008022 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008023 .decode_dirent = nfs4_decode_dirent,
8024 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04008025 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04008026 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05008027 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008028 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04008029 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04008030 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008031 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008032 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04008033 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008034 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008035 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00008036 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04008037 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04008038 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04008039 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04008040 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04008041 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00008042 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04008043 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04008044 .create_server = nfs4_create_server,
8045 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008046};
8047
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008048static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
8049 .prefix = XATTR_NAME_NFSV4_ACL,
8050 .list = nfs4_xattr_list_nfs4_acl,
8051 .get = nfs4_xattr_get_nfs4_acl,
8052 .set = nfs4_xattr_set_nfs4_acl,
8053};
8054
8055const struct xattr_handler *nfs4_xattr_handlers[] = {
8056 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04008057#ifdef CONFIG_NFS_V4_SECURITY_LABEL
8058 &nfs4_xattr_nfs4_label_handler,
8059#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008060 NULL
8061};
8062
Linus Torvalds1da177e2005-04-16 15:20:36 -07008063/*
8064 * Local variables:
8065 * c-basic-offset: 8
8066 * End:
8067 */