blob: a1f620950f11016c725e7ca288f141ff25fd0898 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Trond Myklebust6926afd12012-01-07 13:22:46 -050054#include <linux/nfs_idmap.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000055#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050056#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010057#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050066#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000067#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Trond Myklebustc6d01c62013-08-09 11:51:26 -040069#include "nfs4trace.h"
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define NFSDBG_FACILITY NFSDBG_PROC
72
Trond Myklebust2066fe82006-09-15 08:30:46 -040073#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define NFS4_POLL_RETRY_MAX (15*HZ)
75
Trond Myklebustcdd4e682006-01-03 09:55:12 +010076struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010077static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080078static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050080static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Chuck Lever81934dd2012-03-01 17:01:57 -050081static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040082static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
83static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040084static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
85 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -040086 struct nfs4_state *state, struct nfs4_label *ilabel,
87 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040088#ifdef CONFIG_NFS_V4_1
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040089static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
90 struct rpc_cred *);
91static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *,
92 struct rpc_cred *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040093#endif
David Quigleyaa9c2662013-05-22 12:50:44 -040094
95#ifdef CONFIG_NFS_V4_SECURITY_LABEL
96static inline struct nfs4_label *
97nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
98 struct iattr *sattr, struct nfs4_label *label)
99{
100 int err;
101
102 if (label == NULL)
103 return NULL;
104
105 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
106 return NULL;
107
108 if (NFS_SERVER(dir)->nfs_client->cl_minorversion < 2)
109 return NULL;
110
111 err = security_dentry_init_security(dentry, sattr->ia_mode,
112 &dentry->d_name, (void **)&label->label, &label->len);
113 if (err == 0)
114 return label;
115
116 return NULL;
117}
118static inline void
119nfs4_label_release_security(struct nfs4_label *label)
120{
121 if (label)
122 security_release_secctx(label->label, label->len);
123}
124static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
125{
126 if (label)
127 return server->attr_bitmask;
128
129 return server->attr_bitmask_nl;
130}
131#else
132static inline struct nfs4_label *
133nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
134 struct iattr *sattr, struct nfs4_label *l)
135{ return NULL; }
136static inline void
137nfs4_label_release_security(struct nfs4_label *label)
138{ return; }
139static inline u32 *
140nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
141{ return server->attr_bitmask; }
142#endif
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500145static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Trond Myklebust52567b02009-10-23 14:46:42 -0400147 if (err >= -1000)
148 return err;
149 switch (err) {
150 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500151 case -NFS4ERR_LAYOUTTRYLATER:
152 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400153 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000154 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400155 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000156 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800157 case -NFS4ERR_BADOWNER:
158 case -NFS4ERR_BADNAME:
159 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400160 case -NFS4ERR_SHARE_DENIED:
161 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400162 case -NFS4ERR_MINOR_VERS_MISMATCH:
163 return -EPROTONOSUPPORT;
Weston Andros Adamson6168f622012-09-10 14:00:46 -0400164 case -NFS4ERR_ACCESS:
165 return -EACCES;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400166 case -NFS4ERR_FILE_OPEN:
167 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400168 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700170 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400171 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400173 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
175
176/*
177 * This is our standard bitmap for GETATTR requests.
178 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400179const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 FATTR4_WORD0_TYPE
181 | FATTR4_WORD0_CHANGE
182 | FATTR4_WORD0_SIZE
183 | FATTR4_WORD0_FSID
184 | FATTR4_WORD0_FILEID,
185 FATTR4_WORD1_MODE
186 | FATTR4_WORD1_NUMLINKS
187 | FATTR4_WORD1_OWNER
188 | FATTR4_WORD1_OWNER_GROUP
189 | FATTR4_WORD1_RAWDEV
190 | FATTR4_WORD1_SPACE_USED
191 | FATTR4_WORD1_TIME_ACCESS
192 | FATTR4_WORD1_TIME_METADATA
David Quigleyaa9c2662013-05-22 12:50:44 -0400193 | FATTR4_WORD1_TIME_MODIFY,
194#ifdef CONFIG_NFS_V4_SECURITY_LABEL
195 FATTR4_WORD2_SECURITY_LABEL
196#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197};
198
Trond Myklebust1549210f2012-06-05 09:16:47 -0400199static const u32 nfs4_pnfs_open_bitmap[3] = {
200 FATTR4_WORD0_TYPE
201 | FATTR4_WORD0_CHANGE
202 | FATTR4_WORD0_SIZE
203 | FATTR4_WORD0_FSID
204 | FATTR4_WORD0_FILEID,
205 FATTR4_WORD1_MODE
206 | FATTR4_WORD1_NUMLINKS
207 | FATTR4_WORD1_OWNER
208 | FATTR4_WORD1_OWNER_GROUP
209 | FATTR4_WORD1_RAWDEV
210 | FATTR4_WORD1_SPACE_USED
211 | FATTR4_WORD1_TIME_ACCESS
212 | FATTR4_WORD1_TIME_METADATA
213 | FATTR4_WORD1_TIME_MODIFY,
214 FATTR4_WORD2_MDSTHRESHOLD
215};
216
Andy Adamsone23008e2012-10-02 21:07:32 -0400217static const u32 nfs4_open_noattr_bitmap[3] = {
218 FATTR4_WORD0_TYPE
219 | FATTR4_WORD0_CHANGE
220 | FATTR4_WORD0_FILEID,
221};
222
David Quigleya09df2c2013-05-22 12:50:41 -0400223const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 FATTR4_WORD0_FILES_AVAIL
225 | FATTR4_WORD0_FILES_FREE
226 | FATTR4_WORD0_FILES_TOTAL,
227 FATTR4_WORD1_SPACE_AVAIL
228 | FATTR4_WORD1_SPACE_FREE
229 | FATTR4_WORD1_SPACE_TOTAL
230};
231
David Quigleya09df2c2013-05-22 12:50:41 -0400232const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 FATTR4_WORD0_MAXLINK
234 | FATTR4_WORD0_MAXNAME,
235 0
236};
237
Fred Isamandae100c2011-07-30 20:52:37 -0400238const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 | FATTR4_WORD0_MAXREAD
240 | FATTR4_WORD0_MAXWRITE
241 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700242 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400243 | FATTR4_WORD1_FS_LAYOUT_TYPES,
244 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245};
246
David Quigleya09df2c2013-05-22 12:50:41 -0400247const u32 nfs4_fs_locations_bitmap[3] = {
Manoj Naik830b8e32006-06-09 09:34:25 -0400248 FATTR4_WORD0_TYPE
249 | FATTR4_WORD0_CHANGE
250 | FATTR4_WORD0_SIZE
251 | FATTR4_WORD0_FSID
252 | FATTR4_WORD0_FILEID
253 | FATTR4_WORD0_FS_LOCATIONS,
254 FATTR4_WORD1_MODE
255 | FATTR4_WORD1_NUMLINKS
256 | FATTR4_WORD1_OWNER
257 | FATTR4_WORD1_OWNER_GROUP
258 | FATTR4_WORD1_RAWDEV
259 | FATTR4_WORD1_SPACE_USED
260 | FATTR4_WORD1_TIME_ACCESS
261 | FATTR4_WORD1_TIME_METADATA
262 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400263 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400264};
265
Al Virobc4785c2006-10-19 23:28:51 -0700266static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 struct nfs4_readdir_arg *readdir)
268{
Al Viro0dbb4c62006-10-19 23:28:49 -0700269 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000272 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
274 return;
275 }
276
277 readdir->cookie = 0;
278 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
279 if (cookie == 2)
280 return;
281
282 /*
283 * NFSv4 servers do not return entries for '.' and '..'
284 * Therefore, we fake these entries here. We let '.'
285 * have cookie 0 and '..' have cookie 1. Note that
286 * when talking to the server, we always send cookie 0
287 * instead of 1 or 2.
288 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800289 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 if (cookie == 0) {
292 *p++ = xdr_one; /* next */
293 *p++ = xdr_zero; /* cookie, first word */
294 *p++ = xdr_one; /* cookie, second word */
295 *p++ = xdr_one; /* entry len */
296 memcpy(p, ".\0\0\0", 4); /* entry */
297 p++;
298 *p++ = xdr_one; /* bitmap length */
299 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
300 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400301 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
303
304 *p++ = xdr_one; /* next */
305 *p++ = xdr_zero; /* cookie, first word */
306 *p++ = xdr_two; /* cookie, second word */
307 *p++ = xdr_two; /* entry len */
308 memcpy(p, "..\0\0", 4); /* entry */
309 p++;
310 *p++ = xdr_one; /* bitmap length */
311 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
312 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400313 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 readdir->pgbase = (char *)p - (char *)start;
316 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800317 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
319
Trond Myklebust65de8722008-12-23 15:21:44 -0500320static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
321{
322 int res = 0;
323
324 might_sleep();
325
326 if (*timeout <= 0)
327 *timeout = NFS4_POLL_RETRY_MIN;
328 if (*timeout > NFS4_POLL_RETRY_MAX)
329 *timeout = NFS4_POLL_RETRY_MAX;
Colin Cross416ad3c2013-05-06 23:50:06 +0000330 freezable_schedule_timeout_killable_unsafe(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500331 if (fatal_signal_pending(current))
332 res = -ERESTARTSYS;
333 *timeout <<= 1;
334 return res;
335}
336
337/* This is the error handling routine for processes that are allowed
338 * to sleep.
339 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800340static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500341{
342 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500343 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500344 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500345 int ret = errorcode;
346
347 exception->retry = 0;
348 switch(errorcode) {
349 case 0:
350 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500351 case -NFS4ERR_OPENMODE:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400352 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400353 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500354 exception->retry = 1;
355 return 0;
356 }
357 if (state == NULL)
358 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400359 ret = nfs4_schedule_stateid_recovery(server, state);
360 if (ret < 0)
361 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500362 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500363 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500364 case -NFS4ERR_ADMIN_REVOKED:
365 case -NFS4ERR_BAD_STATEID:
Trond Myklebustb570a972013-04-12 15:04:51 -0400366 if (inode != NULL && nfs4_have_delegation(inode, FMODE_READ)) {
367 nfs_remove_bad_delegation(inode);
368 exception->retry = 1;
369 break;
370 }
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500371 if (state == NULL)
372 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400373 ret = nfs4_schedule_stateid_recovery(server, state);
374 if (ret < 0)
375 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500376 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400377 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -0400378 if (state != NULL) {
379 ret = nfs4_schedule_stateid_recovery(server, state);
380 if (ret < 0)
381 break;
382 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500383 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500384 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500385 nfs4_schedule_lease_recovery(clp);
386 goto wait_on_recovery;
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 Myklebust035168ab2010-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 Myklebust035168ab2010-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 Myklebust035168ab2010-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 Myklebust035168ab2010-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 Myklebust6926afd12012-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 Myklebust6926afd12012-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 Myklebust6926afd12012-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 Myklebustdb4f2e62013-04-01 15:56:46 -04001553static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001555 switch (err) {
1556 default:
1557 printk(KERN_ERR "NFS: %s: unhandled error "
1558 "%d.\n", __func__, err);
1559 case 0:
1560 case -ENOENT:
1561 case -ESTALE:
1562 break;
1563 case -NFS4ERR_BADSESSION:
1564 case -NFS4ERR_BADSLOT:
1565 case -NFS4ERR_BAD_HIGH_SLOT:
1566 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1567 case -NFS4ERR_DEADSESSION:
1568 set_bit(NFS_DELEGATED_STATE, &state->flags);
1569 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1570 return -EAGAIN;
1571 case -NFS4ERR_STALE_CLIENTID:
1572 case -NFS4ERR_STALE_STATEID:
1573 set_bit(NFS_DELEGATED_STATE, &state->flags);
1574 case -NFS4ERR_EXPIRED:
1575 /* Don't recall a delegation if it was lost */
1576 nfs4_schedule_lease_recovery(server->nfs_client);
1577 return -EAGAIN;
1578 case -NFS4ERR_DELEG_REVOKED:
1579 case -NFS4ERR_ADMIN_REVOKED:
1580 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001581 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001582 nfs_inode_find_state_and_recover(state->inode,
1583 stateid);
1584 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001585 return 0;
1586 case -NFS4ERR_DELAY:
1587 case -NFS4ERR_GRACE:
1588 set_bit(NFS_DELEGATED_STATE, &state->flags);
1589 ssleep(1);
1590 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001591 case -ENOMEM:
1592 case -NFS4ERR_DENIED:
1593 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1594 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 return err;
1597}
1598
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001599int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1600{
1601 struct nfs_server *server = NFS_SERVER(state->inode);
1602 struct nfs4_opendata *opendata;
1603 int err;
1604
1605 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1606 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1607 if (IS_ERR(opendata))
1608 return PTR_ERR(opendata);
1609 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1610 err = nfs4_open_recover(opendata, state);
1611 nfs4_opendata_put(opendata);
1612 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1613}
1614
Chuck Leverbe05c862013-08-09 12:49:47 -04001615static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1616{
1617 struct nfs4_opendata *data = calldata;
1618
1619 nfs40_setup_sequence(data->o_arg.server, &data->o_arg.seq_args,
1620 &data->o_res.seq_res, task);
1621}
1622
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001623static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1624{
1625 struct nfs4_opendata *data = calldata;
1626
Chuck Leverbe05c862013-08-09 12:49:47 -04001627 nfs40_sequence_done(task, &data->o_res.seq_res);
1628
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001629 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001630 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001631 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001632 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001633 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001634 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001635 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001636}
1637
1638static void nfs4_open_confirm_release(void *calldata)
1639{
1640 struct nfs4_opendata *data = calldata;
1641 struct nfs4_state *state = NULL;
1642
1643 /* If this request hasn't been cancelled, do nothing */
1644 if (data->cancelled == 0)
1645 goto out_free;
1646 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001647 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001648 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001649 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001650 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001651 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001652out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001653 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001654}
1655
1656static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04001657 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001658 .rpc_call_done = nfs4_open_confirm_done,
1659 .rpc_release = nfs4_open_confirm_release,
1660};
1661
1662/*
1663 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1664 */
1665static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1666{
1667 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1668 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001669 struct rpc_message msg = {
1670 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1671 .rpc_argp = &data->c_arg,
1672 .rpc_resp = &data->c_res,
1673 .rpc_cred = data->owner->so_cred,
1674 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001675 struct rpc_task_setup task_setup_data = {
1676 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001677 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001678 .callback_ops = &nfs4_open_confirm_ops,
1679 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001680 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001681 .flags = RPC_TASK_ASYNC,
1682 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 int status;
1684
Chuck Leverbe05c862013-08-09 12:49:47 -04001685 nfs4_init_sequence(&data->o_arg.seq_args, &data->o_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001686 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001687 data->rpc_done = 0;
1688 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001689 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001690 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001691 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001692 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001693 status = nfs4_wait_for_completion_rpc_task(task);
1694 if (status != 0) {
1695 data->cancelled = 1;
1696 smp_wmb();
1697 } else
1698 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001699 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 return status;
1701}
1702
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001703static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001705 struct nfs4_opendata *data = calldata;
1706 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001707 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001708
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001709 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001710 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001711 /*
1712 * Check if we still need to send an OPEN call, or if we can use
1713 * a delegation instead.
1714 */
1715 if (data->state != NULL) {
1716 struct nfs_delegation *delegation;
1717
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001718 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001719 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001720 rcu_read_lock();
1721 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001722 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
Trond Myklebustcd4c9be2013-04-23 14:46:25 -04001723 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
Trond Myklebust652f89f2011-12-09 19:05:58 -05001724 can_open_delegated(delegation, data->o_arg.fmode))
1725 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001726 rcu_read_unlock();
1727 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001728 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04001729 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust8188df12013-04-23 14:31:19 -04001730 switch (data->o_arg.claim) {
1731 case NFS4_OPEN_CLAIM_PREVIOUS:
1732 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1733 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001734 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04001735 case NFS4_OPEN_CLAIM_FH:
1736 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001737 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1738 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001739 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04001740 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001741 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001742 &data->o_res.seq_res,
1743 task) != 0)
1744 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04001745
1746 /* Set the create mode (note dependency on the session type) */
1747 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1748 if (data->o_arg.open_flags & O_EXCL) {
1749 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1750 if (nfs4_has_persistent_session(clp))
1751 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1752 else if (clp->cl_mvops->minor_version > 0)
1753 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1754 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04001755 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001756unlock_no_action:
1757 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001758out_no_action:
1759 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001760out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001761 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001762}
1763
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001764static void nfs4_open_done(struct rpc_task *task, void *calldata)
1765{
1766 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001768 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001769
Trond Myklebust14516c32010-07-31 14:29:06 -04001770 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1771 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001772
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001773 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001774 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1775 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001776 case S_IFREG:
1777 break;
1778 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001779 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001780 break;
1781 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001782 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001783 break;
1784 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001785 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001786 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001787 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001788 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001789 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1790 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001791 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001792 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001793}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001794
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001795static void nfs4_open_release(void *calldata)
1796{
1797 struct nfs4_opendata *data = calldata;
1798 struct nfs4_state *state = NULL;
1799
1800 /* If this request hasn't been cancelled, do nothing */
1801 if (data->cancelled == 0)
1802 goto out_free;
1803 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001804 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001805 goto out_free;
1806 /* In case we need an open_confirm, no cleanup! */
1807 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1808 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001809 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001810 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001811 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001812out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001813 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001814}
1815
1816static const struct rpc_call_ops nfs4_open_ops = {
1817 .rpc_call_prepare = nfs4_open_prepare,
1818 .rpc_call_done = nfs4_open_done,
1819 .rpc_release = nfs4_open_release,
1820};
1821
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001822static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001823{
1824 struct inode *dir = data->dir->d_inode;
1825 struct nfs_server *server = NFS_SERVER(dir);
1826 struct nfs_openargs *o_arg = &data->o_arg;
1827 struct nfs_openres *o_res = &data->o_res;
1828 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001829 struct rpc_message msg = {
1830 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1831 .rpc_argp = o_arg,
1832 .rpc_resp = o_res,
1833 .rpc_cred = data->owner->so_cred,
1834 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001835 struct rpc_task_setup task_setup_data = {
1836 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001837 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001838 .callback_ops = &nfs4_open_ops,
1839 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001840 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001841 .flags = RPC_TASK_ASYNC,
1842 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001843 int status;
1844
Chuck Levera9c92d62013-08-09 12:48:18 -04001845 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001846 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001847 data->rpc_done = 0;
1848 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001849 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001850 data->is_recover = 0;
1851 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001852 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001853 data->is_recover = 1;
1854 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04001855 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001856 if (IS_ERR(task))
1857 return PTR_ERR(task);
1858 status = nfs4_wait_for_completion_rpc_task(task);
1859 if (status != 0) {
1860 data->cancelled = 1;
1861 smp_wmb();
1862 } else
1863 status = data->rpc_status;
1864 rpc_put_task(task);
1865
1866 return status;
1867}
1868
1869static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1870{
1871 struct inode *dir = data->dir->d_inode;
1872 struct nfs_openres *o_res = &data->o_res;
1873 int status;
1874
1875 status = nfs4_run_open_task(data, 1);
1876 if (status != 0 || !data->rpc_done)
1877 return status;
1878
Trond Myklebust6926afd12012-01-07 13:22:46 -05001879 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1880
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001881 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1882 status = _nfs4_proc_open_confirm(data);
1883 if (status != 0)
1884 return status;
1885 }
1886
1887 return status;
1888}
1889
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001890static int nfs4_opendata_access(struct rpc_cred *cred,
1891 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001892 struct nfs4_state *state, fmode_t fmode,
1893 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001894{
1895 struct nfs_access_entry cache;
1896 u32 mask;
1897
1898 /* access call failed or for some reason the server doesn't
1899 * support any access modes -- defer access call until later */
1900 if (opendata->o_res.access_supported == 0)
1901 return 0;
1902
1903 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001904 /* don't check MAY_WRITE - a newly created file may not have
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001905 * write mode bits, but POSIX allows the creating process to write.
1906 * use openflags to check for exec, because fmode won't
1907 * always have FMODE_EXEC set when file open for exec. */
1908 if (openflags & __FMODE_EXEC) {
1909 /* ONLY check for exec rights */
1910 mask = MAY_EXEC;
1911 } else if (fmode & FMODE_READ)
1912 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001913
1914 cache.cred = cred;
1915 cache.jiffies = jiffies;
1916 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1917 nfs_access_add_cache(state->inode, &cache);
1918
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001919 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001920 return 0;
1921
1922 /* even though OPEN succeeded, access is denied. Close the file */
1923 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001924 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001925}
1926
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001927/*
1928 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1929 */
1930static int _nfs4_proc_open(struct nfs4_opendata *data)
1931{
1932 struct inode *dir = data->dir->d_inode;
1933 struct nfs_server *server = NFS_SERVER(dir);
1934 struct nfs_openargs *o_arg = &data->o_arg;
1935 struct nfs_openres *o_res = &data->o_res;
1936 int status;
1937
1938 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001939 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001940 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001941 if (status != 0) {
1942 if (status == -NFS4ERR_BADNAME &&
1943 !(o_arg->open_flags & O_CREAT))
1944 return -ENOENT;
1945 return status;
1946 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001947
Trond Myklebust6926afd12012-01-07 13:22:46 -05001948 nfs_fattr_map_and_free_names(server, &data->f_attr);
1949
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001950 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001951 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001952 if (o_arg->open_flags & O_EXCL)
1953 data->file_created = 1;
1954 else if (o_res->cinfo.before != o_res->cinfo.after)
1955 data->file_created = 1;
1956 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001957 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1958 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001960 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001962 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 }
1964 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
David Quigley1775fd32013-05-22 12:50:42 -04001965 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001966 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967}
1968
Andy Adamsond83217c2011-03-01 01:34:17 +00001969static int nfs4_recover_expired_lease(struct nfs_server *server)
1970{
1971 return nfs4_client_recover_expired_lease(server->nfs_client);
1972}
1973
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974/*
1975 * OPEN_EXPIRED:
1976 * reclaim state on the server after a network partition.
1977 * Assumes caller holds the appropriate lock
1978 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001979static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001981 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001982 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001984 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001985 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001986 if (IS_ERR(opendata))
1987 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001988 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001989 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001990 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001991 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001992 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993}
1994
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001995static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001996{
Trond Myklebust539cd032007-06-05 11:46:42 -04001997 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001998 struct nfs4_exception exception = { };
1999 int err;
2000
2001 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002002 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002003 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002004 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2005 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002006 switch (err) {
2007 default:
2008 goto out;
2009 case -NFS4ERR_GRACE:
2010 case -NFS4ERR_DELAY:
2011 nfs4_handle_exception(server, err, &exception);
2012 err = 0;
2013 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002014 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002015out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002016 return err;
2017}
2018
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2020{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002022 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
Trond Myklebust864472e2006-01-03 09:55:15 +01002024 ctx = nfs4_state_find_open_context(state);
2025 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002026 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002027 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002028 put_nfs_open_context(ctx);
2029 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030}
2031
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002032#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002033static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002034{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002035 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002036 nfs4_stateid *stateid = &state->stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002037 struct nfs_delegation *delegation;
2038 struct rpc_cred *cred = NULL;
2039 int status = -NFS4ERR_BAD_STATEID;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002040
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002041 /* If a state reset has been done, test_stateid is unneeded */
2042 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2043 return;
2044
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002045 /* Get the delegation credential for use by test/free_stateid */
2046 rcu_read_lock();
2047 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2048 if (delegation != NULL &&
2049 nfs4_stateid_match(&delegation->stateid, stateid)) {
2050 cred = get_rpccred(delegation->cred);
2051 rcu_read_unlock();
2052 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002053 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002054 } else
2055 rcu_read_unlock();
2056
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002057 if (status != NFS_OK) {
2058 /* Free the stateid unless the server explicitly
2059 * informs us the stateid is unrecognized. */
2060 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002061 nfs41_free_stateid(server, stateid, cred);
Bryan Schumaker69388672012-09-26 15:25:52 -04002062 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002063
Bryan Schumaker69388672012-09-26 15:25:52 -04002064 write_seqlock(&state->seqlock);
2065 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2066 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002067 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2068 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002069
2070 if (cred != NULL)
2071 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002072}
2073
2074/**
2075 * nfs41_check_open_stateid - possibly free an open stateid
2076 *
2077 * @state: NFSv4 state for an inode
2078 *
2079 * Returns NFS_OK if recovery for this stateid is now finished.
2080 * Otherwise a negative NFS4ERR value is returned.
2081 */
2082static int nfs41_check_open_stateid(struct nfs4_state *state)
2083{
2084 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002085 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002086 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002087 int status;
2088
2089 /* If a state reset has been done, test_stateid is unneeded */
2090 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2091 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2092 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2093 return -NFS4ERR_BAD_STATEID;
2094
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002095 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002096 trace_nfs4_test_open_stateid(state, NULL, status);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002097 if (status != NFS_OK) {
2098 /* Free the stateid unless the server explicitly
2099 * informs us the stateid is unrecognized. */
2100 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002101 nfs41_free_stateid(server, stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002102
2103 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2104 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2105 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002106 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002107 }
2108 return status;
2109}
2110
2111static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2112{
Chuck Levereb64cf92012-07-11 16:30:05 -04002113 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002114
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002115 nfs41_clear_delegation_stateid(state);
2116 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002117 if (status != NFS_OK)
2118 status = nfs4_open_expired(sp, state);
2119 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002120}
2121#endif
2122
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002124 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2125 * fields corresponding to attributes that were used to store the verifier.
2126 * Make sure we clobber those fields in the later setattr call
2127 */
2128static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2129{
2130 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2131 !(sattr->ia_valid & ATTR_ATIME_SET))
2132 sattr->ia_valid |= ATTR_ATIME;
2133
2134 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2135 !(sattr->ia_valid & ATTR_MTIME_SET))
2136 sattr->ia_valid |= ATTR_MTIME;
2137}
2138
Trond Myklebustc21443c2013-02-07 14:26:21 -05002139static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2140 fmode_t fmode,
2141 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002142 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002143{
2144 struct nfs4_state_owner *sp = opendata->owner;
2145 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002146 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002147 struct nfs4_state *state;
2148 unsigned int seq;
2149 int ret;
2150
2151 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2152
2153 ret = _nfs4_proc_open(opendata);
2154 if (ret != 0)
2155 goto out;
2156
2157 state = nfs4_opendata_to_nfs4_state(opendata);
2158 ret = PTR_ERR(state);
2159 if (IS_ERR(state))
2160 goto out;
2161 if (server->caps & NFS_CAP_POSIX_LOCK)
2162 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2163
Trond Myklebust275bb302013-05-29 13:11:28 -04002164 dentry = opendata->dentry;
2165 if (dentry->d_inode == NULL) {
2166 /* FIXME: Is this d_drop() ever needed? */
2167 d_drop(dentry);
2168 dentry = d_add_unique(dentry, igrab(state->inode));
2169 if (dentry == NULL) {
2170 dentry = opendata->dentry;
2171 } else if (dentry != ctx->dentry) {
2172 dput(ctx->dentry);
2173 ctx->dentry = dget(dentry);
2174 }
2175 nfs_set_verifier(dentry,
2176 nfs_save_change_attribute(opendata->dir->d_inode));
2177 }
2178
Trond Myklebustc21443c2013-02-07 14:26:21 -05002179 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2180 if (ret != 0)
2181 goto out;
2182
Trond Myklebust3efb9722013-05-29 13:17:04 -04002183 ctx->state = state;
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002184 if (dentry->d_inode == state->inode) {
2185 nfs_inode_attach_open_context(ctx);
2186 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2187 nfs4_schedule_stateid_recovery(server, state);
2188 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002189out:
2190 return ret;
2191}
2192
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002193/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002194 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 */
Andy Adamson82be4172012-05-23 05:02:35 -04002196static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002197 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002198 int flags,
2199 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002200 struct nfs4_label *label,
2201 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202{
2203 struct nfs4_state_owner *sp;
2204 struct nfs4_state *state = NULL;
2205 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002206 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002207 struct dentry *dentry = ctx->dentry;
2208 struct rpc_cred *cred = ctx->cred;
2209 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2210 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002211 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002212 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002213 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
2215 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002217 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2218 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2220 goto out_err;
2221 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002222 status = nfs4_recover_expired_lease(server);
2223 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002224 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04002225 if (dentry->d_inode != NULL)
2226 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002227 status = -ENOMEM;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002228 if (dentry->d_inode)
2229 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002230 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002231 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002232 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002233 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
David Quigley14c43f72013-05-22 12:50:43 -04002235 if (label) {
2236 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2237 if (IS_ERR(olabel)) {
2238 status = PTR_ERR(olabel);
2239 goto err_opendata_put;
2240 }
2241 }
2242
Andy Adamson82be4172012-05-23 05:02:35 -04002243 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2244 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2245 if (!opendata->f_attr.mdsthreshold)
David Quigley14c43f72013-05-22 12:50:43 -04002246 goto err_free_label;
Trond Myklebust1549210f2012-06-05 09:16:47 -04002247 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002248 }
Al Viro82a2c1b2011-06-22 18:30:55 -04002249 if (dentry->d_inode != NULL)
2250 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002251
Trond Myklebust3efb9722013-05-29 13:17:04 -04002252 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002253 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002254 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002255 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002256
Trond Myklebust549b19c2013-04-16 18:42:34 -04002257 if ((opendata->o_arg.open_flags & O_EXCL) &&
2258 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002259 nfs4_exclusive_attrset(opendata, sattr);
2260
2261 nfs_fattr_init(opendata->o_res.f_attr);
2262 status = nfs4_do_setattr(state->inode, cred,
2263 opendata->o_res.f_attr, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002264 state, label, olabel);
2265 if (status == 0) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002266 nfs_setattr_update_inode(state->inode, sattr);
David Quigley1775fd32013-05-22 12:50:42 -04002267 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
David Quigleyaa9c2662013-05-22 12:50:44 -04002268 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
David Quigley1775fd32013-05-22 12:50:42 -04002269 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002270 }
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002271 if (opendata->file_created)
2272 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002273
2274 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2275 *ctx_th = opendata->f_attr.mdsthreshold;
2276 else
2277 kfree(opendata->f_attr.mdsthreshold);
2278 opendata->f_attr.mdsthreshold = NULL;
2279
David Quigley14c43f72013-05-22 12:50:43 -04002280 nfs4_label_free(olabel);
2281
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002282 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002285err_free_label:
2286 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002287err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002288 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002289 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002290err_put_state_owner:
2291 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 return status;
2294}
2295
2296
Andy Adamson82be4172012-05-23 05:02:35 -04002297static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002298 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002299 int flags,
2300 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002301 struct nfs4_label *label,
2302 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002304 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 struct nfs4_exception exception = { };
2306 struct nfs4_state *res;
2307 int status;
2308
2309 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002310 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002311 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002312 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 if (status == 0)
2314 break;
2315 /* NOTE: BAD_SEQID means the server and client disagree about the
2316 * book-keeping w.r.t. state-changing operations
2317 * (OPEN/CLOSE/LOCK/LOCKU...)
2318 * It is actually a sign of a bug on the client or on the server.
2319 *
2320 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002321 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 * have unhashed the old state_owner for us, and that we can
2323 * therefore safely retry using a new one. We should still warn
2324 * the user though...
2325 */
2326 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002327 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002328 " returned a bad sequence-id error!\n",
2329 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 exception.retry = 1;
2331 continue;
2332 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002333 /*
2334 * BAD_STATEID on OPEN means that the server cancelled our
2335 * state before it received the OPEN_CONFIRM.
2336 * Recover by retrying the request as per the discussion
2337 * on Page 181 of RFC3530.
2338 */
2339 if (status == -NFS4ERR_BAD_STATEID) {
2340 exception.retry = 1;
2341 continue;
2342 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002343 if (status == -EAGAIN) {
2344 /* We must have found a delegation */
2345 exception.retry = 1;
2346 continue;
2347 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002348 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2349 continue;
2350 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 status, &exception));
2352 } while (exception.retry);
2353 return res;
2354}
2355
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002356static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2357 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002358 struct nfs4_state *state, struct nfs4_label *ilabel,
2359 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002361 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002363 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 .iap = sattr,
2365 .server = server,
2366 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002367 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 };
2369 struct nfs_setattrres res = {
2370 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002371 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 .server = server,
2373 };
2374 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002375 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2376 .rpc_argp = &arg,
2377 .rpc_resp = &res,
2378 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002380 unsigned long timestamp = jiffies;
Trond Myklebustee3ae842013-04-29 10:35:36 -04002381 fmode_t fmode;
2382 bool truncate;
Trond Myklebust65e43082005-08-16 11:49:44 -04002383 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
David Quigleyaa9c2662013-05-22 12:50:44 -04002385 arg.bitmask = nfs4_bitmask(server, ilabel);
2386 if (ilabel)
2387 arg.bitmask = nfs4_bitmask(server, olabel);
2388
Trond Myklebust0e574af2005-10-27 22:12:38 -04002389 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
Trond Myklebustee3ae842013-04-29 10:35:36 -04002391 /* Servers should only apply open mode checks for file size changes */
2392 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2393 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2394
2395 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2396 /* Use that stateid */
2397 } else if (truncate && state != NULL && nfs4_valid_open_stateid(state)) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002398 struct nfs_lockowner lockowner = {
2399 .l_owner = current->files,
2400 .l_pid = current->tgid,
2401 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002402 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002403 &lockowner);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002404 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002405 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
Bryan Schumaker7c513052011-03-24 17:12:24 +00002407 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002408 if (status == 0 && state != NULL)
2409 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002410 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411}
2412
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002413static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2414 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002415 struct nfs4_state *state, struct nfs4_label *ilabel,
2416 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002418 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002419 struct nfs4_exception exception = {
2420 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002421 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002422 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 int err;
2424 do {
David Quigley1775fd32013-05-22 12:50:42 -04002425 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
Trond Myklebustc1578b72013-08-12 16:58:42 -04002426 trace_nfs4_setattr(inode, err);
Trond Myklebust451146b2012-04-18 16:29:11 -04002427 switch (err) {
2428 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002429 if (!(sattr->ia_valid & ATTR_SIZE)) {
2430 pr_warn_once("NFSv4: server %s is incorrectly "
2431 "applying open mode checks to "
2432 "a SETATTR that is not "
2433 "changing file size.\n",
2434 server->nfs_client->cl_hostname);
2435 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002436 if (state && !(state->state & FMODE_WRITE)) {
2437 err = -EBADF;
2438 if (sattr->ia_valid & ATTR_OPEN)
2439 err = -EACCES;
2440 goto out;
2441 }
2442 }
2443 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002445out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 return err;
2447}
2448
2449struct nfs4_closedata {
2450 struct inode *inode;
2451 struct nfs4_state *state;
2452 struct nfs_closeargs arg;
2453 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002454 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002455 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002456 bool roc;
2457 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458};
2459
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002460static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002461{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002462 struct nfs4_closedata *calldata = data;
2463 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002464 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002465
Fred Isamanf7e89172011-01-06 11:36:32 +00002466 if (calldata->roc)
2467 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002468 nfs4_put_open_state(calldata->state);
2469 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002470 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002471 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002472 kfree(calldata);
2473}
2474
Trond Myklebust88069f72009-12-08 08:33:16 -05002475static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2476 fmode_t fmode)
2477{
2478 spin_lock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002479 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002480 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
2481 case FMODE_WRITE:
2482 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2483 break;
2484 case FMODE_READ:
2485 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2486 break;
2487 case 0:
2488 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2489 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2490 clear_bit(NFS_OPEN_STATE, &state->flags);
2491 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002492 spin_unlock(&state->owner->so_lock);
2493}
2494
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002495static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002497 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 struct nfs_server *server = NFS_SERVER(calldata->inode);
2500
Chuck Levera3ca5652012-03-01 17:00:40 -05002501 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002502 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2503 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002504 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 /* hmm. we are done with the inode, and in the process of freeing
2506 * the state_owner. we keep this around to process errors
2507 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 switch (task->tk_status) {
2509 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002510 if (calldata->roc)
2511 pnfs_roc_set_barrier(state->inode,
2512 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002513 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002514 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002515 nfs4_close_clear_stateid_flags(state,
2516 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 break;
2518 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002519 case -NFS4ERR_OLD_STATEID:
2520 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002522 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002523 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002525 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2526 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002528 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002529 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002530 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531}
2532
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002533static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002535 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002536 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002537 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002538 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002539
Chuck Levera3ca5652012-03-01 17:00:40 -05002540 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002541 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002542 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002543
Trond Myklebust88069f72009-12-08 08:33:16 -05002544 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2545 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002546 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002547 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002548 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002549 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002550 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2551 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2552 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002553 }
2554 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002555 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2556 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2557 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002558 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002559 }
Trond Myklebust5d422302013-03-14 16:57:48 -04002560 if (!nfs4_valid_open_stateid(state))
2561 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002562 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002563
2564 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002565 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002566 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002567 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002568
Fred Isamanf7e89172011-01-06 11:36:32 +00002569 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002570 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002571 if (calldata->roc &&
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002572 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2573 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002574 goto out_wait;
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002575 }
Fred Isamanf7e89172011-01-06 11:36:32 +00002576 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002577
Trond Myklebust516a6af2005-10-27 22:12:41 -04002578 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002579 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002580 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002581 &calldata->arg.seq_args,
2582 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002583 task) != 0)
2584 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002585 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002586 return;
2587out_no_action:
2588 task->tk_action = NULL;
2589out_wait:
2590 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591}
2592
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002593static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002594 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002595 .rpc_call_done = nfs4_close_done,
2596 .rpc_release = nfs4_free_closedata,
2597};
2598
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599/*
2600 * It is possible for data to be read/written from a mem-mapped file
2601 * after the sys_close call (which hits the vfs layer as a flush).
2602 * This means that we can't safely call nfsv4 close on a file until
2603 * the inode is cleared. This in turn means that we are not good
2604 * NFSv4 citizens - we do not indicate to the server to update the file's
2605 * share state even when we are done with one of the three share
2606 * stateid's in the inode.
2607 *
2608 * NOTE: Caller must be holding the sp->so_owner semaphore!
2609 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002610int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002612 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002614 struct nfs4_state_owner *sp = state->owner;
2615 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002616 struct rpc_message msg = {
2617 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2618 .rpc_cred = state->owner->so_cred,
2619 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002620 struct rpc_task_setup task_setup_data = {
2621 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002622 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002623 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002624 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002625 .flags = RPC_TASK_ASYNC,
2626 };
Trond Myklebust95121352005-10-18 14:20:12 -07002627 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04002629 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
2630 &task_setup_data.rpc_client, &msg);
2631
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002632 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002634 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04002635 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002636 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002638 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002639 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002641 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002642 if (calldata->arg.seqid == NULL)
2643 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002644 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002645 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002646 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002647 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002648 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002649 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002650 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002651
Trond Myklebust1174dd12010-12-21 10:52:24 -05002652 msg.rpc_argp = &calldata->arg;
2653 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002654 task_setup_data.callback_data = calldata;
2655 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002656 if (IS_ERR(task))
2657 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002658 status = 0;
2659 if (wait)
2660 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002661 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002662 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002663out_free_calldata:
2664 kfree(calldata);
2665out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002666 nfs4_put_open_state(state);
2667 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002668 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669}
2670
Trond Myklebust2b484292010-09-17 10:56:51 -04002671static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002672nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
2673 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04002676 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2677
2678 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679
Trond Myklebust565277f2007-10-15 18:17:53 -04002680 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002681 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04002682
2683 nfs4_label_release_security(label);
2684
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002685 if (IS_ERR(state))
2686 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04002687 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688}
2689
Trond Myklebust1185a552009-12-03 15:54:02 -05002690static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002691{
2692 if (ctx->state == NULL)
2693 return;
2694 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002695 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002696 else
Al Viro643168c2011-06-22 18:20:23 -04002697 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002698}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699
2700static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2701{
Benny Halevy43652ad2009-04-01 09:21:54 -04002702 struct nfs4_server_caps_arg args = {
2703 .fhandle = fhandle,
2704 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 struct nfs4_server_caps_res res = {};
2706 struct rpc_message msg = {
2707 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002708 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 .rpc_resp = &res,
2710 };
2711 int status;
2712
Bryan Schumaker7c513052011-03-24 17:12:24 +00002713 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 if (status == 0) {
2715 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002716 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2717 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2718 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2719 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2720 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2722 server->caps |= NFS_CAP_ACLS;
2723 if (res.has_links != 0)
2724 server->caps |= NFS_CAP_HARDLINKS;
2725 if (res.has_symlinks != 0)
2726 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002727 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2728 server->caps |= NFS_CAP_FILEID;
2729 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2730 server->caps |= NFS_CAP_MODE;
2731 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2732 server->caps |= NFS_CAP_NLINK;
2733 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2734 server->caps |= NFS_CAP_OWNER;
2735 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2736 server->caps |= NFS_CAP_OWNER_GROUP;
2737 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2738 server->caps |= NFS_CAP_ATIME;
2739 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2740 server->caps |= NFS_CAP_CTIME;
2741 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2742 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04002743#ifdef CONFIG_NFS_V4_SECURITY_LABEL
2744 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
2745 server->caps |= NFS_CAP_SECURITY_LABEL;
2746#endif
2747 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
2748 sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002749
David Quigleyaa9c2662013-05-22 12:50:44 -04002750 if (server->caps & NFS_CAP_SECURITY_LABEL) {
2751 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2752 res.attr_bitmask[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2753 }
Trond Myklebusta65318b2009-03-11 14:10:28 -04002754 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2755 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2756 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002758 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002760
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 return status;
2762}
2763
Trond Myklebust55a97592006-06-09 09:34:19 -04002764int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765{
2766 struct nfs4_exception exception = { };
2767 int err;
2768 do {
2769 err = nfs4_handle_exception(server,
2770 _nfs4_server_capabilities(server, fhandle),
2771 &exception);
2772 } while (exception.retry);
2773 return err;
2774}
2775
2776static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2777 struct nfs_fsinfo *info)
2778{
David Quigleyaa9c2662013-05-22 12:50:44 -04002779 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04002781 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 };
2783 struct nfs4_lookup_res res = {
2784 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002785 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 .fh = fhandle,
2787 };
2788 struct rpc_message msg = {
2789 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2790 .rpc_argp = &args,
2791 .rpc_resp = &res,
2792 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002793
David Quigleyaa9c2662013-05-22 12:50:44 -04002794 bitmask[0] = nfs4_fattr_bitmap[0];
2795 bitmask[1] = nfs4_fattr_bitmap[1];
2796 /*
2797 * Process the label in the upcoming getfattr
2798 */
2799 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
2800
Trond Myklebust0e574af2005-10-27 22:12:38 -04002801 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002802 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803}
2804
2805static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2806 struct nfs_fsinfo *info)
2807{
2808 struct nfs4_exception exception = { };
2809 int err;
2810 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002811 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04002812 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002813 switch (err) {
2814 case 0:
2815 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002816 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002817 default:
2818 err = nfs4_handle_exception(server, err, &exception);
2819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002821out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 return err;
2823}
2824
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002825static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2826 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2827{
Trond Myklebustc2190662013-08-26 19:23:04 -04002828 struct rpc_auth_create_args auth_args = {
2829 .pseudoflavor = flavor,
2830 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002831 struct rpc_auth *auth;
2832 int ret;
2833
Trond Myklebustc2190662013-08-26 19:23:04 -04002834 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002835 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04002836 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002837 goto out;
2838 }
2839 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002840out:
2841 return ret;
2842}
2843
Chuck Lever9a744ba2013-03-16 15:56:02 -04002844/*
2845 * Retry pseudoroot lookup with various security flavors. We do this when:
2846 *
2847 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2848 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2849 *
2850 * Returns zero on success, or a negative NFS4ERR value, or a
2851 * negative errno value.
2852 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002853static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002854 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855{
Chuck Lever9a744ba2013-03-16 15:56:02 -04002856 /* Per 3530bis 15.33.5 */
2857 static const rpc_authflavor_t flav_array[] = {
2858 RPC_AUTH_GSS_KRB5P,
2859 RPC_AUTH_GSS_KRB5I,
2860 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04002861 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04002862 RPC_AUTH_NULL,
2863 };
2864 int status = -EPERM;
2865 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866
Chuck Lever9a744ba2013-03-16 15:56:02 -04002867 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002868 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002869 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002870 continue;
2871 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002872 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04002873
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002874 /*
2875 * -EACCESS could mean that the user doesn't have correct permissions
2876 * to access the mount. It could also mean that we tried to mount
2877 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2878 * existing mount programs don't handle -EACCES very well so it should
2879 * be mapped to -EPERM instead.
2880 */
2881 if (status == -EACCES)
2882 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002883 return status;
2884}
2885
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002886static int nfs4_do_find_root_sec(struct nfs_server *server,
2887 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
2888{
2889 int mv = server->nfs_client->cl_minorversion;
2890 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
2891}
2892
2893/**
2894 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
2895 * @server: initialized nfs_server handle
2896 * @fhandle: we fill in the pseudo-fs root file handle
2897 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002898 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002899 *
2900 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002901 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002902int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002903 struct nfs_fsinfo *info,
2904 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002905{
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002906 int status;
2907
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002908 switch (auth_probe) {
2909 case false:
2910 status = nfs4_lookup_root(server, fhandle, info);
2911 if (status != -NFS4ERR_WRONGSEC)
2912 break;
2913 /* Did user force a 'sec=' mount option? */
2914 if (server->flags & NFS_MOUNT_SECFLAVOUR)
2915 break;
2916 default:
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002917 status = nfs4_do_find_root_sec(server, fhandle, info);
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002918 }
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002919
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 if (status == 0)
2921 status = nfs4_server_capabilities(server, fhandle);
2922 if (status == 0)
2923 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002924
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002925 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926}
2927
Bryan Schumakerbae36242012-05-10 15:07:31 -04002928static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2929 struct nfs_fsinfo *info)
2930{
2931 int error;
2932 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04002933 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04002934
2935 error = nfs4_server_capabilities(server, mntfh);
2936 if (error < 0) {
2937 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2938 return error;
2939 }
2940
David Quigley14c43f72013-05-22 12:50:43 -04002941 label = nfs4_label_alloc(server, GFP_KERNEL);
2942 if (IS_ERR(label))
2943 return PTR_ERR(label);
2944
David Quigley1775fd32013-05-22 12:50:42 -04002945 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04002946 if (error < 0) {
2947 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04002948 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04002949 }
2950
2951 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2952 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2953 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2954
David Quigley14c43f72013-05-22 12:50:43 -04002955err_free_label:
2956 nfs4_label_free(label);
2957
Bryan Schumakerbae36242012-05-10 15:07:31 -04002958 return error;
2959}
2960
Manoj Naik6b97fd32006-06-09 09:34:29 -04002961/*
2962 * Get locations and (maybe) other attributes of a referral.
2963 * Note that we'll actually follow the referral later when
2964 * we detect fsid mismatch in inode revalidation
2965 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002966static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2967 const struct qstr *name, struct nfs_fattr *fattr,
2968 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002969{
2970 int status = -ENOMEM;
2971 struct page *page = NULL;
2972 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002973
2974 page = alloc_page(GFP_KERNEL);
2975 if (page == NULL)
2976 goto out;
2977 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2978 if (locations == NULL)
2979 goto out;
2980
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002981 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002982 if (status != 0)
2983 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04002984
2985 /*
2986 * If the fsid didn't change, this is a migration event, not a
2987 * referral. Cause us to drop into the exception handler, which
2988 * will kick off migration recovery.
2989 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002990 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002991 dprintk("%s: server did not return a different fsid for"
2992 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04002993 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002994 goto out;
2995 }
Andy Adamson533eb462011-06-13 18:25:56 -04002996 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2997 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002998
Andy Adamson533eb462011-06-13 18:25:56 -04002999 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003000 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003001 memset(fhandle, 0, sizeof(struct nfs_fh));
3002out:
3003 if (page)
3004 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003005 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003006 return status;
3007}
3008
David Quigley1775fd32013-05-22 12:50:42 -04003009static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3010 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011{
3012 struct nfs4_getattr_arg args = {
3013 .fh = fhandle,
3014 .bitmask = server->attr_bitmask,
3015 };
3016 struct nfs4_getattr_res res = {
3017 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003018 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 .server = server,
3020 };
3021 struct rpc_message msg = {
3022 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3023 .rpc_argp = &args,
3024 .rpc_resp = &res,
3025 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003026
3027 args.bitmask = nfs4_bitmask(server, label);
3028
Trond Myklebust0e574af2005-10-27 22:12:38 -04003029 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003030 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031}
3032
David Quigley1775fd32013-05-22 12:50:42 -04003033static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3034 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035{
3036 struct nfs4_exception exception = { };
3037 int err;
3038 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003039 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3040 trace_nfs4_getattr(server, fhandle, fattr, err);
3041 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 &exception);
3043 } while (exception.retry);
3044 return err;
3045}
3046
3047/*
3048 * The file is not closed if it is opened due to the a request to change
3049 * the size of the file. The open call will not be needed once the
3050 * VFS layer lookup-intents are implemented.
3051 *
3052 * Close is called when the inode is destroyed.
3053 * If we haven't opened the file for O_WRONLY, we
3054 * need to in the size_change case to obtain a stateid.
3055 *
3056 * Got race?
3057 * Because OPEN is always done by name in nfsv4, it is
3058 * possible that we opened a different file by the same
3059 * name. We can recognize this race condition, but we
3060 * can't do anything about it besides returning an error.
3061 *
3062 * This will be fixed with VFS changes (lookup-intent).
3063 */
3064static int
3065nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3066 struct iattr *sattr)
3067{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003068 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003069 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003070 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003071 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 int status;
3073
Benny Halevy8a1636c2010-07-14 15:43:57 -04003074 if (pnfs_ld_layoutret_on_setattr(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003075 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003076
Trond Myklebust0e574af2005-10-27 22:12:38 -04003077 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078
Andy Adamson26699402012-05-30 16:12:24 -04003079 /* Deal with open(O_TRUNC) */
3080 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003081 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003082
3083 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003084 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003085 return 0;
3086
Trond Myklebustd5308382005-11-04 15:33:38 -05003087 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003088 if (sattr->ia_valid & ATTR_FILE) {
3089 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003090
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003091 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003092 if (ctx) {
3093 cred = ctx->cred;
3094 state = ctx->state;
3095 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003096 }
3097
David Quigley14c43f72013-05-22 12:50:43 -04003098 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3099 if (IS_ERR(label))
3100 return PTR_ERR(label);
3101
3102 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003103 if (status == 0) {
Trond Myklebust65e43082005-08-16 11:49:44 -04003104 nfs_setattr_update_inode(inode, sattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003105 nfs_setsecurity(inode, fattr, label);
3106 }
David Quigley14c43f72013-05-22 12:50:43 -04003107 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 return status;
3109}
3110
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003111static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3112 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003113 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003114{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003115 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003116 int status;
3117 struct nfs4_lookup_arg args = {
3118 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003119 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003120 .name = name,
3121 };
3122 struct nfs4_lookup_res res = {
3123 .server = server,
3124 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003125 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003126 .fh = fhandle,
3127 };
3128 struct rpc_message msg = {
3129 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3130 .rpc_argp = &args,
3131 .rpc_resp = &res,
3132 };
3133
David Quigleyaa9c2662013-05-22 12:50:44 -04003134 args.bitmask = nfs4_bitmask(server, label);
3135
David Howells2b3de442006-08-22 20:06:09 -04003136 nfs_fattr_init(fattr);
3137
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003139 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 dprintk("NFS reply lookup: %d\n", status);
3141 return status;
3142}
3143
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003144static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003145{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003146 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003147 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003148 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3149 fattr->nlink = 2;
3150}
3151
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003152static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3153 struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003154 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003155{
3156 struct nfs4_exception exception = { };
3157 struct rpc_clnt *client = *clnt;
3158 int err;
3159 do {
David Quigley1775fd32013-05-22 12:50:42 -04003160 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003161 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003162 switch (err) {
3163 case -NFS4ERR_BADNAME:
3164 err = -ENOENT;
3165 goto out;
3166 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003167 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003168 goto out;
3169 case -NFS4ERR_WRONGSEC:
3170 err = -EPERM;
3171 if (client != *clnt)
3172 goto out;
Trond Myklebust41d058c2013-09-07 17:49:05 -04003173 /* No security negotiation if the user specified 'sec=' */
3174 if (NFS_SERVER(dir)->flags & NFS_MOUNT_SECFLAVOUR)
3175 goto out;
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003176 client = nfs4_create_sec_client(client, dir, name);
3177 if (IS_ERR(client))
3178 return PTR_ERR(client);
3179
3180 exception.retry = 1;
3181 break;
3182 default:
3183 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3184 }
3185 } while (exception.retry);
3186
3187out:
3188 if (err == 0)
3189 *clnt = client;
3190 else if (client != *clnt)
3191 rpc_shutdown_client(client);
3192
3193 return err;
3194}
3195
Bryan Schumaker80a16b22012-04-27 13:27:46 -04003196static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003197 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3198 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003200 int status;
3201 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003202
David Quigley1775fd32013-05-22 12:50:42 -04003203 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003204 if (client != NFS_CLIENT(dir)) {
3205 rpc_shutdown_client(client);
3206 nfs_fixup_secinfo_attributes(fattr);
3207 }
3208 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209}
3210
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003211struct rpc_clnt *
3212nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3213 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3214{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003215 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003216 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003217
David Quigley1775fd32013-05-22 12:50:42 -04003218 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003219 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003220 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003221 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003222}
3223
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3225{
Trond Myklebust76b32992007-08-10 17:45:11 -04003226 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 struct nfs4_accessargs args = {
3228 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003229 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003231 struct nfs4_accessres res = {
3232 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003233 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 struct rpc_message msg = {
3235 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3236 .rpc_argp = &args,
3237 .rpc_resp = &res,
3238 .rpc_cred = entry->cred,
3239 };
3240 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003241 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242
3243 /*
3244 * Determine which access bits we want to ask for...
3245 */
3246 if (mode & MAY_READ)
3247 args.access |= NFS4_ACCESS_READ;
3248 if (S_ISDIR(inode->i_mode)) {
3249 if (mode & MAY_WRITE)
3250 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3251 if (mode & MAY_EXEC)
3252 args.access |= NFS4_ACCESS_LOOKUP;
3253 } else {
3254 if (mode & MAY_WRITE)
3255 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3256 if (mode & MAY_EXEC)
3257 args.access |= NFS4_ACCESS_EXECUTE;
3258 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003259
3260 res.fattr = nfs_alloc_fattr();
3261 if (res.fattr == NULL)
3262 return -ENOMEM;
3263
Bryan Schumaker7c513052011-03-24 17:12:24 +00003264 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003266 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003267 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003269 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 return status;
3271}
3272
3273static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3274{
3275 struct nfs4_exception exception = { };
3276 int err;
3277 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003278 err = _nfs4_proc_access(inode, entry);
3279 trace_nfs4_access(inode, err);
3280 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 &exception);
3282 } while (exception.retry);
3283 return err;
3284}
3285
3286/*
3287 * TODO: For the time being, we don't try to get any attributes
3288 * along with any of the zero-copy operations READ, READDIR,
3289 * READLINK, WRITE.
3290 *
3291 * In the case of the first three, we want to put the GETATTR
3292 * after the read-type operation -- this is because it is hard
3293 * to predict the length of a GETATTR response in v4, and thus
3294 * align the READ data correctly. This means that the GETATTR
3295 * may end up partially falling into the page cache, and we should
3296 * shift it into the 'tail' of the xdr_buf before processing.
3297 * To do this efficiently, we need to know the total length
3298 * of data received, which doesn't seem to be available outside
3299 * of the RPC layer.
3300 *
3301 * In the case of WRITE, we also want to put the GETATTR after
3302 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003303 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 *
3305 * Both of these changes to the XDR layer would in fact be quite
3306 * minor, but I decided to leave them for a subsequent patch.
3307 */
3308static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3309 unsigned int pgbase, unsigned int pglen)
3310{
3311 struct nfs4_readlink args = {
3312 .fh = NFS_FH(inode),
3313 .pgbase = pgbase,
3314 .pglen = pglen,
3315 .pages = &page,
3316 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003317 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 struct rpc_message msg = {
3319 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3320 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003321 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 };
3323
Bryan Schumaker7c513052011-03-24 17:12:24 +00003324 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 -07003325}
3326
3327static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3328 unsigned int pgbase, unsigned int pglen)
3329{
3330 struct nfs4_exception exception = { };
3331 int err;
3332 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003333 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3334 trace_nfs4_readlink(inode, err);
3335 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 &exception);
3337 } while (exception.retry);
3338 return err;
3339}
3340
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003342 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344static int
3345nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003346 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347{
David Quigleyaa9c2662013-05-22 12:50:44 -04003348 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003349 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 struct nfs4_state *state;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003351 int opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 int status = 0;
3353
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003354 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3355 if (IS_ERR(ctx))
3356 return PTR_ERR(ctx);
3357
David Quigleyaa9c2662013-05-22 12:50:44 -04003358 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3359
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003360 sattr->ia_mode &= ~current_umask();
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003361 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, &opened);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 if (IS_ERR(state)) {
3363 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003364 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003367 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003368 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 return status;
3370}
3371
3372static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3373{
Trond Myklebust16e42952005-10-27 22:12:44 -04003374 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003375 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003377 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003379 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003380 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003381 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003383 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3384 .rpc_argp = &args,
3385 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003387 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003388
Bryan Schumaker7c513052011-03-24 17:12:24 +00003389 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003390 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003391 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 return status;
3393}
3394
3395static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3396{
3397 struct nfs4_exception exception = { };
3398 int err;
3399 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003400 err = _nfs4_proc_remove(dir, name);
3401 trace_nfs4_remove(dir, name, err);
3402 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 &exception);
3404 } while (exception.retry);
3405 return err;
3406}
3407
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003408static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003410 struct nfs_server *server = NFS_SERVER(dir);
3411 struct nfs_removeargs *args = msg->rpc_argp;
3412 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003414 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04003416 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003417
3418 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419}
3420
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003421static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3422{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003423 nfs4_setup_sequence(NFS_SERVER(data->dir),
3424 &data->args.seq_args,
3425 &data->res.seq_res,
3426 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427}
3428
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003429static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003431 struct nfs_unlinkdata *data = task->tk_calldata;
3432 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003433
Trond Myklebust14516c32010-07-31 14:29:06 -04003434 if (!nfs4_sequence_done(task, &res->seq_res))
3435 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003436 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003437 return 0;
3438 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003439 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440}
3441
Jeff Laytond3d41522010-09-17 17:31:57 -04003442static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3443{
3444 struct nfs_server *server = NFS_SERVER(dir);
3445 struct nfs_renameargs *arg = msg->rpc_argp;
3446 struct nfs_renameres *res = msg->rpc_resp;
3447
3448 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003449 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04003450 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003451}
3452
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003453static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3454{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003455 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3456 &data->args.seq_args,
3457 &data->res.seq_res,
3458 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003459}
3460
3461static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3462 struct inode *new_dir)
3463{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003464 struct nfs_renamedata *data = task->tk_calldata;
3465 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04003466
3467 if (!nfs4_sequence_done(task, &res->seq_res))
3468 return 0;
3469 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3470 return 0;
3471
3472 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003473 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003474 return 1;
3475}
3476
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3478 struct inode *new_dir, struct qstr *new_name)
3479{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003480 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003481 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 .old_dir = NFS_FH(old_dir),
3483 .new_dir = NFS_FH(new_dir),
3484 .old_name = old_name,
3485 .new_name = new_name,
3486 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003487 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003488 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003489 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 struct rpc_message msg = {
3491 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3492 .rpc_argp = &arg,
3493 .rpc_resp = &res,
3494 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003495 int status = -ENOMEM;
David Quigley14c43f72013-05-22 12:50:43 -04003496
Bryan Schumaker7c513052011-03-24 17:12:24 +00003497 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 if (!status) {
3499 update_changeattr(old_dir, &res.old_cinfo);
3500 update_changeattr(new_dir, &res.new_cinfo);
3501 }
3502 return status;
3503}
3504
3505static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3506 struct inode *new_dir, struct qstr *new_name)
3507{
3508 struct nfs4_exception exception = { };
3509 int err;
3510 do {
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003511 err = _nfs4_proc_rename(old_dir, old_name,
3512 new_dir, new_name);
3513 trace_nfs4_rename(old_dir, old_name, new_dir, new_name, err);
3514 err = nfs4_handle_exception(NFS_SERVER(old_dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 &exception);
3516 } while (exception.retry);
3517 return err;
3518}
3519
3520static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3521{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003522 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 struct nfs4_link_arg arg = {
3524 .fh = NFS_FH(inode),
3525 .dir_fh = NFS_FH(dir),
3526 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003527 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003529 struct nfs4_link_res res = {
3530 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003531 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003532 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 struct rpc_message msg = {
3534 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3535 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003536 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003538 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539
Trond Myklebust136f2622010-04-16 16:22:49 -04003540 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003541 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003542 goto out;
3543
David Quigley14c43f72013-05-22 12:50:43 -04003544 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3545 if (IS_ERR(res.label)) {
3546 status = PTR_ERR(res.label);
3547 goto out;
3548 }
David Quigleyaa9c2662013-05-22 12:50:44 -04003549 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04003550
Bryan Schumaker7c513052011-03-24 17:12:24 +00003551 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003552 if (!status) {
3553 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04003554 status = nfs_post_op_update_inode(inode, res.fattr);
3555 if (!status)
3556 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003557 }
David Quigley14c43f72013-05-22 12:50:43 -04003558
3559
3560 nfs4_label_free(res.label);
3561
Trond Myklebust136f2622010-04-16 16:22:49 -04003562out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003563 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 return status;
3565}
3566
3567static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3568{
3569 struct nfs4_exception exception = { };
3570 int err;
3571 do {
3572 err = nfs4_handle_exception(NFS_SERVER(inode),
3573 _nfs4_proc_link(inode, dir, name),
3574 &exception);
3575 } while (exception.retry);
3576 return err;
3577}
3578
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003579struct nfs4_createdata {
3580 struct rpc_message msg;
3581 struct nfs4_create_arg arg;
3582 struct nfs4_create_res res;
3583 struct nfs_fh fh;
3584 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003585 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003586};
3587
3588static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3589 struct qstr *name, struct iattr *sattr, u32 ftype)
3590{
3591 struct nfs4_createdata *data;
3592
3593 data = kzalloc(sizeof(*data), GFP_KERNEL);
3594 if (data != NULL) {
3595 struct nfs_server *server = NFS_SERVER(dir);
3596
David Quigley14c43f72013-05-22 12:50:43 -04003597 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3598 if (IS_ERR(data->label))
3599 goto out_free;
3600
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003601 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3602 data->msg.rpc_argp = &data->arg;
3603 data->msg.rpc_resp = &data->res;
3604 data->arg.dir_fh = NFS_FH(dir);
3605 data->arg.server = server;
3606 data->arg.name = name;
3607 data->arg.attrs = sattr;
3608 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04003609 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003610 data->res.server = server;
3611 data->res.fh = &data->fh;
3612 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003613 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003614 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003615 }
3616 return data;
David Quigley14c43f72013-05-22 12:50:43 -04003617out_free:
3618 kfree(data);
3619 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003620}
3621
3622static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3623{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003624 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003625 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003626 if (status == 0) {
3627 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04003628 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003629 }
3630 return status;
3631}
3632
3633static void nfs4_free_createdata(struct nfs4_createdata *data)
3634{
David Quigley14c43f72013-05-22 12:50:43 -04003635 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003636 kfree(data);
3637}
3638
Chuck Lever4f390c12006-08-22 20:06:22 -04003639static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003640 struct page *page, unsigned int len, struct iattr *sattr,
3641 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003643 struct nfs4_createdata *data;
3644 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645
Chuck Lever94a6d752006-08-22 20:06:23 -04003646 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003647 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003648
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003649 status = -ENOMEM;
3650 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3651 if (data == NULL)
3652 goto out;
3653
3654 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3655 data->arg.u.symlink.pages = &page;
3656 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04003657 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003659 status = nfs4_do_create(dir, dentry, data);
3660
3661 nfs4_free_createdata(data);
3662out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 return status;
3664}
3665
Chuck Lever4f390c12006-08-22 20:06:22 -04003666static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003667 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668{
3669 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003670 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04003672
3673 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3674
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003676 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3677 trace_nfs4_symlink(dir, &dentry->d_name, err);
3678 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 &exception);
3680 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003681
3682 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 return err;
3684}
3685
3686static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003687 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003689 struct nfs4_createdata *data;
3690 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003692 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3693 if (data == NULL)
3694 goto out;
3695
David Quigley1775fd32013-05-22 12:50:42 -04003696 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003697 status = nfs4_do_create(dir, dentry, data);
3698
3699 nfs4_free_createdata(data);
3700out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 return status;
3702}
3703
3704static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3705 struct iattr *sattr)
3706{
3707 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003708 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003710
David Quigleyaa9c2662013-05-22 12:50:44 -04003711 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3712
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003713 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003715 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3716 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3717 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 &exception);
3719 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003720 nfs4_label_release_security(label);
3721
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 return err;
3723}
3724
3725static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003726 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727{
3728 struct inode *dir = dentry->d_inode;
3729 struct nfs4_readdir_arg args = {
3730 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003731 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 .pgbase = 0,
3733 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003734 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003735 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736 };
3737 struct nfs4_readdir_res res;
3738 struct rpc_message msg = {
3739 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3740 .rpc_argp = &args,
3741 .rpc_resp = &res,
3742 .rpc_cred = cred,
3743 };
3744 int status;
3745
Harvey Harrison3110ff82008-05-02 13:42:44 -07003746 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003747 dentry->d_parent->d_name.name,
3748 dentry->d_name.name,
3749 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003750 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003752 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 -05003753 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003754 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003755 status += args.pgbase;
3756 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003757
3758 nfs_invalidate_atime(dir);
3759
Harvey Harrison3110ff82008-05-02 13:42:44 -07003760 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 return status;
3762}
3763
3764static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003765 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766{
3767 struct nfs4_exception exception = { };
3768 int err;
3769 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003770 err = _nfs4_proc_readdir(dentry, cred, cookie,
3771 pages, count, plus);
3772 trace_nfs4_readdir(dentry->d_inode, err);
3773 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 &exception);
3775 } while (exception.retry);
3776 return err;
3777}
3778
3779static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04003780 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003782 struct nfs4_createdata *data;
3783 int mode = sattr->ia_mode;
3784 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003786 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3787 if (data == NULL)
3788 goto out;
3789
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003791 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003793 data->arg.ftype = NF4BLK;
3794 data->arg.u.device.specdata1 = MAJOR(rdev);
3795 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 }
3797 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003798 data->arg.ftype = NF4CHR;
3799 data->arg.u.device.specdata1 = MAJOR(rdev);
3800 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003801 } else if (!S_ISSOCK(mode)) {
3802 status = -EINVAL;
3803 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 }
David Quigley1775fd32013-05-22 12:50:42 -04003805
David Quigleyaa9c2662013-05-22 12:50:44 -04003806 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003807 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003808out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003809 nfs4_free_createdata(data);
3810out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 return status;
3812}
3813
3814static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3815 struct iattr *sattr, dev_t rdev)
3816{
3817 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003818 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003820
David Quigleyaa9c2662013-05-22 12:50:44 -04003821 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3822
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003823 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003825 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
3826 trace_nfs4_mknod(dir, &dentry->d_name, err);
3827 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 &exception);
3829 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003830
3831 nfs4_label_release_security(label);
3832
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 return err;
3834}
3835
3836static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3837 struct nfs_fsstat *fsstat)
3838{
3839 struct nfs4_statfs_arg args = {
3840 .fh = fhandle,
3841 .bitmask = server->attr_bitmask,
3842 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003843 struct nfs4_statfs_res res = {
3844 .fsstat = fsstat,
3845 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 struct rpc_message msg = {
3847 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3848 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003849 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 };
3851
Trond Myklebust0e574af2005-10-27 22:12:38 -04003852 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003853 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854}
3855
3856static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3857{
3858 struct nfs4_exception exception = { };
3859 int err;
3860 do {
3861 err = nfs4_handle_exception(server,
3862 _nfs4_proc_statfs(server, fhandle, fsstat),
3863 &exception);
3864 } while (exception.retry);
3865 return err;
3866}
3867
3868static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3869 struct nfs_fsinfo *fsinfo)
3870{
3871 struct nfs4_fsinfo_arg args = {
3872 .fh = fhandle,
3873 .bitmask = server->attr_bitmask,
3874 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003875 struct nfs4_fsinfo_res res = {
3876 .fsinfo = fsinfo,
3877 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 struct rpc_message msg = {
3879 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3880 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003881 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 };
3883
Bryan Schumaker7c513052011-03-24 17:12:24 +00003884 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885}
3886
3887static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3888{
3889 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04003890 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 int err;
3892
3893 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04003894 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003895 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04003896 if (err == 0) {
3897 struct nfs_client *clp = server->nfs_client;
3898
3899 spin_lock(&clp->cl_lock);
3900 clp->cl_lease_time = fsinfo->lease_time * HZ;
3901 clp->cl_last_renewal = now;
3902 spin_unlock(&clp->cl_lock);
3903 break;
3904 }
3905 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 } while (exception.retry);
3907 return err;
3908}
3909
3910static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3911{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003912 int error;
3913
Trond Myklebust0e574af2005-10-27 22:12:38 -04003914 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003915 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003916 if (error == 0) {
3917 /* block layout checks this! */
3918 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003919 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003920 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003921
3922 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923}
3924
3925static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3926 struct nfs_pathconf *pathconf)
3927{
3928 struct nfs4_pathconf_arg args = {
3929 .fh = fhandle,
3930 .bitmask = server->attr_bitmask,
3931 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003932 struct nfs4_pathconf_res res = {
3933 .pathconf = pathconf,
3934 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 struct rpc_message msg = {
3936 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3937 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003938 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 };
3940
3941 /* None of the pathconf attributes are mandatory to implement */
3942 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3943 memset(pathconf, 0, sizeof(*pathconf));
3944 return 0;
3945 }
3946
Trond Myklebust0e574af2005-10-27 22:12:38 -04003947 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003948 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949}
3950
3951static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3952 struct nfs_pathconf *pathconf)
3953{
3954 struct nfs4_exception exception = { };
3955 int err;
3956
3957 do {
3958 err = nfs4_handle_exception(server,
3959 _nfs4_proc_pathconf(server, fhandle, pathconf),
3960 &exception);
3961 } while (exception.retry);
3962 return err;
3963}
3964
Trond Myklebust5521abf2013-03-16 20:54:34 -04003965int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04003966 const struct nfs_open_context *ctx,
3967 const struct nfs_lock_context *l_ctx,
3968 fmode_t fmode)
3969{
3970 const struct nfs_lockowner *lockowner = NULL;
3971
3972 if (l_ctx != NULL)
3973 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003974 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04003975}
3976EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
3977
Trond Myklebust5521abf2013-03-16 20:54:34 -04003978static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
3979 const struct nfs_open_context *ctx,
3980 const struct nfs_lock_context *l_ctx,
3981 fmode_t fmode)
3982{
3983 nfs4_stateid current_stateid;
3984
3985 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode))
3986 return false;
3987 return nfs4_stateid_match(stateid, &current_stateid);
3988}
3989
3990static bool nfs4_error_stateid_expired(int err)
3991{
3992 switch (err) {
3993 case -NFS4ERR_DELEG_REVOKED:
3994 case -NFS4ERR_ADMIN_REVOKED:
3995 case -NFS4ERR_BAD_STATEID:
3996 case -NFS4ERR_STALE_STATEID:
3997 case -NFS4ERR_OLD_STATEID:
3998 case -NFS4ERR_OPENMODE:
3999 case -NFS4ERR_EXPIRED:
4000 return true;
4001 }
4002 return false;
4003}
4004
Benny Halevyd20581a2011-05-22 19:52:03 +03004005void __nfs4_read_done_cb(struct nfs_read_data *data)
4006{
Fred Isamancd841602012-04-20 14:47:44 -04004007 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03004008}
4009
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004010static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011{
Fred Isamancd841602012-04-20 14:47:44 -04004012 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004014 trace_nfs4_read(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004015 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004016 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05004017 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004019
Benny Halevyd20581a2011-05-22 19:52:03 +03004020 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05004022 renew_lease(server, data->timestamp);
4023 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024}
4025
Trond Myklebust5521abf2013-03-16 20:54:34 -04004026static bool nfs4_read_stateid_changed(struct rpc_task *task,
4027 struct nfs_readargs *args)
4028{
4029
4030 if (!nfs4_error_stateid_expired(task->tk_status) ||
4031 nfs4_stateid_is_current(&args->stateid,
4032 args->context,
4033 args->lock_context,
4034 FMODE_READ))
4035 return false;
4036 rpc_restart_call_prepare(task);
4037 return true;
4038}
4039
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004040static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
4041{
4042
4043 dprintk("--> %s\n", __func__);
4044
4045 if (!nfs4_sequence_done(task, &data->res.seq_res))
4046 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004047 if (nfs4_read_stateid_changed(task, &data->args))
4048 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03004049 return data->read_done_cb ? data->read_done_cb(task, data) :
4050 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004051}
4052
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004053static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 data->timestamp = jiffies;
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004056 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004057 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Chuck Levera9c92d62013-08-09 12:48:18 -04004058 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059}
4060
NeilBrownef1820f2013-09-04 17:04:49 +10004061static int nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004062{
Trond Myklebust9b206142013-03-17 15:52:00 -04004063 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004064 &data->args.seq_args,
4065 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004066 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004067 return 0;
4068 if (nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4069 data->args.lock_context, FMODE_READ) == -EIO)
4070 return -EIO;
4071 if (unlikely(test_bit(NFS_CONTEXT_BAD, &data->args.context->flags)))
4072 return -EIO;
4073 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074}
4075
Fred Isamanb029bc92011-03-03 15:13:42 +00004076static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077{
Fred Isamancd841602012-04-20 14:47:44 -04004078 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004080 trace_nfs4_write(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004081 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004082 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004083 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004085 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04004087 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004088 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004089 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090}
4091
Trond Myklebust5521abf2013-03-16 20:54:34 -04004092static bool nfs4_write_stateid_changed(struct rpc_task *task,
4093 struct nfs_writeargs *args)
4094{
4095
4096 if (!nfs4_error_stateid_expired(task->tk_status) ||
4097 nfs4_stateid_is_current(&args->stateid,
4098 args->context,
4099 args->lock_context,
4100 FMODE_WRITE))
4101 return false;
4102 rpc_restart_call_prepare(task);
4103 return true;
4104}
4105
Fred Isamanb029bc92011-03-03 15:13:42 +00004106static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
4107{
4108 if (!nfs4_sequence_done(task, &data->res.seq_res))
4109 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004110 if (nfs4_write_stateid_changed(task, &data->args))
4111 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03004112 return data->write_done_cb ? data->write_done_cb(task, data) :
4113 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00004114}
4115
Trond Myklebust5a37f852012-04-28 14:55:16 -04004116static
4117bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00004118{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004119 const struct nfs_pgio_header *hdr = data->header;
4120
4121 /* Don't request attributes for pNFS or O_DIRECT writes */
4122 if (data->ds_clp != NULL || hdr->dreq != NULL)
4123 return false;
4124 /* Otherwise, request attributes if and only if we don't hold
4125 * a delegation
4126 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004127 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004128}
Fred Isamana69aef12011-03-03 15:13:47 +00004129
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004130static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131{
Fred Isamancd841602012-04-20 14:47:44 -04004132 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004133
Trond Myklebust5a37f852012-04-28 14:55:16 -04004134 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00004135 data->args.bitmask = NULL;
4136 data->res.fattr = NULL;
4137 } else
4138 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004139
Fred Isamanb029bc92011-03-03 15:13:42 +00004140 if (!data->write_done_cb)
4141 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004142 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 data->timestamp = jiffies;
4144
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004145 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004146 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147}
4148
NeilBrownef1820f2013-09-04 17:04:49 +10004149static int nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04004150{
Trond Myklebust9b206142013-03-17 15:52:00 -04004151 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004152 &data->args.seq_args,
4153 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004154 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004155 return 0;
4156 if (nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4157 data->args.lock_context, FMODE_WRITE) == -EIO)
4158 return -EIO;
4159 if (unlikely(test_bit(NFS_CONTEXT_BAD, &data->args.context->flags)))
4160 return -EIO;
4161 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162}
4163
Fred Isaman0b7c0152012-04-20 14:47:39 -04004164static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4165{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004166 nfs4_setup_sequence(NFS_SERVER(data->inode),
4167 &data->args.seq_args,
4168 &data->res.seq_res,
4169 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170}
4171
Fred Isaman0b7c0152012-04-20 14:47:39 -04004172static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004175
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004176 trace_nfs4_commit(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004177 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004178 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004179 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004181 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182}
4183
Fred Isaman0b7c0152012-04-20 14:47:39 -04004184static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004185{
4186 if (!nfs4_sequence_done(task, &data->res.seq_res))
4187 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004188 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004189}
4190
Fred Isaman0b7c0152012-04-20 14:47:39 -04004191static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004193 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004194
Fred Isaman0b7c0152012-04-20 14:47:39 -04004195 if (data->commit_done_cb == NULL)
4196 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004197 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004198 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004199 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200}
4201
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004202struct nfs4_renewdata {
4203 struct nfs_client *client;
4204 unsigned long timestamp;
4205};
4206
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207/*
4208 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4209 * standalone procedure for queueing an asynchronous RENEW.
4210 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004211static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004212{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004213 struct nfs4_renewdata *data = calldata;
4214 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004215
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004216 if (atomic_read(&clp->cl_count) > 1)
4217 nfs4_schedule_state_renewal(clp);
4218 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004219 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004220}
4221
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004222static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004224 struct nfs4_renewdata *data = calldata;
4225 struct nfs_client *clp = data->client;
4226 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004228 trace_nfs4_renew_async(clp, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04004230 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004231 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4232 return;
4233 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004234 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004235 return;
4236 }
4237 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004239 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240}
4241
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004242static const struct rpc_call_ops nfs4_renew_ops = {
4243 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004244 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004245};
4246
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004247static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248{
4249 struct rpc_message msg = {
4250 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4251 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004252 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004254 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004256 if (renew_flags == 0)
4257 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004258 if (!atomic_inc_not_zero(&clp->cl_count))
4259 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004260 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004261 if (data == NULL)
4262 return -ENOMEM;
4263 data->client = clp;
4264 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004265 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004266 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267}
4268
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004269static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270{
4271 struct rpc_message msg = {
4272 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4273 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004274 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 };
4276 unsigned long now = jiffies;
4277 int status;
4278
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004279 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 if (status < 0)
4281 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004282 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 return 0;
4284}
4285
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004286static inline int nfs4_server_supports_acls(struct nfs_server *server)
4287{
4288 return (server->caps & NFS_CAP_ACLS)
4289 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
4290 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
4291}
4292
Trond Myklebust21f498c2012-08-24 10:59:25 -04004293/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4294 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004295 * the stack.
4296 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004297#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004298
Neil Hormane9e3d722011-03-04 19:26:03 -05004299static int buf_to_pages_noslab(const void *buf, size_t buflen,
4300 struct page **pages, unsigned int *pgbase)
4301{
4302 struct page *newpage, **spages;
4303 int rc = 0;
4304 size_t len;
4305 spages = pages;
4306
4307 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004308 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004309 newpage = alloc_page(GFP_KERNEL);
4310
4311 if (newpage == NULL)
4312 goto unwind;
4313 memcpy(page_address(newpage), buf, len);
4314 buf += len;
4315 buflen -= len;
4316 *pages++ = newpage;
4317 rc++;
4318 } while (buflen != 0);
4319
4320 return rc;
4321
4322unwind:
4323 for(; rc > 0; rc--)
4324 __free_page(spages[rc-1]);
4325 return -ENOMEM;
4326}
4327
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004328struct nfs4_cached_acl {
4329 int cached;
4330 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004331 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004332};
4333
4334static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004335{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004336 struct nfs_inode *nfsi = NFS_I(inode);
4337
4338 spin_lock(&inode->i_lock);
4339 kfree(nfsi->nfs4_acl);
4340 nfsi->nfs4_acl = acl;
4341 spin_unlock(&inode->i_lock);
4342}
4343
4344static void nfs4_zap_acl_attr(struct inode *inode)
4345{
4346 nfs4_set_cached_acl(inode, NULL);
4347}
4348
4349static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4350{
4351 struct nfs_inode *nfsi = NFS_I(inode);
4352 struct nfs4_cached_acl *acl;
4353 int ret = -ENOENT;
4354
4355 spin_lock(&inode->i_lock);
4356 acl = nfsi->nfs4_acl;
4357 if (acl == NULL)
4358 goto out;
4359 if (buf == NULL) /* user is just asking for length */
4360 goto out_len;
4361 if (acl->cached == 0)
4362 goto out;
4363 ret = -ERANGE; /* see getxattr(2) man page */
4364 if (acl->len > buflen)
4365 goto out;
4366 memcpy(buf, acl->data, acl->len);
4367out_len:
4368 ret = acl->len;
4369out:
4370 spin_unlock(&inode->i_lock);
4371 return ret;
4372}
4373
Sachin Prabhu5794d212012-04-17 14:36:40 +01004374static 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 +00004375{
4376 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004377 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004378
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004379 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004380 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004381 if (acl == NULL)
4382 goto out;
4383 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004384 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004385 } else {
4386 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4387 if (acl == NULL)
4388 goto out;
4389 acl->cached = 0;
4390 }
4391 acl->len = acl_len;
4392out:
4393 nfs4_set_cached_acl(inode, acl);
4394}
4395
Andy Adamsonbf118a32011-12-07 11:55:27 -05004396/*
4397 * The getxattr API returns the required buffer length when called with a
4398 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4399 * the required buf. On a NULL buf, we send a page of data to the server
4400 * guessing that the ACL request can be serviced by a page. If so, we cache
4401 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4402 * the cache. If not so, we throw away the page, and cache the required
4403 * length. The next getxattr call will then produce another round trip to
4404 * the server, this time with the input buf of the required size.
4405 */
Trond Myklebust16b42892006-08-24 12:27:15 -04004406static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004407{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004408 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004409 struct nfs_getaclargs args = {
4410 .fh = NFS_FH(inode),
4411 .acl_pages = pages,
4412 .acl_len = buflen,
4413 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004414 struct nfs_getaclres res = {
4415 .acl_len = buflen,
4416 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004417 struct rpc_message msg = {
4418 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4419 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004420 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004421 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004422 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4423 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004424
Andy Adamsonbf118a32011-12-07 11:55:27 -05004425 /* As long as we're doing a round trip to the server anyway,
4426 * let's be prepared for a page of acl data. */
4427 if (npages == 0)
4428 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004429 if (npages > ARRAY_SIZE(pages))
4430 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004431
Andy Adamsonbf118a32011-12-07 11:55:27 -05004432 for (i = 0; i < npages; i++) {
4433 pages[i] = alloc_page(GFP_KERNEL);
4434 if (!pages[i])
4435 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004436 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004437
4438 /* for decoding across pages */
4439 res.acl_scratch = alloc_page(GFP_KERNEL);
4440 if (!res.acl_scratch)
4441 goto out_free;
4442
Andy Adamsonbf118a32011-12-07 11:55:27 -05004443 args.acl_len = npages * PAGE_SIZE;
4444 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004445
Peng Taode040be2012-01-10 22:42:47 +08004446 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004447 __func__, buf, buflen, npages, args.acl_len);
4448 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4449 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004450 if (ret)
4451 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004452
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004453 /* Handle the case where the passed-in buffer is too short */
4454 if (res.acl_flags & NFS4_ACL_TRUNC) {
4455 /* Did the user only issue a request for the acl length? */
4456 if (buf == NULL)
4457 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004458 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004459 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004460 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004461 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004462 if (buf) {
4463 if (res.acl_len > buflen) {
4464 ret = -ERANGE;
4465 goto out_free;
4466 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004467 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004468 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004469out_ok:
4470 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004471out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004472 for (i = 0; i < npages; i++)
4473 if (pages[i])
4474 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004475 if (res.acl_scratch)
4476 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004477 return ret;
4478}
4479
Trond Myklebust16b42892006-08-24 12:27:15 -04004480static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4481{
4482 struct nfs4_exception exception = { };
4483 ssize_t ret;
4484 do {
4485 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04004486 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04004487 if (ret >= 0)
4488 break;
4489 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4490 } while (exception.retry);
4491 return ret;
4492}
4493
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004494static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4495{
4496 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004497 int ret;
4498
4499 if (!nfs4_server_supports_acls(server))
4500 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004501 ret = nfs_revalidate_inode(server, inode);
4502 if (ret < 0)
4503 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004504 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4505 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004506 ret = nfs4_read_cached_acl(inode, buf, buflen);
4507 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004508 /* -ENOENT is returned if there is no ACL or if there is an ACL
4509 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004510 return ret;
4511 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004512}
4513
Trond Myklebust16b42892006-08-24 12:27:15 -04004514static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004515{
4516 struct nfs_server *server = NFS_SERVER(inode);
4517 struct page *pages[NFS4ACL_MAXPAGES];
4518 struct nfs_setaclargs arg = {
4519 .fh = NFS_FH(inode),
4520 .acl_pages = pages,
4521 .acl_len = buflen,
4522 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004523 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004524 struct rpc_message msg = {
4525 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4526 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004527 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004528 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004529 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004530 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004531
4532 if (!nfs4_server_supports_acls(server))
4533 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004534 if (npages > ARRAY_SIZE(pages))
4535 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004536 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4537 if (i < 0)
4538 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004539 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004540 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004541
4542 /*
4543 * Free each page after tx, so the only ref left is
4544 * held by the network stack
4545 */
4546 for (; i > 0; i--)
4547 put_page(pages[i-1]);
4548
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004549 /*
4550 * Acl update can result in inode attribute update.
4551 * so mark the attribute cache invalid.
4552 */
4553 spin_lock(&inode->i_lock);
4554 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4555 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004556 nfs_access_zap_cache(inode);
4557 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004558 return ret;
4559}
4560
Trond Myklebust16b42892006-08-24 12:27:15 -04004561static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4562{
4563 struct nfs4_exception exception = { };
4564 int err;
4565 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004566 err = __nfs4_proc_set_acl(inode, buf, buflen);
4567 trace_nfs4_set_acl(inode, err);
4568 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04004569 &exception);
4570 } while (exception.retry);
4571 return err;
4572}
4573
David Quigleyaa9c2662013-05-22 12:50:44 -04004574#ifdef CONFIG_NFS_V4_SECURITY_LABEL
4575static int _nfs4_get_security_label(struct inode *inode, void *buf,
4576 size_t buflen)
4577{
4578 struct nfs_server *server = NFS_SERVER(inode);
4579 struct nfs_fattr fattr;
4580 struct nfs4_label label = {0, 0, buflen, buf};
4581
4582 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4583 struct nfs4_getattr_arg args = {
4584 .fh = NFS_FH(inode),
4585 .bitmask = bitmask,
4586 };
4587 struct nfs4_getattr_res res = {
4588 .fattr = &fattr,
4589 .label = &label,
4590 .server = server,
4591 };
4592 struct rpc_message msg = {
4593 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4594 .rpc_argp = &args,
4595 .rpc_resp = &res,
4596 };
4597 int ret;
4598
4599 nfs_fattr_init(&fattr);
4600
4601 ret = rpc_call_sync(server->client, &msg, 0);
4602 if (ret)
4603 return ret;
4604 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4605 return -ENOENT;
4606 if (buflen < label.len)
4607 return -ERANGE;
4608 return 0;
4609}
4610
4611static int nfs4_get_security_label(struct inode *inode, void *buf,
4612 size_t buflen)
4613{
4614 struct nfs4_exception exception = { };
4615 int err;
4616
4617 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4618 return -EOPNOTSUPP;
4619
4620 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004621 err = _nfs4_get_security_label(inode, buf, buflen);
4622 trace_nfs4_get_security_label(inode, err);
4623 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004624 &exception);
4625 } while (exception.retry);
4626 return err;
4627}
4628
4629static int _nfs4_do_set_security_label(struct inode *inode,
4630 struct nfs4_label *ilabel,
4631 struct nfs_fattr *fattr,
4632 struct nfs4_label *olabel)
4633{
4634
4635 struct iattr sattr = {0};
4636 struct nfs_server *server = NFS_SERVER(inode);
4637 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4638 struct nfs_setattrargs args = {
4639 .fh = NFS_FH(inode),
4640 .iap = &sattr,
4641 .server = server,
4642 .bitmask = bitmask,
4643 .label = ilabel,
4644 };
4645 struct nfs_setattrres res = {
4646 .fattr = fattr,
4647 .label = olabel,
4648 .server = server,
4649 };
4650 struct rpc_message msg = {
4651 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
4652 .rpc_argp = &args,
4653 .rpc_resp = &res,
4654 };
4655 int status;
4656
4657 nfs4_stateid_copy(&args.stateid, &zero_stateid);
4658
4659 status = rpc_call_sync(server->client, &msg, 0);
4660 if (status)
4661 dprintk("%s failed: %d\n", __func__, status);
4662
4663 return status;
4664}
4665
4666static int nfs4_do_set_security_label(struct inode *inode,
4667 struct nfs4_label *ilabel,
4668 struct nfs_fattr *fattr,
4669 struct nfs4_label *olabel)
4670{
4671 struct nfs4_exception exception = { };
4672 int err;
4673
4674 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004675 err = _nfs4_do_set_security_label(inode, ilabel,
4676 fattr, olabel);
4677 trace_nfs4_set_security_label(inode, err);
4678 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004679 &exception);
4680 } while (exception.retry);
4681 return err;
4682}
4683
4684static int
4685nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4686{
4687 struct nfs4_label ilabel, *olabel = NULL;
4688 struct nfs_fattr fattr;
4689 struct rpc_cred *cred;
4690 struct inode *inode = dentry->d_inode;
4691 int status;
4692
4693 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4694 return -EOPNOTSUPP;
4695
4696 nfs_fattr_init(&fattr);
4697
4698 ilabel.pi = 0;
4699 ilabel.lfs = 0;
4700 ilabel.label = (char *)buf;
4701 ilabel.len = buflen;
4702
4703 cred = rpc_lookup_cred();
4704 if (IS_ERR(cred))
4705 return PTR_ERR(cred);
4706
4707 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4708 if (IS_ERR(olabel)) {
4709 status = -PTR_ERR(olabel);
4710 goto out;
4711 }
4712
4713 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4714 if (status == 0)
4715 nfs_setsecurity(inode, &fattr, olabel);
4716
4717 nfs4_label_free(olabel);
4718out:
4719 put_rpccred(cred);
4720 return status;
4721}
4722#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4723
4724
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004726nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004728 struct nfs_client *clp = server->nfs_client;
4729
4730 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731 return 0;
4732 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004733 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004734 case -NFS4ERR_ADMIN_REVOKED:
4735 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004736 if (state == NULL)
4737 break;
4738 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004739 case -NFS4ERR_OPENMODE:
4740 if (state == NULL)
4741 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004742 if (nfs4_schedule_stateid_recovery(server, state) < 0)
Chuck Lever9f51a782013-10-17 14:13:13 -04004743 goto recovery_failed;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004744 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004745 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004746 if (state != NULL) {
4747 if (nfs4_schedule_stateid_recovery(server, state) < 0)
Chuck Lever9f51a782013-10-17 14:13:13 -04004748 goto recovery_failed;
Trond Myklebust5d422302013-03-14 16:57:48 -04004749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004751 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004752 nfs4_schedule_lease_recovery(clp);
4753 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -04004754 case -NFS4ERR_MOVED:
4755 if (nfs4_schedule_migration_recovery(server) < 0)
4756 goto recovery_failed;
4757 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004758#if defined(CONFIG_NFS_V4_1)
4759 case -NFS4ERR_BADSESSION:
4760 case -NFS4ERR_BADSLOT:
4761 case -NFS4ERR_BAD_HIGH_SLOT:
4762 case -NFS4ERR_DEADSESSION:
4763 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4764 case -NFS4ERR_SEQ_FALSE_RETRY:
4765 case -NFS4ERR_SEQ_MISORDERED:
4766 dprintk("%s ERROR %d, Reset session\n", __func__,
4767 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004768 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Chuck Leverf1478c12013-10-17 14:13:07 -04004769 goto restart_call;
Andy Adamson4745e312009-04-01 09:22:42 -04004770#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004772 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004773 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004775 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776 case -NFS4ERR_OLD_STATEID:
Chuck Leverf1478c12013-10-17 14:13:07 -04004777 goto restart_call;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 }
4779 task->tk_status = nfs4_map_errors(task->tk_status);
4780 return 0;
Chuck Lever9f51a782013-10-17 14:13:13 -04004781recovery_failed:
Trond Myklebust5d422302013-03-14 16:57:48 -04004782 task->tk_status = -EIO;
4783 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004784wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004785 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004786 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4787 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
Chuck Lever519ae252013-10-17 14:13:19 -04004788 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
4789 goto recovery_failed;
Chuck Leverf1478c12013-10-17 14:13:07 -04004790restart_call:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004791 task->tk_status = 0;
4792 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793}
4794
Chuck Leverf0920752012-05-21 22:45:41 -04004795static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4796 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004797{
4798 __be32 verf[2];
4799
Chuck Lever2c820d92012-05-21 22:45:33 -04004800 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4801 /* An impossible timestamp guarantees this value
4802 * will never match a generated boot time. */
4803 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04004804 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04004805 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004806 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04004807 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
4808 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04004809 }
Chuck Levercd937102012-03-02 17:14:31 -05004810 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4811}
4812
Chuck Levere984a552012-09-14 17:24:21 -04004813static unsigned int
4814nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4815 char *buf, size_t len)
4816{
4817 unsigned int result;
4818
4819 rcu_read_lock();
4820 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4821 clp->cl_ipaddr,
4822 rpc_peeraddr2str(clp->cl_rpcclient,
4823 RPC_DISPLAY_ADDR),
4824 rpc_peeraddr2str(clp->cl_rpcclient,
4825 RPC_DISPLAY_PROTO));
4826 rcu_read_unlock();
4827 return result;
4828}
4829
4830static unsigned int
4831nfs4_init_uniform_client_string(const struct nfs_client *clp,
4832 char *buf, size_t len)
4833{
Trond Myklebust55b59292013-07-24 16:36:35 -04004834 const char *nodename = clp->cl_rpcclient->cl_nodename;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004835
4836 if (nfs4_client_id_uniquifier[0] != '\0')
Trond Myklebust55b59292013-07-24 16:36:35 -04004837 return scnprintf(buf, len, "Linux NFSv%u.%u %s/%s",
4838 clp->rpc_ops->version,
4839 clp->cl_minorversion,
4840 nfs4_client_id_uniquifier,
4841 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004842 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4843 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004844 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004845}
4846
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004847/**
4848 * nfs4_proc_setclientid - Negotiate client ID
4849 * @clp: state data structure
4850 * @program: RPC program for NFSv4 callback service
4851 * @port: IP port number for NFS4 callback service
4852 * @cred: RPC credential to use for this call
4853 * @res: where to place the result
4854 *
4855 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4856 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004857int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4858 unsigned short port, struct rpc_cred *cred,
4859 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860{
4861 nfs4_verifier sc_verifier;
4862 struct nfs4_setclientid setclientid = {
4863 .sc_verifier = &sc_verifier,
4864 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004865 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866 };
4867 struct rpc_message msg = {
4868 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4869 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004870 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004871 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004873 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874
Chuck Leverde734832012-07-11 16:30:50 -04004875 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004876 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004877 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4878 setclientid.sc_name_len =
4879 nfs4_init_uniform_client_string(clp,
4880 setclientid.sc_name,
4881 sizeof(setclientid.sc_name));
4882 else
4883 setclientid.sc_name_len =
4884 nfs4_init_nonuniform_client_string(clp,
4885 setclientid.sc_name,
4886 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004887 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004888 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004889 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Djalal Harounife2d5392013-06-18 17:58:12 +01004890 sizeof(setclientid.sc_netid), "%s",
Chuck Leverd4d3c502007-12-10 14:57:09 -05004891 rpc_peeraddr2str(clp->cl_rpcclient,
4892 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004893 rcu_read_unlock();
4894 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004895 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 clp->cl_ipaddr, port >> 8, port & 255);
4897
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004898 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4899 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4900 setclientid.sc_name_len, setclientid.sc_name);
4901 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004902 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004903 dprintk("NFS reply setclientid: %d\n", status);
4904 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905}
4906
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004907/**
4908 * nfs4_proc_setclientid_confirm - Confirm client ID
4909 * @clp: state data structure
4910 * @res: result of a previous SETCLIENTID
4911 * @cred: RPC credential to use for this call
4912 *
4913 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4914 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004915int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004916 struct nfs4_setclientid_res *arg,
4917 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 struct rpc_message msg = {
4920 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004921 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004922 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 int status;
4925
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004926 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4927 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4928 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004929 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004930 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004931 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 return status;
4933}
4934
Trond Myklebustfe650402006-01-03 09:55:18 +01004935struct nfs4_delegreturndata {
4936 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004937 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004938 struct nfs_fh fh;
4939 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004940 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004941 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004942 int rpc_status;
4943};
4944
Trond Myklebustfe650402006-01-03 09:55:18 +01004945static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4946{
4947 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004948
Trond Myklebust14516c32010-07-31 14:29:06 -04004949 if (!nfs4_sequence_done(task, &data->res.seq_res))
4950 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004951
Trond Myklebustca8acf82013-08-13 10:36:56 -04004952 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004953 switch (task->tk_status) {
4954 case -NFS4ERR_STALE_STATEID:
4955 case -NFS4ERR_EXPIRED:
4956 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004957 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004958 break;
4959 default:
4960 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4961 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004962 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004963 return;
4964 }
4965 }
4966 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004967}
4968
4969static void nfs4_delegreturn_release(void *calldata)
4970{
Trond Myklebustfe650402006-01-03 09:55:18 +01004971 kfree(calldata);
4972}
4973
Andy Adamson938e1012009-04-01 09:22:28 -04004974static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4975{
4976 struct nfs4_delegreturndata *d_data;
4977
4978 d_data = (struct nfs4_delegreturndata *)data;
4979
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004980 nfs4_setup_sequence(d_data->res.server,
4981 &d_data->args.seq_args,
4982 &d_data->res.seq_res,
4983 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004984}
Andy Adamson938e1012009-04-01 09:22:28 -04004985
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004986static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004987 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01004988 .rpc_call_done = nfs4_delegreturn_done,
4989 .rpc_release = nfs4_delegreturn_release,
4990};
4991
Trond Myklebuste6f81072008-01-24 18:14:34 -05004992static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004993{
4994 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004995 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004996 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004997 struct rpc_message msg = {
4998 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4999 .rpc_cred = cred,
5000 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005001 struct rpc_task_setup task_setup_data = {
5002 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005003 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005004 .callback_ops = &nfs4_delegreturn_ops,
5005 .flags = RPC_TASK_ASYNC,
5006 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005007 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005008
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005009 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005010 if (data == NULL)
5011 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005012 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01005013 data->args.fhandle = &data->fh;
5014 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005015 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005016 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005017 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005018 data->res.fattr = &data->fattr;
5019 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005020 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005021 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005022 data->rpc_status = 0;
5023
Trond Myklebustc970aa82007-07-14 15:39:59 -04005024 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005025 msg.rpc_argp = &data->args;
5026 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005027 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005028 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005029 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005030 if (!issync)
5031 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005032 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005033 if (status != 0)
5034 goto out;
5035 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005036 if (status == 0)
5037 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5038 else
5039 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005040out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005041 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005042 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043}
5044
Trond Myklebuste6f81072008-01-24 18:14:34 -05005045int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046{
5047 struct nfs_server *server = NFS_SERVER(inode);
5048 struct nfs4_exception exception = { };
5049 int err;
5050 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005051 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Trond Myklebustca8acf82013-08-13 10:36:56 -04005052 trace_nfs4_delegreturn(inode, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 switch (err) {
5054 case -NFS4ERR_STALE_STATEID:
5055 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 case 0:
5057 return 0;
5058 }
5059 err = nfs4_handle_exception(server, err, &exception);
5060 } while (exception.retry);
5061 return err;
5062}
5063
5064#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5065#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5066
5067/*
5068 * sleep, with exponential backoff, and retry the LOCK operation.
5069 */
5070static unsigned long
5071nfs4_set_lock_task_retry(unsigned long timeout)
5072{
Colin Cross416ad3c2013-05-06 23:50:06 +00005073 freezable_schedule_timeout_killable_unsafe(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 timeout <<= 1;
5075 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5076 return NFS4_LOCK_MAXTIMEOUT;
5077 return timeout;
5078}
5079
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5081{
5082 struct inode *inode = state->inode;
5083 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005084 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005085 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005087 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005089 struct nfs_lockt_res res = {
5090 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091 };
5092 struct rpc_message msg = {
5093 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5094 .rpc_argp = &arg,
5095 .rpc_resp = &res,
5096 .rpc_cred = state->owner->so_cred,
5097 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 struct nfs4_lock_state *lsp;
5099 int status;
5100
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005101 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005102 status = nfs4_set_lock_state(state, request);
5103 if (status != 0)
5104 goto out;
5105 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005106 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005107 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005108 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005109 switch (status) {
5110 case 0:
5111 request->fl_type = F_UNLCK;
5112 break;
5113 case -NFS4ERR_DENIED:
5114 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005116 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005117 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005118out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119 return status;
5120}
5121
5122static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5123{
5124 struct nfs4_exception exception = { };
5125 int err;
5126
5127 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005128 err = _nfs4_proc_getlk(state, cmd, request);
5129 trace_nfs4_get_lock(request, state, cmd, err);
5130 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 &exception);
5132 } while (exception.retry);
5133 return err;
5134}
5135
5136static int do_vfs_lock(struct file *file, struct file_lock *fl)
5137{
5138 int res = 0;
5139 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5140 case FL_POSIX:
5141 res = posix_lock_file_wait(file, fl);
5142 break;
5143 case FL_FLOCK:
5144 res = flock_lock_file_wait(file, fl);
5145 break;
5146 default:
5147 BUG();
5148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149 return res;
5150}
5151
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005152struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005153 struct nfs_locku_args arg;
5154 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005155 struct nfs4_lock_state *lsp;
5156 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005157 struct file_lock fl;
5158 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005159 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005160};
5161
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005162static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5163 struct nfs_open_context *ctx,
5164 struct nfs4_lock_state *lsp,
5165 struct nfs_seqid *seqid)
5166{
5167 struct nfs4_unlockdata *p;
5168 struct inode *inode = lsp->ls_state->inode;
5169
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005170 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005171 if (p == NULL)
5172 return NULL;
5173 p->arg.fh = NFS_FH(inode);
5174 p->arg.fl = &p->fl;
5175 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005176 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005177 p->arg.stateid = &lsp->ls_stateid;
5178 p->lsp = lsp;
5179 atomic_inc(&lsp->ls_count);
5180 /* Ensure we don't close file until we're done freeing locks! */
5181 p->ctx = get_nfs_open_context(ctx);
5182 memcpy(&p->fl, fl, sizeof(p->fl));
5183 p->server = NFS_SERVER(inode);
5184 return p;
5185}
5186
Trond Myklebust06f814a2006-01-03 09:55:07 +01005187static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005188{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005189 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005190 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005191 nfs4_put_lock_state(calldata->lsp);
5192 put_nfs_open_context(calldata->ctx);
5193 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005194}
5195
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005196static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005197{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005198 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005199
Trond Myklebust14516c32010-07-31 14:29:06 -04005200 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5201 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005202 switch (task->tk_status) {
5203 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05005204 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
5205 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005206 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005207 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005208 case -NFS4ERR_BAD_STATEID:
5209 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005210 case -NFS4ERR_STALE_STATEID:
5211 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005212 break;
5213 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005214 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005215 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005216 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005217 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005218}
5219
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005220static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005221{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005222 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005224 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005225 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04005226 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005227 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005228 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005229 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005230 calldata->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04005231 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005232 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005233 &calldata->res.seq_res,
5234 task) != 0)
5235 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005236 return;
5237out_no_action:
5238 task->tk_action = NULL;
5239out_wait:
5240 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241}
5242
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005243static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005244 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005245 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005246 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005247};
5248
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005249static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5250 struct nfs_open_context *ctx,
5251 struct nfs4_lock_state *lsp,
5252 struct nfs_seqid *seqid)
5253{
5254 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005255 struct rpc_message msg = {
5256 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5257 .rpc_cred = ctx->cred,
5258 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005259 struct rpc_task_setup task_setup_data = {
5260 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005261 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005262 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005263 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005264 .flags = RPC_TASK_ASYNC,
5265 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005266
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005267 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5268 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5269
Frank Filz137d6ac2007-07-09 15:32:29 -07005270 /* Ensure this is an unlock - when canceling a lock, the
5271 * canceled lock is passed in, and it won't be an unlock.
5272 */
5273 fl->fl_type = F_UNLCK;
5274
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005275 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5276 if (data == NULL) {
5277 nfs_free_seqid(seqid);
5278 return ERR_PTR(-ENOMEM);
5279 }
5280
Chuck Levera9c92d62013-08-09 12:48:18 -04005281 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005282 msg.rpc_argp = &data->arg;
5283 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005284 task_setup_data.callback_data = data;
5285 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005286}
5287
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5289{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005290 struct inode *inode = state->inode;
5291 struct nfs4_state_owner *sp = state->owner;
5292 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005293 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005294 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005295 struct rpc_task *task;
5296 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005297 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298
Trond Myklebust9b073572006-06-29 16:38:34 -04005299 status = nfs4_set_lock_state(state, request);
5300 /* Unlock _before_ we do the RPC call */
5301 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005302 /* Exclude nfs_delegation_claim_locks() */
5303 mutex_lock(&sp->so_delegreturn_mutex);
5304 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005305 down_read(&nfsi->rwsem);
5306 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
5307 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005308 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005309 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005310 }
5311 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005312 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005313 if (status != 0)
5314 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005315 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005316 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005317 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5318 goto out;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005319 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005320 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005321 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04005322 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005323 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005324 status = PTR_ERR(task);
5325 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005326 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005327 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005328 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005329out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005330 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005331 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005332 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333}
5334
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005335struct nfs4_lockdata {
5336 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005337 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005338 struct nfs4_lock_state *lsp;
5339 struct nfs_open_context *ctx;
5340 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005341 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005342 int rpc_status;
5343 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005344 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005345};
5346
5347static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005348 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5349 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005350{
5351 struct nfs4_lockdata *p;
5352 struct inode *inode = lsp->ls_state->inode;
5353 struct nfs_server *server = NFS_SERVER(inode);
5354
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005355 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005356 if (p == NULL)
5357 return NULL;
5358
5359 p->arg.fh = NFS_FH(inode);
5360 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005361 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005362 if (p->arg.open_seqid == NULL)
5363 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005364 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005365 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005366 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005367 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04005368 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005369 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005370 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005371 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005372 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005373 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005374 atomic_inc(&lsp->ls_count);
5375 p->ctx = get_nfs_open_context(ctx);
5376 memcpy(&p->fl, fl, sizeof(p->fl));
5377 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005378out_free_seqid:
5379 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005380out_free:
5381 kfree(p);
5382 return NULL;
5383}
5384
5385static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5386{
5387 struct nfs4_lockdata *data = calldata;
5388 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389
Harvey Harrison3110ff82008-05-02 13:42:44 -07005390 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005391 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005392 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005393 /* Do we need to do an open_to_lock_owner? */
5394 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005395 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005396 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005397 }
Trond Myklebust8e472f32013-04-20 01:30:53 -04005398 data->arg.open_stateid = &state->open_stateid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005399 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005400 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005401 } else
5402 data->arg.new_lock_owner = 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04005403 if (!nfs4_valid_open_stateid(state)) {
5404 data->rpc_status = -EBADF;
5405 task->tk_action = NULL;
5406 goto out_release_open_seqid;
5407 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005408 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04005409 if (nfs4_setup_sequence(data->server,
5410 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005411 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005412 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04005413 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04005414out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005415 nfs_release_seqid(data->arg.open_seqid);
5416out_release_lock_seqid:
5417 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005418out_wait:
5419 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005420 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005421}
5422
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005423static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5424{
5425 struct nfs4_lockdata *data = calldata;
5426
Harvey Harrison3110ff82008-05-02 13:42:44 -07005427 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005428
Trond Myklebust14516c32010-07-31 14:29:06 -04005429 if (!nfs4_sequence_done(task, &data->res.seq_res))
5430 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04005431
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005432 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005433 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005434 if (data->rpc_status == 0)
5435 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
5436 else
5437 goto out;
5438 }
5439 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05005440 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005441 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04005442 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005443 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005444out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07005445 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005446}
5447
5448static void nfs4_lock_release(void *calldata)
5449{
5450 struct nfs4_lockdata *data = calldata;
5451
Harvey Harrison3110ff82008-05-02 13:42:44 -07005452 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005453 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005454 if (data->cancelled != 0) {
5455 struct rpc_task *task;
5456 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5457 data->arg.lock_seqid);
5458 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08005459 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005460 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005461 } else
5462 nfs_free_seqid(data->arg.lock_seqid);
5463 nfs4_put_lock_state(data->lsp);
5464 put_nfs_open_context(data->ctx);
5465 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005466 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005467}
5468
5469static const struct rpc_call_ops nfs4_lock_ops = {
5470 .rpc_call_prepare = nfs4_lock_prepare,
5471 .rpc_call_done = nfs4_lock_done,
5472 .rpc_release = nfs4_lock_release,
5473};
5474
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005475static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5476{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005477 switch (error) {
5478 case -NFS4ERR_ADMIN_REVOKED:
5479 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05005480 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005481 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04005482 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05005483 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005484 break;
5485 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005486 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05005487 case -NFS4ERR_EXPIRED:
5488 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005489 };
5490}
5491
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005492static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005493{
5494 struct nfs4_lockdata *data;
5495 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005496 struct rpc_message msg = {
5497 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5498 .rpc_cred = state->owner->so_cred,
5499 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005500 struct rpc_task_setup task_setup_data = {
5501 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005502 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005503 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005504 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005505 .flags = RPC_TASK_ASYNC,
5506 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005507 int ret;
5508
Harvey Harrison3110ff82008-05-02 13:42:44 -07005509 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005510 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005511 fl->fl_u.nfs4_fl.owner,
5512 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005513 if (data == NULL)
5514 return -ENOMEM;
5515 if (IS_SETLKW(cmd))
5516 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04005517 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005518 msg.rpc_argp = &data->arg;
5519 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005520 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005521 if (recovery_type > NFS_LOCK_NEW) {
5522 if (recovery_type == NFS_LOCK_RECLAIM)
5523 data->arg.reclaim = NFS_LOCK_RECLAIM;
5524 nfs4_set_sequence_privileged(&data->arg.seq_args);
5525 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04005526 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005527 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005528 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005529 ret = nfs4_wait_for_completion_rpc_task(task);
5530 if (ret == 0) {
5531 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005532 if (ret)
5533 nfs4_handle_setlk_error(data->server, data->lsp,
5534 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005535 } else
5536 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005537 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005538 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005539 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540}
5541
5542static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5543{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005544 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005545 struct nfs4_exception exception = {
5546 .inode = state->inode,
5547 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005548 int err;
5549
5550 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005551 /* Cache the lock if possible... */
5552 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5553 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005554 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005555 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
Trond Myklebust168667c2010-10-19 19:47:49 -04005556 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00005557 break;
5558 nfs4_handle_exception(server, err, &exception);
5559 } while (exception.retry);
5560 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561}
5562
5563static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5564{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005565 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005566 struct nfs4_exception exception = {
5567 .inode = state->inode,
5568 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005569 int err;
5570
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005571 err = nfs4_set_lock_state(state, request);
5572 if (err != 0)
5573 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04005574 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10005575 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
5576 return 0;
5577 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005578 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005579 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5580 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005581 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005582 trace_nfs4_lock_expired(request, state, F_SETLK, err);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005583 switch (err) {
5584 default:
5585 goto out;
5586 case -NFS4ERR_GRACE:
5587 case -NFS4ERR_DELAY:
5588 nfs4_handle_exception(server, err, &exception);
5589 err = 0;
5590 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005591 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005592out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00005593 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594}
5595
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005596#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005597/**
5598 * nfs41_check_expired_locks - possibly free a lock stateid
5599 *
5600 * @state: NFSv4 state for an inode
5601 *
5602 * Returns NFS_OK if recovery for this stateid is now finished.
5603 * Otherwise a negative NFS4ERR value is returned.
5604 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005605static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005606{
Chuck Levereb64cf92012-07-11 16:30:05 -04005607 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005608 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005609 struct nfs_server *server = NFS_SERVER(state->inode);
5610
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005611 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04005612 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005613 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
5614
5615 status = nfs41_test_stateid(server,
5616 &lsp->ls_stateid,
5617 cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04005618 trace_nfs4_test_lock_stateid(state, lsp, status);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005619 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005620 /* Free the stateid unless the server
5621 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04005622 if (status != -NFS4ERR_BAD_STATEID)
5623 nfs41_free_stateid(server,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005624 &lsp->ls_stateid,
5625 cred);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005626 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005627 ret = status;
5628 }
5629 }
5630 };
5631
5632 return ret;
5633}
5634
5635static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5636{
5637 int status = NFS_OK;
5638
5639 if (test_bit(LK_STATE_IN_USE, &state->flags))
5640 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005641 if (status != NFS_OK)
5642 status = nfs4_lock_expired(state, request);
5643 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005644}
5645#endif
5646
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5648{
Trond Myklebust9a99af42013-02-04 20:17:49 -05005649 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005650 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005651 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af42013-02-04 20:17:49 -05005652 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005653 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005655 if ((fl_flags & FL_POSIX) &&
5656 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5657 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005658 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005659 status = nfs4_set_lock_state(state, request);
5660 if (status != 0)
5661 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005662 request->fl_flags |= FL_ACCESS;
5663 status = do_vfs_lock(request->fl_file, request);
5664 if (status < 0)
5665 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005666 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005667 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005668 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005669 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005670 request->fl_flags = fl_flags & ~FL_SLEEP;
5671 status = do_vfs_lock(request->fl_file, request);
5672 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005673 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05005674 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5675 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005676 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005677 if (status != 0)
Trond Myklebust9a99af42013-02-04 20:17:49 -05005678 goto out;
5679 down_read(&nfsi->rwsem);
5680 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5681 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005682 goto out_unlock;
Trond Myklebust9a99af42013-02-04 20:17:49 -05005683 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005684 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005685 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005686 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005687 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5688 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005689out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005690 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005691out:
5692 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693 return status;
5694}
5695
5696static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5697{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005698 struct nfs4_exception exception = {
5699 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005700 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005701 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702 int err;
5703
5704 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005705 err = _nfs4_proc_setlk(state, cmd, request);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005706 trace_nfs4_set_lock(request, state, cmd, err);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005707 if (err == -NFS4ERR_DENIED)
5708 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005710 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711 } while (exception.retry);
5712 return err;
5713}
5714
5715static int
5716nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5717{
5718 struct nfs_open_context *ctx;
5719 struct nfs4_state *state;
5720 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5721 int status;
5722
5723 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005724 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725 state = ctx->state;
5726
5727 if (request->fl_start < 0 || request->fl_end < 0)
5728 return -EINVAL;
5729
Trond Myklebustd9531262009-07-21 19:22:38 -04005730 if (IS_GETLK(cmd)) {
5731 if (state != NULL)
5732 return nfs4_proc_getlk(state, F_GETLK, request);
5733 return 0;
5734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735
5736 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5737 return -EINVAL;
5738
Trond Myklebustd9531262009-07-21 19:22:38 -04005739 if (request->fl_type == F_UNLCK) {
5740 if (state != NULL)
5741 return nfs4_proc_unlck(state, cmd, request);
5742 return 0;
5743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744
Trond Myklebustd9531262009-07-21 19:22:38 -04005745 if (state == NULL)
5746 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005747 /*
5748 * Don't rely on the VFS having checked the file open mode,
5749 * since it won't do this for flock() locks.
5750 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005751 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005752 case F_RDLCK:
5753 if (!(filp->f_mode & FMODE_READ))
5754 return -EBADF;
5755 break;
5756 case F_WRLCK:
5757 if (!(filp->f_mode & FMODE_WRITE))
5758 return -EBADF;
5759 }
5760
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761 do {
5762 status = nfs4_proc_setlk(state, cmd, request);
5763 if ((status != -EAGAIN) || IS_SETLK(cmd))
5764 break;
5765 timeout = nfs4_set_lock_task_retry(timeout);
5766 status = -ERESTARTSYS;
5767 if (signalled())
5768 break;
5769 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770 return status;
5771}
5772
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04005773int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05005774{
5775 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05005776 int err;
5777
5778 err = nfs4_set_lock_state(state, fl);
5779 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04005780 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04005781 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04005782 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05005783}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005784
Trond Myklebustcf470c32012-03-07 13:49:12 -05005785struct nfs_release_lockowner_data {
5786 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005787 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005788 struct nfs_release_lockowner_args args;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005789 struct nfs4_sequence_args seq_args;
5790 struct nfs4_sequence_res seq_res;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005791};
5792
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005793static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
5794{
5795 struct nfs_release_lockowner_data *data = calldata;
5796 nfs40_setup_sequence(data->server,
5797 &data->seq_args, &data->seq_res, task);
5798}
5799
5800static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
5801{
5802 struct nfs_release_lockowner_data *data = calldata;
5803 nfs40_sequence_done(task, &data->seq_res);
5804}
5805
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005806static void nfs4_release_lockowner_release(void *calldata)
5807{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005808 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005809 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005810 kfree(calldata);
5811}
5812
Trond Myklebust17280172012-03-11 13:11:00 -04005813static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005814 .rpc_call_prepare = nfs4_release_lockowner_prepare,
5815 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005816 .rpc_release = nfs4_release_lockowner_release,
5817};
5818
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04005819static int nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005820{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005821 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005822 struct rpc_message msg = {
5823 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5824 };
5825
5826 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005827 return -EINVAL;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005828
Trond Myklebustcf470c32012-03-07 13:49:12 -05005829 data = kmalloc(sizeof(*data), GFP_NOFS);
5830 if (!data)
5831 return -ENOMEM;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005832 nfs4_init_sequence(&data->seq_args, &data->seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05005833 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005834 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005835 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5836 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5837 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005838
Trond Myklebustcf470c32012-03-07 13:49:12 -05005839 msg.rpc_argp = &data->args;
5840 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5841 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005842}
5843
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005844#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5845
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005846static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5847 const void *buf, size_t buflen,
5848 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005849{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005850 if (strcmp(key, "") != 0)
5851 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005852
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005853 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005854}
5855
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005856static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5857 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005858{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005859 if (strcmp(key, "") != 0)
5860 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005861
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005862 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005863}
5864
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005865static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5866 size_t list_len, const char *name,
5867 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005868{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005869 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005870
J. Bruce Fields096455a2006-03-20 23:23:42 -05005871 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5872 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005873
5874 if (list && len <= list_len)
5875 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005876 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005877}
5878
David Quigleyc9bccef2013-05-22 12:50:45 -04005879#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5880static inline int nfs4_server_supports_labels(struct nfs_server *server)
5881{
5882 return server->caps & NFS_CAP_SECURITY_LABEL;
5883}
5884
5885static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
5886 const void *buf, size_t buflen,
5887 int flags, int type)
5888{
5889 if (security_ismaclabel(key))
5890 return nfs4_set_security_label(dentry, buf, buflen);
5891
5892 return -EOPNOTSUPP;
5893}
5894
5895static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
5896 void *buf, size_t buflen, int type)
5897{
5898 if (security_ismaclabel(key))
5899 return nfs4_get_security_label(dentry->d_inode, buf, buflen);
5900 return -EOPNOTSUPP;
5901}
5902
5903static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
5904 size_t list_len, const char *name,
5905 size_t name_len, int type)
5906{
5907 size_t len = 0;
5908
5909 if (nfs_server_capable(dentry->d_inode, NFS_CAP_SECURITY_LABEL)) {
5910 len = security_inode_listsecurity(dentry->d_inode, NULL, 0);
5911 if (list && len <= list_len)
5912 security_inode_listsecurity(dentry->d_inode, list, len);
5913 }
5914 return len;
5915}
5916
5917static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
5918 .prefix = XATTR_SECURITY_PREFIX,
5919 .list = nfs4_xattr_list_nfs4_label,
5920 .get = nfs4_xattr_get_nfs4_label,
5921 .set = nfs4_xattr_set_nfs4_label,
5922};
5923#endif
5924
5925
Andy Adamson533eb462011-06-13 18:25:56 -04005926/*
5927 * nfs_fhget will use either the mounted_on_fileid or the fileid
5928 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005929static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5930{
Andy Adamson533eb462011-06-13 18:25:56 -04005931 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5932 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5933 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005934 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005935 return;
5936
5937 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005938 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005939 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5940 fattr->nlink = 2;
5941}
5942
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005943static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5944 const struct qstr *name,
5945 struct nfs4_fs_locations *fs_locations,
5946 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005947{
5948 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04005949 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005950 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005951 };
5952 struct nfs4_fs_locations_arg args = {
5953 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005954 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005955 .page = page,
5956 .bitmask = bitmask,
5957 };
Benny Halevy22958462009-04-01 09:22:02 -04005958 struct nfs4_fs_locations_res res = {
5959 .fs_locations = fs_locations,
5960 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005961 struct rpc_message msg = {
5962 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5963 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005964 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005965 };
5966 int status;
5967
Harvey Harrison3110ff82008-05-02 13:42:44 -07005968 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005969
5970 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5971 * is not supported */
5972 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5973 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5974 else
5975 bitmask[0] |= FATTR4_WORD0_FILEID;
5976
Trond Myklebustc228fd32007-01-13 02:28:11 -05005977 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005978 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005979 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005980 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005981 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005982 return status;
5983}
5984
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005985int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5986 const struct qstr *name,
5987 struct nfs4_fs_locations *fs_locations,
5988 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005989{
5990 struct nfs4_exception exception = { };
5991 int err;
5992 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04005993 err = _nfs4_proc_fs_locations(client, dir, name,
5994 fs_locations, page);
5995 trace_nfs4_get_fs_locations(dir, name, err);
5996 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005997 &exception);
5998 } while (exception.retry);
5999 return err;
6000}
6001
Chuck Leverb03d7352013-10-17 14:12:50 -04006002/*
6003 * This operation also signals the server that this client is
6004 * performing migration recovery. The server can stop returning
6005 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6006 * appended to this compound to identify the client ID which is
6007 * performing recovery.
6008 */
6009static int _nfs40_proc_get_locations(struct inode *inode,
6010 struct nfs4_fs_locations *locations,
6011 struct page *page, struct rpc_cred *cred)
6012{
6013 struct nfs_server *server = NFS_SERVER(inode);
6014 struct rpc_clnt *clnt = server->client;
6015 u32 bitmask[2] = {
6016 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6017 };
6018 struct nfs4_fs_locations_arg args = {
6019 .clientid = server->nfs_client->cl_clientid,
6020 .fh = NFS_FH(inode),
6021 .page = page,
6022 .bitmask = bitmask,
6023 .migration = 1, /* skip LOOKUP */
6024 .renew = 1, /* append RENEW */
6025 };
6026 struct nfs4_fs_locations_res res = {
6027 .fs_locations = locations,
6028 .migration = 1,
6029 .renew = 1,
6030 };
6031 struct rpc_message msg = {
6032 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6033 .rpc_argp = &args,
6034 .rpc_resp = &res,
6035 .rpc_cred = cred,
6036 };
6037 unsigned long now = jiffies;
6038 int status;
6039
6040 nfs_fattr_init(&locations->fattr);
6041 locations->server = server;
6042 locations->nlocations = 0;
6043
6044 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6045 nfs4_set_sequence_privileged(&args.seq_args);
6046 status = nfs4_call_sync_sequence(clnt, server, &msg,
6047 &args.seq_args, &res.seq_res);
6048 if (status)
6049 return status;
6050
6051 renew_lease(server, now);
6052 return 0;
6053}
6054
6055#ifdef CONFIG_NFS_V4_1
6056
6057/*
6058 * This operation also signals the server that this client is
6059 * performing migration recovery. The server can stop asserting
6060 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6061 * performing this operation is identified in the SEQUENCE
6062 * operation in this compound.
6063 *
6064 * When the client supports GETATTR(fs_locations_info), it can
6065 * be plumbed in here.
6066 */
6067static int _nfs41_proc_get_locations(struct inode *inode,
6068 struct nfs4_fs_locations *locations,
6069 struct page *page, struct rpc_cred *cred)
6070{
6071 struct nfs_server *server = NFS_SERVER(inode);
6072 struct rpc_clnt *clnt = server->client;
6073 u32 bitmask[2] = {
6074 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6075 };
6076 struct nfs4_fs_locations_arg args = {
6077 .fh = NFS_FH(inode),
6078 .page = page,
6079 .bitmask = bitmask,
6080 .migration = 1, /* skip LOOKUP */
6081 };
6082 struct nfs4_fs_locations_res res = {
6083 .fs_locations = locations,
6084 .migration = 1,
6085 };
6086 struct rpc_message msg = {
6087 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6088 .rpc_argp = &args,
6089 .rpc_resp = &res,
6090 .rpc_cred = cred,
6091 };
6092 int status;
6093
6094 nfs_fattr_init(&locations->fattr);
6095 locations->server = server;
6096 locations->nlocations = 0;
6097
6098 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6099 nfs4_set_sequence_privileged(&args.seq_args);
6100 status = nfs4_call_sync_sequence(clnt, server, &msg,
6101 &args.seq_args, &res.seq_res);
6102 if (status == NFS4_OK &&
6103 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6104 status = -NFS4ERR_LEASE_MOVED;
6105 return status;
6106}
6107
6108#endif /* CONFIG_NFS_V4_1 */
6109
6110/**
6111 * nfs4_proc_get_locations - discover locations for a migrated FSID
6112 * @inode: inode on FSID that is migrating
6113 * @locations: result of query
6114 * @page: buffer
6115 * @cred: credential to use for this operation
6116 *
6117 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6118 * operation failed, or a negative errno if a local error occurred.
6119 *
6120 * On success, "locations" is filled in, but if the server has
6121 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6122 * asserted.
6123 *
6124 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6125 * from this client that require migration recovery.
6126 */
6127int nfs4_proc_get_locations(struct inode *inode,
6128 struct nfs4_fs_locations *locations,
6129 struct page *page, struct rpc_cred *cred)
6130{
6131 struct nfs_server *server = NFS_SERVER(inode);
6132 struct nfs_client *clp = server->nfs_client;
6133 const struct nfs4_mig_recovery_ops *ops =
6134 clp->cl_mvops->mig_recovery_ops;
6135 struct nfs4_exception exception = { };
6136 int status;
6137
6138 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6139 (unsigned long long)server->fsid.major,
6140 (unsigned long long)server->fsid.minor,
6141 clp->cl_hostname);
6142 nfs_display_fhandle(NFS_FH(inode), __func__);
6143
6144 do {
6145 status = ops->get_locations(inode, locations, page, cred);
6146 if (status != -NFS4ERR_DELAY)
6147 break;
6148 nfs4_handle_exception(server, status, &exception);
6149 } while (exception.retry);
6150 return status;
6151}
6152
Andy Adamson5ec16a82013-08-08 10:57:55 -04006153/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006154 * If 'use_integrity' is true and the state managment nfs_client
6155 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6156 * and the machine credential as per RFC3530bis and RFC5661 Security
6157 * Considerations sections. Otherwise, just use the user cred with the
6158 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04006159 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006160static 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 +00006161{
6162 int status;
6163 struct nfs4_secinfo_arg args = {
6164 .dir_fh = NFS_FH(dir),
6165 .name = name,
6166 };
6167 struct nfs4_secinfo_res res = {
6168 .flavors = flavors,
6169 };
6170 struct rpc_message msg = {
6171 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6172 .rpc_argp = &args,
6173 .rpc_resp = &res,
6174 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006175 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006176 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006177
6178 if (use_integrity) {
6179 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006180 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6181 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006182 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006183
6184 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006185
6186 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6187 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6188
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006189 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6190 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006191 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006192
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006193 if (cred)
6194 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006195
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006196 return status;
6197}
6198
Bryan Schumaker72de53e2012-04-27 13:27:40 -04006199int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6200 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006201{
6202 struct nfs4_exception exception = { };
6203 int err;
6204 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006205 err = -NFS4ERR_WRONGSEC;
6206
6207 /* try to use integrity protection with machine cred */
6208 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6209 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6210
6211 /*
6212 * if unable to use integrity protection, or SECINFO with
6213 * integrity protection returns NFS4ERR_WRONGSEC (which is
6214 * disallowed by spec, but exists in deployed servers) use
6215 * the current filesystem's rpc_client and the user cred.
6216 */
6217 if (err == -NFS4ERR_WRONGSEC)
6218 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6219
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006220 trace_nfs4_secinfo(dir, name, err);
6221 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006222 &exception);
6223 } while (exception.retry);
6224 return err;
6225}
6226
Andy Adamson557134a2009-04-01 09:21:53 -04006227#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04006228/*
Andy Adamson357f54d2010-12-14 10:11:57 -05006229 * Check the exchange flags returned by the server for invalid flags, having
6230 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6231 * DS flags set.
6232 */
6233static int nfs4_check_cl_exchange_flags(u32 flags)
6234{
6235 if (flags & ~EXCHGID4_FLAG_MASK_R)
6236 goto out_inval;
6237 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6238 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6239 goto out_inval;
6240 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6241 goto out_inval;
6242 return NFS_OK;
6243out_inval:
6244 return -NFS4ERR_INVAL;
6245}
6246
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006247static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006248nfs41_same_server_scope(struct nfs41_server_scope *a,
6249 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006250{
6251 if (a->server_scope_sz == b->server_scope_sz &&
6252 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6253 return true;
6254
6255 return false;
6256}
6257
Andy Adamson357f54d2010-12-14 10:11:57 -05006258/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006259 * nfs4_proc_bind_conn_to_session()
6260 *
6261 * The 4.1 client currently uses the same TCP connection for the
6262 * fore and backchannel.
6263 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006264int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006265{
6266 int status;
6267 struct nfs41_bind_conn_to_session_res res;
6268 struct rpc_message msg = {
6269 .rpc_proc =
6270 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
6271 .rpc_argp = clp,
6272 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006273 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006274 };
6275
6276 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006277
6278 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
6279 if (unlikely(res.session == NULL)) {
6280 status = -ENOMEM;
6281 goto out;
6282 }
6283
6284 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006285 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006286 if (status == 0) {
6287 if (memcmp(res.session->sess_id.data,
6288 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6289 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6290 status = -EIO;
6291 goto out_session;
6292 }
6293 if (res.dir != NFS4_CDFS4_BOTH) {
6294 dprintk("NFS: %s: Unexpected direction from server\n",
6295 __func__);
6296 status = -EIO;
6297 goto out_session;
6298 }
6299 if (res.use_conn_in_rdma_mode) {
6300 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6301 __func__);
6302 status = -EIO;
6303 goto out_session;
6304 }
6305 }
6306out_session:
6307 kfree(res.session);
6308out:
6309 dprintk("<-- %s status= %d\n", __func__, status);
6310 return status;
6311}
6312
6313/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006314 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6315 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04006316 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006317static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
6318 .how = SP4_MACH_CRED,
6319 .enforce.u.words = {
6320 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6321 1 << (OP_EXCHANGE_ID - 32) |
6322 1 << (OP_CREATE_SESSION - 32) |
6323 1 << (OP_DESTROY_SESSION - 32) |
6324 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006325 },
6326 .allow.u.words = {
6327 [0] = 1 << (OP_CLOSE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006328 1 << (OP_LOCKU) |
6329 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006330 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006331 1 << (OP_SECINFO_NO_NAME - 32) |
6332 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006333 1 << (OP_FREE_STATEID - 32) |
6334 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006335 }
6336};
6337
6338/*
6339 * Select the state protection mode for client `clp' given the server results
6340 * from exchange_id in `sp'.
6341 *
6342 * Returns 0 on success, negative errno otherwise.
6343 */
6344static int nfs4_sp4_select_mode(struct nfs_client *clp,
6345 struct nfs41_state_protection *sp)
6346{
6347 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
6348 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6349 1 << (OP_EXCHANGE_ID - 32) |
6350 1 << (OP_CREATE_SESSION - 32) |
6351 1 << (OP_DESTROY_SESSION - 32) |
6352 1 << (OP_DESTROY_CLIENTID - 32)
6353 };
6354 unsigned int i;
6355
6356 if (sp->how == SP4_MACH_CRED) {
6357 /* Print state protect result */
6358 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
6359 for (i = 0; i <= LAST_NFS4_OP; i++) {
6360 if (test_bit(i, sp->enforce.u.longs))
6361 dfprintk(MOUNT, " enforce op %d\n", i);
6362 if (test_bit(i, sp->allow.u.longs))
6363 dfprintk(MOUNT, " allow op %d\n", i);
6364 }
6365
6366 /* make sure nothing is on enforce list that isn't supported */
6367 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
6368 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
6369 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6370 return -EINVAL;
6371 }
6372 }
6373
6374 /*
6375 * Minimal mode - state operations are allowed to use machine
6376 * credential. Note this already happens by default, so the
6377 * client doesn't have to do anything more than the negotiation.
6378 *
6379 * NOTE: we don't care if EXCHANGE_ID is in the list -
6380 * we're already using the machine cred for exchange_id
6381 * and will never use a different cred.
6382 */
6383 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
6384 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
6385 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
6386 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
6387 dfprintk(MOUNT, "sp4_mach_cred:\n");
6388 dfprintk(MOUNT, " minimal mode enabled\n");
6389 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
6390 } else {
6391 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6392 return -EINVAL;
6393 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006394
6395 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
6396 test_bit(OP_LOCKU, sp->allow.u.longs)) {
6397 dfprintk(MOUNT, " cleanup mode enabled\n");
6398 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
6399 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006400
6401 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
6402 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
6403 dfprintk(MOUNT, " secinfo mode enabled\n");
6404 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
6405 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006406
6407 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
6408 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
6409 dfprintk(MOUNT, " stateid mode enabled\n");
6410 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
6411 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04006412
6413 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
6414 dfprintk(MOUNT, " write mode enabled\n");
6415 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
6416 }
6417
6418 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
6419 dfprintk(MOUNT, " commit mode enabled\n");
6420 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
6421 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006422 }
6423
6424 return 0;
6425}
6426
6427/*
6428 * _nfs4_proc_exchange_id()
6429 *
6430 * Wrapper for EXCHANGE_ID operation.
6431 */
6432static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
6433 u32 sp4_how)
Benny Halevy99fe60d2009-04-01 09:22:29 -04006434{
6435 nfs4_verifier verifier;
6436 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05006437 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04006438 .client = clp,
Trond Myklebust4f0b4292013-05-20 12:24:03 -04006439 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6440 EXCHGID4_FLAG_BIND_PRINC_STATEID,
Benny Halevy99fe60d2009-04-01 09:22:29 -04006441 };
6442 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04006443 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04006444 };
6445 int status;
6446 struct rpc_message msg = {
6447 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
6448 .rpc_argp = &args,
6449 .rpc_resp = &res,
6450 .rpc_cred = cred,
6451 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04006452
Chuck Leverf0920752012-05-21 22:45:41 -04006453 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04006454 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
6455 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006456 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
6457 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6458 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006459
Chuck Leveracdeb692012-05-21 22:46:16 -04006460 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006461 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04006462 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006463 status = -ENOMEM;
6464 goto out;
6465 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006466
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006467 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006468 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006469 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04006470 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04006471 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04006472 }
6473
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006474 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006475 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006476 status = -ENOMEM;
6477 goto out_server_scope;
6478 }
6479
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006480 switch (sp4_how) {
6481 case SP4_NONE:
6482 args.state_protect.how = SP4_NONE;
6483 break;
6484
6485 case SP4_MACH_CRED:
6486 args.state_protect = nfs4_sp4_mach_cred_request;
6487 break;
6488
6489 default:
6490 /* unsupported! */
6491 WARN_ON_ONCE(1);
6492 status = -EINVAL;
6493 goto out_server_scope;
6494 }
6495
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006496 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006497 trace_nfs4_exchange_id(clp, status);
Chuck Lever177313f2012-05-21 22:44:58 -04006498 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04006499 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006500
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006501 if (status == 0)
6502 status = nfs4_sp4_select_mode(clp, &res.state_protect);
6503
Chuck Lever177313f2012-05-21 22:44:58 -04006504 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04006505 clp->cl_clientid = res.clientid;
6506 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
6507 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
6508 clp->cl_seqid = res.seqid;
6509
Chuck Leveracdeb692012-05-21 22:46:16 -04006510 kfree(clp->cl_serverowner);
6511 clp->cl_serverowner = res.server_owner;
6512 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04006513
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006514 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04006515 kfree(clp->cl_implid);
6516 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006517
Chuck Lever177313f2012-05-21 22:44:58 -04006518 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006519 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006520 res.server_scope)) {
6521 dprintk("%s: server_scope mismatch detected\n",
6522 __func__);
6523 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006524 kfree(clp->cl_serverscope);
6525 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006526 }
6527
Chuck Lever177313f2012-05-21 22:44:58 -04006528 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006529 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006530 goto out;
6531 }
Trond Myklebust32b01312012-05-26 13:41:04 -04006532 } else
6533 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006534
Chuck Leveracdeb692012-05-21 22:46:16 -04006535out_server_owner:
6536 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006537out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006538 kfree(res.server_scope);
6539out:
Chuck Lever177313f2012-05-21 22:44:58 -04006540 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006541 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006542 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006543 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04006544 clp->cl_implid->date.seconds,
6545 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006546 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006547 return status;
6548}
6549
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006550/*
6551 * nfs4_proc_exchange_id()
6552 *
6553 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6554 *
6555 * Since the clientid has expired, all compounds using sessions
6556 * associated with the stale clientid will be returning
6557 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
6558 * be in some phase of session reset.
6559 *
6560 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
6561 */
6562int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
6563{
6564 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
6565 int status;
6566
6567 /* try SP4_MACH_CRED if krb5i/p */
6568 if (authflavor == RPC_AUTH_GSS_KRB5I ||
6569 authflavor == RPC_AUTH_GSS_KRB5P) {
6570 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
6571 if (!status)
6572 return 0;
6573 }
6574
6575 /* try SP4_NONE */
6576 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
6577}
6578
Trond Myklebust66245532012-05-25 17:18:09 -04006579static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
6580 struct rpc_cred *cred)
6581{
6582 struct rpc_message msg = {
6583 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
6584 .rpc_argp = clp,
6585 .rpc_cred = cred,
6586 };
6587 int status;
6588
6589 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006590 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04006591 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04006592 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04006593 "DESTROY_CLIENTID.", status, clp->cl_hostname);
6594 return status;
6595}
6596
6597static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
6598 struct rpc_cred *cred)
6599{
6600 unsigned int loop;
6601 int ret;
6602
6603 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
6604 ret = _nfs4_proc_destroy_clientid(clp, cred);
6605 switch (ret) {
6606 case -NFS4ERR_DELAY:
6607 case -NFS4ERR_CLIENTID_BUSY:
6608 ssleep(1);
6609 break;
6610 default:
6611 return ret;
6612 }
6613 }
6614 return 0;
6615}
6616
6617int nfs4_destroy_clientid(struct nfs_client *clp)
6618{
6619 struct rpc_cred *cred;
6620 int ret = 0;
6621
6622 if (clp->cl_mvops->minor_version < 1)
6623 goto out;
6624 if (clp->cl_exchange_flags == 0)
6625 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04006626 if (clp->cl_preserve_clid)
6627 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04006628 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04006629 ret = nfs4_proc_destroy_clientid(clp, cred);
6630 if (cred)
6631 put_rpccred(cred);
6632 switch (ret) {
6633 case 0:
6634 case -NFS4ERR_STALE_CLIENTID:
6635 clp->cl_exchange_flags = 0;
6636 }
6637out:
6638 return ret;
6639}
6640
Andy Adamson2050f0c2009-04-01 09:22:30 -04006641struct nfs4_get_lease_time_data {
6642 struct nfs4_get_lease_time_args *args;
6643 struct nfs4_get_lease_time_res *res;
6644 struct nfs_client *clp;
6645};
6646
6647static void nfs4_get_lease_time_prepare(struct rpc_task *task,
6648 void *calldata)
6649{
Andy Adamson2050f0c2009-04-01 09:22:30 -04006650 struct nfs4_get_lease_time_data *data =
6651 (struct nfs4_get_lease_time_data *)calldata;
6652
6653 dprintk("--> %s\n", __func__);
6654 /* just setup sequence, do not trigger session recovery
6655 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006656 nfs41_setup_sequence(data->clp->cl_session,
6657 &data->args->la_seq_args,
6658 &data->res->lr_seq_res,
6659 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006660 dprintk("<-- %s\n", __func__);
6661}
6662
6663/*
6664 * Called from nfs4_state_manager thread for session setup, so don't recover
6665 * from sequence operation or clientid errors.
6666 */
6667static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
6668{
6669 struct nfs4_get_lease_time_data *data =
6670 (struct nfs4_get_lease_time_data *)calldata;
6671
6672 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006673 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
6674 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04006675 switch (task->tk_status) {
6676 case -NFS4ERR_DELAY:
6677 case -NFS4ERR_GRACE:
6678 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
6679 rpc_delay(task, NFS4_POLL_RETRY_MIN);
6680 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006681 /* fall through */
6682 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006683 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006684 return;
6685 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04006686 dprintk("<-- %s\n", __func__);
6687}
6688
Trond Myklebust17280172012-03-11 13:11:00 -04006689static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04006690 .rpc_call_prepare = nfs4_get_lease_time_prepare,
6691 .rpc_call_done = nfs4_get_lease_time_done,
6692};
6693
6694int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
6695{
6696 struct rpc_task *task;
6697 struct nfs4_get_lease_time_args args;
6698 struct nfs4_get_lease_time_res res = {
6699 .lr_fsinfo = fsinfo,
6700 };
6701 struct nfs4_get_lease_time_data data = {
6702 .args = &args,
6703 .res = &res,
6704 .clp = clp,
6705 };
6706 struct rpc_message msg = {
6707 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
6708 .rpc_argp = &args,
6709 .rpc_resp = &res,
6710 };
6711 struct rpc_task_setup task_setup = {
6712 .rpc_client = clp->cl_rpcclient,
6713 .rpc_message = &msg,
6714 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006715 .callback_data = &data,
6716 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04006717 };
6718 int status;
6719
Chuck Levera9c92d62013-08-09 12:48:18 -04006720 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006721 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006722 dprintk("--> %s\n", __func__);
6723 task = rpc_run_task(&task_setup);
6724
6725 if (IS_ERR(task))
6726 status = PTR_ERR(task);
6727 else {
6728 status = task->tk_status;
6729 rpc_put_task(task);
6730 }
6731 dprintk("<-- %s return %d\n", __func__, status);
6732
6733 return status;
6734}
6735
Andy Adamsonfc931582009-04-01 09:22:31 -04006736/*
6737 * Initialize the values to be used by the client in CREATE_SESSION
6738 * If nfs4_init_session set the fore channel request and response sizes,
6739 * use them.
6740 *
6741 * Set the back channel max_resp_sz_cached to zero to force the client to
6742 * always set csa_cachethis to FALSE because the current implementation
6743 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
6744 */
6745static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
6746{
Andy Adamson18aad3d2013-06-26 12:21:49 -04006747 unsigned int max_rqst_sz, max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006748
Andy Adamson18aad3d2013-06-26 12:21:49 -04006749 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
6750 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
6751
Andy Adamsonfc931582009-04-01 09:22:31 -04006752 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04006753 args->fc_attrs.max_rqst_sz = max_rqst_sz;
6754 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006755 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05006756 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04006757
6758 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05006759 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04006760 __func__,
6761 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05006762 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04006763
6764 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04006765 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
6766 args->bc_attrs.max_resp_sz = PAGE_SIZE;
6767 args->bc_attrs.max_resp_sz_cached = 0;
6768 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
6769 args->bc_attrs.max_reqs = 1;
6770
6771 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
6772 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
6773 __func__,
6774 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
6775 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
6776 args->bc_attrs.max_reqs);
6777}
6778
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006779static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04006780{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006781 struct nfs4_channel_attrs *sent = &args->fc_attrs;
6782 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
6783
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006784 if (rcvd->max_resp_sz > sent->max_resp_sz)
6785 return -EINVAL;
6786 /*
6787 * Our requested max_ops is the minimum we need; we're not
6788 * prepared to break up compounds into smaller pieces than that.
6789 * So, no point even trying to continue if the server won't
6790 * cooperate:
6791 */
6792 if (rcvd->max_ops < sent->max_ops)
6793 return -EINVAL;
6794 if (rcvd->max_reqs == 0)
6795 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006796 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
6797 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006798 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04006799}
6800
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006801static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
6802{
6803 struct nfs4_channel_attrs *sent = &args->bc_attrs;
6804 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04006805
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006806 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
6807 return -EINVAL;
6808 if (rcvd->max_resp_sz < sent->max_resp_sz)
6809 return -EINVAL;
6810 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
6811 return -EINVAL;
6812 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006813 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006814 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006815 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006816 return -EINVAL;
6817 return 0;
6818}
Andy Adamson8d353012009-04-01 09:22:32 -04006819
Andy Adamson8d353012009-04-01 09:22:32 -04006820static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
6821 struct nfs4_session *session)
6822{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006823 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04006824
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006825 ret = nfs4_verify_fore_channel_attrs(args, session);
6826 if (ret)
6827 return ret;
6828 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04006829}
6830
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006831static int _nfs4_proc_create_session(struct nfs_client *clp,
6832 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04006833{
6834 struct nfs4_session *session = clp->cl_session;
6835 struct nfs41_create_session_args args = {
6836 .client = clp,
6837 .cb_program = NFS4_CALLBACK,
6838 };
6839 struct nfs41_create_session_res res = {
6840 .client = clp,
6841 };
6842 struct rpc_message msg = {
6843 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
6844 .rpc_argp = &args,
6845 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006846 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04006847 };
6848 int status;
6849
6850 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04006851 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04006852
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006853 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006854 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04006855
Trond Myklebust43095d32012-11-20 11:13:12 -05006856 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04006857 /* Verify the session's negotiated channel_attrs values */
6858 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04006859 /* Increment the clientid slot sequence id */
6860 clp->cl_seqid++;
6861 }
6862
6863 return status;
6864}
6865
6866/*
6867 * Issues a CREATE_SESSION operation to the server.
6868 * It is the responsibility of the caller to verify the session is
6869 * expired before calling this routine.
6870 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006871int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04006872{
6873 int status;
6874 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04006875 struct nfs4_session *session = clp->cl_session;
6876
6877 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
6878
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006879 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04006880 if (status)
6881 goto out;
6882
Andy Adamsonaacd5532011-11-09 13:58:21 -05006883 /* Init or reset the session slot tables */
6884 status = nfs4_setup_session_slot_tables(session);
6885 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04006886 if (status)
6887 goto out;
6888
6889 ptr = (unsigned *)&session->sess_id.data[0];
6890 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
6891 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04006892out:
6893 dprintk("<-- %s\n", __func__);
6894 return status;
6895}
6896
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006897/*
6898 * Issue the over-the-wire RPC DESTROY_SESSION.
6899 * The caller must serialize access to this routine.
6900 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006901int nfs4_proc_destroy_session(struct nfs4_session *session,
6902 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006903{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006904 struct rpc_message msg = {
6905 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
6906 .rpc_argp = session,
6907 .rpc_cred = cred,
6908 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006909 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006910
6911 dprintk("--> nfs4_proc_destroy_session\n");
6912
6913 /* session is still being setup */
6914 if (session->clp->cl_cons_state != NFS_CS_READY)
6915 return status;
6916
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006917 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006918 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006919
6920 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04006921 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006922 "Session has been destroyed regardless...\n", status);
6923
6924 dprintk("<-- nfs4_proc_destroy_session\n");
6925 return status;
6926}
6927
Trond Myklebust7b38c362012-05-23 13:23:31 -04006928/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006929 * Renew the cl_session lease.
6930 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006931struct nfs4_sequence_data {
6932 struct nfs_client *clp;
6933 struct nfs4_sequence_args args;
6934 struct nfs4_sequence_res res;
6935};
6936
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006937static void nfs41_sequence_release(void *data)
6938{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006939 struct nfs4_sequence_data *calldata = data;
6940 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006941
Alexandros Batsakis71358402010-02-05 03:45:05 -08006942 if (atomic_read(&clp->cl_count) > 1)
6943 nfs4_schedule_state_renewal(clp);
6944 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006945 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006946}
6947
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006948static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6949{
6950 switch(task->tk_status) {
6951 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006952 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6953 return -EAGAIN;
6954 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006955 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006956 }
6957 return 0;
6958}
6959
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006960static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006961{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006962 struct nfs4_sequence_data *calldata = data;
6963 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006964
Trond Myklebust14516c32010-07-31 14:29:06 -04006965 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6966 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006967
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006968 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006969 if (task->tk_status < 0) {
6970 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006971 if (atomic_read(&clp->cl_count) == 1)
6972 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006973
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006974 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6975 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006976 return;
6977 }
6978 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006979 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006980out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006981 dprintk("<-- %s\n", __func__);
6982}
6983
6984static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6985{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006986 struct nfs4_sequence_data *calldata = data;
6987 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006988 struct nfs4_sequence_args *args;
6989 struct nfs4_sequence_res *res;
6990
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006991 args = task->tk_msg.rpc_argp;
6992 res = task->tk_msg.rpc_resp;
6993
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006994 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006995}
6996
6997static const struct rpc_call_ops nfs41_sequence_ops = {
6998 .rpc_call_done = nfs41_sequence_call_done,
6999 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007000 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007001};
7002
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007003static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
7004 struct rpc_cred *cred,
7005 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007006{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007007 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007008 struct rpc_message msg = {
7009 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
7010 .rpc_cred = cred,
7011 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007012 struct rpc_task_setup task_setup_data = {
7013 .rpc_client = clp->cl_rpcclient,
7014 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007015 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04007016 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007017 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007018
Alexandros Batsakis71358402010-02-05 03:45:05 -08007019 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007020 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04007021 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007022 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08007023 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007024 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007025 }
Chuck Levera9c92d62013-08-09 12:48:18 -04007026 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007027 if (is_privileged)
7028 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007029 msg.rpc_argp = &calldata->args;
7030 msg.rpc_resp = &calldata->res;
7031 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007032 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007033
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007034 return rpc_run_task(&task_setup_data);
7035}
7036
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007037static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007038{
7039 struct rpc_task *task;
7040 int ret = 0;
7041
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007042 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
7043 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007044 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007045 if (IS_ERR(task))
7046 ret = PTR_ERR(task);
7047 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08007048 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007049 dprintk("<-- %s status=%d\n", __func__, ret);
7050 return ret;
7051}
7052
7053static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
7054{
7055 struct rpc_task *task;
7056 int ret;
7057
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007058 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007059 if (IS_ERR(task)) {
7060 ret = PTR_ERR(task);
7061 goto out;
7062 }
7063 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05007064 if (!ret) {
7065 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
7066
7067 if (task->tk_status == 0)
7068 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007069 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05007070 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007071 rpc_put_task(task);
7072out:
7073 dprintk("<-- %s status=%d\n", __func__, ret);
7074 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007075}
7076
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007077struct nfs4_reclaim_complete_data {
7078 struct nfs_client *clp;
7079 struct nfs41_reclaim_complete_args arg;
7080 struct nfs41_reclaim_complete_res res;
7081};
7082
7083static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
7084{
7085 struct nfs4_reclaim_complete_data *calldata = data;
7086
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007087 nfs41_setup_sequence(calldata->clp->cl_session,
7088 &calldata->arg.seq_args,
7089 &calldata->res.seq_res,
7090 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007091}
7092
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007093static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7094{
7095 switch(task->tk_status) {
7096 case 0:
7097 case -NFS4ERR_COMPLETE_ALREADY:
7098 case -NFS4ERR_WRONG_CRED: /* What to do here? */
7099 break;
7100 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007101 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007102 /* fall through */
7103 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007104 return -EAGAIN;
7105 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007106 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007107 }
7108 return 0;
7109}
7110
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007111static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
7112{
7113 struct nfs4_reclaim_complete_data *calldata = data;
7114 struct nfs_client *clp = calldata->clp;
7115 struct nfs4_sequence_res *res = &calldata->res.seq_res;
7116
7117 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007118 if (!nfs41_sequence_done(task, res))
7119 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007120
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007121 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007122 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
7123 rpc_restart_call_prepare(task);
7124 return;
7125 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007126 dprintk("<-- %s\n", __func__);
7127}
7128
7129static void nfs4_free_reclaim_complete_data(void *data)
7130{
7131 struct nfs4_reclaim_complete_data *calldata = data;
7132
7133 kfree(calldata);
7134}
7135
7136static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
7137 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
7138 .rpc_call_done = nfs4_reclaim_complete_done,
7139 .rpc_release = nfs4_free_reclaim_complete_data,
7140};
7141
7142/*
7143 * Issue a global reclaim complete.
7144 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04007145static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
7146 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007147{
7148 struct nfs4_reclaim_complete_data *calldata;
7149 struct rpc_task *task;
7150 struct rpc_message msg = {
7151 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04007152 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007153 };
7154 struct rpc_task_setup task_setup_data = {
7155 .rpc_client = clp->cl_rpcclient,
7156 .rpc_message = &msg,
7157 .callback_ops = &nfs4_reclaim_complete_call_ops,
7158 .flags = RPC_TASK_ASYNC,
7159 };
7160 int status = -ENOMEM;
7161
7162 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04007163 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007164 if (calldata == NULL)
7165 goto out;
7166 calldata->clp = clp;
7167 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007168
Chuck Levera9c92d62013-08-09 12:48:18 -04007169 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007170 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007171 msg.rpc_argp = &calldata->arg;
7172 msg.rpc_resp = &calldata->res;
7173 task_setup_data.callback_data = calldata;
7174 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007175 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007176 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007177 goto out;
7178 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05007179 status = nfs4_wait_for_completion_rpc_task(task);
7180 if (status == 0)
7181 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007182 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007183 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007184out:
7185 dprintk("<-- %s status=%d\n", __func__, status);
7186 return status;
7187}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007188
7189static void
7190nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
7191{
7192 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00007193 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007194 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007195
7196 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00007197 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
7198 * right now covering the LAYOUTGET we are about to send.
7199 * However, that is not so catastrophic, and there seems
7200 * to be no way to prevent it completely.
7201 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007202 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05007203 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007204 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00007205 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
7206 NFS_I(lgp->args.inode)->layout,
7207 lgp->args.ctx->state)) {
7208 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00007209 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007210}
7211
7212static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
7213{
7214 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07007215 struct inode *inode = lgp->args.inode;
7216 struct nfs_server *server = NFS_SERVER(inode);
7217 struct pnfs_layout_hdr *lo;
7218 struct nfs4_state *state = NULL;
Weston Andros Adamson30005122013-02-28 20:30:10 -05007219 unsigned long timeo, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007220
7221 dprintk("--> %s\n", __func__);
7222
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007223 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07007224 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007225
7226 switch (task->tk_status) {
7227 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07007228 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007229 case -NFS4ERR_LAYOUTTRYLATER:
7230 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05007231 timeo = rpc_get_timeout(task->tk_client);
7232 giveup = lgp->args.timestamp + timeo;
7233 if (time_after(giveup, jiffies))
7234 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07007235 break;
7236 case -NFS4ERR_EXPIRED:
7237 case -NFS4ERR_BAD_STATEID:
7238 spin_lock(&inode->i_lock);
7239 lo = NFS_I(inode)->layout;
7240 if (!lo || list_empty(&lo->plh_segs)) {
7241 spin_unlock(&inode->i_lock);
7242 /* If the open stateid was bad, then recover it. */
7243 state = lgp->args.ctx->state;
7244 } else {
7245 LIST_HEAD(head);
7246
7247 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
7248 spin_unlock(&inode->i_lock);
7249 /* Mark the bad layout state as invalid, then
7250 * retry using the open stateid. */
7251 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007252 }
7253 }
Trond Myklebustee314c22012-10-01 17:25:48 -07007254 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
7255 rpc_restart_call_prepare(task);
7256out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007257 dprintk("<-- %s\n", __func__);
7258}
7259
Idan Kedar85541162012-08-02 11:47:10 +03007260static size_t max_response_pages(struct nfs_server *server)
7261{
7262 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
7263 return nfs_page_array_len(0, max_resp_sz);
7264}
7265
7266static void nfs4_free_pages(struct page **pages, size_t size)
7267{
7268 int i;
7269
7270 if (!pages)
7271 return;
7272
7273 for (i = 0; i < size; i++) {
7274 if (!pages[i])
7275 break;
7276 __free_page(pages[i]);
7277 }
7278 kfree(pages);
7279}
7280
7281static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
7282{
7283 struct page **pages;
7284 int i;
7285
7286 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
7287 if (!pages) {
7288 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
7289 return NULL;
7290 }
7291
7292 for (i = 0; i < size; i++) {
7293 pages[i] = alloc_page(gfp_flags);
7294 if (!pages[i]) {
7295 dprintk("%s: failed to allocate page\n", __func__);
7296 nfs4_free_pages(pages, size);
7297 return NULL;
7298 }
7299 }
7300
7301 return pages;
7302}
7303
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007304static void nfs4_layoutget_release(void *calldata)
7305{
7306 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007307 struct inode *inode = lgp->args.inode;
7308 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007309 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007310
7311 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03007312 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007313 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007314 put_nfs_open_context(lgp->args.ctx);
7315 kfree(calldata);
7316 dprintk("<-- %s\n", __func__);
7317}
7318
7319static const struct rpc_call_ops nfs4_layoutget_call_ops = {
7320 .rpc_call_prepare = nfs4_layoutget_prepare,
7321 .rpc_call_done = nfs4_layoutget_done,
7322 .rpc_release = nfs4_layoutget_release,
7323};
7324
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007325struct pnfs_layout_segment *
7326nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007327{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007328 struct inode *inode = lgp->args.inode;
7329 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007330 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007331 struct rpc_task *task;
7332 struct rpc_message msg = {
7333 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
7334 .rpc_argp = &lgp->args,
7335 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04007336 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007337 };
7338 struct rpc_task_setup task_setup_data = {
7339 .rpc_client = server->client,
7340 .rpc_message = &msg,
7341 .callback_ops = &nfs4_layoutget_call_ops,
7342 .callback_data = lgp,
7343 .flags = RPC_TASK_ASYNC,
7344 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007345 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007346 int status = 0;
7347
7348 dprintk("--> %s\n", __func__);
7349
Idan Kedar85541162012-08-02 11:47:10 +03007350 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
7351 if (!lgp->args.layout.pages) {
7352 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007353 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03007354 }
7355 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05007356 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03007357
Weston Andros Adamson35124a02011-03-24 16:48:21 -04007358 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007359 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04007360 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007361
7362 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
7363 pnfs_get_layout_hdr(NFS_I(inode)->layout);
7364
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007365 task = rpc_run_task(&task_setup_data);
7366 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007367 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007368 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00007369 if (status == 0)
7370 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007371 trace_nfs4_layoutget(lgp->args.ctx,
7372 &lgp->args.range,
7373 &lgp->res.range,
7374 status);
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05007375 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
7376 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007377 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007378 rpc_put_task(task);
7379 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007380 if (status)
7381 return ERR_PTR(status);
7382 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007383}
7384
Benny Halevycbe82602011-05-22 19:52:37 +03007385static void
7386nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
7387{
7388 struct nfs4_layoutreturn *lrp = calldata;
7389
7390 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007391 nfs41_setup_sequence(lrp->clp->cl_session,
7392 &lrp->args.seq_args,
7393 &lrp->res.seq_res,
7394 task);
Benny Halevycbe82602011-05-22 19:52:37 +03007395}
7396
7397static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
7398{
7399 struct nfs4_layoutreturn *lrp = calldata;
7400 struct nfs_server *server;
7401
7402 dprintk("--> %s\n", __func__);
7403
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007404 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03007405 return;
7406
7407 server = NFS_SERVER(lrp->args.inode);
7408 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007409 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03007410 return;
7411 }
Benny Halevycbe82602011-05-22 19:52:37 +03007412 dprintk("<-- %s\n", __func__);
7413}
7414
7415static void nfs4_layoutreturn_release(void *calldata)
7416{
7417 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04007418 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03007419
7420 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04007421 spin_lock(&lo->plh_inode->i_lock);
7422 if (lrp->res.lrs_present)
7423 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
7424 lo->plh_block_lgets--;
7425 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04007426 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03007427 kfree(calldata);
7428 dprintk("<-- %s\n", __func__);
7429}
7430
7431static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
7432 .rpc_call_prepare = nfs4_layoutreturn_prepare,
7433 .rpc_call_done = nfs4_layoutreturn_done,
7434 .rpc_release = nfs4_layoutreturn_release,
7435};
7436
7437int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
7438{
7439 struct rpc_task *task;
7440 struct rpc_message msg = {
7441 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
7442 .rpc_argp = &lrp->args,
7443 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04007444 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03007445 };
7446 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04007447 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03007448 .rpc_message = &msg,
7449 .callback_ops = &nfs4_layoutreturn_call_ops,
7450 .callback_data = lrp,
7451 };
7452 int status;
7453
7454 dprintk("--> %s\n", __func__);
Chuck Levera9c92d62013-08-09 12:48:18 -04007455 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03007456 task = rpc_run_task(&task_setup_data);
7457 if (IS_ERR(task))
7458 return PTR_ERR(task);
7459 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007460 trace_nfs4_layoutreturn(lrp->args.inode, status);
Benny Halevycbe82602011-05-22 19:52:37 +03007461 dprintk("<-- %s status=%d\n", __func__, status);
7462 rpc_put_task(task);
7463 return status;
7464}
7465
Andy Adamson7f11d8d2011-07-30 20:52:35 -04007466/*
7467 * Retrieve the list of Data Server devices from the MDS.
7468 */
7469static int _nfs4_getdevicelist(struct nfs_server *server,
7470 const struct nfs_fh *fh,
7471 struct pnfs_devicelist *devlist)
7472{
7473 struct nfs4_getdevicelist_args args = {
7474 .fh = fh,
7475 .layoutclass = server->pnfs_curr_ld->id,
7476 };
7477 struct nfs4_getdevicelist_res res = {
7478 .devlist = devlist,
7479 };
7480 struct rpc_message msg = {
7481 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
7482 .rpc_argp = &args,
7483 .rpc_resp = &res,
7484 };
7485 int status;
7486
7487 dprintk("--> %s\n", __func__);
7488 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
7489 &res.seq_res, 0);
7490 dprintk("<-- %s status=%d\n", __func__, status);
7491 return status;
7492}
7493
7494int nfs4_proc_getdevicelist(struct nfs_server *server,
7495 const struct nfs_fh *fh,
7496 struct pnfs_devicelist *devlist)
7497{
7498 struct nfs4_exception exception = { };
7499 int err;
7500
7501 do {
7502 err = nfs4_handle_exception(server,
7503 _nfs4_getdevicelist(server, fh, devlist),
7504 &exception);
7505 } while (exception.retry);
7506
7507 dprintk("%s: err=%d, num_devs=%u\n", __func__,
7508 err, devlist->num_devs);
7509
7510 return err;
7511}
7512EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
7513
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007514static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007515_nfs4_proc_getdeviceinfo(struct nfs_server *server,
7516 struct pnfs_device *pdev,
7517 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007518{
7519 struct nfs4_getdeviceinfo_args args = {
7520 .pdev = pdev,
7521 };
7522 struct nfs4_getdeviceinfo_res res = {
7523 .pdev = pdev,
7524 };
7525 struct rpc_message msg = {
7526 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
7527 .rpc_argp = &args,
7528 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007529 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007530 };
7531 int status;
7532
7533 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00007534 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007535 dprintk("<-- %s status=%d\n", __func__, status);
7536
7537 return status;
7538}
7539
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007540int nfs4_proc_getdeviceinfo(struct nfs_server *server,
7541 struct pnfs_device *pdev,
7542 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007543{
7544 struct nfs4_exception exception = { };
7545 int err;
7546
7547 do {
7548 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007549 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007550 &exception);
7551 } while (exception.retry);
7552 return err;
7553}
7554EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
7555
Andy Adamson863a3c62011-03-23 13:27:54 +00007556static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
7557{
7558 struct nfs4_layoutcommit_data *data = calldata;
7559 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007560 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00007561
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007562 nfs41_setup_sequence(session,
7563 &data->args.seq_args,
7564 &data->res.seq_res,
7565 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00007566}
7567
7568static void
7569nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
7570{
7571 struct nfs4_layoutcommit_data *data = calldata;
7572 struct nfs_server *server = NFS_SERVER(data->args.inode);
7573
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007574 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00007575 return;
7576
7577 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007578 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
7579 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
7580 case -NFS4ERR_BADLAYOUT: /* no layout */
7581 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00007582 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007583 break;
7584 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00007585 nfs_post_op_update_inode_force_wcc(data->args.inode,
7586 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007587 break;
7588 default:
7589 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
7590 rpc_restart_call_prepare(task);
7591 return;
7592 }
7593 }
Andy Adamson863a3c62011-03-23 13:27:54 +00007594}
7595
7596static void nfs4_layoutcommit_release(void *calldata)
7597{
7598 struct nfs4_layoutcommit_data *data = calldata;
7599
Andy Adamsondb29c082011-07-30 20:52:38 -04007600 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00007601 put_rpccred(data->cred);
7602 kfree(data);
7603}
7604
7605static const struct rpc_call_ops nfs4_layoutcommit_ops = {
7606 .rpc_call_prepare = nfs4_layoutcommit_prepare,
7607 .rpc_call_done = nfs4_layoutcommit_done,
7608 .rpc_release = nfs4_layoutcommit_release,
7609};
7610
7611int
Andy Adamsonef311532011-03-12 02:58:10 -05007612nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00007613{
7614 struct rpc_message msg = {
7615 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
7616 .rpc_argp = &data->args,
7617 .rpc_resp = &data->res,
7618 .rpc_cred = data->cred,
7619 };
7620 struct rpc_task_setup task_setup_data = {
7621 .task = &data->task,
7622 .rpc_client = NFS_CLIENT(data->args.inode),
7623 .rpc_message = &msg,
7624 .callback_ops = &nfs4_layoutcommit_ops,
7625 .callback_data = data,
7626 .flags = RPC_TASK_ASYNC,
7627 };
7628 struct rpc_task *task;
7629 int status = 0;
7630
7631 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
7632 "lbw: %llu inode %lu\n",
7633 data->task.tk_pid, sync,
7634 data->args.lastbytewritten,
7635 data->args.inode->i_ino);
7636
Chuck Levera9c92d62013-08-09 12:48:18 -04007637 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00007638 task = rpc_run_task(&task_setup_data);
7639 if (IS_ERR(task))
7640 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05007641 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00007642 goto out;
7643 status = nfs4_wait_for_completion_rpc_task(task);
7644 if (status != 0)
7645 goto out;
7646 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007647 trace_nfs4_layoutcommit(data->args.inode, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00007648out:
7649 dprintk("%s: status %d\n", __func__, status);
7650 rpc_put_task(task);
7651 return status;
7652}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007653
Andy Adamson97431202013-08-08 10:57:56 -04007654/**
7655 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
7656 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
7657 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007658static int
7659_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007660 struct nfs_fsinfo *info,
7661 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007662{
7663 struct nfs41_secinfo_no_name_args args = {
7664 .style = SECINFO_STYLE_CURRENT_FH,
7665 };
7666 struct nfs4_secinfo_res res = {
7667 .flavors = flavors,
7668 };
7669 struct rpc_message msg = {
7670 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
7671 .rpc_argp = &args,
7672 .rpc_resp = &res,
7673 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007674 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007675 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007676 int status;
7677
7678 if (use_integrity) {
7679 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007680 cred = nfs4_get_clid_cred(server->nfs_client);
7681 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007682 }
7683
7684 dprintk("--> %s\n", __func__);
7685 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
7686 &res.seq_res, 0);
7687 dprintk("<-- %s status=%d\n", __func__, status);
7688
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007689 if (cred)
7690 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007691
7692 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007693}
7694
7695static int
7696nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7697 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
7698{
7699 struct nfs4_exception exception = { };
7700 int err;
7701 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007702 /* first try using integrity protection */
7703 err = -NFS4ERR_WRONGSEC;
7704
7705 /* try to use integrity protection with machine cred */
7706 if (_nfs4_is_integrity_protected(server->nfs_client))
7707 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
7708 flavors, true);
7709
7710 /*
7711 * if unable to use integrity protection, or SECINFO with
7712 * integrity protection returns NFS4ERR_WRONGSEC (which is
7713 * disallowed by spec, but exists in deployed servers) use
7714 * the current filesystem's rpc_client and the user cred.
7715 */
7716 if (err == -NFS4ERR_WRONGSEC)
7717 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
7718 flavors, false);
7719
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007720 switch (err) {
7721 case 0:
7722 case -NFS4ERR_WRONGSEC:
7723 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007724 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007725 default:
7726 err = nfs4_handle_exception(server, err, &exception);
7727 }
7728 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007729out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007730 return err;
7731}
7732
7733static int
7734nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
7735 struct nfs_fsinfo *info)
7736{
7737 int err;
7738 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04007739 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007740 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04007741 struct nfs4_secinfo4 *secinfo;
7742 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007743
7744 page = alloc_page(GFP_KERNEL);
7745 if (!page) {
7746 err = -ENOMEM;
7747 goto out;
7748 }
7749
7750 flavors = page_address(page);
7751 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
7752
7753 /*
7754 * Fall back on "guess and check" method if
7755 * the server doesn't support SECINFO_NO_NAME
7756 */
7757 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
7758 err = nfs4_find_root_sec(server, fhandle, info);
7759 goto out_freepage;
7760 }
7761 if (err)
7762 goto out_freepage;
7763
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04007764 for (i = 0; i < flavors->num_flavors; i++) {
7765 secinfo = &flavors->flavors[i];
7766
7767 switch (secinfo->flavor) {
7768 case RPC_AUTH_NULL:
7769 case RPC_AUTH_UNIX:
7770 case RPC_AUTH_GSS:
7771 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
7772 &secinfo->flavor_info);
7773 break;
7774 default:
7775 flavor = RPC_AUTH_MAXFLAVOR;
7776 break;
7777 }
7778
7779 if (flavor != RPC_AUTH_MAXFLAVOR) {
7780 err = nfs4_lookup_root_sec(server, fhandle,
7781 info, flavor);
7782 if (!err)
7783 break;
7784 }
7785 }
7786
7787 if (flavor == RPC_AUTH_MAXFLAVOR)
7788 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007789
7790out_freepage:
7791 put_page(page);
7792 if (err == -EACCES)
7793 return -EPERM;
7794out:
7795 return err;
7796}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007797
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007798static int _nfs41_test_stateid(struct nfs_server *server,
7799 nfs4_stateid *stateid,
7800 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04007801{
7802 int status;
7803 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007804 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04007805 };
7806 struct nfs41_test_stateid_res res;
7807 struct rpc_message msg = {
7808 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
7809 .rpc_argp = &args,
7810 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007811 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04007812 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007813 struct rpc_clnt *rpc_client = server->client;
7814
7815 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
7816 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007817
Chuck Lever38527b12012-07-11 16:30:23 -04007818 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04007819 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007820 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007821 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007822 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04007823 if (status != NFS_OK) {
7824 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04007825 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04007826 }
7827 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04007828 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04007829}
7830
Chuck Lever38527b12012-07-11 16:30:23 -04007831/**
7832 * nfs41_test_stateid - perform a TEST_STATEID operation
7833 *
7834 * @server: server / transport on which to perform the operation
7835 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007836 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04007837 *
7838 * Returns NFS_OK if the server recognizes that "stateid" is valid.
7839 * Otherwise a negative NFS4ERR value is returned if the operation
7840 * failed or the state ID is not currently valid.
7841 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007842static int nfs41_test_stateid(struct nfs_server *server,
7843 nfs4_stateid *stateid,
7844 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04007845{
7846 struct nfs4_exception exception = { };
7847 int err;
7848 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007849 err = _nfs41_test_stateid(server, stateid, cred);
Chuck Lever377e5072012-07-11 16:29:45 -04007850 if (err != -NFS4ERR_DELAY)
7851 break;
7852 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04007853 } while (exception.retry);
7854 return err;
7855}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007856
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007857struct nfs_free_stateid_data {
7858 struct nfs_server *server;
7859 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007860 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007861};
7862
7863static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
7864{
7865 struct nfs_free_stateid_data *data = calldata;
7866 nfs41_setup_sequence(nfs4_get_session(data->server),
7867 &data->args.seq_args,
7868 &data->res.seq_res,
7869 task);
7870}
7871
7872static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
7873{
7874 struct nfs_free_stateid_data *data = calldata;
7875
7876 nfs41_sequence_done(task, &data->res.seq_res);
7877
7878 switch (task->tk_status) {
7879 case -NFS4ERR_DELAY:
7880 if (nfs4_async_handle_error(task, data->server, NULL) == -EAGAIN)
7881 rpc_restart_call_prepare(task);
7882 }
7883}
7884
7885static void nfs41_free_stateid_release(void *calldata)
7886{
7887 kfree(calldata);
7888}
7889
Trond Myklebust17f26b12013-08-21 15:48:42 -04007890static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007891 .rpc_call_prepare = nfs41_free_stateid_prepare,
7892 .rpc_call_done = nfs41_free_stateid_done,
7893 .rpc_release = nfs41_free_stateid_release,
7894};
7895
7896static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
7897 nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007898 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007899 bool privileged)
7900{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007901 struct rpc_message msg = {
7902 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007903 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007904 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007905 struct rpc_task_setup task_setup = {
7906 .rpc_client = server->client,
7907 .rpc_message = &msg,
7908 .callback_ops = &nfs41_free_stateid_ops,
7909 .flags = RPC_TASK_ASYNC,
7910 };
7911 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007912
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007913 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
7914 &task_setup.rpc_client, &msg);
7915
Chuck Lever38527b12012-07-11 16:30:23 -04007916 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007917 data = kmalloc(sizeof(*data), GFP_NOFS);
7918 if (!data)
7919 return ERR_PTR(-ENOMEM);
7920 data->server = server;
7921 nfs4_stateid_copy(&data->args.stateid, stateid);
7922
7923 task_setup.callback_data = data;
7924
7925 msg.rpc_argp = &data->args;
7926 msg.rpc_resp = &data->res;
Chuck Levera9c92d62013-08-09 12:48:18 -04007927 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007928 if (privileged)
7929 nfs4_set_sequence_privileged(&data->args.seq_args);
7930
7931 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007932}
7933
Chuck Lever38527b12012-07-11 16:30:23 -04007934/**
7935 * nfs41_free_stateid - perform a FREE_STATEID operation
7936 *
7937 * @server: server / transport on which to perform the operation
7938 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007939 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04007940 *
7941 * Returns NFS_OK if the server freed "stateid". Otherwise a
7942 * negative NFS4ERR value is returned.
7943 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007944static int nfs41_free_stateid(struct nfs_server *server,
7945 nfs4_stateid *stateid,
7946 struct rpc_cred *cred)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007947{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007948 struct rpc_task *task;
7949 int ret;
7950
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007951 task = _nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007952 if (IS_ERR(task))
7953 return PTR_ERR(task);
7954 ret = rpc_wait_for_completion_task(task);
7955 if (!ret)
7956 ret = task->tk_status;
7957 rpc_put_task(task);
7958 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007959}
Trond Myklebust36281ca2012-03-04 18:13:56 -05007960
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007961static int nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
7962{
7963 struct rpc_task *task;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007964 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007965
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007966 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007967 nfs4_free_lock_state(server, lsp);
7968 if (IS_ERR(task))
7969 return PTR_ERR(task);
7970 rpc_put_task(task);
7971 return 0;
7972}
7973
Trond Myklebust36281ca2012-03-04 18:13:56 -05007974static bool nfs41_match_stateid(const nfs4_stateid *s1,
7975 const nfs4_stateid *s2)
7976{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007977 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007978 return false;
7979
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007980 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007981 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007982 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007983 return true;
7984
7985 return false;
7986}
7987
Andy Adamson557134a2009-04-01 09:21:53 -04007988#endif /* CONFIG_NFS_V4_1 */
7989
Trond Myklebust36281ca2012-03-04 18:13:56 -05007990static bool nfs4_match_stateid(const nfs4_stateid *s1,
7991 const nfs4_stateid *s2)
7992{
Trond Myklebustf597c532012-03-04 18:13:56 -05007993 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05007994}
7995
7996
Trond Myklebust17280172012-03-11 13:11:00 -04007997static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007998 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007999 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008000 .recover_open = nfs4_open_reclaim,
8001 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04008002 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04008003 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008004};
8005
Andy Adamson591d71c2009-04-01 09:22:47 -04008006#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008007static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008008 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8009 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8010 .recover_open = nfs4_open_reclaim,
8011 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05008012 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008013 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04008014 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04008015};
8016#endif /* CONFIG_NFS_V4_1 */
8017
Trond Myklebust17280172012-03-11 13:11:00 -04008018static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008019 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008020 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008021 .recover_open = nfs4_open_expired,
8022 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04008023 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008024};
8025
Andy Adamson591d71c2009-04-01 09:22:47 -04008026#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008027static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008028 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8029 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04008030 .recover_open = nfs41_open_expired,
8031 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05008032 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04008033};
8034#endif /* CONFIG_NFS_V4_1 */
8035
Trond Myklebust17280172012-03-11 13:11:00 -04008036static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008037 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04008038 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008039 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04008040};
8041
8042#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008043static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008044 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04008045 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008046 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04008047};
8048#endif
8049
Chuck Leverec011fe2013-10-17 14:12:39 -04008050static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008051 .get_locations = _nfs40_proc_get_locations,
Chuck Leverec011fe2013-10-17 14:12:39 -04008052};
8053
8054#if defined(CONFIG_NFS_V4_1)
8055static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008056 .get_locations = _nfs41_proc_get_locations,
Chuck Leverec011fe2013-10-17 14:12:39 -04008057};
8058#endif /* CONFIG_NFS_V4_1 */
8059
Trond Myklebust97dc1352010-06-16 09:52:26 -04008060static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
8061 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008062 .init_caps = NFS_CAP_READDIRPLUS
8063 | NFS_CAP_ATOMIC_OPEN
8064 | NFS_CAP_CHANGE_ATTR
8065 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008066 .init_client = nfs40_init_client,
8067 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008068 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008069 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008070 .free_lock_state = nfs4_release_lockowner,
Chuck Lever9915ea72013-08-09 12:48:27 -04008071 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008072 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
8073 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
8074 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008075 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008076};
8077
8078#if defined(CONFIG_NFS_V4_1)
8079static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
8080 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008081 .init_caps = NFS_CAP_READDIRPLUS
8082 | NFS_CAP_ATOMIC_OPEN
8083 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04008084 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04008085 | NFS_CAP_STATEID_NFSV41
8086 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008087 .init_client = nfs41_init_client,
8088 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008089 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008090 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008091 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04008092 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008093 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8094 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8095 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008096 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008097};
8098#endif
8099
Steve Dickson42c2c422013-05-22 12:50:38 -04008100#if defined(CONFIG_NFS_V4_2)
8101static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
8102 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04008103 .init_caps = NFS_CAP_READDIRPLUS
8104 | NFS_CAP_ATOMIC_OPEN
8105 | NFS_CAP_CHANGE_ATTR
8106 | NFS_CAP_POSIX_LOCK
8107 | NFS_CAP_STATEID_NFSV41
8108 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008109 .init_client = nfs41_init_client,
8110 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04008111 .match_stateid = nfs41_match_stateid,
8112 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04008113 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04008114 .call_sync_ops = &nfs41_call_sync_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04008115 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8116 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8117 .state_renewal_ops = &nfs41_state_renewal_ops,
8118};
8119#endif
8120
Trond Myklebust97dc1352010-06-16 09:52:26 -04008121const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
8122 [0] = &nfs_v4_0_minor_ops,
8123#if defined(CONFIG_NFS_V4_1)
8124 [1] = &nfs_v4_1_minor_ops,
8125#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04008126#if defined(CONFIG_NFS_V4_2)
8127 [2] = &nfs_v4_2_minor_ops,
8128#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04008129};
8130
Trond Myklebust17f26b12013-08-21 15:48:42 -04008131static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04008132 .create = nfs_create,
8133 .lookup = nfs_lookup,
8134 .atomic_open = nfs_atomic_open,
8135 .link = nfs_link,
8136 .unlink = nfs_unlink,
8137 .symlink = nfs_symlink,
8138 .mkdir = nfs_mkdir,
8139 .rmdir = nfs_rmdir,
8140 .mknod = nfs_mknod,
8141 .rename = nfs_rename,
8142 .permission = nfs_permission,
8143 .getattr = nfs_getattr,
8144 .setattr = nfs_setattr,
8145 .getxattr = generic_getxattr,
8146 .setxattr = generic_setxattr,
8147 .listxattr = generic_listxattr,
8148 .removexattr = generic_removexattr,
8149};
8150
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08008151static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008152 .permission = nfs_permission,
8153 .getattr = nfs_getattr,
8154 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008155 .getxattr = generic_getxattr,
8156 .setxattr = generic_setxattr,
8157 .listxattr = generic_listxattr,
8158 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008159};
8160
David Howells509de812006-08-22 20:06:11 -04008161const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008162 .version = 4, /* protocol version */
8163 .dentry_ops = &nfs4_dentry_operations,
8164 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008165 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04008166 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008167 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04008168 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04008169 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008170 .getattr = nfs4_proc_getattr,
8171 .setattr = nfs4_proc_setattr,
8172 .lookup = nfs4_proc_lookup,
8173 .access = nfs4_proc_access,
8174 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008175 .create = nfs4_proc_create,
8176 .remove = nfs4_proc_remove,
8177 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04008178 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008179 .unlink_done = nfs4_proc_unlink_done,
8180 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04008181 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04008182 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04008183 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008184 .link = nfs4_proc_link,
8185 .symlink = nfs4_proc_symlink,
8186 .mkdir = nfs4_proc_mkdir,
8187 .rmdir = nfs4_proc_remove,
8188 .readdir = nfs4_proc_readdir,
8189 .mknod = nfs4_proc_mknod,
8190 .statfs = nfs4_proc_statfs,
8191 .fsinfo = nfs4_proc_fsinfo,
8192 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04008193 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008194 .decode_dirent = nfs4_decode_dirent,
8195 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04008196 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04008197 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05008198 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008199 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04008200 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04008201 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008202 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008203 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04008204 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008205 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008206 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00008207 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04008208 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04008209 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04008210 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04008211 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04008212 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00008213 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04008214 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04008215 .create_server = nfs4_create_server,
8216 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008217};
8218
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008219static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
8220 .prefix = XATTR_NAME_NFSV4_ACL,
8221 .list = nfs4_xattr_list_nfs4_acl,
8222 .get = nfs4_xattr_get_nfs4_acl,
8223 .set = nfs4_xattr_set_nfs4_acl,
8224};
8225
8226const struct xattr_handler *nfs4_xattr_handlers[] = {
8227 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04008228#ifdef CONFIG_NFS_V4_SECURITY_LABEL
8229 &nfs4_xattr_nfs4_label_handler,
8230#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008231 NULL
8232};
8233
Linus Torvalds1da177e2005-04-16 15:20:36 -07008234/*
8235 * Local variables:
8236 * c-basic-offset: 8
8237 * End:
8238 */