blob: 552e4f7a82258a7dde8a826a3b4484b771bc0c93 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Trond Myklebust6926afd2012-01-07 13:22:46 -050054#include <linux/nfs_idmap.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000055#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050056#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010057#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050066#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000067#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Trond Myklebustc6d01c62013-08-09 11:51:26 -040069#include "nfs4trace.h"
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define NFSDBG_FACILITY NFSDBG_PROC
72
Trond Myklebust2066fe82006-09-15 08:30:46 -040073#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define NFS4_POLL_RETRY_MAX (15*HZ)
75
Trond Myklebustcdd4e682006-01-03 09:55:12 +010076struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010077static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080078static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050080static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Chuck Lever81934dd2012-03-01 17:01:57 -050081static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040082static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
83static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040084static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
85 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -040086 struct nfs4_state *state, struct nfs4_label *ilabel,
87 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040088#ifdef CONFIG_NFS_V4_1
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040089static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
90 struct rpc_cred *);
91static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *,
92 struct rpc_cred *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040093#endif
David Quigleyaa9c2662013-05-22 12:50:44 -040094
95#ifdef CONFIG_NFS_V4_SECURITY_LABEL
96static inline struct nfs4_label *
97nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
98 struct iattr *sattr, struct nfs4_label *label)
99{
100 int err;
101
102 if (label == NULL)
103 return NULL;
104
105 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
106 return NULL;
107
108 if (NFS_SERVER(dir)->nfs_client->cl_minorversion < 2)
109 return NULL;
110
111 err = security_dentry_init_security(dentry, sattr->ia_mode,
112 &dentry->d_name, (void **)&label->label, &label->len);
113 if (err == 0)
114 return label;
115
116 return NULL;
117}
118static inline void
119nfs4_label_release_security(struct nfs4_label *label)
120{
121 if (label)
122 security_release_secctx(label->label, label->len);
123}
124static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
125{
126 if (label)
127 return server->attr_bitmask;
128
129 return server->attr_bitmask_nl;
130}
131#else
132static inline struct nfs4_label *
133nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
134 struct iattr *sattr, struct nfs4_label *l)
135{ return NULL; }
136static inline void
137nfs4_label_release_security(struct nfs4_label *label)
138{ return; }
139static inline u32 *
140nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
141{ return server->attr_bitmask; }
142#endif
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500145static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Trond Myklebust52567b02009-10-23 14:46:42 -0400147 if (err >= -1000)
148 return err;
149 switch (err) {
150 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500151 case -NFS4ERR_LAYOUTTRYLATER:
152 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400153 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000154 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400155 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000156 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800157 case -NFS4ERR_BADOWNER:
158 case -NFS4ERR_BADNAME:
159 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400160 case -NFS4ERR_SHARE_DENIED:
161 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400162 case -NFS4ERR_MINOR_VERS_MISMATCH:
163 return -EPROTONOSUPPORT;
Weston Andros Adamson6168f622012-09-10 14:00:46 -0400164 case -NFS4ERR_ACCESS:
165 return -EACCES;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400166 case -NFS4ERR_FILE_OPEN:
167 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400168 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700170 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400171 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400173 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
175
176/*
177 * This is our standard bitmap for GETATTR requests.
178 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400179const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 FATTR4_WORD0_TYPE
181 | FATTR4_WORD0_CHANGE
182 | FATTR4_WORD0_SIZE
183 | FATTR4_WORD0_FSID
184 | FATTR4_WORD0_FILEID,
185 FATTR4_WORD1_MODE
186 | FATTR4_WORD1_NUMLINKS
187 | FATTR4_WORD1_OWNER
188 | FATTR4_WORD1_OWNER_GROUP
189 | FATTR4_WORD1_RAWDEV
190 | FATTR4_WORD1_SPACE_USED
191 | FATTR4_WORD1_TIME_ACCESS
192 | FATTR4_WORD1_TIME_METADATA
David Quigleyaa9c2662013-05-22 12:50:44 -0400193 | FATTR4_WORD1_TIME_MODIFY,
194#ifdef CONFIG_NFS_V4_SECURITY_LABEL
195 FATTR4_WORD2_SECURITY_LABEL
196#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197};
198
Trond Myklebust1549210f2012-06-05 09:16:47 -0400199static const u32 nfs4_pnfs_open_bitmap[3] = {
200 FATTR4_WORD0_TYPE
201 | FATTR4_WORD0_CHANGE
202 | FATTR4_WORD0_SIZE
203 | FATTR4_WORD0_FSID
204 | FATTR4_WORD0_FILEID,
205 FATTR4_WORD1_MODE
206 | FATTR4_WORD1_NUMLINKS
207 | FATTR4_WORD1_OWNER
208 | FATTR4_WORD1_OWNER_GROUP
209 | FATTR4_WORD1_RAWDEV
210 | FATTR4_WORD1_SPACE_USED
211 | FATTR4_WORD1_TIME_ACCESS
212 | FATTR4_WORD1_TIME_METADATA
213 | FATTR4_WORD1_TIME_MODIFY,
214 FATTR4_WORD2_MDSTHRESHOLD
215};
216
Andy Adamsone23008e2012-10-02 21:07:32 -0400217static const u32 nfs4_open_noattr_bitmap[3] = {
218 FATTR4_WORD0_TYPE
219 | FATTR4_WORD0_CHANGE
220 | FATTR4_WORD0_FILEID,
221};
222
David Quigleya09df2c2013-05-22 12:50:41 -0400223const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 FATTR4_WORD0_FILES_AVAIL
225 | FATTR4_WORD0_FILES_FREE
226 | FATTR4_WORD0_FILES_TOTAL,
227 FATTR4_WORD1_SPACE_AVAIL
228 | FATTR4_WORD1_SPACE_FREE
229 | FATTR4_WORD1_SPACE_TOTAL
230};
231
David Quigleya09df2c2013-05-22 12:50:41 -0400232const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 FATTR4_WORD0_MAXLINK
234 | FATTR4_WORD0_MAXNAME,
235 0
236};
237
Fred Isamandae100c2011-07-30 20:52:37 -0400238const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 | FATTR4_WORD0_MAXREAD
240 | FATTR4_WORD0_MAXWRITE
241 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700242 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400243 | FATTR4_WORD1_FS_LAYOUT_TYPES,
244 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245};
246
David Quigleya09df2c2013-05-22 12:50:41 -0400247const u32 nfs4_fs_locations_bitmap[3] = {
Manoj Naik830b8e32006-06-09 09:34:25 -0400248 FATTR4_WORD0_TYPE
249 | FATTR4_WORD0_CHANGE
250 | FATTR4_WORD0_SIZE
251 | FATTR4_WORD0_FSID
252 | FATTR4_WORD0_FILEID
253 | FATTR4_WORD0_FS_LOCATIONS,
254 FATTR4_WORD1_MODE
255 | FATTR4_WORD1_NUMLINKS
256 | FATTR4_WORD1_OWNER
257 | FATTR4_WORD1_OWNER_GROUP
258 | FATTR4_WORD1_RAWDEV
259 | FATTR4_WORD1_SPACE_USED
260 | FATTR4_WORD1_TIME_ACCESS
261 | FATTR4_WORD1_TIME_METADATA
262 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400263 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400264};
265
Al Virobc4785c2006-10-19 23:28:51 -0700266static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 struct nfs4_readdir_arg *readdir)
268{
Al Viro0dbb4c62006-10-19 23:28:49 -0700269 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000272 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
274 return;
275 }
276
277 readdir->cookie = 0;
278 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
279 if (cookie == 2)
280 return;
281
282 /*
283 * NFSv4 servers do not return entries for '.' and '..'
284 * Therefore, we fake these entries here. We let '.'
285 * have cookie 0 and '..' have cookie 1. Note that
286 * when talking to the server, we always send cookie 0
287 * instead of 1 or 2.
288 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800289 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 if (cookie == 0) {
292 *p++ = xdr_one; /* next */
293 *p++ = xdr_zero; /* cookie, first word */
294 *p++ = xdr_one; /* cookie, second word */
295 *p++ = xdr_one; /* entry len */
296 memcpy(p, ".\0\0\0", 4); /* entry */
297 p++;
298 *p++ = xdr_one; /* bitmap length */
299 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
300 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400301 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
303
304 *p++ = xdr_one; /* next */
305 *p++ = xdr_zero; /* cookie, first word */
306 *p++ = xdr_two; /* cookie, second word */
307 *p++ = xdr_two; /* entry len */
308 memcpy(p, "..\0\0", 4); /* entry */
309 p++;
310 *p++ = xdr_one; /* bitmap length */
311 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
312 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400313 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 readdir->pgbase = (char *)p - (char *)start;
316 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800317 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
319
Trond Myklebust65de8722008-12-23 15:21:44 -0500320static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
321{
322 int res = 0;
323
324 might_sleep();
325
326 if (*timeout <= 0)
327 *timeout = NFS4_POLL_RETRY_MIN;
328 if (*timeout > NFS4_POLL_RETRY_MAX)
329 *timeout = NFS4_POLL_RETRY_MAX;
Colin Cross416ad3c2013-05-06 23:50:06 +0000330 freezable_schedule_timeout_killable_unsafe(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500331 if (fatal_signal_pending(current))
332 res = -ERESTARTSYS;
333 *timeout <<= 1;
334 return res;
335}
336
337/* This is the error handling routine for processes that are allowed
338 * to sleep.
339 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800340static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500341{
342 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500343 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500344 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500345 int ret = errorcode;
346
347 exception->retry = 0;
348 switch(errorcode) {
349 case 0:
350 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500351 case -NFS4ERR_OPENMODE:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400352 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400353 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500354 exception->retry = 1;
355 return 0;
356 }
357 if (state == NULL)
358 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400359 ret = nfs4_schedule_stateid_recovery(server, state);
360 if (ret < 0)
361 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500362 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500363 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500364 case -NFS4ERR_ADMIN_REVOKED:
365 case -NFS4ERR_BAD_STATEID:
Trond Myklebustb570a972013-04-12 15:04:51 -0400366 if (inode != NULL && nfs4_have_delegation(inode, FMODE_READ)) {
367 nfs_remove_bad_delegation(inode);
368 exception->retry = 1;
369 break;
370 }
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500371 if (state == NULL)
372 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400373 ret = nfs4_schedule_stateid_recovery(server, state);
374 if (ret < 0)
375 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500376 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400377 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -0400378 if (state != NULL) {
379 ret = nfs4_schedule_stateid_recovery(server, state);
380 if (ret < 0)
381 break;
382 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500383 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500384 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500385 nfs4_schedule_lease_recovery(clp);
386 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400387 case -NFS4ERR_MOVED:
388 ret = nfs4_schedule_migration_recovery(server);
389 if (ret < 0)
390 break;
391 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500392#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400393 case -NFS4ERR_BADSESSION:
394 case -NFS4ERR_BADSLOT:
395 case -NFS4ERR_BAD_HIGH_SLOT:
396 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
397 case -NFS4ERR_DEADSESSION:
398 case -NFS4ERR_SEQ_FALSE_RETRY:
399 case -NFS4ERR_SEQ_MISORDERED:
400 dprintk("%s ERROR: %d Reset session\n", __func__,
401 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400402 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400403 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500404#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500405 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500406 if (exception->timeout > HZ) {
407 /* We have retried a decent amount, time to
408 * fail
409 */
410 ret = -EBUSY;
411 break;
412 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500413 case -NFS4ERR_GRACE:
414 case -NFS4ERR_DELAY:
415 ret = nfs4_delay(server->client, &exception->timeout);
416 if (ret != 0)
417 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400418 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500419 case -NFS4ERR_OLD_STATEID:
420 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800421 break;
422 case -NFS4ERR_BADOWNER:
423 /* The following works around a Linux server bug! */
424 case -NFS4ERR_BADNAME:
425 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
426 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
427 exception->retry = 1;
428 printk(KERN_WARNING "NFS: v4 server %s "
429 "does not accept raw "
430 "uid/gids. "
431 "Reenabling the idmapper.\n",
432 server->nfs_client->cl_hostname);
433 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500434 }
435 /* We failed to handle the error */
436 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500437wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500438 ret = nfs4_wait_clnt_recover(clp);
Chuck Lever519ae252013-10-17 14:13:19 -0400439 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
440 return -EIO;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500441 if (ret == 0)
442 exception->retry = 1;
443 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500444}
445
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400446/*
447 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
448 * or 'false' otherwise.
449 */
450static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
451{
452 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
453
454 if (flavor == RPC_AUTH_GSS_KRB5I ||
455 flavor == RPC_AUTH_GSS_KRB5P)
456 return true;
457
458 return false;
459}
Trond Myklebust65de8722008-12-23 15:21:44 -0500460
Trond Myklebust452e9352010-07-31 14:29:06 -0400461static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 spin_lock(&clp->cl_lock);
464 if (time_before(clp->cl_last_renewal,timestamp))
465 clp->cl_last_renewal = timestamp;
466 spin_unlock(&clp->cl_lock);
467}
468
Trond Myklebust452e9352010-07-31 14:29:06 -0400469static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
470{
471 do_renew_lease(server->nfs_client, timestamp);
472}
473
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400474struct nfs4_call_sync_data {
475 const struct nfs_server *seq_server;
476 struct nfs4_sequence_args *seq_args;
477 struct nfs4_sequence_res *seq_res;
478};
479
Chuck Levera9c92d62013-08-09 12:48:18 -0400480static void nfs4_init_sequence(struct nfs4_sequence_args *args,
481 struct nfs4_sequence_res *res, int cache_reply)
482{
483 args->sa_slot = NULL;
484 args->sa_cache_this = cache_reply;
485 args->sa_privileged = 0;
486
487 res->sr_slot = NULL;
488}
489
490static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
491{
492 args->sa_privileged = 1;
493}
494
Chuck Lever3bd23842013-08-09 12:49:19 -0400495static int nfs40_setup_sequence(const struct nfs_server *server,
496 struct nfs4_sequence_args *args,
497 struct nfs4_sequence_res *res,
498 struct rpc_task *task)
499{
500 struct nfs4_slot_table *tbl = server->nfs_client->cl_slot_tbl;
501 struct nfs4_slot *slot;
502
503 /* slot already allocated? */
504 if (res->sr_slot != NULL)
505 goto out_start;
506
507 spin_lock(&tbl->slot_tbl_lock);
508 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
509 goto out_sleep;
510
511 slot = nfs4_alloc_slot(tbl);
512 if (IS_ERR(slot)) {
513 if (slot == ERR_PTR(-ENOMEM))
514 task->tk_timeout = HZ >> 2;
515 goto out_sleep;
516 }
517 spin_unlock(&tbl->slot_tbl_lock);
518
519 args->sa_slot = slot;
520 res->sr_slot = slot;
521
522out_start:
523 rpc_call_start(task);
524 return 0;
525
526out_sleep:
527 if (args->sa_privileged)
528 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
529 NULL, RPC_PRIORITY_PRIVILEGED);
530 else
531 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
532 spin_unlock(&tbl->slot_tbl_lock);
533 return -EAGAIN;
534}
535
536static int nfs40_sequence_done(struct rpc_task *task,
537 struct nfs4_sequence_res *res)
538{
539 struct nfs4_slot *slot = res->sr_slot;
540 struct nfs4_slot_table *tbl;
541
542 if (!RPC_WAS_SENT(task))
543 goto out;
544
545 tbl = slot->table;
546 spin_lock(&tbl->slot_tbl_lock);
547 if (!nfs41_wake_and_assign_slot(tbl, slot))
548 nfs4_free_slot(tbl, slot);
549 spin_unlock(&tbl->slot_tbl_lock);
550
551 res->sr_slot = NULL;
552out:
553 return 1;
554}
555
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400556#if defined(CONFIG_NFS_V4_1)
557
Trond Myklebustd185a332010-06-16 09:52:25 -0400558static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400559{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500560 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400561 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500562 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400563
Benny Halevydfb4f3092010-09-24 09:17:01 -0400564 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400565 /* just wake up the next guy waiting since
566 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500567 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500568 return;
Andy Adamson13615872009-04-01 09:22:17 -0400569 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500570 tbl = res->sr_slot->table;
571 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500572
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500573 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500574 /* Be nice to the server: try to ensure that the last transmitted
575 * value for highest_user_slotid <= target_highest_slotid
576 */
577 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
578 send_new_highest_used_slotid = true;
579
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500580 if (nfs41_wake_and_assign_slot(tbl, res->sr_slot)) {
581 send_new_highest_used_slotid = false;
582 goto out_unlock;
583 }
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500584 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500585
586 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
587 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500588out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500589 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400590 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500591 if (send_new_highest_used_slotid)
592 nfs41_server_notify_highest_slotid_update(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400593}
594
Trond Myklebust14516c32010-07-31 14:29:06 -0400595static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400596{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500597 struct nfs4_session *session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500598 struct nfs4_slot *slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400599 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500600 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500601 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400602
Bryan Schumaker468f8612011-04-18 15:57:32 -0400603 /* don't increment the sequence number if the task wasn't sent */
604 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400605 goto out;
606
Trond Myklebust933602e2012-11-16 12:12:38 -0500607 slot = res->sr_slot;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500608 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500609
Trond Myklebustac20d162012-12-15 15:36:07 -0500610 if (slot->interrupted) {
611 slot->interrupted = 0;
612 interrupted = true;
613 }
614
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400615 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500616 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400617 switch (res->sr_status) {
618 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400619 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500620 ++slot->seq_nr;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500621 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500622 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500623 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500624 if (res->sr_status_flags != 0)
625 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500626 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400627 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500628 case 1:
629 /*
630 * sr_status remains 1 if an RPC level error occurred.
631 * The server may or may not have processed the sequence
632 * operation..
633 * Mark the slot as having hosted an interrupted RPC call.
634 */
635 slot->interrupted = 1;
636 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400637 case -NFS4ERR_DELAY:
638 /* The server detected a resend of the RPC call and
639 * returned NFS4ERR_DELAY as per Section 2.10.6.2
640 * of RFC5661.
641 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500642 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400643 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500644 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500645 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400646 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500647 case -NFS4ERR_BADSLOT:
648 /*
649 * The slot id we used was probably retired. Try again
650 * using a different slot id.
651 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500652 goto retry_nowait;
653 case -NFS4ERR_SEQ_MISORDERED:
654 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500655 * Was the last operation on this sequence interrupted?
656 * If so, retry after bumping the sequence number.
657 */
658 if (interrupted) {
659 ++slot->seq_nr;
660 goto retry_nowait;
661 }
662 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500663 * Could this slot have been previously retired?
664 * If so, then the server may be expecting seq_nr = 1!
665 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500666 if (slot->seq_nr != 1) {
667 slot->seq_nr = 1;
668 goto retry_nowait;
669 }
670 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500671 case -NFS4ERR_SEQ_FALSE_RETRY:
672 ++slot->seq_nr;
673 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400674 default:
675 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500676 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400677 }
678out:
679 /* The session may be reset by one of the error handlers. */
680 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400681 nfs41_sequence_free_slot(res);
Trond Myklebust85563072012-12-11 10:31:12 -0500682 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500683retry_nowait:
684 if (rpc_restart_call_prepare(task)) {
685 task->tk_status = 0;
686 ret = 0;
687 }
688 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400689out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400690 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400691 goto out;
692 rpc_delay(task, NFS4_POLL_RETRY_MAX);
693 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400694}
695
Trond Myklebust14516c32010-07-31 14:29:06 -0400696static int nfs4_sequence_done(struct rpc_task *task,
697 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400698{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500699 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400700 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400701 if (!res->sr_slot->table->session)
702 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400703 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400704}
705
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000706int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400707 struct nfs4_sequence_args *args,
708 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400709 struct rpc_task *task)
710{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400711 struct nfs4_slot *slot;
712 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400713
714 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400715 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400716 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400717 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400718
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400719 tbl = &session->fc_slot_table;
720
Trond Myklebust69d206b2012-11-22 13:21:02 -0500721 task->tk_timeout = 0;
722
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400723 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400724 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400725 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500726 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500727 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400728 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400729 }
730
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500731 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500732 if (IS_ERR(slot)) {
733 /* If out of memory, try again in 1/4 second */
734 if (slot == ERR_PTR(-ENOMEM))
735 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400736 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400737 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400738 }
739 spin_unlock(&tbl->slot_tbl_lock);
740
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500741 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400742
Chuck Levere8d92382013-08-09 12:47:51 -0400743 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500744 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400745
Benny Halevydfb4f3092010-09-24 09:17:01 -0400746 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500747 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400748 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400749 /*
750 * sr_status is only set in decode_sequence, and so will remain
751 * set to 1 if an rpc level failure occurs.
752 */
753 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400754 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400755out_success:
756 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400757 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400758out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400759 /* Privileged tasks are queued with top priority */
760 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400761 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
762 NULL, RPC_PRIORITY_PRIVILEGED);
763 else
764 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400765 spin_unlock(&tbl->slot_tbl_lock);
766 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400767}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000768EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400769
Chuck Lever5a580e02013-08-09 12:48:09 -0400770static int nfs4_setup_sequence(const struct nfs_server *server,
771 struct nfs4_sequence_args *args,
772 struct nfs4_sequence_res *res,
773 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400774{
Trond Myklebust035168a2010-06-16 09:52:26 -0400775 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400776 int ret = 0;
777
Chuck Lever3bd23842013-08-09 12:49:19 -0400778 if (!session)
779 return nfs40_setup_sequence(server, args, res, task);
Trond Myklebust035168a2010-06-16 09:52:26 -0400780
Chuck Levere8d92382013-08-09 12:47:51 -0400781 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400782 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400783 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168a2010-06-16 09:52:26 -0400784
Trond Myklebust9d12b212012-01-17 22:04:25 -0500785 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400786
Andy Adamsonce5039c2009-04-01 09:22:13 -0400787 dprintk("<-- %s status=%d\n", __func__, ret);
788 return ret;
789}
790
Andy Adamsonce5039c2009-04-01 09:22:13 -0400791static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
792{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400793 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400794 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400795
Trond Myklebust035168a2010-06-16 09:52:26 -0400796 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
797
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400798 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400799}
800
Andy Adamson69ab40c2009-04-01 09:22:19 -0400801static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
802{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400803 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400804
Trond Myklebust14516c32010-07-31 14:29:06 -0400805 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400806}
807
Trond Myklebust17280172012-03-11 13:11:00 -0400808static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400809 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400810 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400811};
812
Chuck Lever3bd23842013-08-09 12:49:19 -0400813#else /* !CONFIG_NFS_V4_1 */
814
Chuck Lever5a580e02013-08-09 12:48:09 -0400815static int nfs4_setup_sequence(const struct nfs_server *server,
816 struct nfs4_sequence_args *args,
817 struct nfs4_sequence_res *res,
818 struct rpc_task *task)
819{
Chuck Lever3bd23842013-08-09 12:49:19 -0400820 return nfs40_setup_sequence(server, args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -0400821}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400822
Trond Myklebust14516c32010-07-31 14:29:06 -0400823static int nfs4_sequence_done(struct rpc_task *task,
824 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400825{
Chuck Lever3bd23842013-08-09 12:49:19 -0400826 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400827}
Chuck Lever3bd23842013-08-09 12:49:19 -0400828
829#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400830
Chuck Lever9915ea72013-08-09 12:48:27 -0400831static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
832{
833 struct nfs4_call_sync_data *data = calldata;
834 nfs4_setup_sequence(data->seq_server,
835 data->seq_args, data->seq_res, task);
836}
837
838static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
839{
840 struct nfs4_call_sync_data *data = calldata;
841 nfs4_sequence_done(task, data->seq_res);
842}
843
844static const struct rpc_call_ops nfs40_call_sync_ops = {
845 .rpc_call_prepare = nfs40_call_sync_prepare,
846 .rpc_call_done = nfs40_call_sync_done,
847};
848
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400849static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -0400850 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500851 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100852 struct nfs4_sequence_args *args,
853 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -0400854{
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100855 int ret;
856 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -0400857 struct nfs_client *clp = server->nfs_client;
858 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100859 .seq_server = server,
860 .seq_args = args,
861 .seq_res = res,
862 };
863 struct rpc_task_setup task_setup = {
864 .rpc_client = clnt,
865 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -0400866 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100867 .callback_data = &data
868 };
869
870 task = rpc_run_task(&task_setup);
871 if (IS_ERR(task))
872 ret = PTR_ERR(task);
873 else {
874 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -0400875 rpc_put_task(task);
876 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100877 return ret;
878}
879
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400880static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000881int nfs4_call_sync(struct rpc_clnt *clnt,
882 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000883 struct rpc_message *msg,
884 struct nfs4_sequence_args *args,
885 struct nfs4_sequence_res *res,
886 int cache_reply)
887{
Chuck Levera9c92d62013-08-09 12:48:18 -0400888 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -0400889 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000890}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
893{
894 struct nfs_inode *nfsi = NFS_I(dir);
895
896 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400897 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700898 if (!cinfo->atomic || cinfo->before != dir->i_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700900 dir->i_version = cinfo->after;
David Howellsde242c02012-12-20 21:52:38 +0000901 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 spin_unlock(&dir->i_lock);
903}
904
905struct nfs4_opendata {
906 struct kref kref;
907 struct nfs_openargs o_arg;
908 struct nfs_openres o_res;
909 struct nfs_open_confirmargs c_arg;
910 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500911 struct nfs4_string owner_name;
912 struct nfs4_string group_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400914 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400916 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 struct nfs4_state_owner *owner;
918 struct nfs4_state *state;
919 struct iattr attrs;
920 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400921 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -0400922 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -0400923 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400924 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 int cancelled;
926};
Trond Myklebustdecf4912005-10-27 22:12:39 -0400927
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400928static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
929 int err, struct nfs4_exception *exception)
930{
931 if (err != -EINVAL)
932 return false;
933 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
934 return false;
935 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
936 exception->retry = 1;
937 return true;
938}
939
940static enum open_claim_type4
941nfs4_map_atomic_open_claim(struct nfs_server *server,
942 enum open_claim_type4 claim)
943{
944 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
945 return claim;
946 switch (claim) {
947 default:
948 return claim;
949 case NFS4_OPEN_CLAIM_FH:
950 return NFS4_OPEN_CLAIM_NULL;
951 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
952 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
953 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
954 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
955 }
956}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100959{
960 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400961 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100962 p->o_res.seqid = p->o_arg.seqid;
963 p->c_res.seqid = p->c_arg.seqid;
964 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400965 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100966 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500967 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100968}
969
Al Viro82a2c1b2011-06-22 18:30:55 -0400970static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100971 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400972 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -0400973 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400974 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400975 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100976{
Al Viro82a2c1b2011-06-22 18:30:55 -0400977 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100978 struct inode *dir = parent->d_inode;
979 struct nfs_server *server = NFS_SERVER(dir);
980 struct nfs4_opendata *p;
981
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400982 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100983 if (p == NULL)
984 goto err;
David Quigley14c43f72013-05-22 12:50:43 -0400985
986 p->f_label = nfs4_label_alloc(server, gfp_mask);
987 if (IS_ERR(p->f_label))
988 goto err_free_p;
989
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400990 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100991 if (p->o_arg.seqid == NULL)
David Quigley14c43f72013-05-22 12:50:43 -0400992 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -0400993 nfs_sb_active(dentry->d_sb);
994 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100995 p->dir = parent;
996 p->owner = sp;
997 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500998 p->o_arg.open_flags = flags;
999 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001000 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1001 * will return permission denied for all bits until close */
1002 if (!(flags & O_EXCL)) {
1003 /* ask server to check for all possible rights as results
1004 * are cached */
1005 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1006 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001007 }
David Howells7539bba2006-08-22 20:06:09 -04001008 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001009 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1010 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001011 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001012 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001013 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001014 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
David Quigley1775fd32013-05-22 12:50:42 -04001015 p->o_arg.label = label;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001016 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1017 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001018 case NFS4_OPEN_CLAIM_NULL:
1019 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1020 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1021 p->o_arg.fh = NFS_FH(dir);
1022 break;
1023 case NFS4_OPEN_CLAIM_PREVIOUS:
1024 case NFS4_OPEN_CLAIM_FH:
1025 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1026 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1027 p->o_arg.fh = NFS_FH(dentry->d_inode);
1028 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001029 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001030 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001031
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001032 p->o_arg.u.attrs = &p->attrs;
1033 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001034
1035 verf[0] = jiffies;
1036 verf[1] = current->pid;
1037 memcpy(p->o_arg.u.verifier.data, verf,
1038 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001039 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001040 p->c_arg.fh = &p->o_res.fh;
1041 p->c_arg.stateid = &p->o_res.stateid;
1042 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001043 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001044 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001045 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001046
1047err_free_label:
1048 nfs4_label_free(p->f_label);
1049err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001050 kfree(p);
1051err:
1052 dput(parent);
1053 return NULL;
1054}
1055
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001056static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001057{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001058 struct nfs4_opendata *p = container_of(kref,
1059 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001060 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001061
1062 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001063 if (p->state != NULL)
1064 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001065 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001066
1067 nfs4_label_free(p->f_label);
1068
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001069 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001070 dput(p->dentry);
1071 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001072 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001073 kfree(p);
1074}
1075
1076static void nfs4_opendata_put(struct nfs4_opendata *p)
1077{
1078 if (p != NULL)
1079 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001080}
1081
Trond Myklebust06f814a2006-01-03 09:55:07 +01001082static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1083{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001084 int ret;
1085
Trond Myklebust06f814a2006-01-03 09:55:07 +01001086 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001087 return ret;
1088}
1089
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001090static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001091{
1092 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001093
Trond Myklebust536e43d2012-01-17 22:04:26 -05001094 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001095 goto out;
1096 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001097 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001098 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1099 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001100 break;
1101 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001102 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1103 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001104 break;
1105 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001106 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1107 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001108 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001109out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001110 return ret;
1111}
1112
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001113static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001114{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001115 if (delegation == NULL)
1116 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001117 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001118 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -05001119 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -04001120 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001121 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1122 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -05001123 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001124 return 1;
1125}
1126
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001127static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001128{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001129 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001130 case FMODE_WRITE:
1131 state->n_wronly++;
1132 break;
1133 case FMODE_READ:
1134 state->n_rdonly++;
1135 break;
1136 case FMODE_READ|FMODE_WRITE:
1137 state->n_rdwr++;
1138 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001139 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001140}
1141
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001142static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001143{
1144 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001145 nfs4_stateid_copy(&state->stateid, stateid);
1146 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust92b40e92013-04-20 01:25:45 -04001147 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001148 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001149 case FMODE_READ:
1150 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1151 break;
1152 case FMODE_WRITE:
1153 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1154 break;
1155 case FMODE_READ|FMODE_WRITE:
1156 set_bit(NFS_O_RDWR_STATE, &state->flags);
1157 }
1158}
1159
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001160static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001161{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001162 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001163 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001164 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001165}
1166
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001167static 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 -07001168{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001169 /*
1170 * Protect the call to nfs4_state_set_mode_locked and
1171 * serialise the stateid update
1172 */
1173 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001174 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001175 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001176 set_bit(NFS_DELEGATED_STATE, &state->flags);
1177 }
1178 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001179 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001180 write_sequnlock(&state->seqlock);
1181 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001182 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001183 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184}
1185
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001186static 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 -05001187{
1188 struct nfs_inode *nfsi = NFS_I(state->inode);
1189 struct nfs_delegation *deleg_cur;
1190 int ret = 0;
1191
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001192 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001193
1194 rcu_read_lock();
1195 deleg_cur = rcu_dereference(nfsi->delegation);
1196 if (deleg_cur == NULL)
1197 goto no_delegation;
1198
1199 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001200 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001201 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001202 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001203 goto no_delegation_unlock;
1204
1205 if (delegation == NULL)
1206 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001207 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001208 goto no_delegation_unlock;
1209
Trond Myklebustb7391f42008-12-23 15:21:52 -05001210 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001211 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001212 ret = 1;
1213no_delegation_unlock:
1214 spin_unlock(&deleg_cur->lock);
1215no_delegation:
1216 rcu_read_unlock();
1217
1218 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001219 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001220 ret = 1;
1221 }
1222
1223 return ret;
1224}
1225
1226
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001227static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001228{
1229 struct nfs_delegation *delegation;
1230
1231 rcu_read_lock();
1232 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001233 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001234 rcu_read_unlock();
1235 return;
1236 }
1237 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001238 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001239}
1240
Trond Myklebust6ee41262007-07-08 14:11:36 -04001241static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001242{
1243 struct nfs4_state *state = opendata->state;
1244 struct nfs_inode *nfsi = NFS_I(state->inode);
1245 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001246 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001247 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001248 nfs4_stateid stateid;
1249 int ret = -EAGAIN;
1250
Trond Myklebustaac00a82007-07-05 19:02:21 -04001251 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001252 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001253 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001254 if (can_open_cached(state, fmode, open_mode)) {
1255 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001256 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001257 goto out_return_state;
1258 }
1259 spin_unlock(&state->owner->so_lock);
1260 }
Trond Myklebust34310432008-12-23 15:21:38 -05001261 rcu_read_lock();
1262 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001263 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001264 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001265 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001266 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001267 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001268 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001269 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001270 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001271 if (!opendata->is_recover) {
1272 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1273 if (ret != 0)
1274 goto out;
1275 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001276 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001277
1278 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001279 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001280 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001281 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001282out:
1283 return ERR_PTR(ret);
1284out_return_state:
1285 atomic_inc(&state->count);
1286 return state;
1287}
1288
Andy Adamsone23008e2012-10-02 21:07:32 -04001289static void
1290nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1291{
1292 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1293 struct nfs_delegation *delegation;
1294 int delegation_flags = 0;
1295
1296 rcu_read_lock();
1297 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1298 if (delegation)
1299 delegation_flags = delegation->flags;
1300 rcu_read_unlock();
1301 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1302 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1303 "returning a delegation for "
1304 "OPEN(CLAIM_DELEGATE_CUR)\n",
1305 clp->cl_hostname);
1306 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1307 nfs_inode_set_delegation(state->inode,
1308 data->owner->so_cred,
1309 &data->o_res);
1310 else
1311 nfs_inode_reclaim_delegation(state->inode,
1312 data->owner->so_cred,
1313 &data->o_res);
1314}
1315
1316/*
1317 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1318 * and update the nfs4_state.
1319 */
1320static struct nfs4_state *
1321_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1322{
1323 struct inode *inode = data->state->inode;
1324 struct nfs4_state *state = data->state;
1325 int ret;
1326
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001327 if (!data->rpc_done) {
1328 if (data->rpc_status) {
1329 ret = data->rpc_status;
1330 goto err;
1331 }
1332 /* cached opens have already been processed */
1333 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001334 }
1335
Andy Adamsone23008e2012-10-02 21:07:32 -04001336 ret = nfs_refresh_inode(inode, &data->f_attr);
1337 if (ret)
1338 goto err;
1339
1340 if (data->o_res.delegation_type != 0)
1341 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001342update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001343 update_open_stateid(state, &data->o_res.stateid, NULL,
1344 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001345 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001346
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 Myklebustdb4f2e632013-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;
Chuck Lever352297b2013-10-17 14:13:24 -04001578 case -NFS4ERR_MOVED:
1579 nfs4_schedule_migration_recovery(server);
1580 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001581 case -NFS4ERR_DELEG_REVOKED:
1582 case -NFS4ERR_ADMIN_REVOKED:
1583 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001584 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001585 nfs_inode_find_state_and_recover(state->inode,
1586 stateid);
1587 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001588 return 0;
1589 case -NFS4ERR_DELAY:
1590 case -NFS4ERR_GRACE:
1591 set_bit(NFS_DELEGATED_STATE, &state->flags);
1592 ssleep(1);
1593 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001594 case -ENOMEM:
1595 case -NFS4ERR_DENIED:
1596 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1597 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 return err;
1600}
1601
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001602int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1603{
1604 struct nfs_server *server = NFS_SERVER(state->inode);
1605 struct nfs4_opendata *opendata;
1606 int err;
1607
1608 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1609 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1610 if (IS_ERR(opendata))
1611 return PTR_ERR(opendata);
1612 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1613 err = nfs4_open_recover(opendata, state);
1614 nfs4_opendata_put(opendata);
1615 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1616}
1617
Chuck Leverbe05c862013-08-09 12:49:47 -04001618static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1619{
1620 struct nfs4_opendata *data = calldata;
1621
1622 nfs40_setup_sequence(data->o_arg.server, &data->o_arg.seq_args,
1623 &data->o_res.seq_res, task);
1624}
1625
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001626static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1627{
1628 struct nfs4_opendata *data = calldata;
1629
Chuck Leverbe05c862013-08-09 12:49:47 -04001630 nfs40_sequence_done(task, &data->o_res.seq_res);
1631
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001632 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001633 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001634 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001635 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001636 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001637 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001638 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001639}
1640
1641static void nfs4_open_confirm_release(void *calldata)
1642{
1643 struct nfs4_opendata *data = calldata;
1644 struct nfs4_state *state = NULL;
1645
1646 /* If this request hasn't been cancelled, do nothing */
1647 if (data->cancelled == 0)
1648 goto out_free;
1649 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001650 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001651 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001652 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001653 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001654 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001655out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001656 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001657}
1658
1659static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04001660 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001661 .rpc_call_done = nfs4_open_confirm_done,
1662 .rpc_release = nfs4_open_confirm_release,
1663};
1664
1665/*
1666 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1667 */
1668static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1669{
1670 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1671 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001672 struct rpc_message msg = {
1673 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1674 .rpc_argp = &data->c_arg,
1675 .rpc_resp = &data->c_res,
1676 .rpc_cred = data->owner->so_cred,
1677 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001678 struct rpc_task_setup task_setup_data = {
1679 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001680 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001681 .callback_ops = &nfs4_open_confirm_ops,
1682 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001683 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001684 .flags = RPC_TASK_ASYNC,
1685 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 int status;
1687
Chuck Leverbe05c862013-08-09 12:49:47 -04001688 nfs4_init_sequence(&data->o_arg.seq_args, &data->o_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001689 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001690 data->rpc_done = 0;
1691 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001692 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001693 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001694 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001695 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001696 status = nfs4_wait_for_completion_rpc_task(task);
1697 if (status != 0) {
1698 data->cancelled = 1;
1699 smp_wmb();
1700 } else
1701 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001702 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 return status;
1704}
1705
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001706static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001708 struct nfs4_opendata *data = calldata;
1709 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001710 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001711
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001712 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001713 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001714 /*
1715 * Check if we still need to send an OPEN call, or if we can use
1716 * a delegation instead.
1717 */
1718 if (data->state != NULL) {
1719 struct nfs_delegation *delegation;
1720
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001721 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001722 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001723 rcu_read_lock();
1724 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001725 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
Trond Myklebustcd4c9be2013-04-23 14:46:25 -04001726 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
Trond Myklebust652f89f2011-12-09 19:05:58 -05001727 can_open_delegated(delegation, data->o_arg.fmode))
1728 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001729 rcu_read_unlock();
1730 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001731 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04001732 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust8188df12013-04-23 14:31:19 -04001733 switch (data->o_arg.claim) {
1734 case NFS4_OPEN_CLAIM_PREVIOUS:
1735 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1736 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001737 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04001738 case NFS4_OPEN_CLAIM_FH:
1739 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001740 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1741 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001742 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001743 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001744 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001745 &data->o_res.seq_res,
1746 task) != 0)
1747 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04001748
1749 /* Set the create mode (note dependency on the session type) */
1750 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1751 if (data->o_arg.open_flags & O_EXCL) {
1752 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1753 if (nfs4_has_persistent_session(clp))
1754 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1755 else if (clp->cl_mvops->minor_version > 0)
1756 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1757 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04001758 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001759unlock_no_action:
1760 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001761out_no_action:
1762 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001763out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001764 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001765}
1766
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001767static void nfs4_open_done(struct rpc_task *task, void *calldata)
1768{
1769 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001771 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001772
Trond Myklebust14516c32010-07-31 14:29:06 -04001773 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1774 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001775
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001776 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001777 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1778 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001779 case S_IFREG:
1780 break;
1781 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001782 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001783 break;
1784 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001785 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001786 break;
1787 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001788 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001789 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001790 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001791 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001792 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1793 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001794 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001795 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001796}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001797
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001798static void nfs4_open_release(void *calldata)
1799{
1800 struct nfs4_opendata *data = calldata;
1801 struct nfs4_state *state = NULL;
1802
1803 /* If this request hasn't been cancelled, do nothing */
1804 if (data->cancelled == 0)
1805 goto out_free;
1806 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001807 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001808 goto out_free;
1809 /* In case we need an open_confirm, no cleanup! */
1810 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1811 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001812 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001813 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001814 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001815out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001816 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001817}
1818
1819static const struct rpc_call_ops nfs4_open_ops = {
1820 .rpc_call_prepare = nfs4_open_prepare,
1821 .rpc_call_done = nfs4_open_done,
1822 .rpc_release = nfs4_open_release,
1823};
1824
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001825static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001826{
1827 struct inode *dir = data->dir->d_inode;
1828 struct nfs_server *server = NFS_SERVER(dir);
1829 struct nfs_openargs *o_arg = &data->o_arg;
1830 struct nfs_openres *o_res = &data->o_res;
1831 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001832 struct rpc_message msg = {
1833 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1834 .rpc_argp = o_arg,
1835 .rpc_resp = o_res,
1836 .rpc_cred = data->owner->so_cred,
1837 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001838 struct rpc_task_setup task_setup_data = {
1839 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001840 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001841 .callback_ops = &nfs4_open_ops,
1842 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001843 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001844 .flags = RPC_TASK_ASYNC,
1845 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001846 int status;
1847
Chuck Levera9c92d62013-08-09 12:48:18 -04001848 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001849 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001850 data->rpc_done = 0;
1851 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001852 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001853 data->is_recover = 0;
1854 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001855 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001856 data->is_recover = 1;
1857 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04001858 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001859 if (IS_ERR(task))
1860 return PTR_ERR(task);
1861 status = nfs4_wait_for_completion_rpc_task(task);
1862 if (status != 0) {
1863 data->cancelled = 1;
1864 smp_wmb();
1865 } else
1866 status = data->rpc_status;
1867 rpc_put_task(task);
1868
1869 return status;
1870}
1871
1872static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1873{
1874 struct inode *dir = data->dir->d_inode;
1875 struct nfs_openres *o_res = &data->o_res;
1876 int status;
1877
1878 status = nfs4_run_open_task(data, 1);
1879 if (status != 0 || !data->rpc_done)
1880 return status;
1881
Trond Myklebust6926afd2012-01-07 13:22:46 -05001882 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1883
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001884 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1885 status = _nfs4_proc_open_confirm(data);
1886 if (status != 0)
1887 return status;
1888 }
1889
1890 return status;
1891}
1892
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001893static int nfs4_opendata_access(struct rpc_cred *cred,
1894 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001895 struct nfs4_state *state, fmode_t fmode,
1896 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001897{
1898 struct nfs_access_entry cache;
1899 u32 mask;
1900
1901 /* access call failed or for some reason the server doesn't
1902 * support any access modes -- defer access call until later */
1903 if (opendata->o_res.access_supported == 0)
1904 return 0;
1905
1906 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001907 /* don't check MAY_WRITE - a newly created file may not have
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001908 * write mode bits, but POSIX allows the creating process to write.
1909 * use openflags to check for exec, because fmode won't
1910 * always have FMODE_EXEC set when file open for exec. */
1911 if (openflags & __FMODE_EXEC) {
1912 /* ONLY check for exec rights */
1913 mask = MAY_EXEC;
1914 } else if (fmode & FMODE_READ)
1915 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001916
1917 cache.cred = cred;
1918 cache.jiffies = jiffies;
1919 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1920 nfs_access_add_cache(state->inode, &cache);
1921
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001922 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001923 return 0;
1924
1925 /* even though OPEN succeeded, access is denied. Close the file */
1926 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001927 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001928}
1929
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001930/*
1931 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1932 */
1933static int _nfs4_proc_open(struct nfs4_opendata *data)
1934{
1935 struct inode *dir = data->dir->d_inode;
1936 struct nfs_server *server = NFS_SERVER(dir);
1937 struct nfs_openargs *o_arg = &data->o_arg;
1938 struct nfs_openres *o_res = &data->o_res;
1939 int status;
1940
1941 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001942 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001943 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001944 if (status != 0) {
1945 if (status == -NFS4ERR_BADNAME &&
1946 !(o_arg->open_flags & O_CREAT))
1947 return -ENOENT;
1948 return status;
1949 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001950
Trond Myklebust6926afd2012-01-07 13:22:46 -05001951 nfs_fattr_map_and_free_names(server, &data->f_attr);
1952
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001953 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001954 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001955 if (o_arg->open_flags & O_EXCL)
1956 data->file_created = 1;
1957 else if (o_res->cinfo.before != o_res->cinfo.after)
1958 data->file_created = 1;
1959 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001960 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1961 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001963 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001965 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 }
1967 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
David Quigley1775fd32013-05-22 12:50:42 -04001968 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001969 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970}
1971
Andy Adamsond83217c2011-03-01 01:34:17 +00001972static int nfs4_recover_expired_lease(struct nfs_server *server)
1973{
1974 return nfs4_client_recover_expired_lease(server->nfs_client);
1975}
1976
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977/*
1978 * OPEN_EXPIRED:
1979 * reclaim state on the server after a network partition.
1980 * Assumes caller holds the appropriate lock
1981 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001982static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001984 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001985 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001987 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001988 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001989 if (IS_ERR(opendata))
1990 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001991 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001992 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001993 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001994 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001995 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996}
1997
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001998static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001999{
Trond Myklebust539cd032007-06-05 11:46:42 -04002000 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002001 struct nfs4_exception exception = { };
2002 int err;
2003
2004 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002005 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002006 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002007 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2008 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002009 switch (err) {
2010 default:
2011 goto out;
2012 case -NFS4ERR_GRACE:
2013 case -NFS4ERR_DELAY:
2014 nfs4_handle_exception(server, err, &exception);
2015 err = 0;
2016 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002017 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002018out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002019 return err;
2020}
2021
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2023{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002025 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
Trond Myklebust864472e2006-01-03 09:55:15 +01002027 ctx = nfs4_state_find_open_context(state);
2028 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002029 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002030 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002031 put_nfs_open_context(ctx);
2032 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033}
2034
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002035#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002036static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002037{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002038 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002039 nfs4_stateid *stateid = &state->stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002040 struct nfs_delegation *delegation;
2041 struct rpc_cred *cred = NULL;
2042 int status = -NFS4ERR_BAD_STATEID;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002043
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002044 /* If a state reset has been done, test_stateid is unneeded */
2045 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2046 return;
2047
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002048 /* Get the delegation credential for use by test/free_stateid */
2049 rcu_read_lock();
2050 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2051 if (delegation != NULL &&
2052 nfs4_stateid_match(&delegation->stateid, stateid)) {
2053 cred = get_rpccred(delegation->cred);
2054 rcu_read_unlock();
2055 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002056 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002057 } else
2058 rcu_read_unlock();
2059
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002060 if (status != NFS_OK) {
2061 /* Free the stateid unless the server explicitly
2062 * informs us the stateid is unrecognized. */
2063 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002064 nfs41_free_stateid(server, stateid, cred);
Bryan Schumaker69388672012-09-26 15:25:52 -04002065 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002066
Bryan Schumaker69388672012-09-26 15:25:52 -04002067 write_seqlock(&state->seqlock);
2068 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2069 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002070 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2071 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002072
2073 if (cred != NULL)
2074 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002075}
2076
2077/**
2078 * nfs41_check_open_stateid - possibly free an open stateid
2079 *
2080 * @state: NFSv4 state for an inode
2081 *
2082 * Returns NFS_OK if recovery for this stateid is now finished.
2083 * Otherwise a negative NFS4ERR value is returned.
2084 */
2085static int nfs41_check_open_stateid(struct nfs4_state *state)
2086{
2087 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002088 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002089 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002090 int status;
2091
2092 /* If a state reset has been done, test_stateid is unneeded */
2093 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2094 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2095 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2096 return -NFS4ERR_BAD_STATEID;
2097
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002098 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002099 trace_nfs4_test_open_stateid(state, NULL, status);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002100 if (status != NFS_OK) {
2101 /* Free the stateid unless the server explicitly
2102 * informs us the stateid is unrecognized. */
2103 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002104 nfs41_free_stateid(server, stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002105
2106 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2107 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2108 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002109 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002110 }
2111 return status;
2112}
2113
2114static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2115{
Chuck Levereb64cf92012-07-11 16:30:05 -04002116 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002117
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002118 nfs41_clear_delegation_stateid(state);
2119 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002120 if (status != NFS_OK)
2121 status = nfs4_open_expired(sp, state);
2122 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002123}
2124#endif
2125
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002127 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2128 * fields corresponding to attributes that were used to store the verifier.
2129 * Make sure we clobber those fields in the later setattr call
2130 */
2131static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2132{
2133 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2134 !(sattr->ia_valid & ATTR_ATIME_SET))
2135 sattr->ia_valid |= ATTR_ATIME;
2136
2137 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2138 !(sattr->ia_valid & ATTR_MTIME_SET))
2139 sattr->ia_valid |= ATTR_MTIME;
2140}
2141
Trond Myklebustc21443c2013-02-07 14:26:21 -05002142static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2143 fmode_t fmode,
2144 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002145 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002146{
2147 struct nfs4_state_owner *sp = opendata->owner;
2148 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002149 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002150 struct nfs4_state *state;
2151 unsigned int seq;
2152 int ret;
2153
2154 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2155
2156 ret = _nfs4_proc_open(opendata);
2157 if (ret != 0)
2158 goto out;
2159
2160 state = nfs4_opendata_to_nfs4_state(opendata);
2161 ret = PTR_ERR(state);
2162 if (IS_ERR(state))
2163 goto out;
2164 if (server->caps & NFS_CAP_POSIX_LOCK)
2165 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2166
Trond Myklebust275bb302013-05-29 13:11:28 -04002167 dentry = opendata->dentry;
2168 if (dentry->d_inode == NULL) {
2169 /* FIXME: Is this d_drop() ever needed? */
2170 d_drop(dentry);
2171 dentry = d_add_unique(dentry, igrab(state->inode));
2172 if (dentry == NULL) {
2173 dentry = opendata->dentry;
2174 } else if (dentry != ctx->dentry) {
2175 dput(ctx->dentry);
2176 ctx->dentry = dget(dentry);
2177 }
2178 nfs_set_verifier(dentry,
2179 nfs_save_change_attribute(opendata->dir->d_inode));
2180 }
2181
Trond Myklebustc21443c2013-02-07 14:26:21 -05002182 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2183 if (ret != 0)
2184 goto out;
2185
Trond Myklebust3efb9722013-05-29 13:17:04 -04002186 ctx->state = state;
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002187 if (dentry->d_inode == state->inode) {
2188 nfs_inode_attach_open_context(ctx);
2189 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2190 nfs4_schedule_stateid_recovery(server, state);
2191 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002192out:
2193 return ret;
2194}
2195
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002196/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002197 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 */
Andy Adamson82be4172012-05-23 05:02:35 -04002199static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002200 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002201 int flags,
2202 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002203 struct nfs4_label *label,
2204 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205{
2206 struct nfs4_state_owner *sp;
2207 struct nfs4_state *state = NULL;
2208 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002209 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002210 struct dentry *dentry = ctx->dentry;
2211 struct rpc_cred *cred = ctx->cred;
2212 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2213 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002214 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002215 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002216 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
2218 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002220 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2221 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2223 goto out_err;
2224 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002225 status = nfs4_recover_expired_lease(server);
2226 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002227 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04002228 if (dentry->d_inode != NULL)
2229 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002230 status = -ENOMEM;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002231 if (dentry->d_inode)
2232 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002233 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002234 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002235 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002236 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
David Quigley14c43f72013-05-22 12:50:43 -04002238 if (label) {
2239 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2240 if (IS_ERR(olabel)) {
2241 status = PTR_ERR(olabel);
2242 goto err_opendata_put;
2243 }
2244 }
2245
Andy Adamson82be4172012-05-23 05:02:35 -04002246 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2247 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2248 if (!opendata->f_attr.mdsthreshold)
David Quigley14c43f72013-05-22 12:50:43 -04002249 goto err_free_label;
Trond Myklebust1549210f2012-06-05 09:16:47 -04002250 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002251 }
Al Viro82a2c1b2011-06-22 18:30:55 -04002252 if (dentry->d_inode != NULL)
2253 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002254
Trond Myklebust3efb9722013-05-29 13:17:04 -04002255 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002256 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002257 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002258 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002259
Trond Myklebust549b19c2013-04-16 18:42:34 -04002260 if ((opendata->o_arg.open_flags & O_EXCL) &&
2261 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002262 nfs4_exclusive_attrset(opendata, sattr);
2263
2264 nfs_fattr_init(opendata->o_res.f_attr);
2265 status = nfs4_do_setattr(state->inode, cred,
2266 opendata->o_res.f_attr, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002267 state, label, olabel);
2268 if (status == 0) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002269 nfs_setattr_update_inode(state->inode, sattr);
David Quigley1775fd32013-05-22 12:50:42 -04002270 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
David Quigleyaa9c2662013-05-22 12:50:44 -04002271 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
David Quigley1775fd32013-05-22 12:50:42 -04002272 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002273 }
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002274 if (opendata->file_created)
2275 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002276
2277 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2278 *ctx_th = opendata->f_attr.mdsthreshold;
2279 else
2280 kfree(opendata->f_attr.mdsthreshold);
2281 opendata->f_attr.mdsthreshold = NULL;
2282
David Quigley14c43f72013-05-22 12:50:43 -04002283 nfs4_label_free(olabel);
2284
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002285 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002288err_free_label:
2289 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002290err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002291 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002292 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002293err_put_state_owner:
2294 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 return status;
2297}
2298
2299
Andy Adamson82be4172012-05-23 05:02:35 -04002300static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002301 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002302 int flags,
2303 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002304 struct nfs4_label *label,
2305 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002307 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 struct nfs4_exception exception = { };
2309 struct nfs4_state *res;
2310 int status;
2311
2312 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002313 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002314 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002315 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 if (status == 0)
2317 break;
2318 /* NOTE: BAD_SEQID means the server and client disagree about the
2319 * book-keeping w.r.t. state-changing operations
2320 * (OPEN/CLOSE/LOCK/LOCKU...)
2321 * It is actually a sign of a bug on the client or on the server.
2322 *
2323 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002324 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 * have unhashed the old state_owner for us, and that we can
2326 * therefore safely retry using a new one. We should still warn
2327 * the user though...
2328 */
2329 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002330 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002331 " returned a bad sequence-id error!\n",
2332 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 exception.retry = 1;
2334 continue;
2335 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002336 /*
2337 * BAD_STATEID on OPEN means that the server cancelled our
2338 * state before it received the OPEN_CONFIRM.
2339 * Recover by retrying the request as per the discussion
2340 * on Page 181 of RFC3530.
2341 */
2342 if (status == -NFS4ERR_BAD_STATEID) {
2343 exception.retry = 1;
2344 continue;
2345 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002346 if (status == -EAGAIN) {
2347 /* We must have found a delegation */
2348 exception.retry = 1;
2349 continue;
2350 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002351 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2352 continue;
2353 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 status, &exception));
2355 } while (exception.retry);
2356 return res;
2357}
2358
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002359static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2360 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002361 struct nfs4_state *state, struct nfs4_label *ilabel,
2362 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002364 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002366 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 .iap = sattr,
2368 .server = server,
2369 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002370 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 };
2372 struct nfs_setattrres res = {
2373 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002374 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 .server = server,
2376 };
2377 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002378 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2379 .rpc_argp = &arg,
2380 .rpc_resp = &res,
2381 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002383 unsigned long timestamp = jiffies;
Trond Myklebustee3ae842013-04-29 10:35:36 -04002384 fmode_t fmode;
2385 bool truncate;
Trond Myklebust65e43082005-08-16 11:49:44 -04002386 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
David Quigleyaa9c2662013-05-22 12:50:44 -04002388 arg.bitmask = nfs4_bitmask(server, ilabel);
2389 if (ilabel)
2390 arg.bitmask = nfs4_bitmask(server, olabel);
2391
Trond Myklebust0e574af2005-10-27 22:12:38 -04002392 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
Trond Myklebustee3ae842013-04-29 10:35:36 -04002394 /* Servers should only apply open mode checks for file size changes */
2395 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2396 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2397
2398 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2399 /* Use that stateid */
2400 } else if (truncate && state != NULL && nfs4_valid_open_stateid(state)) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002401 struct nfs_lockowner lockowner = {
2402 .l_owner = current->files,
2403 .l_pid = current->tgid,
2404 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002405 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002406 &lockowner);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002407 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002408 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
Bryan Schumaker7c513052011-03-24 17:12:24 +00002410 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002411 if (status == 0 && state != NULL)
2412 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002413 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414}
2415
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002416static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2417 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002418 struct nfs4_state *state, struct nfs4_label *ilabel,
2419 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002421 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002422 struct nfs4_exception exception = {
2423 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002424 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002425 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 int err;
2427 do {
David Quigley1775fd32013-05-22 12:50:42 -04002428 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
Trond Myklebustc1578b72013-08-12 16:58:42 -04002429 trace_nfs4_setattr(inode, err);
Trond Myklebust451146b2012-04-18 16:29:11 -04002430 switch (err) {
2431 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002432 if (!(sattr->ia_valid & ATTR_SIZE)) {
2433 pr_warn_once("NFSv4: server %s is incorrectly "
2434 "applying open mode checks to "
2435 "a SETATTR that is not "
2436 "changing file size.\n",
2437 server->nfs_client->cl_hostname);
2438 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002439 if (state && !(state->state & FMODE_WRITE)) {
2440 err = -EBADF;
2441 if (sattr->ia_valid & ATTR_OPEN)
2442 err = -EACCES;
2443 goto out;
2444 }
2445 }
2446 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002448out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 return err;
2450}
2451
2452struct nfs4_closedata {
2453 struct inode *inode;
2454 struct nfs4_state *state;
2455 struct nfs_closeargs arg;
2456 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002457 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002458 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002459 bool roc;
2460 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461};
2462
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002463static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002464{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002465 struct nfs4_closedata *calldata = data;
2466 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002467 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002468
Fred Isamanf7e89172011-01-06 11:36:32 +00002469 if (calldata->roc)
2470 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002471 nfs4_put_open_state(calldata->state);
2472 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002473 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002474 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002475 kfree(calldata);
2476}
2477
Trond Myklebust88069f72009-12-08 08:33:16 -05002478static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2479 fmode_t fmode)
2480{
2481 spin_lock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002482 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002483 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
2484 case FMODE_WRITE:
2485 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2486 break;
2487 case FMODE_READ:
2488 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2489 break;
2490 case 0:
2491 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2492 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2493 clear_bit(NFS_OPEN_STATE, &state->flags);
2494 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002495 spin_unlock(&state->owner->so_lock);
2496}
2497
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002498static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002500 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 struct nfs_server *server = NFS_SERVER(calldata->inode);
2503
Chuck Levera3ca5652012-03-01 17:00:40 -05002504 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002505 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2506 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002507 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 /* hmm. we are done with the inode, and in the process of freeing
2509 * the state_owner. we keep this around to process errors
2510 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 switch (task->tk_status) {
2512 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002513 if (calldata->roc)
2514 pnfs_roc_set_barrier(state->inode,
2515 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002516 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002517 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002518 nfs4_close_clear_stateid_flags(state,
2519 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 break;
2521 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002522 case -NFS4ERR_OLD_STATEID:
2523 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002525 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002526 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002528 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2529 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002531 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002532 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002533 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534}
2535
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002536static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002538 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002539 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002540 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002541 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002542
Chuck Levera3ca5652012-03-01 17:00:40 -05002543 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002544 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002545 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002546
Trond Myklebust88069f72009-12-08 08:33:16 -05002547 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2548 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002549 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002550 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002551 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002552 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002553 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2554 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2555 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002556 }
2557 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002558 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2559 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2560 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002561 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002562 }
Trond Myklebust5d422302013-03-14 16:57:48 -04002563 if (!nfs4_valid_open_stateid(state))
2564 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002565 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002566
2567 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002568 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002569 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002570 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002571
Fred Isamanf7e89172011-01-06 11:36:32 +00002572 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002573 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002574 if (calldata->roc &&
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002575 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2576 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002577 goto out_wait;
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002578 }
Fred Isamanf7e89172011-01-06 11:36:32 +00002579 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002580
Trond Myklebust516a6af2005-10-27 22:12:41 -04002581 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002582 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002583 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002584 &calldata->arg.seq_args,
2585 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002586 task) != 0)
2587 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002588 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002589 return;
2590out_no_action:
2591 task->tk_action = NULL;
2592out_wait:
2593 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594}
2595
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002596static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002597 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002598 .rpc_call_done = nfs4_close_done,
2599 .rpc_release = nfs4_free_closedata,
2600};
2601
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602/*
2603 * It is possible for data to be read/written from a mem-mapped file
2604 * after the sys_close call (which hits the vfs layer as a flush).
2605 * This means that we can't safely call nfsv4 close on a file until
2606 * the inode is cleared. This in turn means that we are not good
2607 * NFSv4 citizens - we do not indicate to the server to update the file's
2608 * share state even when we are done with one of the three share
2609 * stateid's in the inode.
2610 *
2611 * NOTE: Caller must be holding the sp->so_owner semaphore!
2612 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002613int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002615 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002617 struct nfs4_state_owner *sp = state->owner;
2618 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002619 struct rpc_message msg = {
2620 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2621 .rpc_cred = state->owner->so_cred,
2622 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002623 struct rpc_task_setup task_setup_data = {
2624 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002625 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002626 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002627 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002628 .flags = RPC_TASK_ASYNC,
2629 };
Trond Myklebust95121352005-10-18 14:20:12 -07002630 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04002632 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
2633 &task_setup_data.rpc_client, &msg);
2634
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002635 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002637 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04002638 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002639 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002641 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002642 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002644 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002645 if (calldata->arg.seqid == NULL)
2646 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002647 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002648 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002649 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002650 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002651 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002652 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002653 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002654
Trond Myklebust1174dd12010-12-21 10:52:24 -05002655 msg.rpc_argp = &calldata->arg;
2656 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002657 task_setup_data.callback_data = calldata;
2658 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002659 if (IS_ERR(task))
2660 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002661 status = 0;
2662 if (wait)
2663 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002664 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002665 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002666out_free_calldata:
2667 kfree(calldata);
2668out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002669 nfs4_put_open_state(state);
2670 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002671 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672}
2673
Trond Myklebust2b484292010-09-17 10:56:51 -04002674static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002675nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
2676 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04002679 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2680
2681 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682
Trond Myklebust565277f2007-10-15 18:17:53 -04002683 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002684 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04002685
2686 nfs4_label_release_security(label);
2687
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002688 if (IS_ERR(state))
2689 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04002690 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691}
2692
Trond Myklebust1185a552009-12-03 15:54:02 -05002693static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002694{
2695 if (ctx->state == NULL)
2696 return;
2697 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002698 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002699 else
Al Viro643168c2011-06-22 18:20:23 -04002700 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002701}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702
2703static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2704{
Benny Halevy43652ad2009-04-01 09:21:54 -04002705 struct nfs4_server_caps_arg args = {
2706 .fhandle = fhandle,
2707 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 struct nfs4_server_caps_res res = {};
2709 struct rpc_message msg = {
2710 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002711 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 .rpc_resp = &res,
2713 };
2714 int status;
2715
Bryan Schumaker7c513052011-03-24 17:12:24 +00002716 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 if (status == 0) {
2718 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002719 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2720 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2721 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2722 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2723 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2725 server->caps |= NFS_CAP_ACLS;
2726 if (res.has_links != 0)
2727 server->caps |= NFS_CAP_HARDLINKS;
2728 if (res.has_symlinks != 0)
2729 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002730 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2731 server->caps |= NFS_CAP_FILEID;
2732 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2733 server->caps |= NFS_CAP_MODE;
2734 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2735 server->caps |= NFS_CAP_NLINK;
2736 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2737 server->caps |= NFS_CAP_OWNER;
2738 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2739 server->caps |= NFS_CAP_OWNER_GROUP;
2740 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2741 server->caps |= NFS_CAP_ATIME;
2742 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2743 server->caps |= NFS_CAP_CTIME;
2744 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2745 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04002746#ifdef CONFIG_NFS_V4_SECURITY_LABEL
2747 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
2748 server->caps |= NFS_CAP_SECURITY_LABEL;
2749#endif
2750 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
2751 sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002752
David Quigleyaa9c2662013-05-22 12:50:44 -04002753 if (server->caps & NFS_CAP_SECURITY_LABEL) {
2754 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2755 res.attr_bitmask[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2756 }
Trond Myklebusta65318b2009-03-11 14:10:28 -04002757 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2758 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2759 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002761 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002763
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 return status;
2765}
2766
Trond Myklebust55a97592006-06-09 09:34:19 -04002767int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768{
2769 struct nfs4_exception exception = { };
2770 int err;
2771 do {
2772 err = nfs4_handle_exception(server,
2773 _nfs4_server_capabilities(server, fhandle),
2774 &exception);
2775 } while (exception.retry);
2776 return err;
2777}
2778
2779static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2780 struct nfs_fsinfo *info)
2781{
David Quigleyaa9c2662013-05-22 12:50:44 -04002782 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04002784 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 };
2786 struct nfs4_lookup_res res = {
2787 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002788 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 .fh = fhandle,
2790 };
2791 struct rpc_message msg = {
2792 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2793 .rpc_argp = &args,
2794 .rpc_resp = &res,
2795 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002796
David Quigleyaa9c2662013-05-22 12:50:44 -04002797 bitmask[0] = nfs4_fattr_bitmap[0];
2798 bitmask[1] = nfs4_fattr_bitmap[1];
2799 /*
2800 * Process the label in the upcoming getfattr
2801 */
2802 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
2803
Trond Myklebust0e574af2005-10-27 22:12:38 -04002804 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002805 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806}
2807
2808static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2809 struct nfs_fsinfo *info)
2810{
2811 struct nfs4_exception exception = { };
2812 int err;
2813 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002814 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04002815 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002816 switch (err) {
2817 case 0:
2818 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002819 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002820 default:
2821 err = nfs4_handle_exception(server, err, &exception);
2822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002824out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 return err;
2826}
2827
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002828static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2829 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2830{
Trond Myklebustc2190662013-08-26 19:23:04 -04002831 struct rpc_auth_create_args auth_args = {
2832 .pseudoflavor = flavor,
2833 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002834 struct rpc_auth *auth;
2835 int ret;
2836
Trond Myklebustc2190662013-08-26 19:23:04 -04002837 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002838 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04002839 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002840 goto out;
2841 }
2842 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002843out:
2844 return ret;
2845}
2846
Chuck Lever9a744ba2013-03-16 15:56:02 -04002847/*
2848 * Retry pseudoroot lookup with various security flavors. We do this when:
2849 *
2850 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2851 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2852 *
2853 * Returns zero on success, or a negative NFS4ERR value, or a
2854 * negative errno value.
2855 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002856static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002857 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858{
Chuck Lever9a744ba2013-03-16 15:56:02 -04002859 /* Per 3530bis 15.33.5 */
2860 static const rpc_authflavor_t flav_array[] = {
2861 RPC_AUTH_GSS_KRB5P,
2862 RPC_AUTH_GSS_KRB5I,
2863 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04002864 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04002865 RPC_AUTH_NULL,
2866 };
2867 int status = -EPERM;
2868 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
Chuck Lever9a744ba2013-03-16 15:56:02 -04002870 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002871 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002872 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002873 continue;
2874 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002875 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04002876
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002877 /*
2878 * -EACCESS could mean that the user doesn't have correct permissions
2879 * to access the mount. It could also mean that we tried to mount
2880 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2881 * existing mount programs don't handle -EACCES very well so it should
2882 * be mapped to -EPERM instead.
2883 */
2884 if (status == -EACCES)
2885 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002886 return status;
2887}
2888
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002889static int nfs4_do_find_root_sec(struct nfs_server *server,
2890 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
2891{
2892 int mv = server->nfs_client->cl_minorversion;
2893 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
2894}
2895
2896/**
2897 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
2898 * @server: initialized nfs_server handle
2899 * @fhandle: we fill in the pseudo-fs root file handle
2900 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002901 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002902 *
2903 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002904 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002905int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002906 struct nfs_fsinfo *info,
2907 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002908{
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002909 int status;
2910
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002911 switch (auth_probe) {
2912 case false:
2913 status = nfs4_lookup_root(server, fhandle, info);
2914 if (status != -NFS4ERR_WRONGSEC)
2915 break;
2916 /* Did user force a 'sec=' mount option? */
2917 if (server->flags & NFS_MOUNT_SECFLAVOUR)
2918 break;
2919 default:
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002920 status = nfs4_do_find_root_sec(server, fhandle, info);
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002921 }
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002922
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 if (status == 0)
2924 status = nfs4_server_capabilities(server, fhandle);
2925 if (status == 0)
2926 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002927
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002928 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929}
2930
Bryan Schumakerbae36242012-05-10 15:07:31 -04002931static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2932 struct nfs_fsinfo *info)
2933{
2934 int error;
2935 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04002936 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04002937
2938 error = nfs4_server_capabilities(server, mntfh);
2939 if (error < 0) {
2940 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2941 return error;
2942 }
2943
David Quigley14c43f72013-05-22 12:50:43 -04002944 label = nfs4_label_alloc(server, GFP_KERNEL);
2945 if (IS_ERR(label))
2946 return PTR_ERR(label);
2947
David Quigley1775fd32013-05-22 12:50:42 -04002948 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04002949 if (error < 0) {
2950 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04002951 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04002952 }
2953
2954 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2955 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2956 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2957
David Quigley14c43f72013-05-22 12:50:43 -04002958err_free_label:
2959 nfs4_label_free(label);
2960
Bryan Schumakerbae36242012-05-10 15:07:31 -04002961 return error;
2962}
2963
Manoj Naik6b97fd32006-06-09 09:34:29 -04002964/*
2965 * Get locations and (maybe) other attributes of a referral.
2966 * Note that we'll actually follow the referral later when
2967 * we detect fsid mismatch in inode revalidation
2968 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002969static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2970 const struct qstr *name, struct nfs_fattr *fattr,
2971 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002972{
2973 int status = -ENOMEM;
2974 struct page *page = NULL;
2975 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002976
2977 page = alloc_page(GFP_KERNEL);
2978 if (page == NULL)
2979 goto out;
2980 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2981 if (locations == NULL)
2982 goto out;
2983
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002984 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002985 if (status != 0)
2986 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04002987
2988 /*
2989 * If the fsid didn't change, this is a migration event, not a
2990 * referral. Cause us to drop into the exception handler, which
2991 * will kick off migration recovery.
2992 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002993 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002994 dprintk("%s: server did not return a different fsid for"
2995 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04002996 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002997 goto out;
2998 }
Andy Adamson533eb462011-06-13 18:25:56 -04002999 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3000 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003001
Andy Adamson533eb462011-06-13 18:25:56 -04003002 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003003 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003004 memset(fhandle, 0, sizeof(struct nfs_fh));
3005out:
3006 if (page)
3007 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003008 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003009 return status;
3010}
3011
David Quigley1775fd32013-05-22 12:50:42 -04003012static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3013 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014{
3015 struct nfs4_getattr_arg args = {
3016 .fh = fhandle,
3017 .bitmask = server->attr_bitmask,
3018 };
3019 struct nfs4_getattr_res res = {
3020 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003021 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 .server = server,
3023 };
3024 struct rpc_message msg = {
3025 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3026 .rpc_argp = &args,
3027 .rpc_resp = &res,
3028 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003029
3030 args.bitmask = nfs4_bitmask(server, label);
3031
Trond Myklebust0e574af2005-10-27 22:12:38 -04003032 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003033 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034}
3035
David Quigley1775fd32013-05-22 12:50:42 -04003036static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3037 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038{
3039 struct nfs4_exception exception = { };
3040 int err;
3041 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003042 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3043 trace_nfs4_getattr(server, fhandle, fattr, err);
3044 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 &exception);
3046 } while (exception.retry);
3047 return err;
3048}
3049
3050/*
3051 * The file is not closed if it is opened due to the a request to change
3052 * the size of the file. The open call will not be needed once the
3053 * VFS layer lookup-intents are implemented.
3054 *
3055 * Close is called when the inode is destroyed.
3056 * If we haven't opened the file for O_WRONLY, we
3057 * need to in the size_change case to obtain a stateid.
3058 *
3059 * Got race?
3060 * Because OPEN is always done by name in nfsv4, it is
3061 * possible that we opened a different file by the same
3062 * name. We can recognize this race condition, but we
3063 * can't do anything about it besides returning an error.
3064 *
3065 * This will be fixed with VFS changes (lookup-intent).
3066 */
3067static int
3068nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3069 struct iattr *sattr)
3070{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003071 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003072 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003073 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003074 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 int status;
3076
Benny Halevy8a1636c2010-07-14 15:43:57 -04003077 if (pnfs_ld_layoutret_on_setattr(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003078 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003079
Trond Myklebust0e574af2005-10-27 22:12:38 -04003080 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081
Andy Adamson26699402012-05-30 16:12:24 -04003082 /* Deal with open(O_TRUNC) */
3083 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003084 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003085
3086 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003087 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003088 return 0;
3089
Trond Myklebustd5308382005-11-04 15:33:38 -05003090 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003091 if (sattr->ia_valid & ATTR_FILE) {
3092 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003093
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003094 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003095 if (ctx) {
3096 cred = ctx->cred;
3097 state = ctx->state;
3098 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003099 }
3100
David Quigley14c43f72013-05-22 12:50:43 -04003101 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3102 if (IS_ERR(label))
3103 return PTR_ERR(label);
3104
3105 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003106 if (status == 0) {
Trond Myklebust65e43082005-08-16 11:49:44 -04003107 nfs_setattr_update_inode(inode, sattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003108 nfs_setsecurity(inode, fattr, label);
3109 }
David Quigley14c43f72013-05-22 12:50:43 -04003110 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 return status;
3112}
3113
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003114static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3115 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003116 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003117{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003118 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003119 int status;
3120 struct nfs4_lookup_arg args = {
3121 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003122 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003123 .name = name,
3124 };
3125 struct nfs4_lookup_res res = {
3126 .server = server,
3127 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003128 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003129 .fh = fhandle,
3130 };
3131 struct rpc_message msg = {
3132 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3133 .rpc_argp = &args,
3134 .rpc_resp = &res,
3135 };
3136
David Quigleyaa9c2662013-05-22 12:50:44 -04003137 args.bitmask = nfs4_bitmask(server, label);
3138
David Howells2b3de442006-08-22 20:06:09 -04003139 nfs_fattr_init(fattr);
3140
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003142 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 dprintk("NFS reply lookup: %d\n", status);
3144 return status;
3145}
3146
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003147static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003148{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003149 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003150 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003151 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3152 fattr->nlink = 2;
3153}
3154
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003155static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3156 struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003157 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003158{
3159 struct nfs4_exception exception = { };
3160 struct rpc_clnt *client = *clnt;
3161 int err;
3162 do {
David Quigley1775fd32013-05-22 12:50:42 -04003163 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003164 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003165 switch (err) {
3166 case -NFS4ERR_BADNAME:
3167 err = -ENOENT;
3168 goto out;
3169 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003170 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003171 goto out;
3172 case -NFS4ERR_WRONGSEC:
3173 err = -EPERM;
3174 if (client != *clnt)
3175 goto out;
Trond Myklebust41d058c2013-09-07 17:49:05 -04003176 /* No security negotiation if the user specified 'sec=' */
3177 if (NFS_SERVER(dir)->flags & NFS_MOUNT_SECFLAVOUR)
3178 goto out;
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003179 client = nfs4_create_sec_client(client, dir, name);
3180 if (IS_ERR(client))
3181 return PTR_ERR(client);
3182
3183 exception.retry = 1;
3184 break;
3185 default:
3186 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3187 }
3188 } while (exception.retry);
3189
3190out:
3191 if (err == 0)
3192 *clnt = client;
3193 else if (client != *clnt)
3194 rpc_shutdown_client(client);
3195
3196 return err;
3197}
3198
Bryan Schumaker80a16b22012-04-27 13:27:46 -04003199static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003200 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3201 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003203 int status;
3204 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003205
David Quigley1775fd32013-05-22 12:50:42 -04003206 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003207 if (client != NFS_CLIENT(dir)) {
3208 rpc_shutdown_client(client);
3209 nfs_fixup_secinfo_attributes(fattr);
3210 }
3211 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212}
3213
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003214struct rpc_clnt *
3215nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3216 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3217{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003218 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003219 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003220
David Quigley1775fd32013-05-22 12:50:42 -04003221 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003222 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003223 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003224 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003225}
3226
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3228{
Trond Myklebust76b32992007-08-10 17:45:11 -04003229 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 struct nfs4_accessargs args = {
3231 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003232 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003234 struct nfs4_accessres res = {
3235 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003236 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 struct rpc_message msg = {
3238 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3239 .rpc_argp = &args,
3240 .rpc_resp = &res,
3241 .rpc_cred = entry->cred,
3242 };
3243 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003244 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245
3246 /*
3247 * Determine which access bits we want to ask for...
3248 */
3249 if (mode & MAY_READ)
3250 args.access |= NFS4_ACCESS_READ;
3251 if (S_ISDIR(inode->i_mode)) {
3252 if (mode & MAY_WRITE)
3253 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3254 if (mode & MAY_EXEC)
3255 args.access |= NFS4_ACCESS_LOOKUP;
3256 } else {
3257 if (mode & MAY_WRITE)
3258 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3259 if (mode & MAY_EXEC)
3260 args.access |= NFS4_ACCESS_EXECUTE;
3261 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003262
3263 res.fattr = nfs_alloc_fattr();
3264 if (res.fattr == NULL)
3265 return -ENOMEM;
3266
Bryan Schumaker7c513052011-03-24 17:12:24 +00003267 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003269 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003270 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003272 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 return status;
3274}
3275
3276static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3277{
3278 struct nfs4_exception exception = { };
3279 int err;
3280 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003281 err = _nfs4_proc_access(inode, entry);
3282 trace_nfs4_access(inode, err);
3283 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 &exception);
3285 } while (exception.retry);
3286 return err;
3287}
3288
3289/*
3290 * TODO: For the time being, we don't try to get any attributes
3291 * along with any of the zero-copy operations READ, READDIR,
3292 * READLINK, WRITE.
3293 *
3294 * In the case of the first three, we want to put the GETATTR
3295 * after the read-type operation -- this is because it is hard
3296 * to predict the length of a GETATTR response in v4, and thus
3297 * align the READ data correctly. This means that the GETATTR
3298 * may end up partially falling into the page cache, and we should
3299 * shift it into the 'tail' of the xdr_buf before processing.
3300 * To do this efficiently, we need to know the total length
3301 * of data received, which doesn't seem to be available outside
3302 * of the RPC layer.
3303 *
3304 * In the case of WRITE, we also want to put the GETATTR after
3305 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003306 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 *
3308 * Both of these changes to the XDR layer would in fact be quite
3309 * minor, but I decided to leave them for a subsequent patch.
3310 */
3311static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3312 unsigned int pgbase, unsigned int pglen)
3313{
3314 struct nfs4_readlink args = {
3315 .fh = NFS_FH(inode),
3316 .pgbase = pgbase,
3317 .pglen = pglen,
3318 .pages = &page,
3319 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003320 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 struct rpc_message msg = {
3322 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3323 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003324 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 };
3326
Bryan Schumaker7c513052011-03-24 17:12:24 +00003327 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 -07003328}
3329
3330static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3331 unsigned int pgbase, unsigned int pglen)
3332{
3333 struct nfs4_exception exception = { };
3334 int err;
3335 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003336 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3337 trace_nfs4_readlink(inode, err);
3338 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 &exception);
3340 } while (exception.retry);
3341 return err;
3342}
3343
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003345 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347static int
3348nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003349 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350{
David Quigleyaa9c2662013-05-22 12:50:44 -04003351 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003352 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 struct nfs4_state *state;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003354 int opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 int status = 0;
3356
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003357 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3358 if (IS_ERR(ctx))
3359 return PTR_ERR(ctx);
3360
David Quigleyaa9c2662013-05-22 12:50:44 -04003361 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3362
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003363 sattr->ia_mode &= ~current_umask();
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003364 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, &opened);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 if (IS_ERR(state)) {
3366 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003367 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003370 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003371 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 return status;
3373}
3374
3375static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3376{
Trond Myklebust16e42952005-10-27 22:12:44 -04003377 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003378 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003380 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003382 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003383 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003384 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003386 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3387 .rpc_argp = &args,
3388 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003390 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003391
Bryan Schumaker7c513052011-03-24 17:12:24 +00003392 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003393 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003394 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 return status;
3396}
3397
3398static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3399{
3400 struct nfs4_exception exception = { };
3401 int err;
3402 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003403 err = _nfs4_proc_remove(dir, name);
3404 trace_nfs4_remove(dir, name, err);
3405 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 &exception);
3407 } while (exception.retry);
3408 return err;
3409}
3410
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003411static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003413 struct nfs_server *server = NFS_SERVER(dir);
3414 struct nfs_removeargs *args = msg->rpc_argp;
3415 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003417 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04003419 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003420
3421 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422}
3423
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003424static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3425{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003426 nfs4_setup_sequence(NFS_SERVER(data->dir),
3427 &data->args.seq_args,
3428 &data->res.seq_res,
3429 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430}
3431
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003432static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003434 struct nfs_unlinkdata *data = task->tk_calldata;
3435 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003436
Trond Myklebust14516c32010-07-31 14:29:06 -04003437 if (!nfs4_sequence_done(task, &res->seq_res))
3438 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003439 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003440 return 0;
3441 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003442 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443}
3444
Jeff Laytond3d41522010-09-17 17:31:57 -04003445static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3446{
3447 struct nfs_server *server = NFS_SERVER(dir);
3448 struct nfs_renameargs *arg = msg->rpc_argp;
3449 struct nfs_renameres *res = msg->rpc_resp;
3450
3451 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003452 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04003453 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003454}
3455
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003456static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3457{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003458 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3459 &data->args.seq_args,
3460 &data->res.seq_res,
3461 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003462}
3463
3464static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3465 struct inode *new_dir)
3466{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003467 struct nfs_renamedata *data = task->tk_calldata;
3468 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04003469
3470 if (!nfs4_sequence_done(task, &res->seq_res))
3471 return 0;
3472 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3473 return 0;
3474
3475 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003476 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003477 return 1;
3478}
3479
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3481 struct inode *new_dir, struct qstr *new_name)
3482{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003483 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003484 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 .old_dir = NFS_FH(old_dir),
3486 .new_dir = NFS_FH(new_dir),
3487 .old_name = old_name,
3488 .new_name = new_name,
3489 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003490 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003491 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003492 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 struct rpc_message msg = {
3494 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3495 .rpc_argp = &arg,
3496 .rpc_resp = &res,
3497 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003498 int status = -ENOMEM;
David Quigley14c43f72013-05-22 12:50:43 -04003499
Bryan Schumaker7c513052011-03-24 17:12:24 +00003500 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 if (!status) {
3502 update_changeattr(old_dir, &res.old_cinfo);
3503 update_changeattr(new_dir, &res.new_cinfo);
3504 }
3505 return status;
3506}
3507
3508static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3509 struct inode *new_dir, struct qstr *new_name)
3510{
3511 struct nfs4_exception exception = { };
3512 int err;
3513 do {
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003514 err = _nfs4_proc_rename(old_dir, old_name,
3515 new_dir, new_name);
3516 trace_nfs4_rename(old_dir, old_name, new_dir, new_name, err);
3517 err = nfs4_handle_exception(NFS_SERVER(old_dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 &exception);
3519 } while (exception.retry);
3520 return err;
3521}
3522
3523static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3524{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003525 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 struct nfs4_link_arg arg = {
3527 .fh = NFS_FH(inode),
3528 .dir_fh = NFS_FH(dir),
3529 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003530 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003532 struct nfs4_link_res res = {
3533 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003534 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003535 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 struct rpc_message msg = {
3537 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3538 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003539 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003541 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542
Trond Myklebust136f2622010-04-16 16:22:49 -04003543 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003544 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003545 goto out;
3546
David Quigley14c43f72013-05-22 12:50:43 -04003547 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3548 if (IS_ERR(res.label)) {
3549 status = PTR_ERR(res.label);
3550 goto out;
3551 }
David Quigleyaa9c2662013-05-22 12:50:44 -04003552 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04003553
Bryan Schumaker7c513052011-03-24 17:12:24 +00003554 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003555 if (!status) {
3556 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04003557 status = nfs_post_op_update_inode(inode, res.fattr);
3558 if (!status)
3559 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003560 }
David Quigley14c43f72013-05-22 12:50:43 -04003561
3562
3563 nfs4_label_free(res.label);
3564
Trond Myklebust136f2622010-04-16 16:22:49 -04003565out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003566 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 return status;
3568}
3569
3570static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3571{
3572 struct nfs4_exception exception = { };
3573 int err;
3574 do {
3575 err = nfs4_handle_exception(NFS_SERVER(inode),
3576 _nfs4_proc_link(inode, dir, name),
3577 &exception);
3578 } while (exception.retry);
3579 return err;
3580}
3581
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003582struct nfs4_createdata {
3583 struct rpc_message msg;
3584 struct nfs4_create_arg arg;
3585 struct nfs4_create_res res;
3586 struct nfs_fh fh;
3587 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003588 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003589};
3590
3591static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3592 struct qstr *name, struct iattr *sattr, u32 ftype)
3593{
3594 struct nfs4_createdata *data;
3595
3596 data = kzalloc(sizeof(*data), GFP_KERNEL);
3597 if (data != NULL) {
3598 struct nfs_server *server = NFS_SERVER(dir);
3599
David Quigley14c43f72013-05-22 12:50:43 -04003600 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3601 if (IS_ERR(data->label))
3602 goto out_free;
3603
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003604 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3605 data->msg.rpc_argp = &data->arg;
3606 data->msg.rpc_resp = &data->res;
3607 data->arg.dir_fh = NFS_FH(dir);
3608 data->arg.server = server;
3609 data->arg.name = name;
3610 data->arg.attrs = sattr;
3611 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04003612 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003613 data->res.server = server;
3614 data->res.fh = &data->fh;
3615 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003616 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003617 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003618 }
3619 return data;
David Quigley14c43f72013-05-22 12:50:43 -04003620out_free:
3621 kfree(data);
3622 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003623}
3624
3625static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3626{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003627 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003628 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003629 if (status == 0) {
3630 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04003631 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003632 }
3633 return status;
3634}
3635
3636static void nfs4_free_createdata(struct nfs4_createdata *data)
3637{
David Quigley14c43f72013-05-22 12:50:43 -04003638 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003639 kfree(data);
3640}
3641
Chuck Lever4f390c12006-08-22 20:06:22 -04003642static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003643 struct page *page, unsigned int len, struct iattr *sattr,
3644 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003646 struct nfs4_createdata *data;
3647 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648
Chuck Lever94a6d752006-08-22 20:06:23 -04003649 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003650 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003651
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003652 status = -ENOMEM;
3653 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3654 if (data == NULL)
3655 goto out;
3656
3657 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3658 data->arg.u.symlink.pages = &page;
3659 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04003660 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003662 status = nfs4_do_create(dir, dentry, data);
3663
3664 nfs4_free_createdata(data);
3665out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 return status;
3667}
3668
Chuck Lever4f390c12006-08-22 20:06:22 -04003669static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003670 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671{
3672 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003673 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04003675
3676 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3677
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003679 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3680 trace_nfs4_symlink(dir, &dentry->d_name, err);
3681 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 &exception);
3683 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003684
3685 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 return err;
3687}
3688
3689static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003690 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003692 struct nfs4_createdata *data;
3693 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003695 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3696 if (data == NULL)
3697 goto out;
3698
David Quigley1775fd32013-05-22 12:50:42 -04003699 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003700 status = nfs4_do_create(dir, dentry, data);
3701
3702 nfs4_free_createdata(data);
3703out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 return status;
3705}
3706
3707static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3708 struct iattr *sattr)
3709{
3710 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003711 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003713
David Quigleyaa9c2662013-05-22 12:50:44 -04003714 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3715
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003716 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003718 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3719 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3720 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 &exception);
3722 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003723 nfs4_label_release_security(label);
3724
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 return err;
3726}
3727
3728static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003729 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730{
3731 struct inode *dir = dentry->d_inode;
3732 struct nfs4_readdir_arg args = {
3733 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003734 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 .pgbase = 0,
3736 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003737 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003738 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 };
3740 struct nfs4_readdir_res res;
3741 struct rpc_message msg = {
3742 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3743 .rpc_argp = &args,
3744 .rpc_resp = &res,
3745 .rpc_cred = cred,
3746 };
3747 int status;
3748
Harvey Harrison3110ff82008-05-02 13:42:44 -07003749 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003750 dentry->d_parent->d_name.name,
3751 dentry->d_name.name,
3752 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003753 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003755 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 -05003756 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003757 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003758 status += args.pgbase;
3759 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003760
3761 nfs_invalidate_atime(dir);
3762
Harvey Harrison3110ff82008-05-02 13:42:44 -07003763 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 return status;
3765}
3766
3767static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003768 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769{
3770 struct nfs4_exception exception = { };
3771 int err;
3772 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003773 err = _nfs4_proc_readdir(dentry, cred, cookie,
3774 pages, count, plus);
3775 trace_nfs4_readdir(dentry->d_inode, err);
3776 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 &exception);
3778 } while (exception.retry);
3779 return err;
3780}
3781
3782static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04003783 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003785 struct nfs4_createdata *data;
3786 int mode = sattr->ia_mode;
3787 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003789 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3790 if (data == NULL)
3791 goto out;
3792
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003794 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003796 data->arg.ftype = NF4BLK;
3797 data->arg.u.device.specdata1 = MAJOR(rdev);
3798 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 }
3800 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003801 data->arg.ftype = NF4CHR;
3802 data->arg.u.device.specdata1 = MAJOR(rdev);
3803 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003804 } else if (!S_ISSOCK(mode)) {
3805 status = -EINVAL;
3806 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 }
David Quigley1775fd32013-05-22 12:50:42 -04003808
David Quigleyaa9c2662013-05-22 12:50:44 -04003809 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003810 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003811out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003812 nfs4_free_createdata(data);
3813out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 return status;
3815}
3816
3817static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3818 struct iattr *sattr, dev_t rdev)
3819{
3820 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003821 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003823
David Quigleyaa9c2662013-05-22 12:50:44 -04003824 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3825
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003826 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003828 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
3829 trace_nfs4_mknod(dir, &dentry->d_name, err);
3830 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 &exception);
3832 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003833
3834 nfs4_label_release_security(label);
3835
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 return err;
3837}
3838
3839static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3840 struct nfs_fsstat *fsstat)
3841{
3842 struct nfs4_statfs_arg args = {
3843 .fh = fhandle,
3844 .bitmask = server->attr_bitmask,
3845 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003846 struct nfs4_statfs_res res = {
3847 .fsstat = fsstat,
3848 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 struct rpc_message msg = {
3850 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3851 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003852 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 };
3854
Trond Myklebust0e574af2005-10-27 22:12:38 -04003855 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003856 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857}
3858
3859static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3860{
3861 struct nfs4_exception exception = { };
3862 int err;
3863 do {
3864 err = nfs4_handle_exception(server,
3865 _nfs4_proc_statfs(server, fhandle, fsstat),
3866 &exception);
3867 } while (exception.retry);
3868 return err;
3869}
3870
3871static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3872 struct nfs_fsinfo *fsinfo)
3873{
3874 struct nfs4_fsinfo_arg args = {
3875 .fh = fhandle,
3876 .bitmask = server->attr_bitmask,
3877 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003878 struct nfs4_fsinfo_res res = {
3879 .fsinfo = fsinfo,
3880 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 struct rpc_message msg = {
3882 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3883 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003884 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 };
3886
Bryan Schumaker7c513052011-03-24 17:12:24 +00003887 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888}
3889
3890static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3891{
3892 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04003893 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894 int err;
3895
3896 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04003897 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003898 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04003899 if (err == 0) {
3900 struct nfs_client *clp = server->nfs_client;
3901
3902 spin_lock(&clp->cl_lock);
3903 clp->cl_lease_time = fsinfo->lease_time * HZ;
3904 clp->cl_last_renewal = now;
3905 spin_unlock(&clp->cl_lock);
3906 break;
3907 }
3908 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 } while (exception.retry);
3910 return err;
3911}
3912
3913static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3914{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003915 int error;
3916
Trond Myklebust0e574af2005-10-27 22:12:38 -04003917 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003918 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003919 if (error == 0) {
3920 /* block layout checks this! */
3921 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003922 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003923 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003924
3925 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926}
3927
3928static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3929 struct nfs_pathconf *pathconf)
3930{
3931 struct nfs4_pathconf_arg args = {
3932 .fh = fhandle,
3933 .bitmask = server->attr_bitmask,
3934 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003935 struct nfs4_pathconf_res res = {
3936 .pathconf = pathconf,
3937 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 struct rpc_message msg = {
3939 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3940 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003941 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 };
3943
3944 /* None of the pathconf attributes are mandatory to implement */
3945 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3946 memset(pathconf, 0, sizeof(*pathconf));
3947 return 0;
3948 }
3949
Trond Myklebust0e574af2005-10-27 22:12:38 -04003950 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003951 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952}
3953
3954static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3955 struct nfs_pathconf *pathconf)
3956{
3957 struct nfs4_exception exception = { };
3958 int err;
3959
3960 do {
3961 err = nfs4_handle_exception(server,
3962 _nfs4_proc_pathconf(server, fhandle, pathconf),
3963 &exception);
3964 } while (exception.retry);
3965 return err;
3966}
3967
Trond Myklebust5521abf2013-03-16 20:54:34 -04003968int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04003969 const struct nfs_open_context *ctx,
3970 const struct nfs_lock_context *l_ctx,
3971 fmode_t fmode)
3972{
3973 const struct nfs_lockowner *lockowner = NULL;
3974
3975 if (l_ctx != NULL)
3976 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003977 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04003978}
3979EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
3980
Trond Myklebust5521abf2013-03-16 20:54:34 -04003981static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
3982 const struct nfs_open_context *ctx,
3983 const struct nfs_lock_context *l_ctx,
3984 fmode_t fmode)
3985{
3986 nfs4_stateid current_stateid;
3987
3988 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode))
3989 return false;
3990 return nfs4_stateid_match(stateid, &current_stateid);
3991}
3992
3993static bool nfs4_error_stateid_expired(int err)
3994{
3995 switch (err) {
3996 case -NFS4ERR_DELEG_REVOKED:
3997 case -NFS4ERR_ADMIN_REVOKED:
3998 case -NFS4ERR_BAD_STATEID:
3999 case -NFS4ERR_STALE_STATEID:
4000 case -NFS4ERR_OLD_STATEID:
4001 case -NFS4ERR_OPENMODE:
4002 case -NFS4ERR_EXPIRED:
4003 return true;
4004 }
4005 return false;
4006}
4007
Benny Halevyd20581a2011-05-22 19:52:03 +03004008void __nfs4_read_done_cb(struct nfs_read_data *data)
4009{
Fred Isamancd841602012-04-20 14:47:44 -04004010 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03004011}
4012
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004013static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014{
Fred Isamancd841602012-04-20 14:47:44 -04004015 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004017 trace_nfs4_read(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004018 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004019 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05004020 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004022
Benny Halevyd20581a2011-05-22 19:52:03 +03004023 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05004025 renew_lease(server, data->timestamp);
4026 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027}
4028
Trond Myklebust5521abf2013-03-16 20:54:34 -04004029static bool nfs4_read_stateid_changed(struct rpc_task *task,
4030 struct nfs_readargs *args)
4031{
4032
4033 if (!nfs4_error_stateid_expired(task->tk_status) ||
4034 nfs4_stateid_is_current(&args->stateid,
4035 args->context,
4036 args->lock_context,
4037 FMODE_READ))
4038 return false;
4039 rpc_restart_call_prepare(task);
4040 return true;
4041}
4042
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004043static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
4044{
4045
4046 dprintk("--> %s\n", __func__);
4047
4048 if (!nfs4_sequence_done(task, &data->res.seq_res))
4049 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004050 if (nfs4_read_stateid_changed(task, &data->args))
4051 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03004052 return data->read_done_cb ? data->read_done_cb(task, data) :
4053 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004054}
4055
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004056static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004059 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004060 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Chuck Levera9c92d62013-08-09 12:48:18 -04004061 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062}
4063
NeilBrownef1820f2013-09-04 17:04:49 +10004064static int nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004065{
Trond Myklebust9b206142013-03-17 15:52:00 -04004066 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004067 &data->args.seq_args,
4068 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004069 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004070 return 0;
4071 if (nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4072 data->args.lock_context, FMODE_READ) == -EIO)
4073 return -EIO;
4074 if (unlikely(test_bit(NFS_CONTEXT_BAD, &data->args.context->flags)))
4075 return -EIO;
4076 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077}
4078
Fred Isamanb029bc92011-03-03 15:13:42 +00004079static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080{
Fred Isamancd841602012-04-20 14:47:44 -04004081 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004083 trace_nfs4_write(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004084 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004085 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004086 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004088 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04004090 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004091 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004092 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093}
4094
Trond Myklebust5521abf2013-03-16 20:54:34 -04004095static bool nfs4_write_stateid_changed(struct rpc_task *task,
4096 struct nfs_writeargs *args)
4097{
4098
4099 if (!nfs4_error_stateid_expired(task->tk_status) ||
4100 nfs4_stateid_is_current(&args->stateid,
4101 args->context,
4102 args->lock_context,
4103 FMODE_WRITE))
4104 return false;
4105 rpc_restart_call_prepare(task);
4106 return true;
4107}
4108
Fred Isamanb029bc92011-03-03 15:13:42 +00004109static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
4110{
4111 if (!nfs4_sequence_done(task, &data->res.seq_res))
4112 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004113 if (nfs4_write_stateid_changed(task, &data->args))
4114 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03004115 return data->write_done_cb ? data->write_done_cb(task, data) :
4116 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00004117}
4118
Trond Myklebust5a37f852012-04-28 14:55:16 -04004119static
4120bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00004121{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004122 const struct nfs_pgio_header *hdr = data->header;
4123
4124 /* Don't request attributes for pNFS or O_DIRECT writes */
4125 if (data->ds_clp != NULL || hdr->dreq != NULL)
4126 return false;
4127 /* Otherwise, request attributes if and only if we don't hold
4128 * a delegation
4129 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004130 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004131}
Fred Isamana69aef12011-03-03 15:13:47 +00004132
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004133static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134{
Fred Isamancd841602012-04-20 14:47:44 -04004135 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004136
Trond Myklebust5a37f852012-04-28 14:55:16 -04004137 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00004138 data->args.bitmask = NULL;
4139 data->res.fattr = NULL;
4140 } else
4141 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004142
Fred Isamanb029bc92011-03-03 15:13:42 +00004143 if (!data->write_done_cb)
4144 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004145 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 data->timestamp = jiffies;
4147
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004148 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004149 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150}
4151
NeilBrownef1820f2013-09-04 17:04:49 +10004152static int nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04004153{
Trond Myklebust9b206142013-03-17 15:52:00 -04004154 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004155 &data->args.seq_args,
4156 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004157 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004158 return 0;
4159 if (nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4160 data->args.lock_context, FMODE_WRITE) == -EIO)
4161 return -EIO;
4162 if (unlikely(test_bit(NFS_CONTEXT_BAD, &data->args.context->flags)))
4163 return -EIO;
4164 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165}
4166
Fred Isaman0b7c0152012-04-20 14:47:39 -04004167static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4168{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004169 nfs4_setup_sequence(NFS_SERVER(data->inode),
4170 &data->args.seq_args,
4171 &data->res.seq_res,
4172 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173}
4174
Fred Isaman0b7c0152012-04-20 14:47:39 -04004175static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004178
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004179 trace_nfs4_commit(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004180 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004181 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004182 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004184 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185}
4186
Fred Isaman0b7c0152012-04-20 14:47:39 -04004187static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004188{
4189 if (!nfs4_sequence_done(task, &data->res.seq_res))
4190 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004191 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004192}
4193
Fred Isaman0b7c0152012-04-20 14:47:39 -04004194static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004196 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004197
Fred Isaman0b7c0152012-04-20 14:47:39 -04004198 if (data->commit_done_cb == NULL)
4199 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004200 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004201 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004202 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203}
4204
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004205struct nfs4_renewdata {
4206 struct nfs_client *client;
4207 unsigned long timestamp;
4208};
4209
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210/*
4211 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4212 * standalone procedure for queueing an asynchronous RENEW.
4213 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004214static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004215{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004216 struct nfs4_renewdata *data = calldata;
4217 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004218
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004219 if (atomic_read(&clp->cl_count) > 1)
4220 nfs4_schedule_state_renewal(clp);
4221 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004222 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004223}
4224
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004225static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004227 struct nfs4_renewdata *data = calldata;
4228 struct nfs_client *clp = data->client;
4229 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004231 trace_nfs4_renew_async(clp, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04004233 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004234 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4235 return;
4236 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004237 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004238 return;
4239 }
4240 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004242 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243}
4244
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004245static const struct rpc_call_ops nfs4_renew_ops = {
4246 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004247 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004248};
4249
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004250static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251{
4252 struct rpc_message msg = {
4253 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4254 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004255 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004257 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004259 if (renew_flags == 0)
4260 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004261 if (!atomic_inc_not_zero(&clp->cl_count))
4262 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004263 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004264 if (data == NULL)
4265 return -ENOMEM;
4266 data->client = clp;
4267 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004268 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004269 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270}
4271
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004272static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273{
4274 struct rpc_message msg = {
4275 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4276 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004277 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 };
4279 unsigned long now = jiffies;
4280 int status;
4281
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004282 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 if (status < 0)
4284 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004285 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 return 0;
4287}
4288
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004289static inline int nfs4_server_supports_acls(struct nfs_server *server)
4290{
4291 return (server->caps & NFS_CAP_ACLS)
4292 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
4293 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
4294}
4295
Trond Myklebust21f498c2012-08-24 10:59:25 -04004296/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4297 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004298 * the stack.
4299 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004300#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004301
Neil Hormane9e3d722011-03-04 19:26:03 -05004302static int buf_to_pages_noslab(const void *buf, size_t buflen,
4303 struct page **pages, unsigned int *pgbase)
4304{
4305 struct page *newpage, **spages;
4306 int rc = 0;
4307 size_t len;
4308 spages = pages;
4309
4310 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004311 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004312 newpage = alloc_page(GFP_KERNEL);
4313
4314 if (newpage == NULL)
4315 goto unwind;
4316 memcpy(page_address(newpage), buf, len);
4317 buf += len;
4318 buflen -= len;
4319 *pages++ = newpage;
4320 rc++;
4321 } while (buflen != 0);
4322
4323 return rc;
4324
4325unwind:
4326 for(; rc > 0; rc--)
4327 __free_page(spages[rc-1]);
4328 return -ENOMEM;
4329}
4330
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004331struct nfs4_cached_acl {
4332 int cached;
4333 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004334 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004335};
4336
4337static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004338{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004339 struct nfs_inode *nfsi = NFS_I(inode);
4340
4341 spin_lock(&inode->i_lock);
4342 kfree(nfsi->nfs4_acl);
4343 nfsi->nfs4_acl = acl;
4344 spin_unlock(&inode->i_lock);
4345}
4346
4347static void nfs4_zap_acl_attr(struct inode *inode)
4348{
4349 nfs4_set_cached_acl(inode, NULL);
4350}
4351
4352static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4353{
4354 struct nfs_inode *nfsi = NFS_I(inode);
4355 struct nfs4_cached_acl *acl;
4356 int ret = -ENOENT;
4357
4358 spin_lock(&inode->i_lock);
4359 acl = nfsi->nfs4_acl;
4360 if (acl == NULL)
4361 goto out;
4362 if (buf == NULL) /* user is just asking for length */
4363 goto out_len;
4364 if (acl->cached == 0)
4365 goto out;
4366 ret = -ERANGE; /* see getxattr(2) man page */
4367 if (acl->len > buflen)
4368 goto out;
4369 memcpy(buf, acl->data, acl->len);
4370out_len:
4371 ret = acl->len;
4372out:
4373 spin_unlock(&inode->i_lock);
4374 return ret;
4375}
4376
Sachin Prabhu5794d212012-04-17 14:36:40 +01004377static 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 +00004378{
4379 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004380 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004381
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004382 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004383 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004384 if (acl == NULL)
4385 goto out;
4386 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004387 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004388 } else {
4389 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4390 if (acl == NULL)
4391 goto out;
4392 acl->cached = 0;
4393 }
4394 acl->len = acl_len;
4395out:
4396 nfs4_set_cached_acl(inode, acl);
4397}
4398
Andy Adamsonbf118a32011-12-07 11:55:27 -05004399/*
4400 * The getxattr API returns the required buffer length when called with a
4401 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4402 * the required buf. On a NULL buf, we send a page of data to the server
4403 * guessing that the ACL request can be serviced by a page. If so, we cache
4404 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4405 * the cache. If not so, we throw away the page, and cache the required
4406 * length. The next getxattr call will then produce another round trip to
4407 * the server, this time with the input buf of the required size.
4408 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004409static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004410{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004411 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004412 struct nfs_getaclargs args = {
4413 .fh = NFS_FH(inode),
4414 .acl_pages = pages,
4415 .acl_len = buflen,
4416 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004417 struct nfs_getaclres res = {
4418 .acl_len = buflen,
4419 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004420 struct rpc_message msg = {
4421 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4422 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004423 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004424 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004425 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4426 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004427
Andy Adamsonbf118a32011-12-07 11:55:27 -05004428 /* As long as we're doing a round trip to the server anyway,
4429 * let's be prepared for a page of acl data. */
4430 if (npages == 0)
4431 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004432 if (npages > ARRAY_SIZE(pages))
4433 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004434
Andy Adamsonbf118a32011-12-07 11:55:27 -05004435 for (i = 0; i < npages; i++) {
4436 pages[i] = alloc_page(GFP_KERNEL);
4437 if (!pages[i])
4438 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004439 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004440
4441 /* for decoding across pages */
4442 res.acl_scratch = alloc_page(GFP_KERNEL);
4443 if (!res.acl_scratch)
4444 goto out_free;
4445
Andy Adamsonbf118a32011-12-07 11:55:27 -05004446 args.acl_len = npages * PAGE_SIZE;
4447 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004448
Peng Taode040be2012-01-10 22:42:47 +08004449 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004450 __func__, buf, buflen, npages, args.acl_len);
4451 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4452 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004453 if (ret)
4454 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004455
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004456 /* Handle the case where the passed-in buffer is too short */
4457 if (res.acl_flags & NFS4_ACL_TRUNC) {
4458 /* Did the user only issue a request for the acl length? */
4459 if (buf == NULL)
4460 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004461 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004462 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004463 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004464 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004465 if (buf) {
4466 if (res.acl_len > buflen) {
4467 ret = -ERANGE;
4468 goto out_free;
4469 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004470 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004471 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004472out_ok:
4473 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004474out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004475 for (i = 0; i < npages; i++)
4476 if (pages[i])
4477 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004478 if (res.acl_scratch)
4479 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004480 return ret;
4481}
4482
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004483static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4484{
4485 struct nfs4_exception exception = { };
4486 ssize_t ret;
4487 do {
4488 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04004489 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004490 if (ret >= 0)
4491 break;
4492 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4493 } while (exception.retry);
4494 return ret;
4495}
4496
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004497static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4498{
4499 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004500 int ret;
4501
4502 if (!nfs4_server_supports_acls(server))
4503 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004504 ret = nfs_revalidate_inode(server, inode);
4505 if (ret < 0)
4506 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004507 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4508 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004509 ret = nfs4_read_cached_acl(inode, buf, buflen);
4510 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004511 /* -ENOENT is returned if there is no ACL or if there is an ACL
4512 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004513 return ret;
4514 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004515}
4516
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004517static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004518{
4519 struct nfs_server *server = NFS_SERVER(inode);
4520 struct page *pages[NFS4ACL_MAXPAGES];
4521 struct nfs_setaclargs arg = {
4522 .fh = NFS_FH(inode),
4523 .acl_pages = pages,
4524 .acl_len = buflen,
4525 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004526 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004527 struct rpc_message msg = {
4528 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4529 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004530 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004531 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004532 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004533 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004534
4535 if (!nfs4_server_supports_acls(server))
4536 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004537 if (npages > ARRAY_SIZE(pages))
4538 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004539 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4540 if (i < 0)
4541 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004542 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004543 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004544
4545 /*
4546 * Free each page after tx, so the only ref left is
4547 * held by the network stack
4548 */
4549 for (; i > 0; i--)
4550 put_page(pages[i-1]);
4551
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004552 /*
4553 * Acl update can result in inode attribute update.
4554 * so mark the attribute cache invalid.
4555 */
4556 spin_lock(&inode->i_lock);
4557 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4558 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004559 nfs_access_zap_cache(inode);
4560 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004561 return ret;
4562}
4563
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004564static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4565{
4566 struct nfs4_exception exception = { };
4567 int err;
4568 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004569 err = __nfs4_proc_set_acl(inode, buf, buflen);
4570 trace_nfs4_set_acl(inode, err);
4571 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004572 &exception);
4573 } while (exception.retry);
4574 return err;
4575}
4576
David Quigleyaa9c2662013-05-22 12:50:44 -04004577#ifdef CONFIG_NFS_V4_SECURITY_LABEL
4578static int _nfs4_get_security_label(struct inode *inode, void *buf,
4579 size_t buflen)
4580{
4581 struct nfs_server *server = NFS_SERVER(inode);
4582 struct nfs_fattr fattr;
4583 struct nfs4_label label = {0, 0, buflen, buf};
4584
4585 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4586 struct nfs4_getattr_arg args = {
4587 .fh = NFS_FH(inode),
4588 .bitmask = bitmask,
4589 };
4590 struct nfs4_getattr_res res = {
4591 .fattr = &fattr,
4592 .label = &label,
4593 .server = server,
4594 };
4595 struct rpc_message msg = {
4596 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4597 .rpc_argp = &args,
4598 .rpc_resp = &res,
4599 };
4600 int ret;
4601
4602 nfs_fattr_init(&fattr);
4603
4604 ret = rpc_call_sync(server->client, &msg, 0);
4605 if (ret)
4606 return ret;
4607 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4608 return -ENOENT;
4609 if (buflen < label.len)
4610 return -ERANGE;
4611 return 0;
4612}
4613
4614static int nfs4_get_security_label(struct inode *inode, void *buf,
4615 size_t buflen)
4616{
4617 struct nfs4_exception exception = { };
4618 int err;
4619
4620 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4621 return -EOPNOTSUPP;
4622
4623 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004624 err = _nfs4_get_security_label(inode, buf, buflen);
4625 trace_nfs4_get_security_label(inode, err);
4626 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004627 &exception);
4628 } while (exception.retry);
4629 return err;
4630}
4631
4632static int _nfs4_do_set_security_label(struct inode *inode,
4633 struct nfs4_label *ilabel,
4634 struct nfs_fattr *fattr,
4635 struct nfs4_label *olabel)
4636{
4637
4638 struct iattr sattr = {0};
4639 struct nfs_server *server = NFS_SERVER(inode);
4640 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4641 struct nfs_setattrargs args = {
4642 .fh = NFS_FH(inode),
4643 .iap = &sattr,
4644 .server = server,
4645 .bitmask = bitmask,
4646 .label = ilabel,
4647 };
4648 struct nfs_setattrres res = {
4649 .fattr = fattr,
4650 .label = olabel,
4651 .server = server,
4652 };
4653 struct rpc_message msg = {
4654 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
4655 .rpc_argp = &args,
4656 .rpc_resp = &res,
4657 };
4658 int status;
4659
4660 nfs4_stateid_copy(&args.stateid, &zero_stateid);
4661
4662 status = rpc_call_sync(server->client, &msg, 0);
4663 if (status)
4664 dprintk("%s failed: %d\n", __func__, status);
4665
4666 return status;
4667}
4668
4669static int nfs4_do_set_security_label(struct inode *inode,
4670 struct nfs4_label *ilabel,
4671 struct nfs_fattr *fattr,
4672 struct nfs4_label *olabel)
4673{
4674 struct nfs4_exception exception = { };
4675 int err;
4676
4677 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004678 err = _nfs4_do_set_security_label(inode, ilabel,
4679 fattr, olabel);
4680 trace_nfs4_set_security_label(inode, err);
4681 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004682 &exception);
4683 } while (exception.retry);
4684 return err;
4685}
4686
4687static int
4688nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4689{
4690 struct nfs4_label ilabel, *olabel = NULL;
4691 struct nfs_fattr fattr;
4692 struct rpc_cred *cred;
4693 struct inode *inode = dentry->d_inode;
4694 int status;
4695
4696 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4697 return -EOPNOTSUPP;
4698
4699 nfs_fattr_init(&fattr);
4700
4701 ilabel.pi = 0;
4702 ilabel.lfs = 0;
4703 ilabel.label = (char *)buf;
4704 ilabel.len = buflen;
4705
4706 cred = rpc_lookup_cred();
4707 if (IS_ERR(cred))
4708 return PTR_ERR(cred);
4709
4710 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4711 if (IS_ERR(olabel)) {
4712 status = -PTR_ERR(olabel);
4713 goto out;
4714 }
4715
4716 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4717 if (status == 0)
4718 nfs_setsecurity(inode, &fattr, olabel);
4719
4720 nfs4_label_free(olabel);
4721out:
4722 put_rpccred(cred);
4723 return status;
4724}
4725#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4726
4727
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004729nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004731 struct nfs_client *clp = server->nfs_client;
4732
4733 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 return 0;
4735 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004736 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004737 case -NFS4ERR_ADMIN_REVOKED:
4738 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004739 if (state == NULL)
4740 break;
4741 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004742 case -NFS4ERR_OPENMODE:
4743 if (state == NULL)
4744 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004745 if (nfs4_schedule_stateid_recovery(server, state) < 0)
Chuck Lever9f51a782013-10-17 14:13:13 -04004746 goto recovery_failed;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004747 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004748 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004749 if (state != NULL) {
4750 if (nfs4_schedule_stateid_recovery(server, state) < 0)
Chuck Lever9f51a782013-10-17 14:13:13 -04004751 goto recovery_failed;
Trond Myklebust5d422302013-03-14 16:57:48 -04004752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004754 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004755 nfs4_schedule_lease_recovery(clp);
4756 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -04004757 case -NFS4ERR_MOVED:
4758 if (nfs4_schedule_migration_recovery(server) < 0)
4759 goto recovery_failed;
4760 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004761#if defined(CONFIG_NFS_V4_1)
4762 case -NFS4ERR_BADSESSION:
4763 case -NFS4ERR_BADSLOT:
4764 case -NFS4ERR_BAD_HIGH_SLOT:
4765 case -NFS4ERR_DEADSESSION:
4766 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4767 case -NFS4ERR_SEQ_FALSE_RETRY:
4768 case -NFS4ERR_SEQ_MISORDERED:
4769 dprintk("%s ERROR %d, Reset session\n", __func__,
4770 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004771 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Chuck Leverf1478c12013-10-17 14:13:07 -04004772 goto restart_call;
Andy Adamson4745e312009-04-01 09:22:42 -04004773#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004775 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004776 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004778 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 case -NFS4ERR_OLD_STATEID:
Chuck Leverf1478c12013-10-17 14:13:07 -04004780 goto restart_call;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 }
4782 task->tk_status = nfs4_map_errors(task->tk_status);
4783 return 0;
Chuck Lever9f51a782013-10-17 14:13:13 -04004784recovery_failed:
Trond Myklebust5d422302013-03-14 16:57:48 -04004785 task->tk_status = -EIO;
4786 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004787wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004788 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004789 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4790 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
Chuck Lever519ae252013-10-17 14:13:19 -04004791 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
4792 goto recovery_failed;
Chuck Leverf1478c12013-10-17 14:13:07 -04004793restart_call:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004794 task->tk_status = 0;
4795 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796}
4797
Chuck Leverf0920752012-05-21 22:45:41 -04004798static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4799 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004800{
4801 __be32 verf[2];
4802
Chuck Lever2c820d92012-05-21 22:45:33 -04004803 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4804 /* An impossible timestamp guarantees this value
4805 * will never match a generated boot time. */
4806 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04004807 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04004808 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004809 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04004810 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
4811 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04004812 }
Chuck Levercd937102012-03-02 17:14:31 -05004813 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4814}
4815
Chuck Levere984a552012-09-14 17:24:21 -04004816static unsigned int
4817nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4818 char *buf, size_t len)
4819{
4820 unsigned int result;
4821
4822 rcu_read_lock();
4823 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4824 clp->cl_ipaddr,
4825 rpc_peeraddr2str(clp->cl_rpcclient,
4826 RPC_DISPLAY_ADDR),
4827 rpc_peeraddr2str(clp->cl_rpcclient,
4828 RPC_DISPLAY_PROTO));
4829 rcu_read_unlock();
4830 return result;
4831}
4832
4833static unsigned int
4834nfs4_init_uniform_client_string(const struct nfs_client *clp,
4835 char *buf, size_t len)
4836{
Trond Myklebust55b59292013-07-24 16:36:35 -04004837 const char *nodename = clp->cl_rpcclient->cl_nodename;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004838
4839 if (nfs4_client_id_uniquifier[0] != '\0')
Trond Myklebust55b59292013-07-24 16:36:35 -04004840 return scnprintf(buf, len, "Linux NFSv%u.%u %s/%s",
4841 clp->rpc_ops->version,
4842 clp->cl_minorversion,
4843 nfs4_client_id_uniquifier,
4844 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004845 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4846 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004847 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004848}
4849
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004850/**
4851 * nfs4_proc_setclientid - Negotiate client ID
4852 * @clp: state data structure
4853 * @program: RPC program for NFSv4 callback service
4854 * @port: IP port number for NFS4 callback service
4855 * @cred: RPC credential to use for this call
4856 * @res: where to place the result
4857 *
4858 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4859 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004860int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4861 unsigned short port, struct rpc_cred *cred,
4862 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863{
4864 nfs4_verifier sc_verifier;
4865 struct nfs4_setclientid setclientid = {
4866 .sc_verifier = &sc_verifier,
4867 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004868 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869 };
4870 struct rpc_message msg = {
4871 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4872 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004873 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004874 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004876 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877
Chuck Leverde734832012-07-11 16:30:50 -04004878 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004879 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004880 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4881 setclientid.sc_name_len =
4882 nfs4_init_uniform_client_string(clp,
4883 setclientid.sc_name,
4884 sizeof(setclientid.sc_name));
4885 else
4886 setclientid.sc_name_len =
4887 nfs4_init_nonuniform_client_string(clp,
4888 setclientid.sc_name,
4889 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004890 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004891 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004892 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Djalal Harounife2d5392013-06-18 17:58:12 +01004893 sizeof(setclientid.sc_netid), "%s",
Chuck Leverd4d3c502007-12-10 14:57:09 -05004894 rpc_peeraddr2str(clp->cl_rpcclient,
4895 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004896 rcu_read_unlock();
4897 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004898 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899 clp->cl_ipaddr, port >> 8, port & 255);
4900
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004901 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4902 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4903 setclientid.sc_name_len, setclientid.sc_name);
4904 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004905 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004906 dprintk("NFS reply setclientid: %d\n", status);
4907 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908}
4909
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004910/**
4911 * nfs4_proc_setclientid_confirm - Confirm client ID
4912 * @clp: state data structure
4913 * @res: result of a previous SETCLIENTID
4914 * @cred: RPC credential to use for this call
4915 *
4916 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4917 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004918int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004919 struct nfs4_setclientid_res *arg,
4920 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 struct rpc_message msg = {
4923 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004924 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004925 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 int status;
4928
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004929 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4930 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4931 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004932 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004933 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004934 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935 return status;
4936}
4937
Trond Myklebustfe650402006-01-03 09:55:18 +01004938struct nfs4_delegreturndata {
4939 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004940 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004941 struct nfs_fh fh;
4942 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004943 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004944 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004945 int rpc_status;
4946};
4947
Trond Myklebustfe650402006-01-03 09:55:18 +01004948static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4949{
4950 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004951
Trond Myklebust14516c32010-07-31 14:29:06 -04004952 if (!nfs4_sequence_done(task, &data->res.seq_res))
4953 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004954
Trond Myklebustca8acf82013-08-13 10:36:56 -04004955 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004956 switch (task->tk_status) {
4957 case -NFS4ERR_STALE_STATEID:
4958 case -NFS4ERR_EXPIRED:
4959 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004960 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004961 break;
4962 default:
4963 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4964 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004965 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004966 return;
4967 }
4968 }
4969 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004970}
4971
4972static void nfs4_delegreturn_release(void *calldata)
4973{
Trond Myklebustfe650402006-01-03 09:55:18 +01004974 kfree(calldata);
4975}
4976
Andy Adamson938e1012009-04-01 09:22:28 -04004977static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4978{
4979 struct nfs4_delegreturndata *d_data;
4980
4981 d_data = (struct nfs4_delegreturndata *)data;
4982
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004983 nfs4_setup_sequence(d_data->res.server,
4984 &d_data->args.seq_args,
4985 &d_data->res.seq_res,
4986 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004987}
Andy Adamson938e1012009-04-01 09:22:28 -04004988
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004989static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004990 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01004991 .rpc_call_done = nfs4_delegreturn_done,
4992 .rpc_release = nfs4_delegreturn_release,
4993};
4994
Trond Myklebuste6f81072008-01-24 18:14:34 -05004995static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004996{
4997 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004998 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004999 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005000 struct rpc_message msg = {
5001 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5002 .rpc_cred = cred,
5003 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005004 struct rpc_task_setup task_setup_data = {
5005 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005006 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005007 .callback_ops = &nfs4_delegreturn_ops,
5008 .flags = RPC_TASK_ASYNC,
5009 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005010 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005011
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005012 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005013 if (data == NULL)
5014 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005015 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01005016 data->args.fhandle = &data->fh;
5017 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005018 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005019 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005020 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005021 data->res.fattr = &data->fattr;
5022 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005023 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005024 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005025 data->rpc_status = 0;
5026
Trond Myklebustc970aa82007-07-14 15:39:59 -04005027 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005028 msg.rpc_argp = &data->args;
5029 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005030 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005031 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005032 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005033 if (!issync)
5034 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005035 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005036 if (status != 0)
5037 goto out;
5038 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005039 if (status == 0)
5040 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5041 else
5042 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005043out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005044 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005045 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046}
5047
Trond Myklebuste6f81072008-01-24 18:14:34 -05005048int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049{
5050 struct nfs_server *server = NFS_SERVER(inode);
5051 struct nfs4_exception exception = { };
5052 int err;
5053 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005054 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Trond Myklebustca8acf82013-08-13 10:36:56 -04005055 trace_nfs4_delegreturn(inode, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 switch (err) {
5057 case -NFS4ERR_STALE_STATEID:
5058 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 case 0:
5060 return 0;
5061 }
5062 err = nfs4_handle_exception(server, err, &exception);
5063 } while (exception.retry);
5064 return err;
5065}
5066
5067#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5068#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5069
5070/*
5071 * sleep, with exponential backoff, and retry the LOCK operation.
5072 */
5073static unsigned long
5074nfs4_set_lock_task_retry(unsigned long timeout)
5075{
Colin Cross416ad3c2013-05-06 23:50:06 +00005076 freezable_schedule_timeout_killable_unsafe(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077 timeout <<= 1;
5078 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5079 return NFS4_LOCK_MAXTIMEOUT;
5080 return timeout;
5081}
5082
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5084{
5085 struct inode *inode = state->inode;
5086 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005087 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005088 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005090 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005092 struct nfs_lockt_res res = {
5093 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094 };
5095 struct rpc_message msg = {
5096 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5097 .rpc_argp = &arg,
5098 .rpc_resp = &res,
5099 .rpc_cred = state->owner->so_cred,
5100 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 struct nfs4_lock_state *lsp;
5102 int status;
5103
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005104 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005105 status = nfs4_set_lock_state(state, request);
5106 if (status != 0)
5107 goto out;
5108 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005109 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005110 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005111 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005112 switch (status) {
5113 case 0:
5114 request->fl_type = F_UNLCK;
5115 break;
5116 case -NFS4ERR_DENIED:
5117 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005119 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005120 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005121out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122 return status;
5123}
5124
5125static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5126{
5127 struct nfs4_exception exception = { };
5128 int err;
5129
5130 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005131 err = _nfs4_proc_getlk(state, cmd, request);
5132 trace_nfs4_get_lock(request, state, cmd, err);
5133 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134 &exception);
5135 } while (exception.retry);
5136 return err;
5137}
5138
5139static int do_vfs_lock(struct file *file, struct file_lock *fl)
5140{
5141 int res = 0;
5142 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5143 case FL_POSIX:
5144 res = posix_lock_file_wait(file, fl);
5145 break;
5146 case FL_FLOCK:
5147 res = flock_lock_file_wait(file, fl);
5148 break;
5149 default:
5150 BUG();
5151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152 return res;
5153}
5154
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005155struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005156 struct nfs_locku_args arg;
5157 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005158 struct nfs4_lock_state *lsp;
5159 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005160 struct file_lock fl;
5161 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005162 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005163};
5164
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005165static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5166 struct nfs_open_context *ctx,
5167 struct nfs4_lock_state *lsp,
5168 struct nfs_seqid *seqid)
5169{
5170 struct nfs4_unlockdata *p;
5171 struct inode *inode = lsp->ls_state->inode;
5172
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005173 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005174 if (p == NULL)
5175 return NULL;
5176 p->arg.fh = NFS_FH(inode);
5177 p->arg.fl = &p->fl;
5178 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005179 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005180 p->arg.stateid = &lsp->ls_stateid;
5181 p->lsp = lsp;
5182 atomic_inc(&lsp->ls_count);
5183 /* Ensure we don't close file until we're done freeing locks! */
5184 p->ctx = get_nfs_open_context(ctx);
5185 memcpy(&p->fl, fl, sizeof(p->fl));
5186 p->server = NFS_SERVER(inode);
5187 return p;
5188}
5189
Trond Myklebust06f814a2006-01-03 09:55:07 +01005190static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005191{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005192 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005193 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005194 nfs4_put_lock_state(calldata->lsp);
5195 put_nfs_open_context(calldata->ctx);
5196 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005197}
5198
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005199static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005200{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005201 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005202
Trond Myklebust14516c32010-07-31 14:29:06 -04005203 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5204 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005205 switch (task->tk_status) {
5206 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05005207 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
5208 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005209 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005210 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005211 case -NFS4ERR_BAD_STATEID:
5212 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005213 case -NFS4ERR_STALE_STATEID:
5214 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005215 break;
5216 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005217 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005218 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005219 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005220 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005221}
5222
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005223static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005224{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005225 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005227 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005228 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04005229 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005230 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005231 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005232 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005233 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005234 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005235 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005236 &calldata->res.seq_res,
5237 task) != 0)
5238 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005239 return;
5240out_no_action:
5241 task->tk_action = NULL;
5242out_wait:
5243 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244}
5245
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005246static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005247 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005248 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005249 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005250};
5251
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005252static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5253 struct nfs_open_context *ctx,
5254 struct nfs4_lock_state *lsp,
5255 struct nfs_seqid *seqid)
5256{
5257 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005258 struct rpc_message msg = {
5259 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5260 .rpc_cred = ctx->cred,
5261 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005262 struct rpc_task_setup task_setup_data = {
5263 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005264 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005265 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005266 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005267 .flags = RPC_TASK_ASYNC,
5268 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005269
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005270 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5271 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5272
Frank Filz137d6ac2007-07-09 15:32:29 -07005273 /* Ensure this is an unlock - when canceling a lock, the
5274 * canceled lock is passed in, and it won't be an unlock.
5275 */
5276 fl->fl_type = F_UNLCK;
5277
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005278 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5279 if (data == NULL) {
5280 nfs_free_seqid(seqid);
5281 return ERR_PTR(-ENOMEM);
5282 }
5283
Chuck Levera9c92d62013-08-09 12:48:18 -04005284 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005285 msg.rpc_argp = &data->arg;
5286 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005287 task_setup_data.callback_data = data;
5288 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005289}
5290
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5292{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005293 struct inode *inode = state->inode;
5294 struct nfs4_state_owner *sp = state->owner;
5295 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005296 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005297 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005298 struct rpc_task *task;
5299 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005300 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301
Trond Myklebust9b073572006-06-29 16:38:34 -04005302 status = nfs4_set_lock_state(state, request);
5303 /* Unlock _before_ we do the RPC call */
5304 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005305 /* Exclude nfs_delegation_claim_locks() */
5306 mutex_lock(&sp->so_delegreturn_mutex);
5307 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005308 down_read(&nfsi->rwsem);
5309 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
5310 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005311 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005312 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005313 }
5314 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005315 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005316 if (status != 0)
5317 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005318 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005319 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005320 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5321 goto out;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005322 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005323 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005324 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04005325 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005326 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005327 status = PTR_ERR(task);
5328 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005329 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005330 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005331 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005332out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005333 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005334 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005335 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336}
5337
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005338struct nfs4_lockdata {
5339 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005340 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005341 struct nfs4_lock_state *lsp;
5342 struct nfs_open_context *ctx;
5343 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005344 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005345 int rpc_status;
5346 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005347 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005348};
5349
5350static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005351 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5352 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005353{
5354 struct nfs4_lockdata *p;
5355 struct inode *inode = lsp->ls_state->inode;
5356 struct nfs_server *server = NFS_SERVER(inode);
5357
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005358 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005359 if (p == NULL)
5360 return NULL;
5361
5362 p->arg.fh = NFS_FH(inode);
5363 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005364 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005365 if (p->arg.open_seqid == NULL)
5366 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005367 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005368 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005369 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005370 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04005371 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005372 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005373 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005374 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005375 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005376 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005377 atomic_inc(&lsp->ls_count);
5378 p->ctx = get_nfs_open_context(ctx);
5379 memcpy(&p->fl, fl, sizeof(p->fl));
5380 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005381out_free_seqid:
5382 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005383out_free:
5384 kfree(p);
5385 return NULL;
5386}
5387
5388static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5389{
5390 struct nfs4_lockdata *data = calldata;
5391 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392
Harvey Harrison3110ff82008-05-02 13:42:44 -07005393 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005394 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005395 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005396 /* Do we need to do an open_to_lock_owner? */
5397 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005398 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005399 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005400 }
Trond Myklebust8e472f32013-04-20 01:30:53 -04005401 data->arg.open_stateid = &state->open_stateid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005402 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005403 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005404 } else
5405 data->arg.new_lock_owner = 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04005406 if (!nfs4_valid_open_stateid(state)) {
5407 data->rpc_status = -EBADF;
5408 task->tk_action = NULL;
5409 goto out_release_open_seqid;
5410 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005411 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005412 if (nfs4_setup_sequence(data->server,
5413 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005414 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005415 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04005416 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04005417out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005418 nfs_release_seqid(data->arg.open_seqid);
5419out_release_lock_seqid:
5420 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005421out_wait:
5422 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005423 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005424}
5425
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005426static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5427{
5428 struct nfs4_lockdata *data = calldata;
5429
Harvey Harrison3110ff82008-05-02 13:42:44 -07005430 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005431
Trond Myklebust14516c32010-07-31 14:29:06 -04005432 if (!nfs4_sequence_done(task, &data->res.seq_res))
5433 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04005434
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005435 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005436 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005437 if (data->rpc_status == 0)
5438 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
5439 else
5440 goto out;
5441 }
5442 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05005443 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005444 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04005445 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005446 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005447out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07005448 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005449}
5450
5451static void nfs4_lock_release(void *calldata)
5452{
5453 struct nfs4_lockdata *data = calldata;
5454
Harvey Harrison3110ff82008-05-02 13:42:44 -07005455 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005456 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005457 if (data->cancelled != 0) {
5458 struct rpc_task *task;
5459 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5460 data->arg.lock_seqid);
5461 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08005462 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005463 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005464 } else
5465 nfs_free_seqid(data->arg.lock_seqid);
5466 nfs4_put_lock_state(data->lsp);
5467 put_nfs_open_context(data->ctx);
5468 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005469 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005470}
5471
5472static const struct rpc_call_ops nfs4_lock_ops = {
5473 .rpc_call_prepare = nfs4_lock_prepare,
5474 .rpc_call_done = nfs4_lock_done,
5475 .rpc_release = nfs4_lock_release,
5476};
5477
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005478static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5479{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005480 switch (error) {
5481 case -NFS4ERR_ADMIN_REVOKED:
5482 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05005483 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005484 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04005485 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05005486 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005487 break;
5488 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005489 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05005490 case -NFS4ERR_EXPIRED:
5491 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005492 };
5493}
5494
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005495static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005496{
5497 struct nfs4_lockdata *data;
5498 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005499 struct rpc_message msg = {
5500 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5501 .rpc_cred = state->owner->so_cred,
5502 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005503 struct rpc_task_setup task_setup_data = {
5504 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005505 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005506 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005507 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005508 .flags = RPC_TASK_ASYNC,
5509 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005510 int ret;
5511
Harvey Harrison3110ff82008-05-02 13:42:44 -07005512 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005513 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005514 fl->fl_u.nfs4_fl.owner,
5515 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005516 if (data == NULL)
5517 return -ENOMEM;
5518 if (IS_SETLKW(cmd))
5519 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04005520 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005521 msg.rpc_argp = &data->arg;
5522 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005523 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005524 if (recovery_type > NFS_LOCK_NEW) {
5525 if (recovery_type == NFS_LOCK_RECLAIM)
5526 data->arg.reclaim = NFS_LOCK_RECLAIM;
5527 nfs4_set_sequence_privileged(&data->arg.seq_args);
5528 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04005529 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005530 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005531 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005532 ret = nfs4_wait_for_completion_rpc_task(task);
5533 if (ret == 0) {
5534 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005535 if (ret)
5536 nfs4_handle_setlk_error(data->server, data->lsp,
5537 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005538 } else
5539 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005540 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005541 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005542 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543}
5544
5545static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5546{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005547 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005548 struct nfs4_exception exception = {
5549 .inode = state->inode,
5550 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005551 int err;
5552
5553 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005554 /* Cache the lock if possible... */
5555 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5556 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005557 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005558 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
Trond Myklebust168667c2010-10-19 19:47:49 -04005559 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00005560 break;
5561 nfs4_handle_exception(server, err, &exception);
5562 } while (exception.retry);
5563 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564}
5565
5566static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5567{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005568 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005569 struct nfs4_exception exception = {
5570 .inode = state->inode,
5571 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005572 int err;
5573
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005574 err = nfs4_set_lock_state(state, request);
5575 if (err != 0)
5576 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04005577 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10005578 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
5579 return 0;
5580 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005581 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005582 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5583 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005584 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005585 trace_nfs4_lock_expired(request, state, F_SETLK, err);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005586 switch (err) {
5587 default:
5588 goto out;
5589 case -NFS4ERR_GRACE:
5590 case -NFS4ERR_DELAY:
5591 nfs4_handle_exception(server, err, &exception);
5592 err = 0;
5593 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005594 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005595out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00005596 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597}
5598
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005599#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005600/**
5601 * nfs41_check_expired_locks - possibly free a lock stateid
5602 *
5603 * @state: NFSv4 state for an inode
5604 *
5605 * Returns NFS_OK if recovery for this stateid is now finished.
5606 * Otherwise a negative NFS4ERR value is returned.
5607 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005608static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005609{
Chuck Levereb64cf92012-07-11 16:30:05 -04005610 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005611 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005612 struct nfs_server *server = NFS_SERVER(state->inode);
5613
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005614 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04005615 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005616 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
5617
5618 status = nfs41_test_stateid(server,
5619 &lsp->ls_stateid,
5620 cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04005621 trace_nfs4_test_lock_stateid(state, lsp, status);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005622 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005623 /* Free the stateid unless the server
5624 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04005625 if (status != -NFS4ERR_BAD_STATEID)
5626 nfs41_free_stateid(server,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005627 &lsp->ls_stateid,
5628 cred);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005629 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005630 ret = status;
5631 }
5632 }
5633 };
5634
5635 return ret;
5636}
5637
5638static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5639{
5640 int status = NFS_OK;
5641
5642 if (test_bit(LK_STATE_IN_USE, &state->flags))
5643 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005644 if (status != NFS_OK)
5645 status = nfs4_lock_expired(state, request);
5646 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005647}
5648#endif
5649
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5651{
Trond Myklebust9a99af492013-02-04 20:17:49 -05005652 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005653 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005654 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005655 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005656 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005658 if ((fl_flags & FL_POSIX) &&
5659 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5660 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005661 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005662 status = nfs4_set_lock_state(state, request);
5663 if (status != 0)
5664 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005665 request->fl_flags |= FL_ACCESS;
5666 status = do_vfs_lock(request->fl_file, request);
5667 if (status < 0)
5668 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005669 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005670 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005671 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005672 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005673 request->fl_flags = fl_flags & ~FL_SLEEP;
5674 status = do_vfs_lock(request->fl_file, request);
5675 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005676 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05005677 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5678 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005679 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005680 if (status != 0)
Trond Myklebust9a99af492013-02-04 20:17:49 -05005681 goto out;
5682 down_read(&nfsi->rwsem);
5683 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5684 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005685 goto out_unlock;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005686 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005687 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005688 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005689 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005690 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5691 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005692out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005693 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005694out:
5695 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696 return status;
5697}
5698
5699static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5700{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005701 struct nfs4_exception exception = {
5702 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005703 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005704 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705 int err;
5706
5707 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005708 err = _nfs4_proc_setlk(state, cmd, request);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005709 trace_nfs4_set_lock(request, state, cmd, err);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005710 if (err == -NFS4ERR_DENIED)
5711 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005713 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714 } while (exception.retry);
5715 return err;
5716}
5717
5718static int
5719nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5720{
5721 struct nfs_open_context *ctx;
5722 struct nfs4_state *state;
5723 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5724 int status;
5725
5726 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005727 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728 state = ctx->state;
5729
5730 if (request->fl_start < 0 || request->fl_end < 0)
5731 return -EINVAL;
5732
Trond Myklebustd9531262009-07-21 19:22:38 -04005733 if (IS_GETLK(cmd)) {
5734 if (state != NULL)
5735 return nfs4_proc_getlk(state, F_GETLK, request);
5736 return 0;
5737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738
5739 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5740 return -EINVAL;
5741
Trond Myklebustd9531262009-07-21 19:22:38 -04005742 if (request->fl_type == F_UNLCK) {
5743 if (state != NULL)
5744 return nfs4_proc_unlck(state, cmd, request);
5745 return 0;
5746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747
Trond Myklebustd9531262009-07-21 19:22:38 -04005748 if (state == NULL)
5749 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005750 /*
5751 * Don't rely on the VFS having checked the file open mode,
5752 * since it won't do this for flock() locks.
5753 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005754 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005755 case F_RDLCK:
5756 if (!(filp->f_mode & FMODE_READ))
5757 return -EBADF;
5758 break;
5759 case F_WRLCK:
5760 if (!(filp->f_mode & FMODE_WRITE))
5761 return -EBADF;
5762 }
5763
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764 do {
5765 status = nfs4_proc_setlk(state, cmd, request);
5766 if ((status != -EAGAIN) || IS_SETLK(cmd))
5767 break;
5768 timeout = nfs4_set_lock_task_retry(timeout);
5769 status = -ERESTARTSYS;
5770 if (signalled())
5771 break;
5772 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773 return status;
5774}
5775
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005776int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05005777{
5778 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05005779 int err;
5780
5781 err = nfs4_set_lock_state(state, fl);
5782 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005783 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04005784 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005785 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05005786}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005787
Trond Myklebustcf470c32012-03-07 13:49:12 -05005788struct nfs_release_lockowner_data {
5789 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005790 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005791 struct nfs_release_lockowner_args args;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005792 struct nfs4_sequence_args seq_args;
5793 struct nfs4_sequence_res seq_res;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005794};
5795
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005796static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
5797{
5798 struct nfs_release_lockowner_data *data = calldata;
5799 nfs40_setup_sequence(data->server,
5800 &data->seq_args, &data->seq_res, task);
5801}
5802
5803static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
5804{
5805 struct nfs_release_lockowner_data *data = calldata;
5806 nfs40_sequence_done(task, &data->seq_res);
5807}
5808
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005809static void nfs4_release_lockowner_release(void *calldata)
5810{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005811 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005812 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005813 kfree(calldata);
5814}
5815
Trond Myklebust17280172012-03-11 13:11:00 -04005816static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005817 .rpc_call_prepare = nfs4_release_lockowner_prepare,
5818 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005819 .rpc_release = nfs4_release_lockowner_release,
5820};
5821
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04005822static int nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005823{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005824 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005825 struct rpc_message msg = {
5826 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5827 };
5828
5829 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005830 return -EINVAL;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005831
Trond Myklebustcf470c32012-03-07 13:49:12 -05005832 data = kmalloc(sizeof(*data), GFP_NOFS);
5833 if (!data)
5834 return -ENOMEM;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005835 nfs4_init_sequence(&data->seq_args, &data->seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05005836 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005837 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005838 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5839 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5840 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005841
Trond Myklebustcf470c32012-03-07 13:49:12 -05005842 msg.rpc_argp = &data->args;
5843 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5844 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005845}
5846
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005847#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5848
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005849static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5850 const void *buf, size_t buflen,
5851 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005852{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005853 if (strcmp(key, "") != 0)
5854 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005855
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005856 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005857}
5858
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005859static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5860 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005861{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005862 if (strcmp(key, "") != 0)
5863 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005864
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005865 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005866}
5867
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005868static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5869 size_t list_len, const char *name,
5870 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005871{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005872 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005873
J. Bruce Fields096455a2006-03-20 23:23:42 -05005874 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5875 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005876
5877 if (list && len <= list_len)
5878 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005879 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005880}
5881
David Quigleyc9bccef2013-05-22 12:50:45 -04005882#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5883static inline int nfs4_server_supports_labels(struct nfs_server *server)
5884{
5885 return server->caps & NFS_CAP_SECURITY_LABEL;
5886}
5887
5888static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
5889 const void *buf, size_t buflen,
5890 int flags, int type)
5891{
5892 if (security_ismaclabel(key))
5893 return nfs4_set_security_label(dentry, buf, buflen);
5894
5895 return -EOPNOTSUPP;
5896}
5897
5898static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
5899 void *buf, size_t buflen, int type)
5900{
5901 if (security_ismaclabel(key))
5902 return nfs4_get_security_label(dentry->d_inode, buf, buflen);
5903 return -EOPNOTSUPP;
5904}
5905
5906static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
5907 size_t list_len, const char *name,
5908 size_t name_len, int type)
5909{
5910 size_t len = 0;
5911
5912 if (nfs_server_capable(dentry->d_inode, NFS_CAP_SECURITY_LABEL)) {
5913 len = security_inode_listsecurity(dentry->d_inode, NULL, 0);
5914 if (list && len <= list_len)
5915 security_inode_listsecurity(dentry->d_inode, list, len);
5916 }
5917 return len;
5918}
5919
5920static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
5921 .prefix = XATTR_SECURITY_PREFIX,
5922 .list = nfs4_xattr_list_nfs4_label,
5923 .get = nfs4_xattr_get_nfs4_label,
5924 .set = nfs4_xattr_set_nfs4_label,
5925};
5926#endif
5927
5928
Andy Adamson533eb462011-06-13 18:25:56 -04005929/*
5930 * nfs_fhget will use either the mounted_on_fileid or the fileid
5931 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005932static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5933{
Andy Adamson533eb462011-06-13 18:25:56 -04005934 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5935 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5936 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005937 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005938 return;
5939
5940 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005941 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005942 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5943 fattr->nlink = 2;
5944}
5945
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005946static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5947 const struct qstr *name,
5948 struct nfs4_fs_locations *fs_locations,
5949 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005950{
5951 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04005952 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005953 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005954 };
5955 struct nfs4_fs_locations_arg args = {
5956 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005957 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005958 .page = page,
5959 .bitmask = bitmask,
5960 };
Benny Halevy22958462009-04-01 09:22:02 -04005961 struct nfs4_fs_locations_res res = {
5962 .fs_locations = fs_locations,
5963 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005964 struct rpc_message msg = {
5965 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5966 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005967 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005968 };
5969 int status;
5970
Harvey Harrison3110ff82008-05-02 13:42:44 -07005971 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005972
5973 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5974 * is not supported */
5975 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5976 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5977 else
5978 bitmask[0] |= FATTR4_WORD0_FILEID;
5979
Trond Myklebustc228fd32007-01-13 02:28:11 -05005980 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005981 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005982 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005983 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005984 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005985 return status;
5986}
5987
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005988int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5989 const struct qstr *name,
5990 struct nfs4_fs_locations *fs_locations,
5991 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005992{
5993 struct nfs4_exception exception = { };
5994 int err;
5995 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04005996 err = _nfs4_proc_fs_locations(client, dir, name,
5997 fs_locations, page);
5998 trace_nfs4_get_fs_locations(dir, name, err);
5999 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006000 &exception);
6001 } while (exception.retry);
6002 return err;
6003}
6004
Chuck Leverb03d7352013-10-17 14:12:50 -04006005/*
6006 * This operation also signals the server that this client is
6007 * performing migration recovery. The server can stop returning
6008 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6009 * appended to this compound to identify the client ID which is
6010 * performing recovery.
6011 */
6012static int _nfs40_proc_get_locations(struct inode *inode,
6013 struct nfs4_fs_locations *locations,
6014 struct page *page, struct rpc_cred *cred)
6015{
6016 struct nfs_server *server = NFS_SERVER(inode);
6017 struct rpc_clnt *clnt = server->client;
6018 u32 bitmask[2] = {
6019 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6020 };
6021 struct nfs4_fs_locations_arg args = {
6022 .clientid = server->nfs_client->cl_clientid,
6023 .fh = NFS_FH(inode),
6024 .page = page,
6025 .bitmask = bitmask,
6026 .migration = 1, /* skip LOOKUP */
6027 .renew = 1, /* append RENEW */
6028 };
6029 struct nfs4_fs_locations_res res = {
6030 .fs_locations = locations,
6031 .migration = 1,
6032 .renew = 1,
6033 };
6034 struct rpc_message msg = {
6035 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6036 .rpc_argp = &args,
6037 .rpc_resp = &res,
6038 .rpc_cred = cred,
6039 };
6040 unsigned long now = jiffies;
6041 int status;
6042
6043 nfs_fattr_init(&locations->fattr);
6044 locations->server = server;
6045 locations->nlocations = 0;
6046
6047 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6048 nfs4_set_sequence_privileged(&args.seq_args);
6049 status = nfs4_call_sync_sequence(clnt, server, &msg,
6050 &args.seq_args, &res.seq_res);
6051 if (status)
6052 return status;
6053
6054 renew_lease(server, now);
6055 return 0;
6056}
6057
6058#ifdef CONFIG_NFS_V4_1
6059
6060/*
6061 * This operation also signals the server that this client is
6062 * performing migration recovery. The server can stop asserting
6063 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6064 * performing this operation is identified in the SEQUENCE
6065 * operation in this compound.
6066 *
6067 * When the client supports GETATTR(fs_locations_info), it can
6068 * be plumbed in here.
6069 */
6070static int _nfs41_proc_get_locations(struct inode *inode,
6071 struct nfs4_fs_locations *locations,
6072 struct page *page, struct rpc_cred *cred)
6073{
6074 struct nfs_server *server = NFS_SERVER(inode);
6075 struct rpc_clnt *clnt = server->client;
6076 u32 bitmask[2] = {
6077 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6078 };
6079 struct nfs4_fs_locations_arg args = {
6080 .fh = NFS_FH(inode),
6081 .page = page,
6082 .bitmask = bitmask,
6083 .migration = 1, /* skip LOOKUP */
6084 };
6085 struct nfs4_fs_locations_res res = {
6086 .fs_locations = locations,
6087 .migration = 1,
6088 };
6089 struct rpc_message msg = {
6090 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6091 .rpc_argp = &args,
6092 .rpc_resp = &res,
6093 .rpc_cred = cred,
6094 };
6095 int status;
6096
6097 nfs_fattr_init(&locations->fattr);
6098 locations->server = server;
6099 locations->nlocations = 0;
6100
6101 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6102 nfs4_set_sequence_privileged(&args.seq_args);
6103 status = nfs4_call_sync_sequence(clnt, server, &msg,
6104 &args.seq_args, &res.seq_res);
6105 if (status == NFS4_OK &&
6106 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6107 status = -NFS4ERR_LEASE_MOVED;
6108 return status;
6109}
6110
6111#endif /* CONFIG_NFS_V4_1 */
6112
6113/**
6114 * nfs4_proc_get_locations - discover locations for a migrated FSID
6115 * @inode: inode on FSID that is migrating
6116 * @locations: result of query
6117 * @page: buffer
6118 * @cred: credential to use for this operation
6119 *
6120 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6121 * operation failed, or a negative errno if a local error occurred.
6122 *
6123 * On success, "locations" is filled in, but if the server has
6124 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6125 * asserted.
6126 *
6127 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6128 * from this client that require migration recovery.
6129 */
6130int nfs4_proc_get_locations(struct inode *inode,
6131 struct nfs4_fs_locations *locations,
6132 struct page *page, struct rpc_cred *cred)
6133{
6134 struct nfs_server *server = NFS_SERVER(inode);
6135 struct nfs_client *clp = server->nfs_client;
6136 const struct nfs4_mig_recovery_ops *ops =
6137 clp->cl_mvops->mig_recovery_ops;
6138 struct nfs4_exception exception = { };
6139 int status;
6140
6141 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6142 (unsigned long long)server->fsid.major,
6143 (unsigned long long)server->fsid.minor,
6144 clp->cl_hostname);
6145 nfs_display_fhandle(NFS_FH(inode), __func__);
6146
6147 do {
6148 status = ops->get_locations(inode, locations, page, cred);
6149 if (status != -NFS4ERR_DELAY)
6150 break;
6151 nfs4_handle_exception(server, status, &exception);
6152 } while (exception.retry);
6153 return status;
6154}
6155
Andy Adamson5ec16a82013-08-08 10:57:55 -04006156/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006157 * If 'use_integrity' is true and the state managment nfs_client
6158 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6159 * and the machine credential as per RFC3530bis and RFC5661 Security
6160 * Considerations sections. Otherwise, just use the user cred with the
6161 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04006162 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006163static 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 +00006164{
6165 int status;
6166 struct nfs4_secinfo_arg args = {
6167 .dir_fh = NFS_FH(dir),
6168 .name = name,
6169 };
6170 struct nfs4_secinfo_res res = {
6171 .flavors = flavors,
6172 };
6173 struct rpc_message msg = {
6174 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6175 .rpc_argp = &args,
6176 .rpc_resp = &res,
6177 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006178 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006179 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006180
6181 if (use_integrity) {
6182 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006183 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6184 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006185 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006186
6187 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006188
6189 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6190 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6191
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006192 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6193 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006194 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006195
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006196 if (cred)
6197 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006198
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006199 return status;
6200}
6201
Bryan Schumaker72de53e2012-04-27 13:27:40 -04006202int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6203 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006204{
6205 struct nfs4_exception exception = { };
6206 int err;
6207 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006208 err = -NFS4ERR_WRONGSEC;
6209
6210 /* try to use integrity protection with machine cred */
6211 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6212 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6213
6214 /*
6215 * if unable to use integrity protection, or SECINFO with
6216 * integrity protection returns NFS4ERR_WRONGSEC (which is
6217 * disallowed by spec, but exists in deployed servers) use
6218 * the current filesystem's rpc_client and the user cred.
6219 */
6220 if (err == -NFS4ERR_WRONGSEC)
6221 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6222
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006223 trace_nfs4_secinfo(dir, name, err);
6224 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006225 &exception);
6226 } while (exception.retry);
6227 return err;
6228}
6229
Andy Adamson557134a2009-04-01 09:21:53 -04006230#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04006231/*
Andy Adamson357f54d2010-12-14 10:11:57 -05006232 * Check the exchange flags returned by the server for invalid flags, having
6233 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6234 * DS flags set.
6235 */
6236static int nfs4_check_cl_exchange_flags(u32 flags)
6237{
6238 if (flags & ~EXCHGID4_FLAG_MASK_R)
6239 goto out_inval;
6240 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6241 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6242 goto out_inval;
6243 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6244 goto out_inval;
6245 return NFS_OK;
6246out_inval:
6247 return -NFS4ERR_INVAL;
6248}
6249
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006250static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006251nfs41_same_server_scope(struct nfs41_server_scope *a,
6252 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006253{
6254 if (a->server_scope_sz == b->server_scope_sz &&
6255 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6256 return true;
6257
6258 return false;
6259}
6260
Andy Adamson357f54d2010-12-14 10:11:57 -05006261/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006262 * nfs4_proc_bind_conn_to_session()
6263 *
6264 * The 4.1 client currently uses the same TCP connection for the
6265 * fore and backchannel.
6266 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006267int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006268{
6269 int status;
6270 struct nfs41_bind_conn_to_session_res res;
6271 struct rpc_message msg = {
6272 .rpc_proc =
6273 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
6274 .rpc_argp = clp,
6275 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006276 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006277 };
6278
6279 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006280
6281 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
6282 if (unlikely(res.session == NULL)) {
6283 status = -ENOMEM;
6284 goto out;
6285 }
6286
6287 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006288 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006289 if (status == 0) {
6290 if (memcmp(res.session->sess_id.data,
6291 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6292 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6293 status = -EIO;
6294 goto out_session;
6295 }
6296 if (res.dir != NFS4_CDFS4_BOTH) {
6297 dprintk("NFS: %s: Unexpected direction from server\n",
6298 __func__);
6299 status = -EIO;
6300 goto out_session;
6301 }
6302 if (res.use_conn_in_rdma_mode) {
6303 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6304 __func__);
6305 status = -EIO;
6306 goto out_session;
6307 }
6308 }
6309out_session:
6310 kfree(res.session);
6311out:
6312 dprintk("<-- %s status= %d\n", __func__, status);
6313 return status;
6314}
6315
6316/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006317 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6318 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04006319 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006320static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
6321 .how = SP4_MACH_CRED,
6322 .enforce.u.words = {
6323 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6324 1 << (OP_EXCHANGE_ID - 32) |
6325 1 << (OP_CREATE_SESSION - 32) |
6326 1 << (OP_DESTROY_SESSION - 32) |
6327 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006328 },
6329 .allow.u.words = {
6330 [0] = 1 << (OP_CLOSE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006331 1 << (OP_LOCKU) |
6332 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006333 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006334 1 << (OP_SECINFO_NO_NAME - 32) |
6335 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006336 1 << (OP_FREE_STATEID - 32) |
6337 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006338 }
6339};
6340
6341/*
6342 * Select the state protection mode for client `clp' given the server results
6343 * from exchange_id in `sp'.
6344 *
6345 * Returns 0 on success, negative errno otherwise.
6346 */
6347static int nfs4_sp4_select_mode(struct nfs_client *clp,
6348 struct nfs41_state_protection *sp)
6349{
6350 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
6351 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6352 1 << (OP_EXCHANGE_ID - 32) |
6353 1 << (OP_CREATE_SESSION - 32) |
6354 1 << (OP_DESTROY_SESSION - 32) |
6355 1 << (OP_DESTROY_CLIENTID - 32)
6356 };
6357 unsigned int i;
6358
6359 if (sp->how == SP4_MACH_CRED) {
6360 /* Print state protect result */
6361 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
6362 for (i = 0; i <= LAST_NFS4_OP; i++) {
6363 if (test_bit(i, sp->enforce.u.longs))
6364 dfprintk(MOUNT, " enforce op %d\n", i);
6365 if (test_bit(i, sp->allow.u.longs))
6366 dfprintk(MOUNT, " allow op %d\n", i);
6367 }
6368
6369 /* make sure nothing is on enforce list that isn't supported */
6370 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
6371 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
6372 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6373 return -EINVAL;
6374 }
6375 }
6376
6377 /*
6378 * Minimal mode - state operations are allowed to use machine
6379 * credential. Note this already happens by default, so the
6380 * client doesn't have to do anything more than the negotiation.
6381 *
6382 * NOTE: we don't care if EXCHANGE_ID is in the list -
6383 * we're already using the machine cred for exchange_id
6384 * and will never use a different cred.
6385 */
6386 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
6387 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
6388 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
6389 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
6390 dfprintk(MOUNT, "sp4_mach_cred:\n");
6391 dfprintk(MOUNT, " minimal mode enabled\n");
6392 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
6393 } else {
6394 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6395 return -EINVAL;
6396 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006397
6398 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
6399 test_bit(OP_LOCKU, sp->allow.u.longs)) {
6400 dfprintk(MOUNT, " cleanup mode enabled\n");
6401 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
6402 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006403
6404 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
6405 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
6406 dfprintk(MOUNT, " secinfo mode enabled\n");
6407 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
6408 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006409
6410 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
6411 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
6412 dfprintk(MOUNT, " stateid mode enabled\n");
6413 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
6414 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04006415
6416 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
6417 dfprintk(MOUNT, " write mode enabled\n");
6418 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
6419 }
6420
6421 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
6422 dfprintk(MOUNT, " commit mode enabled\n");
6423 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
6424 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006425 }
6426
6427 return 0;
6428}
6429
6430/*
6431 * _nfs4_proc_exchange_id()
6432 *
6433 * Wrapper for EXCHANGE_ID operation.
6434 */
6435static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
6436 u32 sp4_how)
Benny Halevy99fe60d2009-04-01 09:22:29 -04006437{
6438 nfs4_verifier verifier;
6439 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05006440 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04006441 .client = clp,
Trond Myklebust4f0b4292013-05-20 12:24:03 -04006442 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6443 EXCHGID4_FLAG_BIND_PRINC_STATEID,
Benny Halevy99fe60d2009-04-01 09:22:29 -04006444 };
6445 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04006446 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04006447 };
6448 int status;
6449 struct rpc_message msg = {
6450 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
6451 .rpc_argp = &args,
6452 .rpc_resp = &res,
6453 .rpc_cred = cred,
6454 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04006455
Chuck Leverf0920752012-05-21 22:45:41 -04006456 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04006457 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
6458 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006459 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
6460 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6461 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006462
Chuck Leveracdeb692012-05-21 22:46:16 -04006463 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006464 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04006465 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006466 status = -ENOMEM;
6467 goto out;
6468 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006469
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006470 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006471 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006472 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04006473 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04006474 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04006475 }
6476
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006477 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006478 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006479 status = -ENOMEM;
6480 goto out_server_scope;
6481 }
6482
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006483 switch (sp4_how) {
6484 case SP4_NONE:
6485 args.state_protect.how = SP4_NONE;
6486 break;
6487
6488 case SP4_MACH_CRED:
6489 args.state_protect = nfs4_sp4_mach_cred_request;
6490 break;
6491
6492 default:
6493 /* unsupported! */
6494 WARN_ON_ONCE(1);
6495 status = -EINVAL;
6496 goto out_server_scope;
6497 }
6498
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006499 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006500 trace_nfs4_exchange_id(clp, status);
Chuck Lever177313f2012-05-21 22:44:58 -04006501 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04006502 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006503
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006504 if (status == 0)
6505 status = nfs4_sp4_select_mode(clp, &res.state_protect);
6506
Chuck Lever177313f2012-05-21 22:44:58 -04006507 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04006508 clp->cl_clientid = res.clientid;
6509 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
6510 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
6511 clp->cl_seqid = res.seqid;
6512
Chuck Leveracdeb692012-05-21 22:46:16 -04006513 kfree(clp->cl_serverowner);
6514 clp->cl_serverowner = res.server_owner;
6515 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04006516
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006517 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04006518 kfree(clp->cl_implid);
6519 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006520
Chuck Lever177313f2012-05-21 22:44:58 -04006521 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006522 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006523 res.server_scope)) {
6524 dprintk("%s: server_scope mismatch detected\n",
6525 __func__);
6526 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006527 kfree(clp->cl_serverscope);
6528 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006529 }
6530
Chuck Lever177313f2012-05-21 22:44:58 -04006531 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006532 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006533 goto out;
6534 }
Trond Myklebust32b01312012-05-26 13:41:04 -04006535 } else
6536 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006537
Chuck Leveracdeb692012-05-21 22:46:16 -04006538out_server_owner:
6539 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006540out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006541 kfree(res.server_scope);
6542out:
Chuck Lever177313f2012-05-21 22:44:58 -04006543 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006544 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006545 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006546 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04006547 clp->cl_implid->date.seconds,
6548 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006549 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006550 return status;
6551}
6552
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006553/*
6554 * nfs4_proc_exchange_id()
6555 *
6556 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6557 *
6558 * Since the clientid has expired, all compounds using sessions
6559 * associated with the stale clientid will be returning
6560 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
6561 * be in some phase of session reset.
6562 *
6563 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
6564 */
6565int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
6566{
6567 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
6568 int status;
6569
6570 /* try SP4_MACH_CRED if krb5i/p */
6571 if (authflavor == RPC_AUTH_GSS_KRB5I ||
6572 authflavor == RPC_AUTH_GSS_KRB5P) {
6573 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
6574 if (!status)
6575 return 0;
6576 }
6577
6578 /* try SP4_NONE */
6579 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
6580}
6581
Trond Myklebust66245532012-05-25 17:18:09 -04006582static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
6583 struct rpc_cred *cred)
6584{
6585 struct rpc_message msg = {
6586 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
6587 .rpc_argp = clp,
6588 .rpc_cred = cred,
6589 };
6590 int status;
6591
6592 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006593 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04006594 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04006595 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04006596 "DESTROY_CLIENTID.", status, clp->cl_hostname);
6597 return status;
6598}
6599
6600static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
6601 struct rpc_cred *cred)
6602{
6603 unsigned int loop;
6604 int ret;
6605
6606 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
6607 ret = _nfs4_proc_destroy_clientid(clp, cred);
6608 switch (ret) {
6609 case -NFS4ERR_DELAY:
6610 case -NFS4ERR_CLIENTID_BUSY:
6611 ssleep(1);
6612 break;
6613 default:
6614 return ret;
6615 }
6616 }
6617 return 0;
6618}
6619
6620int nfs4_destroy_clientid(struct nfs_client *clp)
6621{
6622 struct rpc_cred *cred;
6623 int ret = 0;
6624
6625 if (clp->cl_mvops->minor_version < 1)
6626 goto out;
6627 if (clp->cl_exchange_flags == 0)
6628 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04006629 if (clp->cl_preserve_clid)
6630 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04006631 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04006632 ret = nfs4_proc_destroy_clientid(clp, cred);
6633 if (cred)
6634 put_rpccred(cred);
6635 switch (ret) {
6636 case 0:
6637 case -NFS4ERR_STALE_CLIENTID:
6638 clp->cl_exchange_flags = 0;
6639 }
6640out:
6641 return ret;
6642}
6643
Andy Adamson2050f0c2009-04-01 09:22:30 -04006644struct nfs4_get_lease_time_data {
6645 struct nfs4_get_lease_time_args *args;
6646 struct nfs4_get_lease_time_res *res;
6647 struct nfs_client *clp;
6648};
6649
6650static void nfs4_get_lease_time_prepare(struct rpc_task *task,
6651 void *calldata)
6652{
Andy Adamson2050f0c2009-04-01 09:22:30 -04006653 struct nfs4_get_lease_time_data *data =
6654 (struct nfs4_get_lease_time_data *)calldata;
6655
6656 dprintk("--> %s\n", __func__);
6657 /* just setup sequence, do not trigger session recovery
6658 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006659 nfs41_setup_sequence(data->clp->cl_session,
6660 &data->args->la_seq_args,
6661 &data->res->lr_seq_res,
6662 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006663 dprintk("<-- %s\n", __func__);
6664}
6665
6666/*
6667 * Called from nfs4_state_manager thread for session setup, so don't recover
6668 * from sequence operation or clientid errors.
6669 */
6670static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
6671{
6672 struct nfs4_get_lease_time_data *data =
6673 (struct nfs4_get_lease_time_data *)calldata;
6674
6675 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006676 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
6677 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04006678 switch (task->tk_status) {
6679 case -NFS4ERR_DELAY:
6680 case -NFS4ERR_GRACE:
6681 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
6682 rpc_delay(task, NFS4_POLL_RETRY_MIN);
6683 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006684 /* fall through */
6685 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006686 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006687 return;
6688 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04006689 dprintk("<-- %s\n", __func__);
6690}
6691
Trond Myklebust17280172012-03-11 13:11:00 -04006692static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04006693 .rpc_call_prepare = nfs4_get_lease_time_prepare,
6694 .rpc_call_done = nfs4_get_lease_time_done,
6695};
6696
6697int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
6698{
6699 struct rpc_task *task;
6700 struct nfs4_get_lease_time_args args;
6701 struct nfs4_get_lease_time_res res = {
6702 .lr_fsinfo = fsinfo,
6703 };
6704 struct nfs4_get_lease_time_data data = {
6705 .args = &args,
6706 .res = &res,
6707 .clp = clp,
6708 };
6709 struct rpc_message msg = {
6710 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
6711 .rpc_argp = &args,
6712 .rpc_resp = &res,
6713 };
6714 struct rpc_task_setup task_setup = {
6715 .rpc_client = clp->cl_rpcclient,
6716 .rpc_message = &msg,
6717 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006718 .callback_data = &data,
6719 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04006720 };
6721 int status;
6722
Chuck Levera9c92d62013-08-09 12:48:18 -04006723 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006724 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006725 dprintk("--> %s\n", __func__);
6726 task = rpc_run_task(&task_setup);
6727
6728 if (IS_ERR(task))
6729 status = PTR_ERR(task);
6730 else {
6731 status = task->tk_status;
6732 rpc_put_task(task);
6733 }
6734 dprintk("<-- %s return %d\n", __func__, status);
6735
6736 return status;
6737}
6738
Andy Adamsonfc931582009-04-01 09:22:31 -04006739/*
6740 * Initialize the values to be used by the client in CREATE_SESSION
6741 * If nfs4_init_session set the fore channel request and response sizes,
6742 * use them.
6743 *
6744 * Set the back channel max_resp_sz_cached to zero to force the client to
6745 * always set csa_cachethis to FALSE because the current implementation
6746 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
6747 */
6748static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
6749{
Andy Adamson18aad3d2013-06-26 12:21:49 -04006750 unsigned int max_rqst_sz, max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006751
Andy Adamson18aad3d2013-06-26 12:21:49 -04006752 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
6753 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
6754
Andy Adamsonfc931582009-04-01 09:22:31 -04006755 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04006756 args->fc_attrs.max_rqst_sz = max_rqst_sz;
6757 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006758 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05006759 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04006760
6761 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05006762 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04006763 __func__,
6764 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05006765 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04006766
6767 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04006768 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
6769 args->bc_attrs.max_resp_sz = PAGE_SIZE;
6770 args->bc_attrs.max_resp_sz_cached = 0;
6771 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
6772 args->bc_attrs.max_reqs = 1;
6773
6774 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
6775 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
6776 __func__,
6777 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
6778 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
6779 args->bc_attrs.max_reqs);
6780}
6781
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006782static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04006783{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006784 struct nfs4_channel_attrs *sent = &args->fc_attrs;
6785 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
6786
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006787 if (rcvd->max_resp_sz > sent->max_resp_sz)
6788 return -EINVAL;
6789 /*
6790 * Our requested max_ops is the minimum we need; we're not
6791 * prepared to break up compounds into smaller pieces than that.
6792 * So, no point even trying to continue if the server won't
6793 * cooperate:
6794 */
6795 if (rcvd->max_ops < sent->max_ops)
6796 return -EINVAL;
6797 if (rcvd->max_reqs == 0)
6798 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006799 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
6800 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006801 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04006802}
6803
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006804static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
6805{
6806 struct nfs4_channel_attrs *sent = &args->bc_attrs;
6807 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04006808
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006809 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
6810 return -EINVAL;
6811 if (rcvd->max_resp_sz < sent->max_resp_sz)
6812 return -EINVAL;
6813 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
6814 return -EINVAL;
6815 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006816 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006817 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006818 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006819 return -EINVAL;
6820 return 0;
6821}
Andy Adamson8d353012009-04-01 09:22:32 -04006822
Andy Adamson8d353012009-04-01 09:22:32 -04006823static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
6824 struct nfs4_session *session)
6825{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006826 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04006827
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006828 ret = nfs4_verify_fore_channel_attrs(args, session);
6829 if (ret)
6830 return ret;
6831 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04006832}
6833
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006834static int _nfs4_proc_create_session(struct nfs_client *clp,
6835 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04006836{
6837 struct nfs4_session *session = clp->cl_session;
6838 struct nfs41_create_session_args args = {
6839 .client = clp,
6840 .cb_program = NFS4_CALLBACK,
6841 };
6842 struct nfs41_create_session_res res = {
6843 .client = clp,
6844 };
6845 struct rpc_message msg = {
6846 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
6847 .rpc_argp = &args,
6848 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006849 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04006850 };
6851 int status;
6852
6853 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04006854 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04006855
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006856 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006857 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04006858
Trond Myklebust43095d32012-11-20 11:13:12 -05006859 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04006860 /* Verify the session's negotiated channel_attrs values */
6861 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04006862 /* Increment the clientid slot sequence id */
6863 clp->cl_seqid++;
6864 }
6865
6866 return status;
6867}
6868
6869/*
6870 * Issues a CREATE_SESSION operation to the server.
6871 * It is the responsibility of the caller to verify the session is
6872 * expired before calling this routine.
6873 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006874int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04006875{
6876 int status;
6877 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04006878 struct nfs4_session *session = clp->cl_session;
6879
6880 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
6881
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006882 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04006883 if (status)
6884 goto out;
6885
Andy Adamsonaacd5532011-11-09 13:58:21 -05006886 /* Init or reset the session slot tables */
6887 status = nfs4_setup_session_slot_tables(session);
6888 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04006889 if (status)
6890 goto out;
6891
6892 ptr = (unsigned *)&session->sess_id.data[0];
6893 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
6894 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04006895out:
6896 dprintk("<-- %s\n", __func__);
6897 return status;
6898}
6899
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006900/*
6901 * Issue the over-the-wire RPC DESTROY_SESSION.
6902 * The caller must serialize access to this routine.
6903 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006904int nfs4_proc_destroy_session(struct nfs4_session *session,
6905 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006906{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006907 struct rpc_message msg = {
6908 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
6909 .rpc_argp = session,
6910 .rpc_cred = cred,
6911 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006912 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006913
6914 dprintk("--> nfs4_proc_destroy_session\n");
6915
6916 /* session is still being setup */
6917 if (session->clp->cl_cons_state != NFS_CS_READY)
6918 return status;
6919
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006920 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006921 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006922
6923 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04006924 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006925 "Session has been destroyed regardless...\n", status);
6926
6927 dprintk("<-- nfs4_proc_destroy_session\n");
6928 return status;
6929}
6930
Trond Myklebust7b38c362012-05-23 13:23:31 -04006931/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006932 * Renew the cl_session lease.
6933 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006934struct nfs4_sequence_data {
6935 struct nfs_client *clp;
6936 struct nfs4_sequence_args args;
6937 struct nfs4_sequence_res res;
6938};
6939
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006940static void nfs41_sequence_release(void *data)
6941{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006942 struct nfs4_sequence_data *calldata = data;
6943 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006944
Alexandros Batsakis71358402010-02-05 03:45:05 -08006945 if (atomic_read(&clp->cl_count) > 1)
6946 nfs4_schedule_state_renewal(clp);
6947 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006948 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006949}
6950
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006951static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6952{
6953 switch(task->tk_status) {
6954 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006955 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6956 return -EAGAIN;
6957 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006958 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006959 }
6960 return 0;
6961}
6962
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006963static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006964{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006965 struct nfs4_sequence_data *calldata = data;
6966 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006967
Trond Myklebust14516c32010-07-31 14:29:06 -04006968 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6969 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006970
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006971 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006972 if (task->tk_status < 0) {
6973 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006974 if (atomic_read(&clp->cl_count) == 1)
6975 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006976
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006977 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6978 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006979 return;
6980 }
6981 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006982 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006983out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006984 dprintk("<-- %s\n", __func__);
6985}
6986
6987static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6988{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006989 struct nfs4_sequence_data *calldata = data;
6990 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006991 struct nfs4_sequence_args *args;
6992 struct nfs4_sequence_res *res;
6993
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006994 args = task->tk_msg.rpc_argp;
6995 res = task->tk_msg.rpc_resp;
6996
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006997 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006998}
6999
7000static const struct rpc_call_ops nfs41_sequence_ops = {
7001 .rpc_call_done = nfs41_sequence_call_done,
7002 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007003 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007004};
7005
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007006static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
7007 struct rpc_cred *cred,
7008 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007009{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007010 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007011 struct rpc_message msg = {
7012 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
7013 .rpc_cred = cred,
7014 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007015 struct rpc_task_setup task_setup_data = {
7016 .rpc_client = clp->cl_rpcclient,
7017 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007018 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04007019 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007020 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007021
Alexandros Batsakis71358402010-02-05 03:45:05 -08007022 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007023 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04007024 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007025 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08007026 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007027 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007028 }
Chuck Levera9c92d62013-08-09 12:48:18 -04007029 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007030 if (is_privileged)
7031 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007032 msg.rpc_argp = &calldata->args;
7033 msg.rpc_resp = &calldata->res;
7034 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007035 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007036
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007037 return rpc_run_task(&task_setup_data);
7038}
7039
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007040static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007041{
7042 struct rpc_task *task;
7043 int ret = 0;
7044
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007045 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
7046 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007047 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007048 if (IS_ERR(task))
7049 ret = PTR_ERR(task);
7050 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08007051 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007052 dprintk("<-- %s status=%d\n", __func__, ret);
7053 return ret;
7054}
7055
7056static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
7057{
7058 struct rpc_task *task;
7059 int ret;
7060
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007061 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007062 if (IS_ERR(task)) {
7063 ret = PTR_ERR(task);
7064 goto out;
7065 }
7066 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05007067 if (!ret) {
7068 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
7069
7070 if (task->tk_status == 0)
7071 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007072 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05007073 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007074 rpc_put_task(task);
7075out:
7076 dprintk("<-- %s status=%d\n", __func__, ret);
7077 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007078}
7079
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007080struct nfs4_reclaim_complete_data {
7081 struct nfs_client *clp;
7082 struct nfs41_reclaim_complete_args arg;
7083 struct nfs41_reclaim_complete_res res;
7084};
7085
7086static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
7087{
7088 struct nfs4_reclaim_complete_data *calldata = data;
7089
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007090 nfs41_setup_sequence(calldata->clp->cl_session,
7091 &calldata->arg.seq_args,
7092 &calldata->res.seq_res,
7093 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007094}
7095
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007096static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7097{
7098 switch(task->tk_status) {
7099 case 0:
7100 case -NFS4ERR_COMPLETE_ALREADY:
7101 case -NFS4ERR_WRONG_CRED: /* What to do here? */
7102 break;
7103 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007104 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007105 /* fall through */
7106 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007107 return -EAGAIN;
7108 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007109 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007110 }
7111 return 0;
7112}
7113
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007114static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
7115{
7116 struct nfs4_reclaim_complete_data *calldata = data;
7117 struct nfs_client *clp = calldata->clp;
7118 struct nfs4_sequence_res *res = &calldata->res.seq_res;
7119
7120 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007121 if (!nfs41_sequence_done(task, res))
7122 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007123
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007124 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007125 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
7126 rpc_restart_call_prepare(task);
7127 return;
7128 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007129 dprintk("<-- %s\n", __func__);
7130}
7131
7132static void nfs4_free_reclaim_complete_data(void *data)
7133{
7134 struct nfs4_reclaim_complete_data *calldata = data;
7135
7136 kfree(calldata);
7137}
7138
7139static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
7140 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
7141 .rpc_call_done = nfs4_reclaim_complete_done,
7142 .rpc_release = nfs4_free_reclaim_complete_data,
7143};
7144
7145/*
7146 * Issue a global reclaim complete.
7147 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04007148static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
7149 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007150{
7151 struct nfs4_reclaim_complete_data *calldata;
7152 struct rpc_task *task;
7153 struct rpc_message msg = {
7154 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04007155 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007156 };
7157 struct rpc_task_setup task_setup_data = {
7158 .rpc_client = clp->cl_rpcclient,
7159 .rpc_message = &msg,
7160 .callback_ops = &nfs4_reclaim_complete_call_ops,
7161 .flags = RPC_TASK_ASYNC,
7162 };
7163 int status = -ENOMEM;
7164
7165 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04007166 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007167 if (calldata == NULL)
7168 goto out;
7169 calldata->clp = clp;
7170 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007171
Chuck Levera9c92d62013-08-09 12:48:18 -04007172 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007173 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007174 msg.rpc_argp = &calldata->arg;
7175 msg.rpc_resp = &calldata->res;
7176 task_setup_data.callback_data = calldata;
7177 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007178 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007179 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007180 goto out;
7181 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05007182 status = nfs4_wait_for_completion_rpc_task(task);
7183 if (status == 0)
7184 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007185 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007186 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007187out:
7188 dprintk("<-- %s status=%d\n", __func__, status);
7189 return status;
7190}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007191
7192static void
7193nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
7194{
7195 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00007196 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007197 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007198
7199 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00007200 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
7201 * right now covering the LAYOUTGET we are about to send.
7202 * However, that is not so catastrophic, and there seems
7203 * to be no way to prevent it completely.
7204 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007205 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05007206 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007207 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00007208 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
7209 NFS_I(lgp->args.inode)->layout,
7210 lgp->args.ctx->state)) {
7211 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00007212 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007213}
7214
7215static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
7216{
7217 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07007218 struct inode *inode = lgp->args.inode;
7219 struct nfs_server *server = NFS_SERVER(inode);
7220 struct pnfs_layout_hdr *lo;
7221 struct nfs4_state *state = NULL;
Weston Andros Adamson30005122013-02-28 20:30:10 -05007222 unsigned long timeo, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007223
7224 dprintk("--> %s\n", __func__);
7225
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007226 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07007227 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007228
7229 switch (task->tk_status) {
7230 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07007231 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007232 case -NFS4ERR_LAYOUTTRYLATER:
7233 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05007234 timeo = rpc_get_timeout(task->tk_client);
7235 giveup = lgp->args.timestamp + timeo;
7236 if (time_after(giveup, jiffies))
7237 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07007238 break;
7239 case -NFS4ERR_EXPIRED:
7240 case -NFS4ERR_BAD_STATEID:
7241 spin_lock(&inode->i_lock);
7242 lo = NFS_I(inode)->layout;
7243 if (!lo || list_empty(&lo->plh_segs)) {
7244 spin_unlock(&inode->i_lock);
7245 /* If the open stateid was bad, then recover it. */
7246 state = lgp->args.ctx->state;
7247 } else {
7248 LIST_HEAD(head);
7249
7250 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
7251 spin_unlock(&inode->i_lock);
7252 /* Mark the bad layout state as invalid, then
7253 * retry using the open stateid. */
7254 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007255 }
7256 }
Trond Myklebustee314c22012-10-01 17:25:48 -07007257 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
7258 rpc_restart_call_prepare(task);
7259out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007260 dprintk("<-- %s\n", __func__);
7261}
7262
Idan Kedar85541162012-08-02 11:47:10 +03007263static size_t max_response_pages(struct nfs_server *server)
7264{
7265 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
7266 return nfs_page_array_len(0, max_resp_sz);
7267}
7268
7269static void nfs4_free_pages(struct page **pages, size_t size)
7270{
7271 int i;
7272
7273 if (!pages)
7274 return;
7275
7276 for (i = 0; i < size; i++) {
7277 if (!pages[i])
7278 break;
7279 __free_page(pages[i]);
7280 }
7281 kfree(pages);
7282}
7283
7284static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
7285{
7286 struct page **pages;
7287 int i;
7288
7289 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
7290 if (!pages) {
7291 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
7292 return NULL;
7293 }
7294
7295 for (i = 0; i < size; i++) {
7296 pages[i] = alloc_page(gfp_flags);
7297 if (!pages[i]) {
7298 dprintk("%s: failed to allocate page\n", __func__);
7299 nfs4_free_pages(pages, size);
7300 return NULL;
7301 }
7302 }
7303
7304 return pages;
7305}
7306
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007307static void nfs4_layoutget_release(void *calldata)
7308{
7309 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007310 struct inode *inode = lgp->args.inode;
7311 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007312 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007313
7314 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03007315 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007316 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007317 put_nfs_open_context(lgp->args.ctx);
7318 kfree(calldata);
7319 dprintk("<-- %s\n", __func__);
7320}
7321
7322static const struct rpc_call_ops nfs4_layoutget_call_ops = {
7323 .rpc_call_prepare = nfs4_layoutget_prepare,
7324 .rpc_call_done = nfs4_layoutget_done,
7325 .rpc_release = nfs4_layoutget_release,
7326};
7327
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007328struct pnfs_layout_segment *
7329nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007330{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007331 struct inode *inode = lgp->args.inode;
7332 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007333 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007334 struct rpc_task *task;
7335 struct rpc_message msg = {
7336 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
7337 .rpc_argp = &lgp->args,
7338 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04007339 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007340 };
7341 struct rpc_task_setup task_setup_data = {
7342 .rpc_client = server->client,
7343 .rpc_message = &msg,
7344 .callback_ops = &nfs4_layoutget_call_ops,
7345 .callback_data = lgp,
7346 .flags = RPC_TASK_ASYNC,
7347 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007348 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007349 int status = 0;
7350
7351 dprintk("--> %s\n", __func__);
7352
Idan Kedar85541162012-08-02 11:47:10 +03007353 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
7354 if (!lgp->args.layout.pages) {
7355 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007356 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03007357 }
7358 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05007359 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03007360
Weston Andros Adamson35124a02011-03-24 16:48:21 -04007361 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007362 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04007363 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007364
7365 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
7366 pnfs_get_layout_hdr(NFS_I(inode)->layout);
7367
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007368 task = rpc_run_task(&task_setup_data);
7369 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007370 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007371 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00007372 if (status == 0)
7373 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007374 trace_nfs4_layoutget(lgp->args.ctx,
7375 &lgp->args.range,
7376 &lgp->res.range,
7377 status);
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05007378 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
7379 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007380 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007381 rpc_put_task(task);
7382 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007383 if (status)
7384 return ERR_PTR(status);
7385 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007386}
7387
Benny Halevycbe82602011-05-22 19:52:37 +03007388static void
7389nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
7390{
7391 struct nfs4_layoutreturn *lrp = calldata;
7392
7393 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007394 nfs41_setup_sequence(lrp->clp->cl_session,
7395 &lrp->args.seq_args,
7396 &lrp->res.seq_res,
7397 task);
Benny Halevycbe82602011-05-22 19:52:37 +03007398}
7399
7400static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
7401{
7402 struct nfs4_layoutreturn *lrp = calldata;
7403 struct nfs_server *server;
7404
7405 dprintk("--> %s\n", __func__);
7406
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007407 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03007408 return;
7409
7410 server = NFS_SERVER(lrp->args.inode);
7411 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007412 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03007413 return;
7414 }
Benny Halevycbe82602011-05-22 19:52:37 +03007415 dprintk("<-- %s\n", __func__);
7416}
7417
7418static void nfs4_layoutreturn_release(void *calldata)
7419{
7420 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04007421 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03007422
7423 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04007424 spin_lock(&lo->plh_inode->i_lock);
7425 if (lrp->res.lrs_present)
7426 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
7427 lo->plh_block_lgets--;
7428 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04007429 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03007430 kfree(calldata);
7431 dprintk("<-- %s\n", __func__);
7432}
7433
7434static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
7435 .rpc_call_prepare = nfs4_layoutreturn_prepare,
7436 .rpc_call_done = nfs4_layoutreturn_done,
7437 .rpc_release = nfs4_layoutreturn_release,
7438};
7439
7440int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
7441{
7442 struct rpc_task *task;
7443 struct rpc_message msg = {
7444 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
7445 .rpc_argp = &lrp->args,
7446 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04007447 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03007448 };
7449 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04007450 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03007451 .rpc_message = &msg,
7452 .callback_ops = &nfs4_layoutreturn_call_ops,
7453 .callback_data = lrp,
7454 };
7455 int status;
7456
7457 dprintk("--> %s\n", __func__);
Chuck Levera9c92d62013-08-09 12:48:18 -04007458 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03007459 task = rpc_run_task(&task_setup_data);
7460 if (IS_ERR(task))
7461 return PTR_ERR(task);
7462 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007463 trace_nfs4_layoutreturn(lrp->args.inode, status);
Benny Halevycbe82602011-05-22 19:52:37 +03007464 dprintk("<-- %s status=%d\n", __func__, status);
7465 rpc_put_task(task);
7466 return status;
7467}
7468
Andy Adamson7f11d8d2011-07-30 20:52:35 -04007469/*
7470 * Retrieve the list of Data Server devices from the MDS.
7471 */
7472static int _nfs4_getdevicelist(struct nfs_server *server,
7473 const struct nfs_fh *fh,
7474 struct pnfs_devicelist *devlist)
7475{
7476 struct nfs4_getdevicelist_args args = {
7477 .fh = fh,
7478 .layoutclass = server->pnfs_curr_ld->id,
7479 };
7480 struct nfs4_getdevicelist_res res = {
7481 .devlist = devlist,
7482 };
7483 struct rpc_message msg = {
7484 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
7485 .rpc_argp = &args,
7486 .rpc_resp = &res,
7487 };
7488 int status;
7489
7490 dprintk("--> %s\n", __func__);
7491 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
7492 &res.seq_res, 0);
7493 dprintk("<-- %s status=%d\n", __func__, status);
7494 return status;
7495}
7496
7497int nfs4_proc_getdevicelist(struct nfs_server *server,
7498 const struct nfs_fh *fh,
7499 struct pnfs_devicelist *devlist)
7500{
7501 struct nfs4_exception exception = { };
7502 int err;
7503
7504 do {
7505 err = nfs4_handle_exception(server,
7506 _nfs4_getdevicelist(server, fh, devlist),
7507 &exception);
7508 } while (exception.retry);
7509
7510 dprintk("%s: err=%d, num_devs=%u\n", __func__,
7511 err, devlist->num_devs);
7512
7513 return err;
7514}
7515EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
7516
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007517static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007518_nfs4_proc_getdeviceinfo(struct nfs_server *server,
7519 struct pnfs_device *pdev,
7520 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007521{
7522 struct nfs4_getdeviceinfo_args args = {
7523 .pdev = pdev,
7524 };
7525 struct nfs4_getdeviceinfo_res res = {
7526 .pdev = pdev,
7527 };
7528 struct rpc_message msg = {
7529 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
7530 .rpc_argp = &args,
7531 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007532 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007533 };
7534 int status;
7535
7536 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00007537 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007538 dprintk("<-- %s status=%d\n", __func__, status);
7539
7540 return status;
7541}
7542
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007543int nfs4_proc_getdeviceinfo(struct nfs_server *server,
7544 struct pnfs_device *pdev,
7545 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007546{
7547 struct nfs4_exception exception = { };
7548 int err;
7549
7550 do {
7551 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007552 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007553 &exception);
7554 } while (exception.retry);
7555 return err;
7556}
7557EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
7558
Andy Adamson863a3c62011-03-23 13:27:54 +00007559static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
7560{
7561 struct nfs4_layoutcommit_data *data = calldata;
7562 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007563 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00007564
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007565 nfs41_setup_sequence(session,
7566 &data->args.seq_args,
7567 &data->res.seq_res,
7568 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00007569}
7570
7571static void
7572nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
7573{
7574 struct nfs4_layoutcommit_data *data = calldata;
7575 struct nfs_server *server = NFS_SERVER(data->args.inode);
7576
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007577 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00007578 return;
7579
7580 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007581 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
7582 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
7583 case -NFS4ERR_BADLAYOUT: /* no layout */
7584 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00007585 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007586 break;
7587 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00007588 nfs_post_op_update_inode_force_wcc(data->args.inode,
7589 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007590 break;
7591 default:
7592 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
7593 rpc_restart_call_prepare(task);
7594 return;
7595 }
7596 }
Andy Adamson863a3c62011-03-23 13:27:54 +00007597}
7598
7599static void nfs4_layoutcommit_release(void *calldata)
7600{
7601 struct nfs4_layoutcommit_data *data = calldata;
7602
Andy Adamsondb29c082011-07-30 20:52:38 -04007603 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00007604 put_rpccred(data->cred);
7605 kfree(data);
7606}
7607
7608static const struct rpc_call_ops nfs4_layoutcommit_ops = {
7609 .rpc_call_prepare = nfs4_layoutcommit_prepare,
7610 .rpc_call_done = nfs4_layoutcommit_done,
7611 .rpc_release = nfs4_layoutcommit_release,
7612};
7613
7614int
Andy Adamsonef311532011-03-12 02:58:10 -05007615nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00007616{
7617 struct rpc_message msg = {
7618 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
7619 .rpc_argp = &data->args,
7620 .rpc_resp = &data->res,
7621 .rpc_cred = data->cred,
7622 };
7623 struct rpc_task_setup task_setup_data = {
7624 .task = &data->task,
7625 .rpc_client = NFS_CLIENT(data->args.inode),
7626 .rpc_message = &msg,
7627 .callback_ops = &nfs4_layoutcommit_ops,
7628 .callback_data = data,
7629 .flags = RPC_TASK_ASYNC,
7630 };
7631 struct rpc_task *task;
7632 int status = 0;
7633
7634 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
7635 "lbw: %llu inode %lu\n",
7636 data->task.tk_pid, sync,
7637 data->args.lastbytewritten,
7638 data->args.inode->i_ino);
7639
Chuck Levera9c92d62013-08-09 12:48:18 -04007640 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00007641 task = rpc_run_task(&task_setup_data);
7642 if (IS_ERR(task))
7643 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05007644 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00007645 goto out;
7646 status = nfs4_wait_for_completion_rpc_task(task);
7647 if (status != 0)
7648 goto out;
7649 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007650 trace_nfs4_layoutcommit(data->args.inode, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00007651out:
7652 dprintk("%s: status %d\n", __func__, status);
7653 rpc_put_task(task);
7654 return status;
7655}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007656
Andy Adamson97431202013-08-08 10:57:56 -04007657/**
7658 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
7659 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
7660 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007661static int
7662_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007663 struct nfs_fsinfo *info,
7664 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007665{
7666 struct nfs41_secinfo_no_name_args args = {
7667 .style = SECINFO_STYLE_CURRENT_FH,
7668 };
7669 struct nfs4_secinfo_res res = {
7670 .flavors = flavors,
7671 };
7672 struct rpc_message msg = {
7673 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
7674 .rpc_argp = &args,
7675 .rpc_resp = &res,
7676 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007677 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007678 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007679 int status;
7680
7681 if (use_integrity) {
7682 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007683 cred = nfs4_get_clid_cred(server->nfs_client);
7684 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007685 }
7686
7687 dprintk("--> %s\n", __func__);
7688 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
7689 &res.seq_res, 0);
7690 dprintk("<-- %s status=%d\n", __func__, status);
7691
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007692 if (cred)
7693 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007694
7695 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007696}
7697
7698static int
7699nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7700 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
7701{
7702 struct nfs4_exception exception = { };
7703 int err;
7704 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007705 /* first try using integrity protection */
7706 err = -NFS4ERR_WRONGSEC;
7707
7708 /* try to use integrity protection with machine cred */
7709 if (_nfs4_is_integrity_protected(server->nfs_client))
7710 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
7711 flavors, true);
7712
7713 /*
7714 * if unable to use integrity protection, or SECINFO with
7715 * integrity protection returns NFS4ERR_WRONGSEC (which is
7716 * disallowed by spec, but exists in deployed servers) use
7717 * the current filesystem's rpc_client and the user cred.
7718 */
7719 if (err == -NFS4ERR_WRONGSEC)
7720 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
7721 flavors, false);
7722
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007723 switch (err) {
7724 case 0:
7725 case -NFS4ERR_WRONGSEC:
7726 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007727 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007728 default:
7729 err = nfs4_handle_exception(server, err, &exception);
7730 }
7731 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007732out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007733 return err;
7734}
7735
7736static int
7737nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
7738 struct nfs_fsinfo *info)
7739{
7740 int err;
7741 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04007742 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007743 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04007744 struct nfs4_secinfo4 *secinfo;
7745 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007746
7747 page = alloc_page(GFP_KERNEL);
7748 if (!page) {
7749 err = -ENOMEM;
7750 goto out;
7751 }
7752
7753 flavors = page_address(page);
7754 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
7755
7756 /*
7757 * Fall back on "guess and check" method if
7758 * the server doesn't support SECINFO_NO_NAME
7759 */
7760 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
7761 err = nfs4_find_root_sec(server, fhandle, info);
7762 goto out_freepage;
7763 }
7764 if (err)
7765 goto out_freepage;
7766
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04007767 for (i = 0; i < flavors->num_flavors; i++) {
7768 secinfo = &flavors->flavors[i];
7769
7770 switch (secinfo->flavor) {
7771 case RPC_AUTH_NULL:
7772 case RPC_AUTH_UNIX:
7773 case RPC_AUTH_GSS:
7774 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
7775 &secinfo->flavor_info);
7776 break;
7777 default:
7778 flavor = RPC_AUTH_MAXFLAVOR;
7779 break;
7780 }
7781
7782 if (flavor != RPC_AUTH_MAXFLAVOR) {
7783 err = nfs4_lookup_root_sec(server, fhandle,
7784 info, flavor);
7785 if (!err)
7786 break;
7787 }
7788 }
7789
7790 if (flavor == RPC_AUTH_MAXFLAVOR)
7791 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007792
7793out_freepage:
7794 put_page(page);
7795 if (err == -EACCES)
7796 return -EPERM;
7797out:
7798 return err;
7799}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007800
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007801static int _nfs41_test_stateid(struct nfs_server *server,
7802 nfs4_stateid *stateid,
7803 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04007804{
7805 int status;
7806 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007807 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04007808 };
7809 struct nfs41_test_stateid_res res;
7810 struct rpc_message msg = {
7811 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
7812 .rpc_argp = &args,
7813 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007814 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04007815 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007816 struct rpc_clnt *rpc_client = server->client;
7817
7818 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
7819 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007820
Chuck Lever38527b12012-07-11 16:30:23 -04007821 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04007822 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007823 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007824 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007825 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04007826 if (status != NFS_OK) {
7827 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04007828 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04007829 }
7830 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04007831 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04007832}
7833
Chuck Lever38527b12012-07-11 16:30:23 -04007834/**
7835 * nfs41_test_stateid - perform a TEST_STATEID operation
7836 *
7837 * @server: server / transport on which to perform the operation
7838 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007839 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04007840 *
7841 * Returns NFS_OK if the server recognizes that "stateid" is valid.
7842 * Otherwise a negative NFS4ERR value is returned if the operation
7843 * failed or the state ID is not currently valid.
7844 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007845static int nfs41_test_stateid(struct nfs_server *server,
7846 nfs4_stateid *stateid,
7847 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04007848{
7849 struct nfs4_exception exception = { };
7850 int err;
7851 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007852 err = _nfs41_test_stateid(server, stateid, cred);
Chuck Lever377e5072012-07-11 16:29:45 -04007853 if (err != -NFS4ERR_DELAY)
7854 break;
7855 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04007856 } while (exception.retry);
7857 return err;
7858}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007859
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007860struct nfs_free_stateid_data {
7861 struct nfs_server *server;
7862 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007863 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007864};
7865
7866static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
7867{
7868 struct nfs_free_stateid_data *data = calldata;
7869 nfs41_setup_sequence(nfs4_get_session(data->server),
7870 &data->args.seq_args,
7871 &data->res.seq_res,
7872 task);
7873}
7874
7875static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
7876{
7877 struct nfs_free_stateid_data *data = calldata;
7878
7879 nfs41_sequence_done(task, &data->res.seq_res);
7880
7881 switch (task->tk_status) {
7882 case -NFS4ERR_DELAY:
7883 if (nfs4_async_handle_error(task, data->server, NULL) == -EAGAIN)
7884 rpc_restart_call_prepare(task);
7885 }
7886}
7887
7888static void nfs41_free_stateid_release(void *calldata)
7889{
7890 kfree(calldata);
7891}
7892
Trond Myklebust17f26b12013-08-21 15:48:42 -04007893static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007894 .rpc_call_prepare = nfs41_free_stateid_prepare,
7895 .rpc_call_done = nfs41_free_stateid_done,
7896 .rpc_release = nfs41_free_stateid_release,
7897};
7898
7899static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
7900 nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007901 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007902 bool privileged)
7903{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007904 struct rpc_message msg = {
7905 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007906 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007907 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007908 struct rpc_task_setup task_setup = {
7909 .rpc_client = server->client,
7910 .rpc_message = &msg,
7911 .callback_ops = &nfs41_free_stateid_ops,
7912 .flags = RPC_TASK_ASYNC,
7913 };
7914 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007915
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007916 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
7917 &task_setup.rpc_client, &msg);
7918
Chuck Lever38527b12012-07-11 16:30:23 -04007919 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007920 data = kmalloc(sizeof(*data), GFP_NOFS);
7921 if (!data)
7922 return ERR_PTR(-ENOMEM);
7923 data->server = server;
7924 nfs4_stateid_copy(&data->args.stateid, stateid);
7925
7926 task_setup.callback_data = data;
7927
7928 msg.rpc_argp = &data->args;
7929 msg.rpc_resp = &data->res;
Chuck Levera9c92d62013-08-09 12:48:18 -04007930 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007931 if (privileged)
7932 nfs4_set_sequence_privileged(&data->args.seq_args);
7933
7934 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007935}
7936
Chuck Lever38527b12012-07-11 16:30:23 -04007937/**
7938 * nfs41_free_stateid - perform a FREE_STATEID operation
7939 *
7940 * @server: server / transport on which to perform the operation
7941 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007942 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04007943 *
7944 * Returns NFS_OK if the server freed "stateid". Otherwise a
7945 * negative NFS4ERR value is returned.
7946 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007947static int nfs41_free_stateid(struct nfs_server *server,
7948 nfs4_stateid *stateid,
7949 struct rpc_cred *cred)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007950{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007951 struct rpc_task *task;
7952 int ret;
7953
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007954 task = _nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007955 if (IS_ERR(task))
7956 return PTR_ERR(task);
7957 ret = rpc_wait_for_completion_task(task);
7958 if (!ret)
7959 ret = task->tk_status;
7960 rpc_put_task(task);
7961 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007962}
Trond Myklebust36281ca2012-03-04 18:13:56 -05007963
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007964static int nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
7965{
7966 struct rpc_task *task;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007967 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007968
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007969 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007970 nfs4_free_lock_state(server, lsp);
7971 if (IS_ERR(task))
7972 return PTR_ERR(task);
7973 rpc_put_task(task);
7974 return 0;
7975}
7976
Trond Myklebust36281ca2012-03-04 18:13:56 -05007977static bool nfs41_match_stateid(const nfs4_stateid *s1,
7978 const nfs4_stateid *s2)
7979{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007980 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007981 return false;
7982
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007983 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007984 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007985 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007986 return true;
7987
7988 return false;
7989}
7990
Andy Adamson557134a2009-04-01 09:21:53 -04007991#endif /* CONFIG_NFS_V4_1 */
7992
Trond Myklebust36281ca2012-03-04 18:13:56 -05007993static bool nfs4_match_stateid(const nfs4_stateid *s1,
7994 const nfs4_stateid *s2)
7995{
Trond Myklebustf597c532012-03-04 18:13:56 -05007996 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05007997}
7998
7999
Trond Myklebust17280172012-03-11 13:11:00 -04008000static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008001 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008002 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008003 .recover_open = nfs4_open_reclaim,
8004 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04008005 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04008006 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008007};
8008
Andy Adamson591d71c2009-04-01 09:22:47 -04008009#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008010static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008011 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8012 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8013 .recover_open = nfs4_open_reclaim,
8014 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05008015 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008016 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04008017 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04008018};
8019#endif /* CONFIG_NFS_V4_1 */
8020
Trond Myklebust17280172012-03-11 13:11:00 -04008021static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008022 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008023 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008024 .recover_open = nfs4_open_expired,
8025 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04008026 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008027};
8028
Andy Adamson591d71c2009-04-01 09:22:47 -04008029#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008030static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008031 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8032 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04008033 .recover_open = nfs41_open_expired,
8034 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05008035 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04008036};
8037#endif /* CONFIG_NFS_V4_1 */
8038
Trond Myklebust17280172012-03-11 13:11:00 -04008039static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008040 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04008041 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008042 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04008043};
8044
8045#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008046static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008047 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04008048 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008049 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04008050};
8051#endif
8052
Chuck Leverec011fe2013-10-17 14:12:39 -04008053static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008054 .get_locations = _nfs40_proc_get_locations,
Chuck Leverec011fe2013-10-17 14:12:39 -04008055};
8056
8057#if defined(CONFIG_NFS_V4_1)
8058static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008059 .get_locations = _nfs41_proc_get_locations,
Chuck Leverec011fe2013-10-17 14:12:39 -04008060};
8061#endif /* CONFIG_NFS_V4_1 */
8062
Trond Myklebust97dc1352010-06-16 09:52:26 -04008063static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
8064 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008065 .init_caps = NFS_CAP_READDIRPLUS
8066 | NFS_CAP_ATOMIC_OPEN
8067 | NFS_CAP_CHANGE_ATTR
8068 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008069 .init_client = nfs40_init_client,
8070 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008071 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008072 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008073 .free_lock_state = nfs4_release_lockowner,
Chuck Lever9915ea72013-08-09 12:48:27 -04008074 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008075 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
8076 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
8077 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008078 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008079};
8080
8081#if defined(CONFIG_NFS_V4_1)
8082static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
8083 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008084 .init_caps = NFS_CAP_READDIRPLUS
8085 | NFS_CAP_ATOMIC_OPEN
8086 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04008087 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04008088 | NFS_CAP_STATEID_NFSV41
8089 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008090 .init_client = nfs41_init_client,
8091 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008092 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008093 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008094 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04008095 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008096 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8097 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8098 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008099 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008100};
8101#endif
8102
Steve Dickson42c2c422013-05-22 12:50:38 -04008103#if defined(CONFIG_NFS_V4_2)
8104static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
8105 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04008106 .init_caps = NFS_CAP_READDIRPLUS
8107 | NFS_CAP_ATOMIC_OPEN
8108 | NFS_CAP_CHANGE_ATTR
8109 | NFS_CAP_POSIX_LOCK
8110 | NFS_CAP_STATEID_NFSV41
8111 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008112 .init_client = nfs41_init_client,
8113 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04008114 .match_stateid = nfs41_match_stateid,
8115 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04008116 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04008117 .call_sync_ops = &nfs41_call_sync_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04008118 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8119 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8120 .state_renewal_ops = &nfs41_state_renewal_ops,
8121};
8122#endif
8123
Trond Myklebust97dc1352010-06-16 09:52:26 -04008124const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
8125 [0] = &nfs_v4_0_minor_ops,
8126#if defined(CONFIG_NFS_V4_1)
8127 [1] = &nfs_v4_1_minor_ops,
8128#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04008129#if defined(CONFIG_NFS_V4_2)
8130 [2] = &nfs_v4_2_minor_ops,
8131#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04008132};
8133
Trond Myklebust17f26b12013-08-21 15:48:42 -04008134static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04008135 .create = nfs_create,
8136 .lookup = nfs_lookup,
8137 .atomic_open = nfs_atomic_open,
8138 .link = nfs_link,
8139 .unlink = nfs_unlink,
8140 .symlink = nfs_symlink,
8141 .mkdir = nfs_mkdir,
8142 .rmdir = nfs_rmdir,
8143 .mknod = nfs_mknod,
8144 .rename = nfs_rename,
8145 .permission = nfs_permission,
8146 .getattr = nfs_getattr,
8147 .setattr = nfs_setattr,
8148 .getxattr = generic_getxattr,
8149 .setxattr = generic_setxattr,
8150 .listxattr = generic_listxattr,
8151 .removexattr = generic_removexattr,
8152};
8153
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08008154static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008155 .permission = nfs_permission,
8156 .getattr = nfs_getattr,
8157 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008158 .getxattr = generic_getxattr,
8159 .setxattr = generic_setxattr,
8160 .listxattr = generic_listxattr,
8161 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008162};
8163
David Howells509de812006-08-22 20:06:11 -04008164const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008165 .version = 4, /* protocol version */
8166 .dentry_ops = &nfs4_dentry_operations,
8167 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008168 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04008169 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008170 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04008171 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04008172 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008173 .getattr = nfs4_proc_getattr,
8174 .setattr = nfs4_proc_setattr,
8175 .lookup = nfs4_proc_lookup,
8176 .access = nfs4_proc_access,
8177 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008178 .create = nfs4_proc_create,
8179 .remove = nfs4_proc_remove,
8180 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04008181 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008182 .unlink_done = nfs4_proc_unlink_done,
8183 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04008184 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04008185 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04008186 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008187 .link = nfs4_proc_link,
8188 .symlink = nfs4_proc_symlink,
8189 .mkdir = nfs4_proc_mkdir,
8190 .rmdir = nfs4_proc_remove,
8191 .readdir = nfs4_proc_readdir,
8192 .mknod = nfs4_proc_mknod,
8193 .statfs = nfs4_proc_statfs,
8194 .fsinfo = nfs4_proc_fsinfo,
8195 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04008196 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008197 .decode_dirent = nfs4_decode_dirent,
8198 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04008199 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04008200 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05008201 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008202 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04008203 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04008204 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008205 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008206 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04008207 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008208 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008209 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00008210 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04008211 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04008212 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04008213 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04008214 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04008215 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00008216 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04008217 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04008218 .create_server = nfs4_create_server,
8219 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008220};
8221
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008222static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
8223 .prefix = XATTR_NAME_NFSV4_ACL,
8224 .list = nfs4_xattr_list_nfs4_acl,
8225 .get = nfs4_xattr_get_nfs4_acl,
8226 .set = nfs4_xattr_set_nfs4_acl,
8227};
8228
8229const struct xattr_handler *nfs4_xattr_handlers[] = {
8230 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04008231#ifdef CONFIG_NFS_V4_SECURITY_LABEL
8232 &nfs4_xattr_nfs4_label_handler,
8233#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008234 NULL
8235};
8236
Linus Torvalds1da177e2005-04-16 15:20:36 -07008237/*
8238 * Local variables:
8239 * c-basic-offset: 8
8240 * End:
8241 */