blob: c71c16e0ac2212e675a3f81f1bc7789922b91263 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Trond Myklebust6926afd2012-01-07 13:22:46 -050054#include <linux/nfs_idmap.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000055#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050056#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010057#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050066#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000067#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Trond Myklebustc6d01c62013-08-09 11:51:26 -040069#include "nfs4trace.h"
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define NFSDBG_FACILITY NFSDBG_PROC
72
Trond Myklebust2066fe82006-09-15 08:30:46 -040073#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define NFS4_POLL_RETRY_MAX (15*HZ)
75
Trond Myklebustcdd4e682006-01-03 09:55:12 +010076struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010077static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080078static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050080static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Chuck Lever81934dd2012-03-01 17:01:57 -050081static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040082static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
83static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040084static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
85 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -040086 struct nfs4_state *state, struct nfs4_label *ilabel,
87 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040088#ifdef CONFIG_NFS_V4_1
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040089static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
90 struct rpc_cred *);
91static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *,
92 struct rpc_cred *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040093#endif
David Quigleyaa9c2662013-05-22 12:50:44 -040094
95#ifdef CONFIG_NFS_V4_SECURITY_LABEL
96static inline struct nfs4_label *
97nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
98 struct iattr *sattr, struct nfs4_label *label)
99{
100 int err;
101
102 if (label == NULL)
103 return NULL;
104
105 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
106 return NULL;
107
108 if (NFS_SERVER(dir)->nfs_client->cl_minorversion < 2)
109 return NULL;
110
111 err = security_dentry_init_security(dentry, sattr->ia_mode,
112 &dentry->d_name, (void **)&label->label, &label->len);
113 if (err == 0)
114 return label;
115
116 return NULL;
117}
118static inline void
119nfs4_label_release_security(struct nfs4_label *label)
120{
121 if (label)
122 security_release_secctx(label->label, label->len);
123}
124static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
125{
126 if (label)
127 return server->attr_bitmask;
128
129 return server->attr_bitmask_nl;
130}
131#else
132static inline struct nfs4_label *
133nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
134 struct iattr *sattr, struct nfs4_label *l)
135{ return NULL; }
136static inline void
137nfs4_label_release_security(struct nfs4_label *label)
138{ return; }
139static inline u32 *
140nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
141{ return server->attr_bitmask; }
142#endif
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500145static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Trond Myklebust52567b02009-10-23 14:46:42 -0400147 if (err >= -1000)
148 return err;
149 switch (err) {
150 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500151 case -NFS4ERR_LAYOUTTRYLATER:
152 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400153 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000154 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400155 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000156 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800157 case -NFS4ERR_BADOWNER:
158 case -NFS4ERR_BADNAME:
159 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400160 case -NFS4ERR_SHARE_DENIED:
161 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400162 case -NFS4ERR_MINOR_VERS_MISMATCH:
163 return -EPROTONOSUPPORT;
Weston Andros Adamson6168f622012-09-10 14:00:46 -0400164 case -NFS4ERR_ACCESS:
165 return -EACCES;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400166 case -NFS4ERR_FILE_OPEN:
167 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400168 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700170 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400171 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400173 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
175
176/*
177 * This is our standard bitmap for GETATTR requests.
178 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400179const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 FATTR4_WORD0_TYPE
181 | FATTR4_WORD0_CHANGE
182 | FATTR4_WORD0_SIZE
183 | FATTR4_WORD0_FSID
184 | FATTR4_WORD0_FILEID,
185 FATTR4_WORD1_MODE
186 | FATTR4_WORD1_NUMLINKS
187 | FATTR4_WORD1_OWNER
188 | FATTR4_WORD1_OWNER_GROUP
189 | FATTR4_WORD1_RAWDEV
190 | FATTR4_WORD1_SPACE_USED
191 | FATTR4_WORD1_TIME_ACCESS
192 | FATTR4_WORD1_TIME_METADATA
David Quigleyaa9c2662013-05-22 12:50:44 -0400193 | FATTR4_WORD1_TIME_MODIFY,
194#ifdef CONFIG_NFS_V4_SECURITY_LABEL
195 FATTR4_WORD2_SECURITY_LABEL
196#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197};
198
Trond Myklebust1549210f2012-06-05 09:16:47 -0400199static const u32 nfs4_pnfs_open_bitmap[3] = {
200 FATTR4_WORD0_TYPE
201 | FATTR4_WORD0_CHANGE
202 | FATTR4_WORD0_SIZE
203 | FATTR4_WORD0_FSID
204 | FATTR4_WORD0_FILEID,
205 FATTR4_WORD1_MODE
206 | FATTR4_WORD1_NUMLINKS
207 | FATTR4_WORD1_OWNER
208 | FATTR4_WORD1_OWNER_GROUP
209 | FATTR4_WORD1_RAWDEV
210 | FATTR4_WORD1_SPACE_USED
211 | FATTR4_WORD1_TIME_ACCESS
212 | FATTR4_WORD1_TIME_METADATA
213 | FATTR4_WORD1_TIME_MODIFY,
214 FATTR4_WORD2_MDSTHRESHOLD
215};
216
Andy Adamsone23008e2012-10-02 21:07:32 -0400217static const u32 nfs4_open_noattr_bitmap[3] = {
218 FATTR4_WORD0_TYPE
219 | FATTR4_WORD0_CHANGE
220 | FATTR4_WORD0_FILEID,
221};
222
David Quigleya09df2c2013-05-22 12:50:41 -0400223const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 FATTR4_WORD0_FILES_AVAIL
225 | FATTR4_WORD0_FILES_FREE
226 | FATTR4_WORD0_FILES_TOTAL,
227 FATTR4_WORD1_SPACE_AVAIL
228 | FATTR4_WORD1_SPACE_FREE
229 | FATTR4_WORD1_SPACE_TOTAL
230};
231
David Quigleya09df2c2013-05-22 12:50:41 -0400232const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 FATTR4_WORD0_MAXLINK
234 | FATTR4_WORD0_MAXNAME,
235 0
236};
237
Fred Isamandae100c2011-07-30 20:52:37 -0400238const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 | FATTR4_WORD0_MAXREAD
240 | FATTR4_WORD0_MAXWRITE
241 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700242 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400243 | FATTR4_WORD1_FS_LAYOUT_TYPES,
244 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245};
246
David Quigleya09df2c2013-05-22 12:50:41 -0400247const u32 nfs4_fs_locations_bitmap[3] = {
Manoj Naik830b8e32006-06-09 09:34:25 -0400248 FATTR4_WORD0_TYPE
249 | FATTR4_WORD0_CHANGE
250 | FATTR4_WORD0_SIZE
251 | FATTR4_WORD0_FSID
252 | FATTR4_WORD0_FILEID
253 | FATTR4_WORD0_FS_LOCATIONS,
254 FATTR4_WORD1_MODE
255 | FATTR4_WORD1_NUMLINKS
256 | FATTR4_WORD1_OWNER
257 | FATTR4_WORD1_OWNER_GROUP
258 | FATTR4_WORD1_RAWDEV
259 | FATTR4_WORD1_SPACE_USED
260 | FATTR4_WORD1_TIME_ACCESS
261 | FATTR4_WORD1_TIME_METADATA
262 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400263 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400264};
265
Al Virobc4785c2006-10-19 23:28:51 -0700266static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 struct nfs4_readdir_arg *readdir)
268{
Al Viro0dbb4c62006-10-19 23:28:49 -0700269 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000272 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
274 return;
275 }
276
277 readdir->cookie = 0;
278 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
279 if (cookie == 2)
280 return;
281
282 /*
283 * NFSv4 servers do not return entries for '.' and '..'
284 * Therefore, we fake these entries here. We let '.'
285 * have cookie 0 and '..' have cookie 1. Note that
286 * when talking to the server, we always send cookie 0
287 * instead of 1 or 2.
288 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800289 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 if (cookie == 0) {
292 *p++ = xdr_one; /* next */
293 *p++ = xdr_zero; /* cookie, first word */
294 *p++ = xdr_one; /* cookie, second word */
295 *p++ = xdr_one; /* entry len */
296 memcpy(p, ".\0\0\0", 4); /* entry */
297 p++;
298 *p++ = xdr_one; /* bitmap length */
299 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
300 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400301 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
303
304 *p++ = xdr_one; /* next */
305 *p++ = xdr_zero; /* cookie, first word */
306 *p++ = xdr_two; /* cookie, second word */
307 *p++ = xdr_two; /* entry len */
308 memcpy(p, "..\0\0", 4); /* entry */
309 p++;
310 *p++ = xdr_one; /* bitmap length */
311 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
312 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400313 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 readdir->pgbase = (char *)p - (char *)start;
316 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800317 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
319
Trond Myklebust65de8722008-12-23 15:21:44 -0500320static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
321{
322 int res = 0;
323
324 might_sleep();
325
326 if (*timeout <= 0)
327 *timeout = NFS4_POLL_RETRY_MIN;
328 if (*timeout > NFS4_POLL_RETRY_MAX)
329 *timeout = NFS4_POLL_RETRY_MAX;
Colin Cross416ad3c2013-05-06 23:50:06 +0000330 freezable_schedule_timeout_killable_unsafe(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500331 if (fatal_signal_pending(current))
332 res = -ERESTARTSYS;
333 *timeout <<= 1;
334 return res;
335}
336
337/* This is the error handling routine for processes that are allowed
338 * to sleep.
339 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800340static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500341{
342 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500343 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500344 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500345 int ret = errorcode;
346
347 exception->retry = 0;
348 switch(errorcode) {
349 case 0:
350 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500351 case -NFS4ERR_OPENMODE:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400352 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400353 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500354 exception->retry = 1;
355 return 0;
356 }
357 if (state == NULL)
358 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400359 ret = nfs4_schedule_stateid_recovery(server, state);
360 if (ret < 0)
361 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500362 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500363 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500364 case -NFS4ERR_ADMIN_REVOKED:
365 case -NFS4ERR_BAD_STATEID:
Trond Myklebustb570a972013-04-12 15:04:51 -0400366 if (inode != NULL && nfs4_have_delegation(inode, FMODE_READ)) {
367 nfs_remove_bad_delegation(inode);
368 exception->retry = 1;
369 break;
370 }
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500371 if (state == NULL)
372 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400373 ret = nfs4_schedule_stateid_recovery(server, state);
374 if (ret < 0)
375 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500376 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400377 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -0400378 if (state != NULL) {
379 ret = nfs4_schedule_stateid_recovery(server, state);
380 if (ret < 0)
381 break;
382 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500383 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500384 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500385 nfs4_schedule_lease_recovery(clp);
386 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500387#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400388 case -NFS4ERR_BADSESSION:
389 case -NFS4ERR_BADSLOT:
390 case -NFS4ERR_BAD_HIGH_SLOT:
391 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
392 case -NFS4ERR_DEADSESSION:
393 case -NFS4ERR_SEQ_FALSE_RETRY:
394 case -NFS4ERR_SEQ_MISORDERED:
395 dprintk("%s ERROR: %d Reset session\n", __func__,
396 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400397 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400398 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500399#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500400 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500401 if (exception->timeout > HZ) {
402 /* We have retried a decent amount, time to
403 * fail
404 */
405 ret = -EBUSY;
406 break;
407 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500408 case -NFS4ERR_GRACE:
409 case -NFS4ERR_DELAY:
410 ret = nfs4_delay(server->client, &exception->timeout);
411 if (ret != 0)
412 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400413 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500414 case -NFS4ERR_OLD_STATEID:
415 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800416 break;
417 case -NFS4ERR_BADOWNER:
418 /* The following works around a Linux server bug! */
419 case -NFS4ERR_BADNAME:
420 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
421 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
422 exception->retry = 1;
423 printk(KERN_WARNING "NFS: v4 server %s "
424 "does not accept raw "
425 "uid/gids. "
426 "Reenabling the idmapper.\n",
427 server->nfs_client->cl_hostname);
428 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500429 }
430 /* We failed to handle the error */
431 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500432wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500433 ret = nfs4_wait_clnt_recover(clp);
434 if (ret == 0)
435 exception->retry = 1;
436 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500437}
438
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400439/*
440 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
441 * or 'false' otherwise.
442 */
443static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
444{
445 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
446
447 if (flavor == RPC_AUTH_GSS_KRB5I ||
448 flavor == RPC_AUTH_GSS_KRB5P)
449 return true;
450
451 return false;
452}
Trond Myklebust65de8722008-12-23 15:21:44 -0500453
Trond Myklebust452e9352010-07-31 14:29:06 -0400454static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 spin_lock(&clp->cl_lock);
457 if (time_before(clp->cl_last_renewal,timestamp))
458 clp->cl_last_renewal = timestamp;
459 spin_unlock(&clp->cl_lock);
460}
461
Trond Myklebust452e9352010-07-31 14:29:06 -0400462static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
463{
464 do_renew_lease(server->nfs_client, timestamp);
465}
466
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400467struct nfs4_call_sync_data {
468 const struct nfs_server *seq_server;
469 struct nfs4_sequence_args *seq_args;
470 struct nfs4_sequence_res *seq_res;
471};
472
Chuck Levera9c92d62013-08-09 12:48:18 -0400473static void nfs4_init_sequence(struct nfs4_sequence_args *args,
474 struct nfs4_sequence_res *res, int cache_reply)
475{
476 args->sa_slot = NULL;
477 args->sa_cache_this = cache_reply;
478 args->sa_privileged = 0;
479
480 res->sr_slot = NULL;
481}
482
483static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
484{
485 args->sa_privileged = 1;
486}
487
Chuck Lever3bd23842013-08-09 12:49:19 -0400488static int nfs40_setup_sequence(const struct nfs_server *server,
489 struct nfs4_sequence_args *args,
490 struct nfs4_sequence_res *res,
491 struct rpc_task *task)
492{
493 struct nfs4_slot_table *tbl = server->nfs_client->cl_slot_tbl;
494 struct nfs4_slot *slot;
495
496 /* slot already allocated? */
497 if (res->sr_slot != NULL)
498 goto out_start;
499
500 spin_lock(&tbl->slot_tbl_lock);
501 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
502 goto out_sleep;
503
504 slot = nfs4_alloc_slot(tbl);
505 if (IS_ERR(slot)) {
506 if (slot == ERR_PTR(-ENOMEM))
507 task->tk_timeout = HZ >> 2;
508 goto out_sleep;
509 }
510 spin_unlock(&tbl->slot_tbl_lock);
511
512 args->sa_slot = slot;
513 res->sr_slot = slot;
514
515out_start:
516 rpc_call_start(task);
517 return 0;
518
519out_sleep:
520 if (args->sa_privileged)
521 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
522 NULL, RPC_PRIORITY_PRIVILEGED);
523 else
524 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
525 spin_unlock(&tbl->slot_tbl_lock);
526 return -EAGAIN;
527}
528
529static int nfs40_sequence_done(struct rpc_task *task,
530 struct nfs4_sequence_res *res)
531{
532 struct nfs4_slot *slot = res->sr_slot;
533 struct nfs4_slot_table *tbl;
534
535 if (!RPC_WAS_SENT(task))
536 goto out;
537
538 tbl = slot->table;
539 spin_lock(&tbl->slot_tbl_lock);
540 if (!nfs41_wake_and_assign_slot(tbl, slot))
541 nfs4_free_slot(tbl, slot);
542 spin_unlock(&tbl->slot_tbl_lock);
543
544 res->sr_slot = NULL;
545out:
546 return 1;
547}
548
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400549#if defined(CONFIG_NFS_V4_1)
550
Trond Myklebustd185a332010-06-16 09:52:25 -0400551static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400552{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500553 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400554 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500555 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400556
Benny Halevydfb4f3092010-09-24 09:17:01 -0400557 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400558 /* just wake up the next guy waiting since
559 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500560 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500561 return;
Andy Adamson13615872009-04-01 09:22:17 -0400562 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500563 tbl = res->sr_slot->table;
564 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500565
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500566 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500567 /* Be nice to the server: try to ensure that the last transmitted
568 * value for highest_user_slotid <= target_highest_slotid
569 */
570 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
571 send_new_highest_used_slotid = true;
572
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500573 if (nfs41_wake_and_assign_slot(tbl, res->sr_slot)) {
574 send_new_highest_used_slotid = false;
575 goto out_unlock;
576 }
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500577 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500578
579 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
580 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500581out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500582 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400583 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500584 if (send_new_highest_used_slotid)
585 nfs41_server_notify_highest_slotid_update(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400586}
587
Trond Myklebust14516c32010-07-31 14:29:06 -0400588static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400589{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500590 struct nfs4_session *session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500591 struct nfs4_slot *slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400592 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500593 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500594 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400595
Bryan Schumaker468f8612011-04-18 15:57:32 -0400596 /* don't increment the sequence number if the task wasn't sent */
597 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400598 goto out;
599
Trond Myklebust933602e2012-11-16 12:12:38 -0500600 slot = res->sr_slot;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500601 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500602
Trond Myklebustac20d162012-12-15 15:36:07 -0500603 if (slot->interrupted) {
604 slot->interrupted = 0;
605 interrupted = true;
606 }
607
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400608 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500609 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400610 switch (res->sr_status) {
611 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400612 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500613 ++slot->seq_nr;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500614 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500615 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500616 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500617 if (res->sr_status_flags != 0)
618 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500619 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400620 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500621 case 1:
622 /*
623 * sr_status remains 1 if an RPC level error occurred.
624 * The server may or may not have processed the sequence
625 * operation..
626 * Mark the slot as having hosted an interrupted RPC call.
627 */
628 slot->interrupted = 1;
629 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400630 case -NFS4ERR_DELAY:
631 /* The server detected a resend of the RPC call and
632 * returned NFS4ERR_DELAY as per Section 2.10.6.2
633 * of RFC5661.
634 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500635 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400636 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500637 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500638 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400639 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500640 case -NFS4ERR_BADSLOT:
641 /*
642 * The slot id we used was probably retired. Try again
643 * using a different slot id.
644 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500645 goto retry_nowait;
646 case -NFS4ERR_SEQ_MISORDERED:
647 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500648 * Was the last operation on this sequence interrupted?
649 * If so, retry after bumping the sequence number.
650 */
651 if (interrupted) {
652 ++slot->seq_nr;
653 goto retry_nowait;
654 }
655 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500656 * Could this slot have been previously retired?
657 * If so, then the server may be expecting seq_nr = 1!
658 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500659 if (slot->seq_nr != 1) {
660 slot->seq_nr = 1;
661 goto retry_nowait;
662 }
663 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500664 case -NFS4ERR_SEQ_FALSE_RETRY:
665 ++slot->seq_nr;
666 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400667 default:
668 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500669 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400670 }
671out:
672 /* The session may be reset by one of the error handlers. */
673 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400674 nfs41_sequence_free_slot(res);
Trond Myklebust85563072012-12-11 10:31:12 -0500675 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500676retry_nowait:
677 if (rpc_restart_call_prepare(task)) {
678 task->tk_status = 0;
679 ret = 0;
680 }
681 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400682out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400683 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400684 goto out;
685 rpc_delay(task, NFS4_POLL_RETRY_MAX);
686 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400687}
688
Trond Myklebust14516c32010-07-31 14:29:06 -0400689static int nfs4_sequence_done(struct rpc_task *task,
690 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400691{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500692 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400693 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400694 if (!res->sr_slot->table->session)
695 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400696 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400697}
698
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000699int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400700 struct nfs4_sequence_args *args,
701 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400702 struct rpc_task *task)
703{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400704 struct nfs4_slot *slot;
705 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400706
707 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400708 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400709 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400710 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400711
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400712 tbl = &session->fc_slot_table;
713
Trond Myklebust69d206b2012-11-22 13:21:02 -0500714 task->tk_timeout = 0;
715
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400716 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400717 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400718 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500719 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500720 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400721 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400722 }
723
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500724 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500725 if (IS_ERR(slot)) {
726 /* If out of memory, try again in 1/4 second */
727 if (slot == ERR_PTR(-ENOMEM))
728 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400729 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400730 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400731 }
732 spin_unlock(&tbl->slot_tbl_lock);
733
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500734 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400735
Chuck Levere8d92382013-08-09 12:47:51 -0400736 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500737 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400738
Benny Halevydfb4f3092010-09-24 09:17:01 -0400739 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500740 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400741 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400742 /*
743 * sr_status is only set in decode_sequence, and so will remain
744 * set to 1 if an rpc level failure occurs.
745 */
746 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400747 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400748out_success:
749 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400750 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400751out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400752 /* Privileged tasks are queued with top priority */
753 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400754 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
755 NULL, RPC_PRIORITY_PRIVILEGED);
756 else
757 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400758 spin_unlock(&tbl->slot_tbl_lock);
759 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400760}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000761EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400762
Chuck Lever5a580e02013-08-09 12:48:09 -0400763static int nfs4_setup_sequence(const struct nfs_server *server,
764 struct nfs4_sequence_args *args,
765 struct nfs4_sequence_res *res,
766 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400767{
Trond Myklebust035168a2010-06-16 09:52:26 -0400768 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400769 int ret = 0;
770
Chuck Lever3bd23842013-08-09 12:49:19 -0400771 if (!session)
772 return nfs40_setup_sequence(server, args, res, task);
Trond Myklebust035168a2010-06-16 09:52:26 -0400773
Chuck Levere8d92382013-08-09 12:47:51 -0400774 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400775 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400776 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168a2010-06-16 09:52:26 -0400777
Trond Myklebust9d12b212012-01-17 22:04:25 -0500778 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400779
Andy Adamsonce5039c2009-04-01 09:22:13 -0400780 dprintk("<-- %s status=%d\n", __func__, ret);
781 return ret;
782}
783
Andy Adamsonce5039c2009-04-01 09:22:13 -0400784static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
785{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400786 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400787 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400788
Trond Myklebust035168a2010-06-16 09:52:26 -0400789 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
790
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400791 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400792}
793
Andy Adamson69ab40c2009-04-01 09:22:19 -0400794static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
795{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400796 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400797
Trond Myklebust14516c32010-07-31 14:29:06 -0400798 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400799}
800
Trond Myklebust17280172012-03-11 13:11:00 -0400801static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400802 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400803 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400804};
805
Chuck Lever3bd23842013-08-09 12:49:19 -0400806#else /* !CONFIG_NFS_V4_1 */
807
Chuck Lever5a580e02013-08-09 12:48:09 -0400808static int nfs4_setup_sequence(const struct nfs_server *server,
809 struct nfs4_sequence_args *args,
810 struct nfs4_sequence_res *res,
811 struct rpc_task *task)
812{
Chuck Lever3bd23842013-08-09 12:49:19 -0400813 return nfs40_setup_sequence(server, args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -0400814}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400815
Trond Myklebust14516c32010-07-31 14:29:06 -0400816static int nfs4_sequence_done(struct rpc_task *task,
817 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400818{
Chuck Lever3bd23842013-08-09 12:49:19 -0400819 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400820}
Chuck Lever3bd23842013-08-09 12:49:19 -0400821
822#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400823
Chuck Lever9915ea72013-08-09 12:48:27 -0400824static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
825{
826 struct nfs4_call_sync_data *data = calldata;
827 nfs4_setup_sequence(data->seq_server,
828 data->seq_args, data->seq_res, task);
829}
830
831static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
832{
833 struct nfs4_call_sync_data *data = calldata;
834 nfs4_sequence_done(task, data->seq_res);
835}
836
837static const struct rpc_call_ops nfs40_call_sync_ops = {
838 .rpc_call_prepare = nfs40_call_sync_prepare,
839 .rpc_call_done = nfs40_call_sync_done,
840};
841
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400842static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -0400843 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500844 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100845 struct nfs4_sequence_args *args,
846 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -0400847{
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100848 int ret;
849 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -0400850 struct nfs_client *clp = server->nfs_client;
851 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100852 .seq_server = server,
853 .seq_args = args,
854 .seq_res = res,
855 };
856 struct rpc_task_setup task_setup = {
857 .rpc_client = clnt,
858 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -0400859 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100860 .callback_data = &data
861 };
862
863 task = rpc_run_task(&task_setup);
864 if (IS_ERR(task))
865 ret = PTR_ERR(task);
866 else {
867 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -0400868 rpc_put_task(task);
869 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100870 return ret;
871}
872
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400873static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000874int nfs4_call_sync(struct rpc_clnt *clnt,
875 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000876 struct rpc_message *msg,
877 struct nfs4_sequence_args *args,
878 struct nfs4_sequence_res *res,
879 int cache_reply)
880{
Chuck Levera9c92d62013-08-09 12:48:18 -0400881 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -0400882 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000883}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
886{
887 struct nfs_inode *nfsi = NFS_I(dir);
888
889 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400890 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700891 if (!cinfo->atomic || cinfo->before != dir->i_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700893 dir->i_version = cinfo->after;
David Howellsde242c02012-12-20 21:52:38 +0000894 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 spin_unlock(&dir->i_lock);
896}
897
898struct nfs4_opendata {
899 struct kref kref;
900 struct nfs_openargs o_arg;
901 struct nfs_openres o_res;
902 struct nfs_open_confirmargs c_arg;
903 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500904 struct nfs4_string owner_name;
905 struct nfs4_string group_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400907 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400909 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 struct nfs4_state_owner *owner;
911 struct nfs4_state *state;
912 struct iattr attrs;
913 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400914 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -0400915 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -0400916 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400917 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 int cancelled;
919};
Trond Myklebustdecf4912005-10-27 22:12:39 -0400920
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400921static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
922 int err, struct nfs4_exception *exception)
923{
924 if (err != -EINVAL)
925 return false;
926 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
927 return false;
928 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
929 exception->retry = 1;
930 return true;
931}
932
933static enum open_claim_type4
934nfs4_map_atomic_open_claim(struct nfs_server *server,
935 enum open_claim_type4 claim)
936{
937 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
938 return claim;
939 switch (claim) {
940 default:
941 return claim;
942 case NFS4_OPEN_CLAIM_FH:
943 return NFS4_OPEN_CLAIM_NULL;
944 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
945 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
946 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
947 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
948 }
949}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100952{
953 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400954 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100955 p->o_res.seqid = p->o_arg.seqid;
956 p->c_res.seqid = p->c_arg.seqid;
957 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400958 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100959 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500960 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100961}
962
Al Viro82a2c1b2011-06-22 18:30:55 -0400963static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100964 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400965 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -0400966 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400967 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400968 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100969{
Al Viro82a2c1b2011-06-22 18:30:55 -0400970 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100971 struct inode *dir = parent->d_inode;
972 struct nfs_server *server = NFS_SERVER(dir);
973 struct nfs4_opendata *p;
974
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400975 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100976 if (p == NULL)
977 goto err;
David Quigley14c43f72013-05-22 12:50:43 -0400978
979 p->f_label = nfs4_label_alloc(server, gfp_mask);
980 if (IS_ERR(p->f_label))
981 goto err_free_p;
982
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400983 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100984 if (p->o_arg.seqid == NULL)
David Quigley14c43f72013-05-22 12:50:43 -0400985 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -0400986 nfs_sb_active(dentry->d_sb);
987 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100988 p->dir = parent;
989 p->owner = sp;
990 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500991 p->o_arg.open_flags = flags;
992 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700993 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
994 * will return permission denied for all bits until close */
995 if (!(flags & O_EXCL)) {
996 /* ask server to check for all possible rights as results
997 * are cached */
998 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
999 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001000 }
David Howells7539bba2006-08-22 20:06:09 -04001001 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001002 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1003 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001004 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001005 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001006 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001007 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
David Quigley1775fd32013-05-22 12:50:42 -04001008 p->o_arg.label = label;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001009 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1010 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001011 case NFS4_OPEN_CLAIM_NULL:
1012 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1013 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1014 p->o_arg.fh = NFS_FH(dir);
1015 break;
1016 case NFS4_OPEN_CLAIM_PREVIOUS:
1017 case NFS4_OPEN_CLAIM_FH:
1018 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1019 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1020 p->o_arg.fh = NFS_FH(dentry->d_inode);
1021 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001022 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001023 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001024
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001025 p->o_arg.u.attrs = &p->attrs;
1026 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001027
1028 verf[0] = jiffies;
1029 verf[1] = current->pid;
1030 memcpy(p->o_arg.u.verifier.data, verf,
1031 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001032 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001033 p->c_arg.fh = &p->o_res.fh;
1034 p->c_arg.stateid = &p->o_res.stateid;
1035 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001036 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001037 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001038 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001039
1040err_free_label:
1041 nfs4_label_free(p->f_label);
1042err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001043 kfree(p);
1044err:
1045 dput(parent);
1046 return NULL;
1047}
1048
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001049static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001050{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001051 struct nfs4_opendata *p = container_of(kref,
1052 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001053 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001054
1055 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001056 if (p->state != NULL)
1057 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001058 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001059
1060 nfs4_label_free(p->f_label);
1061
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001062 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001063 dput(p->dentry);
1064 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001065 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001066 kfree(p);
1067}
1068
1069static void nfs4_opendata_put(struct nfs4_opendata *p)
1070{
1071 if (p != NULL)
1072 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001073}
1074
Trond Myklebust06f814a2006-01-03 09:55:07 +01001075static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1076{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001077 int ret;
1078
Trond Myklebust06f814a2006-01-03 09:55:07 +01001079 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001080 return ret;
1081}
1082
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001083static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001084{
1085 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001086
Trond Myklebust536e43d2012-01-17 22:04:26 -05001087 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001088 goto out;
1089 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001090 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001091 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1092 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001093 break;
1094 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001095 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1096 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001097 break;
1098 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001099 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1100 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001101 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001102out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001103 return ret;
1104}
1105
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001106static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001107{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001108 if (delegation == NULL)
1109 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001110 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001111 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -05001112 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -04001113 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001114 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1115 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -05001116 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001117 return 1;
1118}
1119
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001120static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001121{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001122 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001123 case FMODE_WRITE:
1124 state->n_wronly++;
1125 break;
1126 case FMODE_READ:
1127 state->n_rdonly++;
1128 break;
1129 case FMODE_READ|FMODE_WRITE:
1130 state->n_rdwr++;
1131 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001132 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001133}
1134
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001135static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001136{
1137 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001138 nfs4_stateid_copy(&state->stateid, stateid);
1139 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust92b40e92013-04-20 01:25:45 -04001140 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001141 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001142 case FMODE_READ:
1143 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1144 break;
1145 case FMODE_WRITE:
1146 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1147 break;
1148 case FMODE_READ|FMODE_WRITE:
1149 set_bit(NFS_O_RDWR_STATE, &state->flags);
1150 }
1151}
1152
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001153static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001154{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001155 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001156 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001157 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001158}
1159
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001160static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001162 /*
1163 * Protect the call to nfs4_state_set_mode_locked and
1164 * serialise the stateid update
1165 */
1166 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001167 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001168 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001169 set_bit(NFS_DELEGATED_STATE, &state->flags);
1170 }
1171 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001172 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001173 write_sequnlock(&state->seqlock);
1174 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001175 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001176 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177}
1178
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001179static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001180{
1181 struct nfs_inode *nfsi = NFS_I(state->inode);
1182 struct nfs_delegation *deleg_cur;
1183 int ret = 0;
1184
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001185 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001186
1187 rcu_read_lock();
1188 deleg_cur = rcu_dereference(nfsi->delegation);
1189 if (deleg_cur == NULL)
1190 goto no_delegation;
1191
1192 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001193 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001194 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001195 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001196 goto no_delegation_unlock;
1197
1198 if (delegation == NULL)
1199 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001200 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001201 goto no_delegation_unlock;
1202
Trond Myklebustb7391f42008-12-23 15:21:52 -05001203 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001204 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001205 ret = 1;
1206no_delegation_unlock:
1207 spin_unlock(&deleg_cur->lock);
1208no_delegation:
1209 rcu_read_unlock();
1210
1211 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001212 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001213 ret = 1;
1214 }
1215
1216 return ret;
1217}
1218
1219
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001220static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001221{
1222 struct nfs_delegation *delegation;
1223
1224 rcu_read_lock();
1225 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001226 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001227 rcu_read_unlock();
1228 return;
1229 }
1230 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001231 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001232}
1233
Trond Myklebust6ee41262007-07-08 14:11:36 -04001234static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001235{
1236 struct nfs4_state *state = opendata->state;
1237 struct nfs_inode *nfsi = NFS_I(state->inode);
1238 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001239 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001240 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001241 nfs4_stateid stateid;
1242 int ret = -EAGAIN;
1243
Trond Myklebustaac00a82007-07-05 19:02:21 -04001244 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001245 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001246 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001247 if (can_open_cached(state, fmode, open_mode)) {
1248 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001249 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001250 goto out_return_state;
1251 }
1252 spin_unlock(&state->owner->so_lock);
1253 }
Trond Myklebust34310432008-12-23 15:21:38 -05001254 rcu_read_lock();
1255 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001256 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001257 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001258 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001259 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001260 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001261 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001262 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001263 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001264 if (!opendata->is_recover) {
1265 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1266 if (ret != 0)
1267 goto out;
1268 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001269 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001270
1271 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001272 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001273 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001274 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001275out:
1276 return ERR_PTR(ret);
1277out_return_state:
1278 atomic_inc(&state->count);
1279 return state;
1280}
1281
Andy Adamsone23008e2012-10-02 21:07:32 -04001282static void
1283nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1284{
1285 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1286 struct nfs_delegation *delegation;
1287 int delegation_flags = 0;
1288
1289 rcu_read_lock();
1290 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1291 if (delegation)
1292 delegation_flags = delegation->flags;
1293 rcu_read_unlock();
1294 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1295 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1296 "returning a delegation for "
1297 "OPEN(CLAIM_DELEGATE_CUR)\n",
1298 clp->cl_hostname);
1299 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1300 nfs_inode_set_delegation(state->inode,
1301 data->owner->so_cred,
1302 &data->o_res);
1303 else
1304 nfs_inode_reclaim_delegation(state->inode,
1305 data->owner->so_cred,
1306 &data->o_res);
1307}
1308
1309/*
1310 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1311 * and update the nfs4_state.
1312 */
1313static struct nfs4_state *
1314_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1315{
1316 struct inode *inode = data->state->inode;
1317 struct nfs4_state *state = data->state;
1318 int ret;
1319
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001320 if (!data->rpc_done) {
1321 if (data->rpc_status) {
1322 ret = data->rpc_status;
1323 goto err;
1324 }
1325 /* cached opens have already been processed */
1326 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001327 }
1328
Andy Adamsone23008e2012-10-02 21:07:32 -04001329 ret = nfs_refresh_inode(inode, &data->f_attr);
1330 if (ret)
1331 goto err;
1332
1333 if (data->o_res.delegation_type != 0)
1334 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001335update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001336 update_open_stateid(state, &data->o_res.stateid, NULL,
1337 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001338 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001339
1340 return state;
1341err:
1342 return ERR_PTR(ret);
1343
1344}
1345
1346static struct nfs4_state *
1347_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001348{
1349 struct inode *inode;
1350 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001351 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001352
Trond Myklebustaac00a82007-07-05 19:02:21 -04001353 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001354 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001355 goto out;
1356 }
1357
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001358 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001359 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001360 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001361 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001362 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001363 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001364 goto err;
1365 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001366 state = nfs4_get_open_state(inode, data->owner);
1367 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001368 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001369 if (data->o_res.delegation_type != 0)
1370 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001371 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001372 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001373 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001374out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001375 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001376 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001377err_put_inode:
1378 iput(inode);
1379err:
1380 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001381}
1382
Andy Adamsone23008e2012-10-02 21:07:32 -04001383static struct nfs4_state *
1384nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1385{
1386 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1387 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1388 return _nfs4_opendata_to_nfs4_state(data);
1389}
1390
Trond Myklebust864472e2006-01-03 09:55:15 +01001391static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1392{
1393 struct nfs_inode *nfsi = NFS_I(state->inode);
1394 struct nfs_open_context *ctx;
1395
1396 spin_lock(&state->inode->i_lock);
1397 list_for_each_entry(ctx, &nfsi->open_files, list) {
1398 if (ctx->state != state)
1399 continue;
1400 get_nfs_open_context(ctx);
1401 spin_unlock(&state->inode->i_lock);
1402 return ctx;
1403 }
1404 spin_unlock(&state->inode->i_lock);
1405 return ERR_PTR(-ENOENT);
1406}
1407
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001408static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1409 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001410{
1411 struct nfs4_opendata *opendata;
1412
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001413 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001414 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001415 if (opendata == NULL)
1416 return ERR_PTR(-ENOMEM);
1417 opendata->state = state;
1418 atomic_inc(&state->count);
1419 return opendata;
1420}
1421
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001422static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001423{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001424 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001425 int ret;
1426
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001427 opendata->o_arg.open_flags = 0;
1428 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001429 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1430 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1431 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001432 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001433 if (ret != 0)
1434 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001435 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001436 if (IS_ERR(newstate))
1437 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001438 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001439 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001440 return 0;
1441}
1442
1443static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1444{
Trond Myklebust864472e2006-01-03 09:55:15 +01001445 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001446 int ret;
1447
1448 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001449 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001450 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001451 smp_rmb();
1452 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001453 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001454 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001455 if (ret != 0)
1456 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001457 if (newstate != state)
1458 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001459 }
1460 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001461 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001462 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001463 if (ret != 0)
1464 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001465 if (newstate != state)
1466 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001467 }
1468 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001469 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001470 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001471 if (ret != 0)
1472 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001473 if (newstate != state)
1474 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001475 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001476 /*
1477 * We may have performed cached opens for all three recoveries.
1478 * Check if we need to update the current stateid.
1479 */
1480 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001481 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001482 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001483 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001484 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001485 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001486 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001487 return 0;
1488}
1489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490/*
1491 * OPEN_RECLAIM:
1492 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001494static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001496 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001497 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001498 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 int status;
1500
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001501 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1502 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001503 if (IS_ERR(opendata))
1504 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001505 rcu_read_lock();
1506 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001507 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001508 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001509 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001510 opendata->o_arg.u.delegation_type = delegation_type;
1511 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001512 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 return status;
1514}
1515
Trond Myklebust539cd032007-06-05 11:46:42 -04001516static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517{
1518 struct nfs_server *server = NFS_SERVER(state->inode);
1519 struct nfs4_exception exception = { };
1520 int err;
1521 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001522 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001523 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001524 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1525 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001526 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001527 break;
1528 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 } while (exception.retry);
1530 return err;
1531}
1532
Trond Myklebust864472e2006-01-03 09:55:15 +01001533static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1534{
1535 struct nfs_open_context *ctx;
1536 int ret;
1537
1538 ctx = nfs4_state_find_open_context(state);
1539 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001540 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001541 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001542 put_nfs_open_context(ctx);
1543 return ret;
1544}
1545
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001546static 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 -07001547{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001548 switch (err) {
1549 default:
1550 printk(KERN_ERR "NFS: %s: unhandled error "
1551 "%d.\n", __func__, err);
1552 case 0:
1553 case -ENOENT:
1554 case -ESTALE:
1555 break;
1556 case -NFS4ERR_BADSESSION:
1557 case -NFS4ERR_BADSLOT:
1558 case -NFS4ERR_BAD_HIGH_SLOT:
1559 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1560 case -NFS4ERR_DEADSESSION:
1561 set_bit(NFS_DELEGATED_STATE, &state->flags);
1562 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1563 return -EAGAIN;
1564 case -NFS4ERR_STALE_CLIENTID:
1565 case -NFS4ERR_STALE_STATEID:
1566 set_bit(NFS_DELEGATED_STATE, &state->flags);
1567 case -NFS4ERR_EXPIRED:
1568 /* Don't recall a delegation if it was lost */
1569 nfs4_schedule_lease_recovery(server->nfs_client);
1570 return -EAGAIN;
1571 case -NFS4ERR_DELEG_REVOKED:
1572 case -NFS4ERR_ADMIN_REVOKED:
1573 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001574 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001575 nfs_inode_find_state_and_recover(state->inode,
1576 stateid);
1577 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001578 return 0;
1579 case -NFS4ERR_DELAY:
1580 case -NFS4ERR_GRACE:
1581 set_bit(NFS_DELEGATED_STATE, &state->flags);
1582 ssleep(1);
1583 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001584 case -ENOMEM:
1585 case -NFS4ERR_DENIED:
1586 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1587 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 return err;
1590}
1591
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001592int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1593{
1594 struct nfs_server *server = NFS_SERVER(state->inode);
1595 struct nfs4_opendata *opendata;
1596 int err;
1597
1598 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1599 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1600 if (IS_ERR(opendata))
1601 return PTR_ERR(opendata);
1602 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1603 err = nfs4_open_recover(opendata, state);
1604 nfs4_opendata_put(opendata);
1605 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1606}
1607
Chuck Leverbe05c862013-08-09 12:49:47 -04001608static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1609{
1610 struct nfs4_opendata *data = calldata;
1611
1612 nfs40_setup_sequence(data->o_arg.server, &data->o_arg.seq_args,
1613 &data->o_res.seq_res, task);
1614}
1615
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001616static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1617{
1618 struct nfs4_opendata *data = calldata;
1619
Chuck Leverbe05c862013-08-09 12:49:47 -04001620 nfs40_sequence_done(task, &data->o_res.seq_res);
1621
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001622 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001623 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001624 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001625 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001626 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001627 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001628 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001629}
1630
1631static void nfs4_open_confirm_release(void *calldata)
1632{
1633 struct nfs4_opendata *data = calldata;
1634 struct nfs4_state *state = NULL;
1635
1636 /* If this request hasn't been cancelled, do nothing */
1637 if (data->cancelled == 0)
1638 goto out_free;
1639 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001640 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001641 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001642 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001643 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001644 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001645out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001646 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001647}
1648
1649static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04001650 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001651 .rpc_call_done = nfs4_open_confirm_done,
1652 .rpc_release = nfs4_open_confirm_release,
1653};
1654
1655/*
1656 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1657 */
1658static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1659{
1660 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1661 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001662 struct rpc_message msg = {
1663 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1664 .rpc_argp = &data->c_arg,
1665 .rpc_resp = &data->c_res,
1666 .rpc_cred = data->owner->so_cred,
1667 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001668 struct rpc_task_setup task_setup_data = {
1669 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001670 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001671 .callback_ops = &nfs4_open_confirm_ops,
1672 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001673 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001674 .flags = RPC_TASK_ASYNC,
1675 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 int status;
1677
Chuck Leverbe05c862013-08-09 12:49:47 -04001678 nfs4_init_sequence(&data->o_arg.seq_args, &data->o_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001679 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001680 data->rpc_done = 0;
1681 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001682 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001683 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001684 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001685 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001686 status = nfs4_wait_for_completion_rpc_task(task);
1687 if (status != 0) {
1688 data->cancelled = 1;
1689 smp_wmb();
1690 } else
1691 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001692 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 return status;
1694}
1695
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001696static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001698 struct nfs4_opendata *data = calldata;
1699 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001700 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001701
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001702 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001703 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001704 /*
1705 * Check if we still need to send an OPEN call, or if we can use
1706 * a delegation instead.
1707 */
1708 if (data->state != NULL) {
1709 struct nfs_delegation *delegation;
1710
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001711 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001712 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001713 rcu_read_lock();
1714 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001715 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
Trond Myklebustcd4c9be2013-04-23 14:46:25 -04001716 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
Trond Myklebust652f89f2011-12-09 19:05:58 -05001717 can_open_delegated(delegation, data->o_arg.fmode))
1718 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001719 rcu_read_unlock();
1720 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001721 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04001722 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust8188df12013-04-23 14:31:19 -04001723 switch (data->o_arg.claim) {
1724 case NFS4_OPEN_CLAIM_PREVIOUS:
1725 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1726 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001727 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04001728 case NFS4_OPEN_CLAIM_FH:
1729 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001730 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1731 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001732 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001733 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001734 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001735 &data->o_res.seq_res,
1736 task) != 0)
1737 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04001738
1739 /* Set the create mode (note dependency on the session type) */
1740 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1741 if (data->o_arg.open_flags & O_EXCL) {
1742 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1743 if (nfs4_has_persistent_session(clp))
1744 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1745 else if (clp->cl_mvops->minor_version > 0)
1746 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1747 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04001748 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001749unlock_no_action:
1750 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001751out_no_action:
1752 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001753out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001754 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001755}
1756
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001757static void nfs4_open_done(struct rpc_task *task, void *calldata)
1758{
1759 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001761 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001762
Trond Myklebust14516c32010-07-31 14:29:06 -04001763 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1764 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001765
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001766 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001767 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1768 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001769 case S_IFREG:
1770 break;
1771 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001772 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001773 break;
1774 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001775 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001776 break;
1777 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001778 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001779 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001780 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001781 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001782 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1783 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001784 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001785 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001786}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001787
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001788static void nfs4_open_release(void *calldata)
1789{
1790 struct nfs4_opendata *data = calldata;
1791 struct nfs4_state *state = NULL;
1792
1793 /* If this request hasn't been cancelled, do nothing */
1794 if (data->cancelled == 0)
1795 goto out_free;
1796 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001797 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001798 goto out_free;
1799 /* In case we need an open_confirm, no cleanup! */
1800 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1801 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001802 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001803 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001804 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001805out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001806 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001807}
1808
1809static const struct rpc_call_ops nfs4_open_ops = {
1810 .rpc_call_prepare = nfs4_open_prepare,
1811 .rpc_call_done = nfs4_open_done,
1812 .rpc_release = nfs4_open_release,
1813};
1814
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001815static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001816{
1817 struct inode *dir = data->dir->d_inode;
1818 struct nfs_server *server = NFS_SERVER(dir);
1819 struct nfs_openargs *o_arg = &data->o_arg;
1820 struct nfs_openres *o_res = &data->o_res;
1821 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001822 struct rpc_message msg = {
1823 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1824 .rpc_argp = o_arg,
1825 .rpc_resp = o_res,
1826 .rpc_cred = data->owner->so_cred,
1827 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001828 struct rpc_task_setup task_setup_data = {
1829 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001830 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001831 .callback_ops = &nfs4_open_ops,
1832 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001833 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001834 .flags = RPC_TASK_ASYNC,
1835 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001836 int status;
1837
Chuck Levera9c92d62013-08-09 12:48:18 -04001838 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001839 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001840 data->rpc_done = 0;
1841 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001842 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001843 data->is_recover = 0;
1844 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001845 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001846 data->is_recover = 1;
1847 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04001848 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001849 if (IS_ERR(task))
1850 return PTR_ERR(task);
1851 status = nfs4_wait_for_completion_rpc_task(task);
1852 if (status != 0) {
1853 data->cancelled = 1;
1854 smp_wmb();
1855 } else
1856 status = data->rpc_status;
1857 rpc_put_task(task);
1858
1859 return status;
1860}
1861
1862static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1863{
1864 struct inode *dir = data->dir->d_inode;
1865 struct nfs_openres *o_res = &data->o_res;
1866 int status;
1867
1868 status = nfs4_run_open_task(data, 1);
1869 if (status != 0 || !data->rpc_done)
1870 return status;
1871
Trond Myklebust6926afd2012-01-07 13:22:46 -05001872 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1873
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001874 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1875 status = _nfs4_proc_open_confirm(data);
1876 if (status != 0)
1877 return status;
1878 }
1879
1880 return status;
1881}
1882
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001883static int nfs4_opendata_access(struct rpc_cred *cred,
1884 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001885 struct nfs4_state *state, fmode_t fmode,
1886 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001887{
1888 struct nfs_access_entry cache;
1889 u32 mask;
1890
1891 /* access call failed or for some reason the server doesn't
1892 * support any access modes -- defer access call until later */
1893 if (opendata->o_res.access_supported == 0)
1894 return 0;
1895
1896 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001897 /* don't check MAY_WRITE - a newly created file may not have
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001898 * write mode bits, but POSIX allows the creating process to write.
1899 * use openflags to check for exec, because fmode won't
1900 * always have FMODE_EXEC set when file open for exec. */
1901 if (openflags & __FMODE_EXEC) {
1902 /* ONLY check for exec rights */
1903 mask = MAY_EXEC;
1904 } else if (fmode & FMODE_READ)
1905 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001906
1907 cache.cred = cred;
1908 cache.jiffies = jiffies;
1909 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1910 nfs_access_add_cache(state->inode, &cache);
1911
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001912 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001913 return 0;
1914
1915 /* even though OPEN succeeded, access is denied. Close the file */
1916 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001917 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001918}
1919
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001920/*
1921 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1922 */
1923static int _nfs4_proc_open(struct nfs4_opendata *data)
1924{
1925 struct inode *dir = data->dir->d_inode;
1926 struct nfs_server *server = NFS_SERVER(dir);
1927 struct nfs_openargs *o_arg = &data->o_arg;
1928 struct nfs_openres *o_res = &data->o_res;
1929 int status;
1930
1931 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001932 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001933 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001934 if (status != 0) {
1935 if (status == -NFS4ERR_BADNAME &&
1936 !(o_arg->open_flags & O_CREAT))
1937 return -ENOENT;
1938 return status;
1939 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001940
Trond Myklebust6926afd2012-01-07 13:22:46 -05001941 nfs_fattr_map_and_free_names(server, &data->f_attr);
1942
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001943 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001944 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001945 if (o_arg->open_flags & O_EXCL)
1946 data->file_created = 1;
1947 else if (o_res->cinfo.before != o_res->cinfo.after)
1948 data->file_created = 1;
1949 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001950 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1951 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001953 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001955 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 }
1957 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
David Quigley1775fd32013-05-22 12:50:42 -04001958 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001959 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960}
1961
Andy Adamsond83217c2011-03-01 01:34:17 +00001962static int nfs4_recover_expired_lease(struct nfs_server *server)
1963{
1964 return nfs4_client_recover_expired_lease(server->nfs_client);
1965}
1966
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967/*
1968 * OPEN_EXPIRED:
1969 * reclaim state on the server after a network partition.
1970 * Assumes caller holds the appropriate lock
1971 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001972static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001974 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001975 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001977 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001978 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001979 if (IS_ERR(opendata))
1980 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001981 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001982 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001983 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001984 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001985 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986}
1987
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001988static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001989{
Trond Myklebust539cd032007-06-05 11:46:42 -04001990 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001991 struct nfs4_exception exception = { };
1992 int err;
1993
1994 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001995 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001996 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001997 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1998 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001999 switch (err) {
2000 default:
2001 goto out;
2002 case -NFS4ERR_GRACE:
2003 case -NFS4ERR_DELAY:
2004 nfs4_handle_exception(server, err, &exception);
2005 err = 0;
2006 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002007 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002008out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002009 return err;
2010}
2011
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2013{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002015 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
Trond Myklebust864472e2006-01-03 09:55:15 +01002017 ctx = nfs4_state_find_open_context(state);
2018 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002019 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002020 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002021 put_nfs_open_context(ctx);
2022 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023}
2024
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002025#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002026static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002027{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002028 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002029 nfs4_stateid *stateid = &state->stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002030 struct nfs_delegation *delegation;
2031 struct rpc_cred *cred = NULL;
2032 int status = -NFS4ERR_BAD_STATEID;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002033
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002034 /* If a state reset has been done, test_stateid is unneeded */
2035 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2036 return;
2037
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002038 /* Get the delegation credential for use by test/free_stateid */
2039 rcu_read_lock();
2040 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2041 if (delegation != NULL &&
2042 nfs4_stateid_match(&delegation->stateid, stateid)) {
2043 cred = get_rpccred(delegation->cred);
2044 rcu_read_unlock();
2045 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002046 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002047 } else
2048 rcu_read_unlock();
2049
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002050 if (status != NFS_OK) {
2051 /* Free the stateid unless the server explicitly
2052 * informs us the stateid is unrecognized. */
2053 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002054 nfs41_free_stateid(server, stateid, cred);
Bryan Schumaker69388672012-09-26 15:25:52 -04002055 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002056
Bryan Schumaker69388672012-09-26 15:25:52 -04002057 write_seqlock(&state->seqlock);
2058 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2059 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002060 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2061 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002062
2063 if (cred != NULL)
2064 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002065}
2066
2067/**
2068 * nfs41_check_open_stateid - possibly free an open stateid
2069 *
2070 * @state: NFSv4 state for an inode
2071 *
2072 * Returns NFS_OK if recovery for this stateid is now finished.
2073 * Otherwise a negative NFS4ERR value is returned.
2074 */
2075static int nfs41_check_open_stateid(struct nfs4_state *state)
2076{
2077 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002078 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002079 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002080 int status;
2081
2082 /* If a state reset has been done, test_stateid is unneeded */
2083 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2084 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2085 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2086 return -NFS4ERR_BAD_STATEID;
2087
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002088 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002089 trace_nfs4_test_open_stateid(state, NULL, status);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002090 if (status != NFS_OK) {
2091 /* Free the stateid unless the server explicitly
2092 * informs us the stateid is unrecognized. */
2093 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002094 nfs41_free_stateid(server, stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002095
2096 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2097 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2098 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002099 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002100 }
2101 return status;
2102}
2103
2104static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2105{
Chuck Levereb64cf92012-07-11 16:30:05 -04002106 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002107
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002108 nfs41_clear_delegation_stateid(state);
2109 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002110 if (status != NFS_OK)
2111 status = nfs4_open_expired(sp, state);
2112 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002113}
2114#endif
2115
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002117 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2118 * fields corresponding to attributes that were used to store the verifier.
2119 * Make sure we clobber those fields in the later setattr call
2120 */
2121static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2122{
2123 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2124 !(sattr->ia_valid & ATTR_ATIME_SET))
2125 sattr->ia_valid |= ATTR_ATIME;
2126
2127 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2128 !(sattr->ia_valid & ATTR_MTIME_SET))
2129 sattr->ia_valid |= ATTR_MTIME;
2130}
2131
Trond Myklebustc21443c2013-02-07 14:26:21 -05002132static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2133 fmode_t fmode,
2134 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002135 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002136{
2137 struct nfs4_state_owner *sp = opendata->owner;
2138 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002139 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002140 struct nfs4_state *state;
2141 unsigned int seq;
2142 int ret;
2143
2144 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2145
2146 ret = _nfs4_proc_open(opendata);
2147 if (ret != 0)
2148 goto out;
2149
2150 state = nfs4_opendata_to_nfs4_state(opendata);
2151 ret = PTR_ERR(state);
2152 if (IS_ERR(state))
2153 goto out;
2154 if (server->caps & NFS_CAP_POSIX_LOCK)
2155 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2156
Trond Myklebust275bb302013-05-29 13:11:28 -04002157 dentry = opendata->dentry;
2158 if (dentry->d_inode == NULL) {
2159 /* FIXME: Is this d_drop() ever needed? */
2160 d_drop(dentry);
2161 dentry = d_add_unique(dentry, igrab(state->inode));
2162 if (dentry == NULL) {
2163 dentry = opendata->dentry;
2164 } else if (dentry != ctx->dentry) {
2165 dput(ctx->dentry);
2166 ctx->dentry = dget(dentry);
2167 }
2168 nfs_set_verifier(dentry,
2169 nfs_save_change_attribute(opendata->dir->d_inode));
2170 }
2171
Trond Myklebustc21443c2013-02-07 14:26:21 -05002172 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2173 if (ret != 0)
2174 goto out;
2175
Trond Myklebust3efb9722013-05-29 13:17:04 -04002176 ctx->state = state;
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002177 if (dentry->d_inode == state->inode) {
2178 nfs_inode_attach_open_context(ctx);
2179 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2180 nfs4_schedule_stateid_recovery(server, state);
2181 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002182out:
2183 return ret;
2184}
2185
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002186/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002187 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 */
Andy Adamson82be4172012-05-23 05:02:35 -04002189static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002190 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002191 int flags,
2192 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002193 struct nfs4_label *label,
2194 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195{
2196 struct nfs4_state_owner *sp;
2197 struct nfs4_state *state = NULL;
2198 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002199 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002200 struct dentry *dentry = ctx->dentry;
2201 struct rpc_cred *cred = ctx->cred;
2202 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2203 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002204 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002205 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002206 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
2208 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002210 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2211 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2213 goto out_err;
2214 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002215 status = nfs4_recover_expired_lease(server);
2216 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002217 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04002218 if (dentry->d_inode != NULL)
2219 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002220 status = -ENOMEM;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002221 if (dentry->d_inode)
2222 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002223 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002224 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002225 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002226 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
David Quigley14c43f72013-05-22 12:50:43 -04002228 if (label) {
2229 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2230 if (IS_ERR(olabel)) {
2231 status = PTR_ERR(olabel);
2232 goto err_opendata_put;
2233 }
2234 }
2235
Andy Adamson82be4172012-05-23 05:02:35 -04002236 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2237 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2238 if (!opendata->f_attr.mdsthreshold)
David Quigley14c43f72013-05-22 12:50:43 -04002239 goto err_free_label;
Trond Myklebust1549210f2012-06-05 09:16:47 -04002240 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002241 }
Al Viro82a2c1b2011-06-22 18:30:55 -04002242 if (dentry->d_inode != NULL)
2243 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002244
Trond Myklebust3efb9722013-05-29 13:17:04 -04002245 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002246 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002247 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002248 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002249
Trond Myklebust549b19c2013-04-16 18:42:34 -04002250 if ((opendata->o_arg.open_flags & O_EXCL) &&
2251 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002252 nfs4_exclusive_attrset(opendata, sattr);
2253
2254 nfs_fattr_init(opendata->o_res.f_attr);
2255 status = nfs4_do_setattr(state->inode, cred,
2256 opendata->o_res.f_attr, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002257 state, label, olabel);
2258 if (status == 0) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002259 nfs_setattr_update_inode(state->inode, sattr);
David Quigley1775fd32013-05-22 12:50:42 -04002260 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
David Quigleyaa9c2662013-05-22 12:50:44 -04002261 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
David Quigley1775fd32013-05-22 12:50:42 -04002262 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002263 }
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002264 if (opendata->file_created)
2265 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002266
2267 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2268 *ctx_th = opendata->f_attr.mdsthreshold;
2269 else
2270 kfree(opendata->f_attr.mdsthreshold);
2271 opendata->f_attr.mdsthreshold = NULL;
2272
David Quigley14c43f72013-05-22 12:50:43 -04002273 nfs4_label_free(olabel);
2274
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002275 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002278err_free_label:
2279 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002280err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002281 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002282 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002283err_put_state_owner:
2284 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 return status;
2287}
2288
2289
Andy Adamson82be4172012-05-23 05:02:35 -04002290static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002291 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002292 int flags,
2293 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002294 struct nfs4_label *label,
2295 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002297 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 struct nfs4_exception exception = { };
2299 struct nfs4_state *res;
2300 int status;
2301
2302 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002303 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002304 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002305 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 if (status == 0)
2307 break;
2308 /* NOTE: BAD_SEQID means the server and client disagree about the
2309 * book-keeping w.r.t. state-changing operations
2310 * (OPEN/CLOSE/LOCK/LOCKU...)
2311 * It is actually a sign of a bug on the client or on the server.
2312 *
2313 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002314 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 * have unhashed the old state_owner for us, and that we can
2316 * therefore safely retry using a new one. We should still warn
2317 * the user though...
2318 */
2319 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002320 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002321 " returned a bad sequence-id error!\n",
2322 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 exception.retry = 1;
2324 continue;
2325 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002326 /*
2327 * BAD_STATEID on OPEN means that the server cancelled our
2328 * state before it received the OPEN_CONFIRM.
2329 * Recover by retrying the request as per the discussion
2330 * on Page 181 of RFC3530.
2331 */
2332 if (status == -NFS4ERR_BAD_STATEID) {
2333 exception.retry = 1;
2334 continue;
2335 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002336 if (status == -EAGAIN) {
2337 /* We must have found a delegation */
2338 exception.retry = 1;
2339 continue;
2340 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002341 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2342 continue;
2343 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 status, &exception));
2345 } while (exception.retry);
2346 return res;
2347}
2348
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002349static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2350 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002351 struct nfs4_state *state, struct nfs4_label *ilabel,
2352 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002354 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002356 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 .iap = sattr,
2358 .server = server,
2359 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002360 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 };
2362 struct nfs_setattrres res = {
2363 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002364 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 .server = server,
2366 };
2367 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002368 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2369 .rpc_argp = &arg,
2370 .rpc_resp = &res,
2371 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002373 unsigned long timestamp = jiffies;
Trond Myklebustee3ae842013-04-29 10:35:36 -04002374 fmode_t fmode;
2375 bool truncate;
Trond Myklebust65e43082005-08-16 11:49:44 -04002376 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
David Quigleyaa9c2662013-05-22 12:50:44 -04002378 arg.bitmask = nfs4_bitmask(server, ilabel);
2379 if (ilabel)
2380 arg.bitmask = nfs4_bitmask(server, olabel);
2381
Trond Myklebust0e574af2005-10-27 22:12:38 -04002382 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
Trond Myklebustee3ae842013-04-29 10:35:36 -04002384 /* Servers should only apply open mode checks for file size changes */
2385 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2386 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2387
2388 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2389 /* Use that stateid */
2390 } else if (truncate && state != NULL && nfs4_valid_open_stateid(state)) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002391 struct nfs_lockowner lockowner = {
2392 .l_owner = current->files,
2393 .l_pid = current->tgid,
2394 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002395 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002396 &lockowner);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002397 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002398 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
Bryan Schumaker7c513052011-03-24 17:12:24 +00002400 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002401 if (status == 0 && state != NULL)
2402 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002403 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404}
2405
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002406static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2407 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002408 struct nfs4_state *state, struct nfs4_label *ilabel,
2409 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002411 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002412 struct nfs4_exception exception = {
2413 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002414 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002415 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 int err;
2417 do {
David Quigley1775fd32013-05-22 12:50:42 -04002418 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
Trond Myklebustc1578b72013-08-12 16:58:42 -04002419 trace_nfs4_setattr(inode, err);
Trond Myklebust451146b2012-04-18 16:29:11 -04002420 switch (err) {
2421 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002422 if (!(sattr->ia_valid & ATTR_SIZE)) {
2423 pr_warn_once("NFSv4: server %s is incorrectly "
2424 "applying open mode checks to "
2425 "a SETATTR that is not "
2426 "changing file size.\n",
2427 server->nfs_client->cl_hostname);
2428 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002429 if (state && !(state->state & FMODE_WRITE)) {
2430 err = -EBADF;
2431 if (sattr->ia_valid & ATTR_OPEN)
2432 err = -EACCES;
2433 goto out;
2434 }
2435 }
2436 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002438out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 return err;
2440}
2441
2442struct nfs4_closedata {
2443 struct inode *inode;
2444 struct nfs4_state *state;
2445 struct nfs_closeargs arg;
2446 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002447 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002448 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002449 bool roc;
2450 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451};
2452
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002453static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002454{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002455 struct nfs4_closedata *calldata = data;
2456 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002457 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002458
Fred Isamanf7e89172011-01-06 11:36:32 +00002459 if (calldata->roc)
2460 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002461 nfs4_put_open_state(calldata->state);
2462 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002463 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002464 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002465 kfree(calldata);
2466}
2467
Trond Myklebust88069f72009-12-08 08:33:16 -05002468static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2469 fmode_t fmode)
2470{
2471 spin_lock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002472 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002473 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
2474 case FMODE_WRITE:
2475 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2476 break;
2477 case FMODE_READ:
2478 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2479 break;
2480 case 0:
2481 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2482 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2483 clear_bit(NFS_OPEN_STATE, &state->flags);
2484 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002485 spin_unlock(&state->owner->so_lock);
2486}
2487
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002488static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002490 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 struct nfs_server *server = NFS_SERVER(calldata->inode);
2493
Chuck Levera3ca5652012-03-01 17:00:40 -05002494 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002495 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2496 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002497 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 /* hmm. we are done with the inode, and in the process of freeing
2499 * the state_owner. we keep this around to process errors
2500 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 switch (task->tk_status) {
2502 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002503 if (calldata->roc)
2504 pnfs_roc_set_barrier(state->inode,
2505 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002506 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002507 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002508 nfs4_close_clear_stateid_flags(state,
2509 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 break;
2511 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002512 case -NFS4ERR_OLD_STATEID:
2513 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002515 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002516 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002518 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2519 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002521 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002522 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002523 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524}
2525
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002526static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002528 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002529 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002530 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002531 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002532
Chuck Levera3ca5652012-03-01 17:00:40 -05002533 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002534 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002535 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002536
Trond Myklebust88069f72009-12-08 08:33:16 -05002537 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2538 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002539 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002540 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002541 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002542 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002543 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2544 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2545 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002546 }
2547 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002548 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2549 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2550 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002551 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002552 }
Trond Myklebust5d422302013-03-14 16:57:48 -04002553 if (!nfs4_valid_open_stateid(state))
2554 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002555 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002556
2557 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002558 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002559 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002560 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002561
Fred Isamanf7e89172011-01-06 11:36:32 +00002562 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002563 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002564 if (calldata->roc &&
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002565 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2566 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002567 goto out_wait;
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002568 }
Fred Isamanf7e89172011-01-06 11:36:32 +00002569 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002570
Trond Myklebust516a6af2005-10-27 22:12:41 -04002571 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002572 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002573 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002574 &calldata->arg.seq_args,
2575 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002576 task) != 0)
2577 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002578 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002579 return;
2580out_no_action:
2581 task->tk_action = NULL;
2582out_wait:
2583 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584}
2585
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002586static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002587 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002588 .rpc_call_done = nfs4_close_done,
2589 .rpc_release = nfs4_free_closedata,
2590};
2591
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592/*
2593 * It is possible for data to be read/written from a mem-mapped file
2594 * after the sys_close call (which hits the vfs layer as a flush).
2595 * This means that we can't safely call nfsv4 close on a file until
2596 * the inode is cleared. This in turn means that we are not good
2597 * NFSv4 citizens - we do not indicate to the server to update the file's
2598 * share state even when we are done with one of the three share
2599 * stateid's in the inode.
2600 *
2601 * NOTE: Caller must be holding the sp->so_owner semaphore!
2602 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002603int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002605 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002607 struct nfs4_state_owner *sp = state->owner;
2608 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002609 struct rpc_message msg = {
2610 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2611 .rpc_cred = state->owner->so_cred,
2612 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002613 struct rpc_task_setup task_setup_data = {
2614 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002615 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002616 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002617 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002618 .flags = RPC_TASK_ASYNC,
2619 };
Trond Myklebust95121352005-10-18 14:20:12 -07002620 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04002622 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
2623 &task_setup_data.rpc_client, &msg);
2624
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002625 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002627 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04002628 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002629 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002631 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002632 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002634 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002635 if (calldata->arg.seqid == NULL)
2636 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002637 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002638 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002639 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002640 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002641 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002642 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002643 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002644
Trond Myklebust1174dd12010-12-21 10:52:24 -05002645 msg.rpc_argp = &calldata->arg;
2646 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002647 task_setup_data.callback_data = calldata;
2648 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002649 if (IS_ERR(task))
2650 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002651 status = 0;
2652 if (wait)
2653 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002654 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002655 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002656out_free_calldata:
2657 kfree(calldata);
2658out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002659 nfs4_put_open_state(state);
2660 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002661 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662}
2663
Trond Myklebust2b484292010-09-17 10:56:51 -04002664static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002665nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
2666 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04002669 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2670
2671 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672
Trond Myklebust565277f2007-10-15 18:17:53 -04002673 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002674 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04002675
2676 nfs4_label_release_security(label);
2677
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002678 if (IS_ERR(state))
2679 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04002680 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681}
2682
Trond Myklebust1185a552009-12-03 15:54:02 -05002683static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002684{
2685 if (ctx->state == NULL)
2686 return;
2687 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002688 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002689 else
Al Viro643168c2011-06-22 18:20:23 -04002690 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002691}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692
2693static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2694{
Benny Halevy43652ad2009-04-01 09:21:54 -04002695 struct nfs4_server_caps_arg args = {
2696 .fhandle = fhandle,
2697 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 struct nfs4_server_caps_res res = {};
2699 struct rpc_message msg = {
2700 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002701 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 .rpc_resp = &res,
2703 };
2704 int status;
2705
Bryan Schumaker7c513052011-03-24 17:12:24 +00002706 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 if (status == 0) {
2708 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002709 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2710 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2711 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2712 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2713 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2715 server->caps |= NFS_CAP_ACLS;
2716 if (res.has_links != 0)
2717 server->caps |= NFS_CAP_HARDLINKS;
2718 if (res.has_symlinks != 0)
2719 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002720 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2721 server->caps |= NFS_CAP_FILEID;
2722 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2723 server->caps |= NFS_CAP_MODE;
2724 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2725 server->caps |= NFS_CAP_NLINK;
2726 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2727 server->caps |= NFS_CAP_OWNER;
2728 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2729 server->caps |= NFS_CAP_OWNER_GROUP;
2730 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2731 server->caps |= NFS_CAP_ATIME;
2732 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2733 server->caps |= NFS_CAP_CTIME;
2734 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2735 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04002736#ifdef CONFIG_NFS_V4_SECURITY_LABEL
2737 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
2738 server->caps |= NFS_CAP_SECURITY_LABEL;
2739#endif
2740 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
2741 sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002742
David Quigleyaa9c2662013-05-22 12:50:44 -04002743 if (server->caps & NFS_CAP_SECURITY_LABEL) {
2744 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2745 res.attr_bitmask[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2746 }
Trond Myklebusta65318b2009-03-11 14:10:28 -04002747 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2748 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2749 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002751 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002753
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 return status;
2755}
2756
Trond Myklebust55a97592006-06-09 09:34:19 -04002757int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758{
2759 struct nfs4_exception exception = { };
2760 int err;
2761 do {
2762 err = nfs4_handle_exception(server,
2763 _nfs4_server_capabilities(server, fhandle),
2764 &exception);
2765 } while (exception.retry);
2766 return err;
2767}
2768
2769static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2770 struct nfs_fsinfo *info)
2771{
David Quigleyaa9c2662013-05-22 12:50:44 -04002772 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04002774 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 };
2776 struct nfs4_lookup_res res = {
2777 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002778 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 .fh = fhandle,
2780 };
2781 struct rpc_message msg = {
2782 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2783 .rpc_argp = &args,
2784 .rpc_resp = &res,
2785 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002786
David Quigleyaa9c2662013-05-22 12:50:44 -04002787 bitmask[0] = nfs4_fattr_bitmap[0];
2788 bitmask[1] = nfs4_fattr_bitmap[1];
2789 /*
2790 * Process the label in the upcoming getfattr
2791 */
2792 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
2793
Trond Myklebust0e574af2005-10-27 22:12:38 -04002794 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002795 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796}
2797
2798static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2799 struct nfs_fsinfo *info)
2800{
2801 struct nfs4_exception exception = { };
2802 int err;
2803 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002804 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04002805 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002806 switch (err) {
2807 case 0:
2808 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002809 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002810 default:
2811 err = nfs4_handle_exception(server, err, &exception);
2812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002814out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 return err;
2816}
2817
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002818static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2819 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2820{
Trond Myklebustc2190662013-08-26 19:23:04 -04002821 struct rpc_auth_create_args auth_args = {
2822 .pseudoflavor = flavor,
2823 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002824 struct rpc_auth *auth;
2825 int ret;
2826
Trond Myklebustc2190662013-08-26 19:23:04 -04002827 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002828 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04002829 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002830 goto out;
2831 }
2832 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002833out:
2834 return ret;
2835}
2836
Chuck Lever9a744ba2013-03-16 15:56:02 -04002837/*
2838 * Retry pseudoroot lookup with various security flavors. We do this when:
2839 *
2840 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2841 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2842 *
2843 * Returns zero on success, or a negative NFS4ERR value, or a
2844 * negative errno value.
2845 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002846static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002847 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848{
Chuck Lever9a744ba2013-03-16 15:56:02 -04002849 /* Per 3530bis 15.33.5 */
2850 static const rpc_authflavor_t flav_array[] = {
2851 RPC_AUTH_GSS_KRB5P,
2852 RPC_AUTH_GSS_KRB5I,
2853 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04002854 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04002855 RPC_AUTH_NULL,
2856 };
2857 int status = -EPERM;
2858 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859
Chuck Lever9a744ba2013-03-16 15:56:02 -04002860 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002861 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002862 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002863 continue;
2864 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002865 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04002866
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002867 /*
2868 * -EACCESS could mean that the user doesn't have correct permissions
2869 * to access the mount. It could also mean that we tried to mount
2870 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2871 * existing mount programs don't handle -EACCES very well so it should
2872 * be mapped to -EPERM instead.
2873 */
2874 if (status == -EACCES)
2875 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002876 return status;
2877}
2878
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002879static int nfs4_do_find_root_sec(struct nfs_server *server,
2880 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
2881{
2882 int mv = server->nfs_client->cl_minorversion;
2883 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
2884}
2885
2886/**
2887 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
2888 * @server: initialized nfs_server handle
2889 * @fhandle: we fill in the pseudo-fs root file handle
2890 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002891 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002892 *
2893 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002894 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002895int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002896 struct nfs_fsinfo *info,
2897 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002898{
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002899 int status;
2900
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002901 switch (auth_probe) {
2902 case false:
2903 status = nfs4_lookup_root(server, fhandle, info);
2904 if (status != -NFS4ERR_WRONGSEC)
2905 break;
2906 /* Did user force a 'sec=' mount option? */
2907 if (server->flags & NFS_MOUNT_SECFLAVOUR)
2908 break;
2909 default:
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002910 status = nfs4_do_find_root_sec(server, fhandle, info);
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002911 }
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002912
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 if (status == 0)
2914 status = nfs4_server_capabilities(server, fhandle);
2915 if (status == 0)
2916 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002917
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002918 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919}
2920
Bryan Schumakerbae36242012-05-10 15:07:31 -04002921static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2922 struct nfs_fsinfo *info)
2923{
2924 int error;
2925 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04002926 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04002927
2928 error = nfs4_server_capabilities(server, mntfh);
2929 if (error < 0) {
2930 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2931 return error;
2932 }
2933
David Quigley14c43f72013-05-22 12:50:43 -04002934 label = nfs4_label_alloc(server, GFP_KERNEL);
2935 if (IS_ERR(label))
2936 return PTR_ERR(label);
2937
David Quigley1775fd32013-05-22 12:50:42 -04002938 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04002939 if (error < 0) {
2940 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04002941 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04002942 }
2943
2944 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2945 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2946 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2947
David Quigley14c43f72013-05-22 12:50:43 -04002948err_free_label:
2949 nfs4_label_free(label);
2950
Bryan Schumakerbae36242012-05-10 15:07:31 -04002951 return error;
2952}
2953
Manoj Naik6b97fd32006-06-09 09:34:29 -04002954/*
2955 * Get locations and (maybe) other attributes of a referral.
2956 * Note that we'll actually follow the referral later when
2957 * we detect fsid mismatch in inode revalidation
2958 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002959static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2960 const struct qstr *name, struct nfs_fattr *fattr,
2961 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002962{
2963 int status = -ENOMEM;
2964 struct page *page = NULL;
2965 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002966
2967 page = alloc_page(GFP_KERNEL);
2968 if (page == NULL)
2969 goto out;
2970 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2971 if (locations == NULL)
2972 goto out;
2973
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002974 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002975 if (status != 0)
2976 goto out;
2977 /* Make sure server returned a different fsid for the referral */
2978 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002979 dprintk("%s: server did not return a different fsid for"
2980 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002981 status = -EIO;
2982 goto out;
2983 }
Andy Adamson533eb462011-06-13 18:25:56 -04002984 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2985 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002986
Andy Adamson533eb462011-06-13 18:25:56 -04002987 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002988 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002989 memset(fhandle, 0, sizeof(struct nfs_fh));
2990out:
2991 if (page)
2992 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002993 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002994 return status;
2995}
2996
David Quigley1775fd32013-05-22 12:50:42 -04002997static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
2998 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999{
3000 struct nfs4_getattr_arg args = {
3001 .fh = fhandle,
3002 .bitmask = server->attr_bitmask,
3003 };
3004 struct nfs4_getattr_res res = {
3005 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003006 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 .server = server,
3008 };
3009 struct rpc_message msg = {
3010 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3011 .rpc_argp = &args,
3012 .rpc_resp = &res,
3013 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003014
3015 args.bitmask = nfs4_bitmask(server, label);
3016
Trond Myklebust0e574af2005-10-27 22:12:38 -04003017 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003018 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019}
3020
David Quigley1775fd32013-05-22 12:50:42 -04003021static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3022 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023{
3024 struct nfs4_exception exception = { };
3025 int err;
3026 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003027 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3028 trace_nfs4_getattr(server, fhandle, fattr, err);
3029 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 &exception);
3031 } while (exception.retry);
3032 return err;
3033}
3034
3035/*
3036 * The file is not closed if it is opened due to the a request to change
3037 * the size of the file. The open call will not be needed once the
3038 * VFS layer lookup-intents are implemented.
3039 *
3040 * Close is called when the inode is destroyed.
3041 * If we haven't opened the file for O_WRONLY, we
3042 * need to in the size_change case to obtain a stateid.
3043 *
3044 * Got race?
3045 * Because OPEN is always done by name in nfsv4, it is
3046 * possible that we opened a different file by the same
3047 * name. We can recognize this race condition, but we
3048 * can't do anything about it besides returning an error.
3049 *
3050 * This will be fixed with VFS changes (lookup-intent).
3051 */
3052static int
3053nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3054 struct iattr *sattr)
3055{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003056 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003057 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003058 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003059 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 int status;
3061
Benny Halevy8a1636c2010-07-14 15:43:57 -04003062 if (pnfs_ld_layoutret_on_setattr(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003063 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003064
Trond Myklebust0e574af2005-10-27 22:12:38 -04003065 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066
Andy Adamson26699402012-05-30 16:12:24 -04003067 /* Deal with open(O_TRUNC) */
3068 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003069 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003070
3071 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003072 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003073 return 0;
3074
Trond Myklebustd5308382005-11-04 15:33:38 -05003075 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003076 if (sattr->ia_valid & ATTR_FILE) {
3077 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003078
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003079 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003080 if (ctx) {
3081 cred = ctx->cred;
3082 state = ctx->state;
3083 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003084 }
3085
David Quigley14c43f72013-05-22 12:50:43 -04003086 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3087 if (IS_ERR(label))
3088 return PTR_ERR(label);
3089
3090 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003091 if (status == 0) {
Trond Myklebust65e43082005-08-16 11:49:44 -04003092 nfs_setattr_update_inode(inode, sattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003093 nfs_setsecurity(inode, fattr, label);
3094 }
David Quigley14c43f72013-05-22 12:50:43 -04003095 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 return status;
3097}
3098
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003099static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3100 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003101 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003102{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003103 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003104 int status;
3105 struct nfs4_lookup_arg args = {
3106 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003107 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003108 .name = name,
3109 };
3110 struct nfs4_lookup_res res = {
3111 .server = server,
3112 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003113 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003114 .fh = fhandle,
3115 };
3116 struct rpc_message msg = {
3117 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3118 .rpc_argp = &args,
3119 .rpc_resp = &res,
3120 };
3121
David Quigleyaa9c2662013-05-22 12:50:44 -04003122 args.bitmask = nfs4_bitmask(server, label);
3123
David Howells2b3de442006-08-22 20:06:09 -04003124 nfs_fattr_init(fattr);
3125
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003127 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 dprintk("NFS reply lookup: %d\n", status);
3129 return status;
3130}
3131
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003132static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003133{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003134 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003135 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003136 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3137 fattr->nlink = 2;
3138}
3139
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003140static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3141 struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003142 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003143{
3144 struct nfs4_exception exception = { };
3145 struct rpc_clnt *client = *clnt;
3146 int err;
3147 do {
David Quigley1775fd32013-05-22 12:50:42 -04003148 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003149 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003150 switch (err) {
3151 case -NFS4ERR_BADNAME:
3152 err = -ENOENT;
3153 goto out;
3154 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003155 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003156 goto out;
3157 case -NFS4ERR_WRONGSEC:
3158 err = -EPERM;
3159 if (client != *clnt)
3160 goto out;
Trond Myklebust41d058c2013-09-07 17:49:05 -04003161 /* No security negotiation if the user specified 'sec=' */
3162 if (NFS_SERVER(dir)->flags & NFS_MOUNT_SECFLAVOUR)
3163 goto out;
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003164 client = nfs4_create_sec_client(client, dir, name);
3165 if (IS_ERR(client))
3166 return PTR_ERR(client);
3167
3168 exception.retry = 1;
3169 break;
3170 default:
3171 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3172 }
3173 } while (exception.retry);
3174
3175out:
3176 if (err == 0)
3177 *clnt = client;
3178 else if (client != *clnt)
3179 rpc_shutdown_client(client);
3180
3181 return err;
3182}
3183
Bryan Schumaker80a16b22012-04-27 13:27:46 -04003184static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003185 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3186 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003188 int status;
3189 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003190
David Quigley1775fd32013-05-22 12:50:42 -04003191 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003192 if (client != NFS_CLIENT(dir)) {
3193 rpc_shutdown_client(client);
3194 nfs_fixup_secinfo_attributes(fattr);
3195 }
3196 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197}
3198
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003199struct rpc_clnt *
3200nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3201 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3202{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003203 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003204 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003205
David Quigley1775fd32013-05-22 12:50:42 -04003206 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003207 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003208 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003209 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003210}
3211
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3213{
Trond Myklebust76b32992007-08-10 17:45:11 -04003214 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 struct nfs4_accessargs args = {
3216 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003217 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003219 struct nfs4_accessres res = {
3220 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003221 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 struct rpc_message msg = {
3223 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3224 .rpc_argp = &args,
3225 .rpc_resp = &res,
3226 .rpc_cred = entry->cred,
3227 };
3228 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003229 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230
3231 /*
3232 * Determine which access bits we want to ask for...
3233 */
3234 if (mode & MAY_READ)
3235 args.access |= NFS4_ACCESS_READ;
3236 if (S_ISDIR(inode->i_mode)) {
3237 if (mode & MAY_WRITE)
3238 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3239 if (mode & MAY_EXEC)
3240 args.access |= NFS4_ACCESS_LOOKUP;
3241 } else {
3242 if (mode & MAY_WRITE)
3243 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3244 if (mode & MAY_EXEC)
3245 args.access |= NFS4_ACCESS_EXECUTE;
3246 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003247
3248 res.fattr = nfs_alloc_fattr();
3249 if (res.fattr == NULL)
3250 return -ENOMEM;
3251
Bryan Schumaker7c513052011-03-24 17:12:24 +00003252 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003254 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003255 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003257 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 return status;
3259}
3260
3261static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3262{
3263 struct nfs4_exception exception = { };
3264 int err;
3265 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003266 err = _nfs4_proc_access(inode, entry);
3267 trace_nfs4_access(inode, err);
3268 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 &exception);
3270 } while (exception.retry);
3271 return err;
3272}
3273
3274/*
3275 * TODO: For the time being, we don't try to get any attributes
3276 * along with any of the zero-copy operations READ, READDIR,
3277 * READLINK, WRITE.
3278 *
3279 * In the case of the first three, we want to put the GETATTR
3280 * after the read-type operation -- this is because it is hard
3281 * to predict the length of a GETATTR response in v4, and thus
3282 * align the READ data correctly. This means that the GETATTR
3283 * may end up partially falling into the page cache, and we should
3284 * shift it into the 'tail' of the xdr_buf before processing.
3285 * To do this efficiently, we need to know the total length
3286 * of data received, which doesn't seem to be available outside
3287 * of the RPC layer.
3288 *
3289 * In the case of WRITE, we also want to put the GETATTR after
3290 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003291 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 *
3293 * Both of these changes to the XDR layer would in fact be quite
3294 * minor, but I decided to leave them for a subsequent patch.
3295 */
3296static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3297 unsigned int pgbase, unsigned int pglen)
3298{
3299 struct nfs4_readlink args = {
3300 .fh = NFS_FH(inode),
3301 .pgbase = pgbase,
3302 .pglen = pglen,
3303 .pages = &page,
3304 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003305 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 struct rpc_message msg = {
3307 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3308 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003309 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 };
3311
Bryan Schumaker7c513052011-03-24 17:12:24 +00003312 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 -07003313}
3314
3315static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3316 unsigned int pgbase, unsigned int pglen)
3317{
3318 struct nfs4_exception exception = { };
3319 int err;
3320 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003321 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3322 trace_nfs4_readlink(inode, err);
3323 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 &exception);
3325 } while (exception.retry);
3326 return err;
3327}
3328
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003330 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332static int
3333nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003334 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335{
David Quigleyaa9c2662013-05-22 12:50:44 -04003336 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003337 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 struct nfs4_state *state;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003339 int opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 int status = 0;
3341
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003342 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3343 if (IS_ERR(ctx))
3344 return PTR_ERR(ctx);
3345
David Quigleyaa9c2662013-05-22 12:50:44 -04003346 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3347
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003348 sattr->ia_mode &= ~current_umask();
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003349 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, &opened);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 if (IS_ERR(state)) {
3351 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003352 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003355 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003356 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 return status;
3358}
3359
3360static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3361{
Trond Myklebust16e42952005-10-27 22:12:44 -04003362 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003363 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003365 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003367 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003368 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003369 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003371 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3372 .rpc_argp = &args,
3373 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003375 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003376
Bryan Schumaker7c513052011-03-24 17:12:24 +00003377 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003378 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003379 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 return status;
3381}
3382
3383static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3384{
3385 struct nfs4_exception exception = { };
3386 int err;
3387 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003388 err = _nfs4_proc_remove(dir, name);
3389 trace_nfs4_remove(dir, name, err);
3390 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 &exception);
3392 } while (exception.retry);
3393 return err;
3394}
3395
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003396static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003398 struct nfs_server *server = NFS_SERVER(dir);
3399 struct nfs_removeargs *args = msg->rpc_argp;
3400 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003402 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04003404 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003405
3406 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407}
3408
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003409static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3410{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003411 nfs4_setup_sequence(NFS_SERVER(data->dir),
3412 &data->args.seq_args,
3413 &data->res.seq_res,
3414 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415}
3416
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003417static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003419 struct nfs_unlinkdata *data = task->tk_calldata;
3420 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003421
Trond Myklebust14516c32010-07-31 14:29:06 -04003422 if (!nfs4_sequence_done(task, &res->seq_res))
3423 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003424 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003425 return 0;
3426 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003427 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428}
3429
Jeff Laytond3d41522010-09-17 17:31:57 -04003430static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3431{
3432 struct nfs_server *server = NFS_SERVER(dir);
3433 struct nfs_renameargs *arg = msg->rpc_argp;
3434 struct nfs_renameres *res = msg->rpc_resp;
3435
3436 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003437 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04003438 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003439}
3440
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003441static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3442{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003443 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3444 &data->args.seq_args,
3445 &data->res.seq_res,
3446 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003447}
3448
3449static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3450 struct inode *new_dir)
3451{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003452 struct nfs_renamedata *data = task->tk_calldata;
3453 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04003454
3455 if (!nfs4_sequence_done(task, &res->seq_res))
3456 return 0;
3457 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3458 return 0;
3459
3460 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003461 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003462 return 1;
3463}
3464
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3466 struct inode *new_dir, struct qstr *new_name)
3467{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003468 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003469 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 .old_dir = NFS_FH(old_dir),
3471 .new_dir = NFS_FH(new_dir),
3472 .old_name = old_name,
3473 .new_name = new_name,
3474 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003475 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003476 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003477 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 struct rpc_message msg = {
3479 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3480 .rpc_argp = &arg,
3481 .rpc_resp = &res,
3482 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003483 int status = -ENOMEM;
David Quigley14c43f72013-05-22 12:50:43 -04003484
Bryan Schumaker7c513052011-03-24 17:12:24 +00003485 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 if (!status) {
3487 update_changeattr(old_dir, &res.old_cinfo);
3488 update_changeattr(new_dir, &res.new_cinfo);
3489 }
3490 return status;
3491}
3492
3493static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3494 struct inode *new_dir, struct qstr *new_name)
3495{
3496 struct nfs4_exception exception = { };
3497 int err;
3498 do {
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003499 err = _nfs4_proc_rename(old_dir, old_name,
3500 new_dir, new_name);
3501 trace_nfs4_rename(old_dir, old_name, new_dir, new_name, err);
3502 err = nfs4_handle_exception(NFS_SERVER(old_dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 &exception);
3504 } while (exception.retry);
3505 return err;
3506}
3507
3508static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3509{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003510 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 struct nfs4_link_arg arg = {
3512 .fh = NFS_FH(inode),
3513 .dir_fh = NFS_FH(dir),
3514 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003515 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003517 struct nfs4_link_res res = {
3518 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003519 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003520 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 struct rpc_message msg = {
3522 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3523 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003524 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003526 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527
Trond Myklebust136f2622010-04-16 16:22:49 -04003528 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003529 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003530 goto out;
3531
David Quigley14c43f72013-05-22 12:50:43 -04003532 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3533 if (IS_ERR(res.label)) {
3534 status = PTR_ERR(res.label);
3535 goto out;
3536 }
David Quigleyaa9c2662013-05-22 12:50:44 -04003537 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04003538
Bryan Schumaker7c513052011-03-24 17:12:24 +00003539 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003540 if (!status) {
3541 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04003542 status = nfs_post_op_update_inode(inode, res.fattr);
3543 if (!status)
3544 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003545 }
David Quigley14c43f72013-05-22 12:50:43 -04003546
3547
3548 nfs4_label_free(res.label);
3549
Trond Myklebust136f2622010-04-16 16:22:49 -04003550out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003551 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 return status;
3553}
3554
3555static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3556{
3557 struct nfs4_exception exception = { };
3558 int err;
3559 do {
3560 err = nfs4_handle_exception(NFS_SERVER(inode),
3561 _nfs4_proc_link(inode, dir, name),
3562 &exception);
3563 } while (exception.retry);
3564 return err;
3565}
3566
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003567struct nfs4_createdata {
3568 struct rpc_message msg;
3569 struct nfs4_create_arg arg;
3570 struct nfs4_create_res res;
3571 struct nfs_fh fh;
3572 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003573 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003574};
3575
3576static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3577 struct qstr *name, struct iattr *sattr, u32 ftype)
3578{
3579 struct nfs4_createdata *data;
3580
3581 data = kzalloc(sizeof(*data), GFP_KERNEL);
3582 if (data != NULL) {
3583 struct nfs_server *server = NFS_SERVER(dir);
3584
David Quigley14c43f72013-05-22 12:50:43 -04003585 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3586 if (IS_ERR(data->label))
3587 goto out_free;
3588
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003589 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3590 data->msg.rpc_argp = &data->arg;
3591 data->msg.rpc_resp = &data->res;
3592 data->arg.dir_fh = NFS_FH(dir);
3593 data->arg.server = server;
3594 data->arg.name = name;
3595 data->arg.attrs = sattr;
3596 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04003597 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003598 data->res.server = server;
3599 data->res.fh = &data->fh;
3600 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003601 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003602 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003603 }
3604 return data;
David Quigley14c43f72013-05-22 12:50:43 -04003605out_free:
3606 kfree(data);
3607 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003608}
3609
3610static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3611{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003612 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003613 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003614 if (status == 0) {
3615 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04003616 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003617 }
3618 return status;
3619}
3620
3621static void nfs4_free_createdata(struct nfs4_createdata *data)
3622{
David Quigley14c43f72013-05-22 12:50:43 -04003623 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003624 kfree(data);
3625}
3626
Chuck Lever4f390c12006-08-22 20:06:22 -04003627static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003628 struct page *page, unsigned int len, struct iattr *sattr,
3629 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003631 struct nfs4_createdata *data;
3632 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633
Chuck Lever94a6d752006-08-22 20:06:23 -04003634 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003635 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003636
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003637 status = -ENOMEM;
3638 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3639 if (data == NULL)
3640 goto out;
3641
3642 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3643 data->arg.u.symlink.pages = &page;
3644 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04003645 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003647 status = nfs4_do_create(dir, dentry, data);
3648
3649 nfs4_free_createdata(data);
3650out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 return status;
3652}
3653
Chuck Lever4f390c12006-08-22 20:06:22 -04003654static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003655 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656{
3657 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003658 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04003660
3661 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3662
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003664 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3665 trace_nfs4_symlink(dir, &dentry->d_name, err);
3666 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 &exception);
3668 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003669
3670 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 return err;
3672}
3673
3674static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003675 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003677 struct nfs4_createdata *data;
3678 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003680 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3681 if (data == NULL)
3682 goto out;
3683
David Quigley1775fd32013-05-22 12:50:42 -04003684 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003685 status = nfs4_do_create(dir, dentry, data);
3686
3687 nfs4_free_createdata(data);
3688out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 return status;
3690}
3691
3692static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3693 struct iattr *sattr)
3694{
3695 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003696 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003698
David Quigleyaa9c2662013-05-22 12:50:44 -04003699 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3700
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003701 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003703 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3704 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3705 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 &exception);
3707 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003708 nfs4_label_release_security(label);
3709
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 return err;
3711}
3712
3713static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003714 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715{
3716 struct inode *dir = dentry->d_inode;
3717 struct nfs4_readdir_arg args = {
3718 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003719 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 .pgbase = 0,
3721 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003722 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003723 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 };
3725 struct nfs4_readdir_res res;
3726 struct rpc_message msg = {
3727 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3728 .rpc_argp = &args,
3729 .rpc_resp = &res,
3730 .rpc_cred = cred,
3731 };
3732 int status;
3733
Harvey Harrison3110ff82008-05-02 13:42:44 -07003734 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003735 dentry->d_parent->d_name.name,
3736 dentry->d_name.name,
3737 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003738 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003740 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 -05003741 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003742 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003743 status += args.pgbase;
3744 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003745
3746 nfs_invalidate_atime(dir);
3747
Harvey Harrison3110ff82008-05-02 13:42:44 -07003748 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 return status;
3750}
3751
3752static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003753 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754{
3755 struct nfs4_exception exception = { };
3756 int err;
3757 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003758 err = _nfs4_proc_readdir(dentry, cred, cookie,
3759 pages, count, plus);
3760 trace_nfs4_readdir(dentry->d_inode, err);
3761 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 &exception);
3763 } while (exception.retry);
3764 return err;
3765}
3766
3767static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04003768 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003770 struct nfs4_createdata *data;
3771 int mode = sattr->ia_mode;
3772 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003774 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3775 if (data == NULL)
3776 goto out;
3777
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003779 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003781 data->arg.ftype = NF4BLK;
3782 data->arg.u.device.specdata1 = MAJOR(rdev);
3783 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 }
3785 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003786 data->arg.ftype = NF4CHR;
3787 data->arg.u.device.specdata1 = MAJOR(rdev);
3788 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003789 } else if (!S_ISSOCK(mode)) {
3790 status = -EINVAL;
3791 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 }
David Quigley1775fd32013-05-22 12:50:42 -04003793
David Quigleyaa9c2662013-05-22 12:50:44 -04003794 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003795 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003796out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003797 nfs4_free_createdata(data);
3798out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 return status;
3800}
3801
3802static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3803 struct iattr *sattr, dev_t rdev)
3804{
3805 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003806 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003808
David Quigleyaa9c2662013-05-22 12:50:44 -04003809 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3810
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003811 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003813 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
3814 trace_nfs4_mknod(dir, &dentry->d_name, err);
3815 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 &exception);
3817 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003818
3819 nfs4_label_release_security(label);
3820
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 return err;
3822}
3823
3824static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3825 struct nfs_fsstat *fsstat)
3826{
3827 struct nfs4_statfs_arg args = {
3828 .fh = fhandle,
3829 .bitmask = server->attr_bitmask,
3830 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003831 struct nfs4_statfs_res res = {
3832 .fsstat = fsstat,
3833 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 struct rpc_message msg = {
3835 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3836 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003837 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 };
3839
Trond Myklebust0e574af2005-10-27 22:12:38 -04003840 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003841 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842}
3843
3844static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3845{
3846 struct nfs4_exception exception = { };
3847 int err;
3848 do {
3849 err = nfs4_handle_exception(server,
3850 _nfs4_proc_statfs(server, fhandle, fsstat),
3851 &exception);
3852 } while (exception.retry);
3853 return err;
3854}
3855
3856static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3857 struct nfs_fsinfo *fsinfo)
3858{
3859 struct nfs4_fsinfo_arg args = {
3860 .fh = fhandle,
3861 .bitmask = server->attr_bitmask,
3862 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003863 struct nfs4_fsinfo_res res = {
3864 .fsinfo = fsinfo,
3865 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 struct rpc_message msg = {
3867 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3868 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003869 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 };
3871
Bryan Schumaker7c513052011-03-24 17:12:24 +00003872 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873}
3874
3875static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3876{
3877 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04003878 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 int err;
3880
3881 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04003882 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003883 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04003884 if (err == 0) {
3885 struct nfs_client *clp = server->nfs_client;
3886
3887 spin_lock(&clp->cl_lock);
3888 clp->cl_lease_time = fsinfo->lease_time * HZ;
3889 clp->cl_last_renewal = now;
3890 spin_unlock(&clp->cl_lock);
3891 break;
3892 }
3893 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894 } while (exception.retry);
3895 return err;
3896}
3897
3898static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3899{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003900 int error;
3901
Trond Myklebust0e574af2005-10-27 22:12:38 -04003902 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003903 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003904 if (error == 0) {
3905 /* block layout checks this! */
3906 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003907 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003908 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003909
3910 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911}
3912
3913static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3914 struct nfs_pathconf *pathconf)
3915{
3916 struct nfs4_pathconf_arg args = {
3917 .fh = fhandle,
3918 .bitmask = server->attr_bitmask,
3919 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003920 struct nfs4_pathconf_res res = {
3921 .pathconf = pathconf,
3922 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 struct rpc_message msg = {
3924 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3925 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003926 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 };
3928
3929 /* None of the pathconf attributes are mandatory to implement */
3930 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3931 memset(pathconf, 0, sizeof(*pathconf));
3932 return 0;
3933 }
3934
Trond Myklebust0e574af2005-10-27 22:12:38 -04003935 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003936 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937}
3938
3939static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3940 struct nfs_pathconf *pathconf)
3941{
3942 struct nfs4_exception exception = { };
3943 int err;
3944
3945 do {
3946 err = nfs4_handle_exception(server,
3947 _nfs4_proc_pathconf(server, fhandle, pathconf),
3948 &exception);
3949 } while (exception.retry);
3950 return err;
3951}
3952
Trond Myklebust5521abf2013-03-16 20:54:34 -04003953int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04003954 const struct nfs_open_context *ctx,
3955 const struct nfs_lock_context *l_ctx,
3956 fmode_t fmode)
3957{
3958 const struct nfs_lockowner *lockowner = NULL;
3959
3960 if (l_ctx != NULL)
3961 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003962 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04003963}
3964EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
3965
Trond Myklebust5521abf2013-03-16 20:54:34 -04003966static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
3967 const struct nfs_open_context *ctx,
3968 const struct nfs_lock_context *l_ctx,
3969 fmode_t fmode)
3970{
3971 nfs4_stateid current_stateid;
3972
3973 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode))
3974 return false;
3975 return nfs4_stateid_match(stateid, &current_stateid);
3976}
3977
3978static bool nfs4_error_stateid_expired(int err)
3979{
3980 switch (err) {
3981 case -NFS4ERR_DELEG_REVOKED:
3982 case -NFS4ERR_ADMIN_REVOKED:
3983 case -NFS4ERR_BAD_STATEID:
3984 case -NFS4ERR_STALE_STATEID:
3985 case -NFS4ERR_OLD_STATEID:
3986 case -NFS4ERR_OPENMODE:
3987 case -NFS4ERR_EXPIRED:
3988 return true;
3989 }
3990 return false;
3991}
3992
Benny Halevyd20581a2011-05-22 19:52:03 +03003993void __nfs4_read_done_cb(struct nfs_read_data *data)
3994{
Fred Isamancd841602012-04-20 14:47:44 -04003995 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003996}
3997
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003998static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999{
Fred Isamancd841602012-04-20 14:47:44 -04004000 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004002 trace_nfs4_read(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004003 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004004 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05004005 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004007
Benny Halevyd20581a2011-05-22 19:52:03 +03004008 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05004010 renew_lease(server, data->timestamp);
4011 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012}
4013
Trond Myklebust5521abf2013-03-16 20:54:34 -04004014static bool nfs4_read_stateid_changed(struct rpc_task *task,
4015 struct nfs_readargs *args)
4016{
4017
4018 if (!nfs4_error_stateid_expired(task->tk_status) ||
4019 nfs4_stateid_is_current(&args->stateid,
4020 args->context,
4021 args->lock_context,
4022 FMODE_READ))
4023 return false;
4024 rpc_restart_call_prepare(task);
4025 return true;
4026}
4027
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004028static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
4029{
4030
4031 dprintk("--> %s\n", __func__);
4032
4033 if (!nfs4_sequence_done(task, &data->res.seq_res))
4034 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004035 if (nfs4_read_stateid_changed(task, &data->args))
4036 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03004037 return data->read_done_cb ? data->read_done_cb(task, data) :
4038 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004039}
4040
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004041static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004044 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004045 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Chuck Levera9c92d62013-08-09 12:48:18 -04004046 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047}
4048
NeilBrownef1820f2013-09-04 17:04:49 +10004049static int nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004050{
Trond Myklebust9b206142013-03-17 15:52:00 -04004051 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004052 &data->args.seq_args,
4053 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004054 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004055 return 0;
4056 if (nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4057 data->args.lock_context, FMODE_READ) == -EIO)
4058 return -EIO;
4059 if (unlikely(test_bit(NFS_CONTEXT_BAD, &data->args.context->flags)))
4060 return -EIO;
4061 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062}
4063
Fred Isamanb029bc92011-03-03 15:13:42 +00004064static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065{
Fred Isamancd841602012-04-20 14:47:44 -04004066 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004068 trace_nfs4_write(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004069 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004070 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004071 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004073 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04004075 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004076 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004077 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078}
4079
Trond Myklebust5521abf2013-03-16 20:54:34 -04004080static bool nfs4_write_stateid_changed(struct rpc_task *task,
4081 struct nfs_writeargs *args)
4082{
4083
4084 if (!nfs4_error_stateid_expired(task->tk_status) ||
4085 nfs4_stateid_is_current(&args->stateid,
4086 args->context,
4087 args->lock_context,
4088 FMODE_WRITE))
4089 return false;
4090 rpc_restart_call_prepare(task);
4091 return true;
4092}
4093
Fred Isamanb029bc92011-03-03 15:13:42 +00004094static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
4095{
4096 if (!nfs4_sequence_done(task, &data->res.seq_res))
4097 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004098 if (nfs4_write_stateid_changed(task, &data->args))
4099 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03004100 return data->write_done_cb ? data->write_done_cb(task, data) :
4101 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00004102}
4103
Trond Myklebust5a37f852012-04-28 14:55:16 -04004104static
4105bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00004106{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004107 const struct nfs_pgio_header *hdr = data->header;
4108
4109 /* Don't request attributes for pNFS or O_DIRECT writes */
4110 if (data->ds_clp != NULL || hdr->dreq != NULL)
4111 return false;
4112 /* Otherwise, request attributes if and only if we don't hold
4113 * a delegation
4114 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004115 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004116}
Fred Isamana69aef12011-03-03 15:13:47 +00004117
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004118static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119{
Fred Isamancd841602012-04-20 14:47:44 -04004120 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004121
Trond Myklebust5a37f852012-04-28 14:55:16 -04004122 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00004123 data->args.bitmask = NULL;
4124 data->res.fattr = NULL;
4125 } else
4126 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004127
Fred Isamanb029bc92011-03-03 15:13:42 +00004128 if (!data->write_done_cb)
4129 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004130 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 data->timestamp = jiffies;
4132
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004133 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004134 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135}
4136
NeilBrownef1820f2013-09-04 17:04:49 +10004137static int nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04004138{
Trond Myklebust9b206142013-03-17 15:52:00 -04004139 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004140 &data->args.seq_args,
4141 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004142 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004143 return 0;
4144 if (nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4145 data->args.lock_context, FMODE_WRITE) == -EIO)
4146 return -EIO;
4147 if (unlikely(test_bit(NFS_CONTEXT_BAD, &data->args.context->flags)))
4148 return -EIO;
4149 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150}
4151
Fred Isaman0b7c0152012-04-20 14:47:39 -04004152static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4153{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004154 nfs4_setup_sequence(NFS_SERVER(data->inode),
4155 &data->args.seq_args,
4156 &data->res.seq_res,
4157 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158}
4159
Fred Isaman0b7c0152012-04-20 14:47:39 -04004160static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004163
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004164 trace_nfs4_commit(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004165 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004166 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004167 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004169 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170}
4171
Fred Isaman0b7c0152012-04-20 14:47:39 -04004172static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004173{
4174 if (!nfs4_sequence_done(task, &data->res.seq_res))
4175 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004176 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004177}
4178
Fred Isaman0b7c0152012-04-20 14:47:39 -04004179static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004181 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004182
Fred Isaman0b7c0152012-04-20 14:47:39 -04004183 if (data->commit_done_cb == NULL)
4184 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004185 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004186 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004187 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188}
4189
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004190struct nfs4_renewdata {
4191 struct nfs_client *client;
4192 unsigned long timestamp;
4193};
4194
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195/*
4196 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4197 * standalone procedure for queueing an asynchronous RENEW.
4198 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004199static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004200{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004201 struct nfs4_renewdata *data = calldata;
4202 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004203
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004204 if (atomic_read(&clp->cl_count) > 1)
4205 nfs4_schedule_state_renewal(clp);
4206 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004207 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004208}
4209
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004210static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004212 struct nfs4_renewdata *data = calldata;
4213 struct nfs_client *clp = data->client;
4214 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004216 trace_nfs4_renew_async(clp, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04004218 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004219 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4220 return;
4221 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004222 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004223 return;
4224 }
4225 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004227 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228}
4229
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004230static const struct rpc_call_ops nfs4_renew_ops = {
4231 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004232 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004233};
4234
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004235static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236{
4237 struct rpc_message msg = {
4238 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4239 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004240 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004242 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004244 if (renew_flags == 0)
4245 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004246 if (!atomic_inc_not_zero(&clp->cl_count))
4247 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004248 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004249 if (data == NULL)
4250 return -ENOMEM;
4251 data->client = clp;
4252 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004253 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004254 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255}
4256
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004257static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258{
4259 struct rpc_message msg = {
4260 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4261 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004262 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 };
4264 unsigned long now = jiffies;
4265 int status;
4266
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004267 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 if (status < 0)
4269 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004270 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 return 0;
4272}
4273
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004274static inline int nfs4_server_supports_acls(struct nfs_server *server)
4275{
4276 return (server->caps & NFS_CAP_ACLS)
4277 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
4278 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
4279}
4280
Trond Myklebust21f498c2012-08-24 10:59:25 -04004281/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4282 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004283 * the stack.
4284 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004285#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004286
Neil Hormane9e3d722011-03-04 19:26:03 -05004287static int buf_to_pages_noslab(const void *buf, size_t buflen,
4288 struct page **pages, unsigned int *pgbase)
4289{
4290 struct page *newpage, **spages;
4291 int rc = 0;
4292 size_t len;
4293 spages = pages;
4294
4295 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004296 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004297 newpage = alloc_page(GFP_KERNEL);
4298
4299 if (newpage == NULL)
4300 goto unwind;
4301 memcpy(page_address(newpage), buf, len);
4302 buf += len;
4303 buflen -= len;
4304 *pages++ = newpage;
4305 rc++;
4306 } while (buflen != 0);
4307
4308 return rc;
4309
4310unwind:
4311 for(; rc > 0; rc--)
4312 __free_page(spages[rc-1]);
4313 return -ENOMEM;
4314}
4315
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004316struct nfs4_cached_acl {
4317 int cached;
4318 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004319 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004320};
4321
4322static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004323{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004324 struct nfs_inode *nfsi = NFS_I(inode);
4325
4326 spin_lock(&inode->i_lock);
4327 kfree(nfsi->nfs4_acl);
4328 nfsi->nfs4_acl = acl;
4329 spin_unlock(&inode->i_lock);
4330}
4331
4332static void nfs4_zap_acl_attr(struct inode *inode)
4333{
4334 nfs4_set_cached_acl(inode, NULL);
4335}
4336
4337static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4338{
4339 struct nfs_inode *nfsi = NFS_I(inode);
4340 struct nfs4_cached_acl *acl;
4341 int ret = -ENOENT;
4342
4343 spin_lock(&inode->i_lock);
4344 acl = nfsi->nfs4_acl;
4345 if (acl == NULL)
4346 goto out;
4347 if (buf == NULL) /* user is just asking for length */
4348 goto out_len;
4349 if (acl->cached == 0)
4350 goto out;
4351 ret = -ERANGE; /* see getxattr(2) man page */
4352 if (acl->len > buflen)
4353 goto out;
4354 memcpy(buf, acl->data, acl->len);
4355out_len:
4356 ret = acl->len;
4357out:
4358 spin_unlock(&inode->i_lock);
4359 return ret;
4360}
4361
Sachin Prabhu5794d212012-04-17 14:36:40 +01004362static 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 +00004363{
4364 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004365 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004366
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004367 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004368 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004369 if (acl == NULL)
4370 goto out;
4371 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004372 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004373 } else {
4374 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4375 if (acl == NULL)
4376 goto out;
4377 acl->cached = 0;
4378 }
4379 acl->len = acl_len;
4380out:
4381 nfs4_set_cached_acl(inode, acl);
4382}
4383
Andy Adamsonbf118a32011-12-07 11:55:27 -05004384/*
4385 * The getxattr API returns the required buffer length when called with a
4386 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4387 * the required buf. On a NULL buf, we send a page of data to the server
4388 * guessing that the ACL request can be serviced by a page. If so, we cache
4389 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4390 * the cache. If not so, we throw away the page, and cache the required
4391 * length. The next getxattr call will then produce another round trip to
4392 * the server, this time with the input buf of the required size.
4393 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004394static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004395{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004396 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004397 struct nfs_getaclargs args = {
4398 .fh = NFS_FH(inode),
4399 .acl_pages = pages,
4400 .acl_len = buflen,
4401 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004402 struct nfs_getaclres res = {
4403 .acl_len = buflen,
4404 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004405 struct rpc_message msg = {
4406 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4407 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004408 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004409 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004410 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4411 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004412
Andy Adamsonbf118a32011-12-07 11:55:27 -05004413 /* As long as we're doing a round trip to the server anyway,
4414 * let's be prepared for a page of acl data. */
4415 if (npages == 0)
4416 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004417 if (npages > ARRAY_SIZE(pages))
4418 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004419
Andy Adamsonbf118a32011-12-07 11:55:27 -05004420 for (i = 0; i < npages; i++) {
4421 pages[i] = alloc_page(GFP_KERNEL);
4422 if (!pages[i])
4423 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004424 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004425
4426 /* for decoding across pages */
4427 res.acl_scratch = alloc_page(GFP_KERNEL);
4428 if (!res.acl_scratch)
4429 goto out_free;
4430
Andy Adamsonbf118a32011-12-07 11:55:27 -05004431 args.acl_len = npages * PAGE_SIZE;
4432 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004433
Peng Taode040be2012-01-10 22:42:47 +08004434 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004435 __func__, buf, buflen, npages, args.acl_len);
4436 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4437 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004438 if (ret)
4439 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004440
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004441 /* Handle the case where the passed-in buffer is too short */
4442 if (res.acl_flags & NFS4_ACL_TRUNC) {
4443 /* Did the user only issue a request for the acl length? */
4444 if (buf == NULL)
4445 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004446 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004447 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004448 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004449 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004450 if (buf) {
4451 if (res.acl_len > buflen) {
4452 ret = -ERANGE;
4453 goto out_free;
4454 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004455 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004456 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004457out_ok:
4458 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004459out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004460 for (i = 0; i < npages; i++)
4461 if (pages[i])
4462 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004463 if (res.acl_scratch)
4464 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004465 return ret;
4466}
4467
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004468static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4469{
4470 struct nfs4_exception exception = { };
4471 ssize_t ret;
4472 do {
4473 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04004474 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004475 if (ret >= 0)
4476 break;
4477 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4478 } while (exception.retry);
4479 return ret;
4480}
4481
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004482static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4483{
4484 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004485 int ret;
4486
4487 if (!nfs4_server_supports_acls(server))
4488 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004489 ret = nfs_revalidate_inode(server, inode);
4490 if (ret < 0)
4491 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004492 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4493 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004494 ret = nfs4_read_cached_acl(inode, buf, buflen);
4495 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004496 /* -ENOENT is returned if there is no ACL or if there is an ACL
4497 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004498 return ret;
4499 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004500}
4501
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004502static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004503{
4504 struct nfs_server *server = NFS_SERVER(inode);
4505 struct page *pages[NFS4ACL_MAXPAGES];
4506 struct nfs_setaclargs arg = {
4507 .fh = NFS_FH(inode),
4508 .acl_pages = pages,
4509 .acl_len = buflen,
4510 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004511 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004512 struct rpc_message msg = {
4513 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4514 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004515 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004516 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004517 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004518 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004519
4520 if (!nfs4_server_supports_acls(server))
4521 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004522 if (npages > ARRAY_SIZE(pages))
4523 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004524 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4525 if (i < 0)
4526 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004527 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004528 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004529
4530 /*
4531 * Free each page after tx, so the only ref left is
4532 * held by the network stack
4533 */
4534 for (; i > 0; i--)
4535 put_page(pages[i-1]);
4536
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004537 /*
4538 * Acl update can result in inode attribute update.
4539 * so mark the attribute cache invalid.
4540 */
4541 spin_lock(&inode->i_lock);
4542 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4543 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004544 nfs_access_zap_cache(inode);
4545 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004546 return ret;
4547}
4548
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004549static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4550{
4551 struct nfs4_exception exception = { };
4552 int err;
4553 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004554 err = __nfs4_proc_set_acl(inode, buf, buflen);
4555 trace_nfs4_set_acl(inode, err);
4556 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004557 &exception);
4558 } while (exception.retry);
4559 return err;
4560}
4561
David Quigleyaa9c2662013-05-22 12:50:44 -04004562#ifdef CONFIG_NFS_V4_SECURITY_LABEL
4563static int _nfs4_get_security_label(struct inode *inode, void *buf,
4564 size_t buflen)
4565{
4566 struct nfs_server *server = NFS_SERVER(inode);
4567 struct nfs_fattr fattr;
4568 struct nfs4_label label = {0, 0, buflen, buf};
4569
4570 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4571 struct nfs4_getattr_arg args = {
4572 .fh = NFS_FH(inode),
4573 .bitmask = bitmask,
4574 };
4575 struct nfs4_getattr_res res = {
4576 .fattr = &fattr,
4577 .label = &label,
4578 .server = server,
4579 };
4580 struct rpc_message msg = {
4581 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4582 .rpc_argp = &args,
4583 .rpc_resp = &res,
4584 };
4585 int ret;
4586
4587 nfs_fattr_init(&fattr);
4588
4589 ret = rpc_call_sync(server->client, &msg, 0);
4590 if (ret)
4591 return ret;
4592 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4593 return -ENOENT;
4594 if (buflen < label.len)
4595 return -ERANGE;
4596 return 0;
4597}
4598
4599static int nfs4_get_security_label(struct inode *inode, void *buf,
4600 size_t buflen)
4601{
4602 struct nfs4_exception exception = { };
4603 int err;
4604
4605 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4606 return -EOPNOTSUPP;
4607
4608 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004609 err = _nfs4_get_security_label(inode, buf, buflen);
4610 trace_nfs4_get_security_label(inode, err);
4611 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004612 &exception);
4613 } while (exception.retry);
4614 return err;
4615}
4616
4617static int _nfs4_do_set_security_label(struct inode *inode,
4618 struct nfs4_label *ilabel,
4619 struct nfs_fattr *fattr,
4620 struct nfs4_label *olabel)
4621{
4622
4623 struct iattr sattr = {0};
4624 struct nfs_server *server = NFS_SERVER(inode);
4625 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4626 struct nfs_setattrargs args = {
4627 .fh = NFS_FH(inode),
4628 .iap = &sattr,
4629 .server = server,
4630 .bitmask = bitmask,
4631 .label = ilabel,
4632 };
4633 struct nfs_setattrres res = {
4634 .fattr = fattr,
4635 .label = olabel,
4636 .server = server,
4637 };
4638 struct rpc_message msg = {
4639 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
4640 .rpc_argp = &args,
4641 .rpc_resp = &res,
4642 };
4643 int status;
4644
4645 nfs4_stateid_copy(&args.stateid, &zero_stateid);
4646
4647 status = rpc_call_sync(server->client, &msg, 0);
4648 if (status)
4649 dprintk("%s failed: %d\n", __func__, status);
4650
4651 return status;
4652}
4653
4654static int nfs4_do_set_security_label(struct inode *inode,
4655 struct nfs4_label *ilabel,
4656 struct nfs_fattr *fattr,
4657 struct nfs4_label *olabel)
4658{
4659 struct nfs4_exception exception = { };
4660 int err;
4661
4662 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004663 err = _nfs4_do_set_security_label(inode, ilabel,
4664 fattr, olabel);
4665 trace_nfs4_set_security_label(inode, err);
4666 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004667 &exception);
4668 } while (exception.retry);
4669 return err;
4670}
4671
4672static int
4673nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4674{
4675 struct nfs4_label ilabel, *olabel = NULL;
4676 struct nfs_fattr fattr;
4677 struct rpc_cred *cred;
4678 struct inode *inode = dentry->d_inode;
4679 int status;
4680
4681 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4682 return -EOPNOTSUPP;
4683
4684 nfs_fattr_init(&fattr);
4685
4686 ilabel.pi = 0;
4687 ilabel.lfs = 0;
4688 ilabel.label = (char *)buf;
4689 ilabel.len = buflen;
4690
4691 cred = rpc_lookup_cred();
4692 if (IS_ERR(cred))
4693 return PTR_ERR(cred);
4694
4695 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4696 if (IS_ERR(olabel)) {
4697 status = -PTR_ERR(olabel);
4698 goto out;
4699 }
4700
4701 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4702 if (status == 0)
4703 nfs_setsecurity(inode, &fattr, olabel);
4704
4705 nfs4_label_free(olabel);
4706out:
4707 put_rpccred(cred);
4708 return status;
4709}
4710#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4711
4712
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004714nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004716 struct nfs_client *clp = server->nfs_client;
4717
4718 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 return 0;
4720 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004721 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004722 case -NFS4ERR_ADMIN_REVOKED:
4723 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004724 if (state == NULL)
4725 break;
4726 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004727 case -NFS4ERR_OPENMODE:
4728 if (state == NULL)
4729 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004730 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4731 goto stateid_invalid;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004732 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004733 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004734 if (state != NULL) {
4735 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4736 goto stateid_invalid;
4737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004739 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004740 nfs4_schedule_lease_recovery(clp);
4741 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004742#if defined(CONFIG_NFS_V4_1)
4743 case -NFS4ERR_BADSESSION:
4744 case -NFS4ERR_BADSLOT:
4745 case -NFS4ERR_BAD_HIGH_SLOT:
4746 case -NFS4ERR_DEADSESSION:
4747 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4748 case -NFS4ERR_SEQ_FALSE_RETRY:
4749 case -NFS4ERR_SEQ_MISORDERED:
4750 dprintk("%s ERROR %d, Reset session\n", __func__,
4751 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004752 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004753 task->tk_status = 0;
4754 return -EAGAIN;
4755#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004757 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004758 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4760 task->tk_status = 0;
4761 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004762 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 case -NFS4ERR_OLD_STATEID:
4764 task->tk_status = 0;
4765 return -EAGAIN;
4766 }
4767 task->tk_status = nfs4_map_errors(task->tk_status);
4768 return 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004769stateid_invalid:
4770 task->tk_status = -EIO;
4771 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004772wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004773 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004774 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4775 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4776 task->tk_status = 0;
4777 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778}
4779
Chuck Leverf0920752012-05-21 22:45:41 -04004780static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4781 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004782{
4783 __be32 verf[2];
4784
Chuck Lever2c820d92012-05-21 22:45:33 -04004785 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4786 /* An impossible timestamp guarantees this value
4787 * will never match a generated boot time. */
4788 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04004789 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04004790 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004791 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04004792 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
4793 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04004794 }
Chuck Levercd937102012-03-02 17:14:31 -05004795 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4796}
4797
Chuck Levere984a552012-09-14 17:24:21 -04004798static unsigned int
4799nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4800 char *buf, size_t len)
4801{
4802 unsigned int result;
4803
4804 rcu_read_lock();
4805 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4806 clp->cl_ipaddr,
4807 rpc_peeraddr2str(clp->cl_rpcclient,
4808 RPC_DISPLAY_ADDR),
4809 rpc_peeraddr2str(clp->cl_rpcclient,
4810 RPC_DISPLAY_PROTO));
4811 rcu_read_unlock();
4812 return result;
4813}
4814
4815static unsigned int
4816nfs4_init_uniform_client_string(const struct nfs_client *clp,
4817 char *buf, size_t len)
4818{
Trond Myklebust55b59292013-07-24 16:36:35 -04004819 const char *nodename = clp->cl_rpcclient->cl_nodename;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004820
4821 if (nfs4_client_id_uniquifier[0] != '\0')
Trond Myklebust55b59292013-07-24 16:36:35 -04004822 return scnprintf(buf, len, "Linux NFSv%u.%u %s/%s",
4823 clp->rpc_ops->version,
4824 clp->cl_minorversion,
4825 nfs4_client_id_uniquifier,
4826 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004827 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4828 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004829 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004830}
4831
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004832/**
4833 * nfs4_proc_setclientid - Negotiate client ID
4834 * @clp: state data structure
4835 * @program: RPC program for NFSv4 callback service
4836 * @port: IP port number for NFS4 callback service
4837 * @cred: RPC credential to use for this call
4838 * @res: where to place the result
4839 *
4840 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4841 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004842int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4843 unsigned short port, struct rpc_cred *cred,
4844 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845{
4846 nfs4_verifier sc_verifier;
4847 struct nfs4_setclientid setclientid = {
4848 .sc_verifier = &sc_verifier,
4849 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004850 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851 };
4852 struct rpc_message msg = {
4853 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4854 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004855 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004856 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004858 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859
Chuck Leverde734832012-07-11 16:30:50 -04004860 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004861 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004862 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4863 setclientid.sc_name_len =
4864 nfs4_init_uniform_client_string(clp,
4865 setclientid.sc_name,
4866 sizeof(setclientid.sc_name));
4867 else
4868 setclientid.sc_name_len =
4869 nfs4_init_nonuniform_client_string(clp,
4870 setclientid.sc_name,
4871 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004872 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004873 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004874 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Djalal Harounife2d5392013-06-18 17:58:12 +01004875 sizeof(setclientid.sc_netid), "%s",
Chuck Leverd4d3c502007-12-10 14:57:09 -05004876 rpc_peeraddr2str(clp->cl_rpcclient,
4877 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004878 rcu_read_unlock();
4879 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004880 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 clp->cl_ipaddr, port >> 8, port & 255);
4882
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004883 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4884 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4885 setclientid.sc_name_len, setclientid.sc_name);
4886 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004887 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004888 dprintk("NFS reply setclientid: %d\n", status);
4889 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890}
4891
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004892/**
4893 * nfs4_proc_setclientid_confirm - Confirm client ID
4894 * @clp: state data structure
4895 * @res: result of a previous SETCLIENTID
4896 * @cred: RPC credential to use for this call
4897 *
4898 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4899 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004900int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004901 struct nfs4_setclientid_res *arg,
4902 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904 struct rpc_message msg = {
4905 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004906 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004907 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 int status;
4910
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004911 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4912 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4913 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004914 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004915 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004916 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 return status;
4918}
4919
Trond Myklebustfe650402006-01-03 09:55:18 +01004920struct nfs4_delegreturndata {
4921 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004922 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004923 struct nfs_fh fh;
4924 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004925 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004926 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004927 int rpc_status;
4928};
4929
Trond Myklebustfe650402006-01-03 09:55:18 +01004930static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4931{
4932 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004933
Trond Myklebust14516c32010-07-31 14:29:06 -04004934 if (!nfs4_sequence_done(task, &data->res.seq_res))
4935 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004936
Trond Myklebustca8acf82013-08-13 10:36:56 -04004937 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004938 switch (task->tk_status) {
4939 case -NFS4ERR_STALE_STATEID:
4940 case -NFS4ERR_EXPIRED:
4941 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004942 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004943 break;
4944 default:
4945 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4946 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004947 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004948 return;
4949 }
4950 }
4951 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004952}
4953
4954static void nfs4_delegreturn_release(void *calldata)
4955{
Trond Myklebustfe650402006-01-03 09:55:18 +01004956 kfree(calldata);
4957}
4958
Andy Adamson938e1012009-04-01 09:22:28 -04004959static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4960{
4961 struct nfs4_delegreturndata *d_data;
4962
4963 d_data = (struct nfs4_delegreturndata *)data;
4964
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004965 nfs4_setup_sequence(d_data->res.server,
4966 &d_data->args.seq_args,
4967 &d_data->res.seq_res,
4968 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004969}
Andy Adamson938e1012009-04-01 09:22:28 -04004970
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004971static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004972 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01004973 .rpc_call_done = nfs4_delegreturn_done,
4974 .rpc_release = nfs4_delegreturn_release,
4975};
4976
Trond Myklebuste6f81072008-01-24 18:14:34 -05004977static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004978{
4979 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004980 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004981 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004982 struct rpc_message msg = {
4983 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4984 .rpc_cred = cred,
4985 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004986 struct rpc_task_setup task_setup_data = {
4987 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004988 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004989 .callback_ops = &nfs4_delegreturn_ops,
4990 .flags = RPC_TASK_ASYNC,
4991 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004992 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004993
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004994 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004995 if (data == NULL)
4996 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04004997 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004998 data->args.fhandle = &data->fh;
4999 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005000 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005001 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005002 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005003 data->res.fattr = &data->fattr;
5004 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005005 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005006 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005007 data->rpc_status = 0;
5008
Trond Myklebustc970aa82007-07-14 15:39:59 -04005009 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005010 msg.rpc_argp = &data->args;
5011 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005012 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005013 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005014 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005015 if (!issync)
5016 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005017 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005018 if (status != 0)
5019 goto out;
5020 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005021 if (status == 0)
5022 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5023 else
5024 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005025out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005026 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005027 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028}
5029
Trond Myklebuste6f81072008-01-24 18:14:34 -05005030int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031{
5032 struct nfs_server *server = NFS_SERVER(inode);
5033 struct nfs4_exception exception = { };
5034 int err;
5035 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005036 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Trond Myklebustca8acf82013-08-13 10:36:56 -04005037 trace_nfs4_delegreturn(inode, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 switch (err) {
5039 case -NFS4ERR_STALE_STATEID:
5040 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041 case 0:
5042 return 0;
5043 }
5044 err = nfs4_handle_exception(server, err, &exception);
5045 } while (exception.retry);
5046 return err;
5047}
5048
5049#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5050#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5051
5052/*
5053 * sleep, with exponential backoff, and retry the LOCK operation.
5054 */
5055static unsigned long
5056nfs4_set_lock_task_retry(unsigned long timeout)
5057{
Colin Cross416ad3c2013-05-06 23:50:06 +00005058 freezable_schedule_timeout_killable_unsafe(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 timeout <<= 1;
5060 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5061 return NFS4_LOCK_MAXTIMEOUT;
5062 return timeout;
5063}
5064
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5066{
5067 struct inode *inode = state->inode;
5068 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005069 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005070 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005072 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005074 struct nfs_lockt_res res = {
5075 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 };
5077 struct rpc_message msg = {
5078 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5079 .rpc_argp = &arg,
5080 .rpc_resp = &res,
5081 .rpc_cred = state->owner->so_cred,
5082 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 struct nfs4_lock_state *lsp;
5084 int status;
5085
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005086 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005087 status = nfs4_set_lock_state(state, request);
5088 if (status != 0)
5089 goto out;
5090 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005091 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005092 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005093 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005094 switch (status) {
5095 case 0:
5096 request->fl_type = F_UNLCK;
5097 break;
5098 case -NFS4ERR_DENIED:
5099 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005101 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005102 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005103out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 return status;
5105}
5106
5107static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5108{
5109 struct nfs4_exception exception = { };
5110 int err;
5111
5112 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005113 err = _nfs4_proc_getlk(state, cmd, request);
5114 trace_nfs4_get_lock(request, state, cmd, err);
5115 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 &exception);
5117 } while (exception.retry);
5118 return err;
5119}
5120
5121static int do_vfs_lock(struct file *file, struct file_lock *fl)
5122{
5123 int res = 0;
5124 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5125 case FL_POSIX:
5126 res = posix_lock_file_wait(file, fl);
5127 break;
5128 case FL_FLOCK:
5129 res = flock_lock_file_wait(file, fl);
5130 break;
5131 default:
5132 BUG();
5133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134 return res;
5135}
5136
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005137struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005138 struct nfs_locku_args arg;
5139 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005140 struct nfs4_lock_state *lsp;
5141 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005142 struct file_lock fl;
5143 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005144 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005145};
5146
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005147static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5148 struct nfs_open_context *ctx,
5149 struct nfs4_lock_state *lsp,
5150 struct nfs_seqid *seqid)
5151{
5152 struct nfs4_unlockdata *p;
5153 struct inode *inode = lsp->ls_state->inode;
5154
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005155 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005156 if (p == NULL)
5157 return NULL;
5158 p->arg.fh = NFS_FH(inode);
5159 p->arg.fl = &p->fl;
5160 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005161 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005162 p->arg.stateid = &lsp->ls_stateid;
5163 p->lsp = lsp;
5164 atomic_inc(&lsp->ls_count);
5165 /* Ensure we don't close file until we're done freeing locks! */
5166 p->ctx = get_nfs_open_context(ctx);
5167 memcpy(&p->fl, fl, sizeof(p->fl));
5168 p->server = NFS_SERVER(inode);
5169 return p;
5170}
5171
Trond Myklebust06f814a2006-01-03 09:55:07 +01005172static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005173{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005174 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005175 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005176 nfs4_put_lock_state(calldata->lsp);
5177 put_nfs_open_context(calldata->ctx);
5178 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005179}
5180
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005181static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005182{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005183 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005184
Trond Myklebust14516c32010-07-31 14:29:06 -04005185 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5186 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005187 switch (task->tk_status) {
5188 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05005189 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
5190 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005191 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005192 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005193 case -NFS4ERR_BAD_STATEID:
5194 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005195 case -NFS4ERR_STALE_STATEID:
5196 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005197 break;
5198 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005199 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005200 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005201 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005202 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005203}
5204
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005205static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005206{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005207 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005209 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005210 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04005211 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005212 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005213 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005214 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005215 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005216 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005217 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005218 &calldata->res.seq_res,
5219 task) != 0)
5220 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005221 return;
5222out_no_action:
5223 task->tk_action = NULL;
5224out_wait:
5225 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226}
5227
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005228static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005229 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005230 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005231 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005232};
5233
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005234static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5235 struct nfs_open_context *ctx,
5236 struct nfs4_lock_state *lsp,
5237 struct nfs_seqid *seqid)
5238{
5239 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005240 struct rpc_message msg = {
5241 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5242 .rpc_cred = ctx->cred,
5243 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005244 struct rpc_task_setup task_setup_data = {
5245 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005246 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005247 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005248 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005249 .flags = RPC_TASK_ASYNC,
5250 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005251
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005252 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5253 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5254
Frank Filz137d6ac2007-07-09 15:32:29 -07005255 /* Ensure this is an unlock - when canceling a lock, the
5256 * canceled lock is passed in, and it won't be an unlock.
5257 */
5258 fl->fl_type = F_UNLCK;
5259
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005260 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5261 if (data == NULL) {
5262 nfs_free_seqid(seqid);
5263 return ERR_PTR(-ENOMEM);
5264 }
5265
Chuck Levera9c92d62013-08-09 12:48:18 -04005266 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005267 msg.rpc_argp = &data->arg;
5268 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005269 task_setup_data.callback_data = data;
5270 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005271}
5272
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5274{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005275 struct inode *inode = state->inode;
5276 struct nfs4_state_owner *sp = state->owner;
5277 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005278 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005279 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005280 struct rpc_task *task;
5281 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005282 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283
Trond Myklebust9b073572006-06-29 16:38:34 -04005284 status = nfs4_set_lock_state(state, request);
5285 /* Unlock _before_ we do the RPC call */
5286 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005287 /* Exclude nfs_delegation_claim_locks() */
5288 mutex_lock(&sp->so_delegreturn_mutex);
5289 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005290 down_read(&nfsi->rwsem);
5291 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
5292 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005293 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005294 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005295 }
5296 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005297 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005298 if (status != 0)
5299 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005300 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005301 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005302 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5303 goto out;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005304 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005305 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005306 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04005307 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005308 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005309 status = PTR_ERR(task);
5310 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005311 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005312 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005313 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005314out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005315 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005316 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005317 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318}
5319
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005320struct nfs4_lockdata {
5321 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005322 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005323 struct nfs4_lock_state *lsp;
5324 struct nfs_open_context *ctx;
5325 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005326 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005327 int rpc_status;
5328 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005329 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005330};
5331
5332static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005333 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5334 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005335{
5336 struct nfs4_lockdata *p;
5337 struct inode *inode = lsp->ls_state->inode;
5338 struct nfs_server *server = NFS_SERVER(inode);
5339
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005340 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005341 if (p == NULL)
5342 return NULL;
5343
5344 p->arg.fh = NFS_FH(inode);
5345 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005346 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005347 if (p->arg.open_seqid == NULL)
5348 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005349 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005350 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005351 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005352 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04005353 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005354 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005355 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005356 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005357 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005358 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005359 atomic_inc(&lsp->ls_count);
5360 p->ctx = get_nfs_open_context(ctx);
5361 memcpy(&p->fl, fl, sizeof(p->fl));
5362 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005363out_free_seqid:
5364 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005365out_free:
5366 kfree(p);
5367 return NULL;
5368}
5369
5370static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5371{
5372 struct nfs4_lockdata *data = calldata;
5373 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374
Harvey Harrison3110ff82008-05-02 13:42:44 -07005375 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005376 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005377 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005378 /* Do we need to do an open_to_lock_owner? */
5379 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005380 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005381 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005382 }
Trond Myklebust8e472f32013-04-20 01:30:53 -04005383 data->arg.open_stateid = &state->open_stateid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005384 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005385 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005386 } else
5387 data->arg.new_lock_owner = 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04005388 if (!nfs4_valid_open_stateid(state)) {
5389 data->rpc_status = -EBADF;
5390 task->tk_action = NULL;
5391 goto out_release_open_seqid;
5392 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005393 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005394 if (nfs4_setup_sequence(data->server,
5395 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005396 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005397 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04005398 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04005399out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005400 nfs_release_seqid(data->arg.open_seqid);
5401out_release_lock_seqid:
5402 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005403out_wait:
5404 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005405 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005406}
5407
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005408static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5409{
5410 struct nfs4_lockdata *data = calldata;
5411
Harvey Harrison3110ff82008-05-02 13:42:44 -07005412 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005413
Trond Myklebust14516c32010-07-31 14:29:06 -04005414 if (!nfs4_sequence_done(task, &data->res.seq_res))
5415 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04005416
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005417 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005418 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005419 if (data->rpc_status == 0)
5420 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
5421 else
5422 goto out;
5423 }
5424 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05005425 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005426 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04005427 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005428 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005429out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07005430 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005431}
5432
5433static void nfs4_lock_release(void *calldata)
5434{
5435 struct nfs4_lockdata *data = calldata;
5436
Harvey Harrison3110ff82008-05-02 13:42:44 -07005437 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005438 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005439 if (data->cancelled != 0) {
5440 struct rpc_task *task;
5441 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5442 data->arg.lock_seqid);
5443 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08005444 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005445 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005446 } else
5447 nfs_free_seqid(data->arg.lock_seqid);
5448 nfs4_put_lock_state(data->lsp);
5449 put_nfs_open_context(data->ctx);
5450 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005451 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005452}
5453
5454static const struct rpc_call_ops nfs4_lock_ops = {
5455 .rpc_call_prepare = nfs4_lock_prepare,
5456 .rpc_call_done = nfs4_lock_done,
5457 .rpc_release = nfs4_lock_release,
5458};
5459
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005460static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5461{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005462 switch (error) {
5463 case -NFS4ERR_ADMIN_REVOKED:
5464 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05005465 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005466 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04005467 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05005468 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005469 break;
5470 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005471 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05005472 case -NFS4ERR_EXPIRED:
5473 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005474 };
5475}
5476
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005477static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005478{
5479 struct nfs4_lockdata *data;
5480 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005481 struct rpc_message msg = {
5482 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5483 .rpc_cred = state->owner->so_cred,
5484 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005485 struct rpc_task_setup task_setup_data = {
5486 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005487 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005488 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005489 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005490 .flags = RPC_TASK_ASYNC,
5491 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005492 int ret;
5493
Harvey Harrison3110ff82008-05-02 13:42:44 -07005494 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005495 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005496 fl->fl_u.nfs4_fl.owner,
5497 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005498 if (data == NULL)
5499 return -ENOMEM;
5500 if (IS_SETLKW(cmd))
5501 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04005502 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005503 msg.rpc_argp = &data->arg;
5504 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005505 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005506 if (recovery_type > NFS_LOCK_NEW) {
5507 if (recovery_type == NFS_LOCK_RECLAIM)
5508 data->arg.reclaim = NFS_LOCK_RECLAIM;
5509 nfs4_set_sequence_privileged(&data->arg.seq_args);
5510 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04005511 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005512 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005513 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005514 ret = nfs4_wait_for_completion_rpc_task(task);
5515 if (ret == 0) {
5516 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005517 if (ret)
5518 nfs4_handle_setlk_error(data->server, data->lsp,
5519 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005520 } else
5521 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005522 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005523 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005524 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525}
5526
5527static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5528{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005529 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005530 struct nfs4_exception exception = {
5531 .inode = state->inode,
5532 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005533 int err;
5534
5535 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005536 /* Cache the lock if possible... */
5537 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5538 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005539 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005540 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
Trond Myklebust168667c2010-10-19 19:47:49 -04005541 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00005542 break;
5543 nfs4_handle_exception(server, err, &exception);
5544 } while (exception.retry);
5545 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546}
5547
5548static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5549{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005550 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005551 struct nfs4_exception exception = {
5552 .inode = state->inode,
5553 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005554 int err;
5555
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005556 err = nfs4_set_lock_state(state, request);
5557 if (err != 0)
5558 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04005559 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10005560 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
5561 return 0;
5562 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005563 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005564 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5565 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005566 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005567 trace_nfs4_lock_expired(request, state, F_SETLK, err);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005568 switch (err) {
5569 default:
5570 goto out;
5571 case -NFS4ERR_GRACE:
5572 case -NFS4ERR_DELAY:
5573 nfs4_handle_exception(server, err, &exception);
5574 err = 0;
5575 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005576 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005577out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00005578 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579}
5580
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005581#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005582/**
5583 * nfs41_check_expired_locks - possibly free a lock stateid
5584 *
5585 * @state: NFSv4 state for an inode
5586 *
5587 * Returns NFS_OK if recovery for this stateid is now finished.
5588 * Otherwise a negative NFS4ERR value is returned.
5589 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005590static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005591{
Chuck Levereb64cf92012-07-11 16:30:05 -04005592 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005593 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005594 struct nfs_server *server = NFS_SERVER(state->inode);
5595
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005596 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04005597 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005598 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
5599
5600 status = nfs41_test_stateid(server,
5601 &lsp->ls_stateid,
5602 cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04005603 trace_nfs4_test_lock_stateid(state, lsp, status);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005604 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005605 /* Free the stateid unless the server
5606 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04005607 if (status != -NFS4ERR_BAD_STATEID)
5608 nfs41_free_stateid(server,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005609 &lsp->ls_stateid,
5610 cred);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005611 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005612 ret = status;
5613 }
5614 }
5615 };
5616
5617 return ret;
5618}
5619
5620static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5621{
5622 int status = NFS_OK;
5623
5624 if (test_bit(LK_STATE_IN_USE, &state->flags))
5625 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005626 if (status != NFS_OK)
5627 status = nfs4_lock_expired(state, request);
5628 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005629}
5630#endif
5631
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5633{
Trond Myklebust9a99af492013-02-04 20:17:49 -05005634 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005635 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005636 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005637 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005638 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005640 if ((fl_flags & FL_POSIX) &&
5641 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5642 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005643 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005644 status = nfs4_set_lock_state(state, request);
5645 if (status != 0)
5646 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005647 request->fl_flags |= FL_ACCESS;
5648 status = do_vfs_lock(request->fl_file, request);
5649 if (status < 0)
5650 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005651 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005652 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005653 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005654 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005655 request->fl_flags = fl_flags & ~FL_SLEEP;
5656 status = do_vfs_lock(request->fl_file, request);
5657 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005658 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05005659 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5660 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005661 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005662 if (status != 0)
Trond Myklebust9a99af492013-02-04 20:17:49 -05005663 goto out;
5664 down_read(&nfsi->rwsem);
5665 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5666 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005667 goto out_unlock;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005668 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005669 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005670 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005671 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005672 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5673 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005674out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005675 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005676out:
5677 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678 return status;
5679}
5680
5681static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5682{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005683 struct nfs4_exception exception = {
5684 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005685 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005686 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687 int err;
5688
5689 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005690 err = _nfs4_proc_setlk(state, cmd, request);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005691 trace_nfs4_set_lock(request, state, cmd, err);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005692 if (err == -NFS4ERR_DENIED)
5693 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005695 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696 } while (exception.retry);
5697 return err;
5698}
5699
5700static int
5701nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5702{
5703 struct nfs_open_context *ctx;
5704 struct nfs4_state *state;
5705 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5706 int status;
5707
5708 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005709 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710 state = ctx->state;
5711
5712 if (request->fl_start < 0 || request->fl_end < 0)
5713 return -EINVAL;
5714
Trond Myklebustd9531262009-07-21 19:22:38 -04005715 if (IS_GETLK(cmd)) {
5716 if (state != NULL)
5717 return nfs4_proc_getlk(state, F_GETLK, request);
5718 return 0;
5719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720
5721 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5722 return -EINVAL;
5723
Trond Myklebustd9531262009-07-21 19:22:38 -04005724 if (request->fl_type == F_UNLCK) {
5725 if (state != NULL)
5726 return nfs4_proc_unlck(state, cmd, request);
5727 return 0;
5728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729
Trond Myklebustd9531262009-07-21 19:22:38 -04005730 if (state == NULL)
5731 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005732 /*
5733 * Don't rely on the VFS having checked the file open mode,
5734 * since it won't do this for flock() locks.
5735 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005736 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005737 case F_RDLCK:
5738 if (!(filp->f_mode & FMODE_READ))
5739 return -EBADF;
5740 break;
5741 case F_WRLCK:
5742 if (!(filp->f_mode & FMODE_WRITE))
5743 return -EBADF;
5744 }
5745
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746 do {
5747 status = nfs4_proc_setlk(state, cmd, request);
5748 if ((status != -EAGAIN) || IS_SETLK(cmd))
5749 break;
5750 timeout = nfs4_set_lock_task_retry(timeout);
5751 status = -ERESTARTSYS;
5752 if (signalled())
5753 break;
5754 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755 return status;
5756}
5757
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005758int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05005759{
5760 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05005761 int err;
5762
5763 err = nfs4_set_lock_state(state, fl);
5764 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005765 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04005766 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005767 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05005768}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005769
Trond Myklebustcf470c32012-03-07 13:49:12 -05005770struct nfs_release_lockowner_data {
5771 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005772 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005773 struct nfs_release_lockowner_args args;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005774 struct nfs4_sequence_args seq_args;
5775 struct nfs4_sequence_res seq_res;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005776};
5777
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005778static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
5779{
5780 struct nfs_release_lockowner_data *data = calldata;
5781 nfs40_setup_sequence(data->server,
5782 &data->seq_args, &data->seq_res, task);
5783}
5784
5785static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
5786{
5787 struct nfs_release_lockowner_data *data = calldata;
5788 nfs40_sequence_done(task, &data->seq_res);
5789}
5790
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005791static void nfs4_release_lockowner_release(void *calldata)
5792{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005793 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005794 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005795 kfree(calldata);
5796}
5797
Trond Myklebust17280172012-03-11 13:11:00 -04005798static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005799 .rpc_call_prepare = nfs4_release_lockowner_prepare,
5800 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005801 .rpc_release = nfs4_release_lockowner_release,
5802};
5803
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04005804static int nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005805{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005806 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005807 struct rpc_message msg = {
5808 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5809 };
5810
5811 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005812 return -EINVAL;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005813
Trond Myklebustcf470c32012-03-07 13:49:12 -05005814 data = kmalloc(sizeof(*data), GFP_NOFS);
5815 if (!data)
5816 return -ENOMEM;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005817 nfs4_init_sequence(&data->seq_args, &data->seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05005818 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005819 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005820 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5821 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5822 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005823
Trond Myklebustcf470c32012-03-07 13:49:12 -05005824 msg.rpc_argp = &data->args;
5825 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5826 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005827}
5828
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005829#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5830
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005831static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5832 const void *buf, size_t buflen,
5833 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005834{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005835 if (strcmp(key, "") != 0)
5836 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005837
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005838 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005839}
5840
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005841static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5842 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005843{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005844 if (strcmp(key, "") != 0)
5845 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005846
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005847 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005848}
5849
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005850static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5851 size_t list_len, const char *name,
5852 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005853{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005854 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005855
J. Bruce Fields096455a2006-03-20 23:23:42 -05005856 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5857 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005858
5859 if (list && len <= list_len)
5860 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005861 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005862}
5863
David Quigleyc9bccef2013-05-22 12:50:45 -04005864#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5865static inline int nfs4_server_supports_labels(struct nfs_server *server)
5866{
5867 return server->caps & NFS_CAP_SECURITY_LABEL;
5868}
5869
5870static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
5871 const void *buf, size_t buflen,
5872 int flags, int type)
5873{
5874 if (security_ismaclabel(key))
5875 return nfs4_set_security_label(dentry, buf, buflen);
5876
5877 return -EOPNOTSUPP;
5878}
5879
5880static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
5881 void *buf, size_t buflen, int type)
5882{
5883 if (security_ismaclabel(key))
5884 return nfs4_get_security_label(dentry->d_inode, buf, buflen);
5885 return -EOPNOTSUPP;
5886}
5887
5888static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
5889 size_t list_len, const char *name,
5890 size_t name_len, int type)
5891{
5892 size_t len = 0;
5893
5894 if (nfs_server_capable(dentry->d_inode, NFS_CAP_SECURITY_LABEL)) {
5895 len = security_inode_listsecurity(dentry->d_inode, NULL, 0);
5896 if (list && len <= list_len)
5897 security_inode_listsecurity(dentry->d_inode, list, len);
5898 }
5899 return len;
5900}
5901
5902static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
5903 .prefix = XATTR_SECURITY_PREFIX,
5904 .list = nfs4_xattr_list_nfs4_label,
5905 .get = nfs4_xattr_get_nfs4_label,
5906 .set = nfs4_xattr_set_nfs4_label,
5907};
5908#endif
5909
5910
Andy Adamson533eb462011-06-13 18:25:56 -04005911/*
5912 * nfs_fhget will use either the mounted_on_fileid or the fileid
5913 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005914static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5915{
Andy Adamson533eb462011-06-13 18:25:56 -04005916 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5917 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5918 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005919 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005920 return;
5921
5922 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005923 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005924 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5925 fattr->nlink = 2;
5926}
5927
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005928static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5929 const struct qstr *name,
5930 struct nfs4_fs_locations *fs_locations,
5931 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005932{
5933 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04005934 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005935 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005936 };
5937 struct nfs4_fs_locations_arg args = {
5938 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005939 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005940 .page = page,
5941 .bitmask = bitmask,
5942 };
Benny Halevy22958462009-04-01 09:22:02 -04005943 struct nfs4_fs_locations_res res = {
5944 .fs_locations = fs_locations,
5945 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005946 struct rpc_message msg = {
5947 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5948 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005949 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005950 };
5951 int status;
5952
Harvey Harrison3110ff82008-05-02 13:42:44 -07005953 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005954
5955 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5956 * is not supported */
5957 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5958 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5959 else
5960 bitmask[0] |= FATTR4_WORD0_FILEID;
5961
Trond Myklebustc228fd32007-01-13 02:28:11 -05005962 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005963 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005964 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005965 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005966 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005967 return status;
5968}
5969
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005970int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5971 const struct qstr *name,
5972 struct nfs4_fs_locations *fs_locations,
5973 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005974{
5975 struct nfs4_exception exception = { };
5976 int err;
5977 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04005978 err = _nfs4_proc_fs_locations(client, dir, name,
5979 fs_locations, page);
5980 trace_nfs4_get_fs_locations(dir, name, err);
5981 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005982 &exception);
5983 } while (exception.retry);
5984 return err;
5985}
5986
Chuck Leverb03d7352013-10-17 14:12:50 -04005987/*
5988 * This operation also signals the server that this client is
5989 * performing migration recovery. The server can stop returning
5990 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
5991 * appended to this compound to identify the client ID which is
5992 * performing recovery.
5993 */
5994static int _nfs40_proc_get_locations(struct inode *inode,
5995 struct nfs4_fs_locations *locations,
5996 struct page *page, struct rpc_cred *cred)
5997{
5998 struct nfs_server *server = NFS_SERVER(inode);
5999 struct rpc_clnt *clnt = server->client;
6000 u32 bitmask[2] = {
6001 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6002 };
6003 struct nfs4_fs_locations_arg args = {
6004 .clientid = server->nfs_client->cl_clientid,
6005 .fh = NFS_FH(inode),
6006 .page = page,
6007 .bitmask = bitmask,
6008 .migration = 1, /* skip LOOKUP */
6009 .renew = 1, /* append RENEW */
6010 };
6011 struct nfs4_fs_locations_res res = {
6012 .fs_locations = locations,
6013 .migration = 1,
6014 .renew = 1,
6015 };
6016 struct rpc_message msg = {
6017 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6018 .rpc_argp = &args,
6019 .rpc_resp = &res,
6020 .rpc_cred = cred,
6021 };
6022 unsigned long now = jiffies;
6023 int status;
6024
6025 nfs_fattr_init(&locations->fattr);
6026 locations->server = server;
6027 locations->nlocations = 0;
6028
6029 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6030 nfs4_set_sequence_privileged(&args.seq_args);
6031 status = nfs4_call_sync_sequence(clnt, server, &msg,
6032 &args.seq_args, &res.seq_res);
6033 if (status)
6034 return status;
6035
6036 renew_lease(server, now);
6037 return 0;
6038}
6039
6040#ifdef CONFIG_NFS_V4_1
6041
6042/*
6043 * This operation also signals the server that this client is
6044 * performing migration recovery. The server can stop asserting
6045 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6046 * performing this operation is identified in the SEQUENCE
6047 * operation in this compound.
6048 *
6049 * When the client supports GETATTR(fs_locations_info), it can
6050 * be plumbed in here.
6051 */
6052static int _nfs41_proc_get_locations(struct inode *inode,
6053 struct nfs4_fs_locations *locations,
6054 struct page *page, struct rpc_cred *cred)
6055{
6056 struct nfs_server *server = NFS_SERVER(inode);
6057 struct rpc_clnt *clnt = server->client;
6058 u32 bitmask[2] = {
6059 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6060 };
6061 struct nfs4_fs_locations_arg args = {
6062 .fh = NFS_FH(inode),
6063 .page = page,
6064 .bitmask = bitmask,
6065 .migration = 1, /* skip LOOKUP */
6066 };
6067 struct nfs4_fs_locations_res res = {
6068 .fs_locations = locations,
6069 .migration = 1,
6070 };
6071 struct rpc_message msg = {
6072 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6073 .rpc_argp = &args,
6074 .rpc_resp = &res,
6075 .rpc_cred = cred,
6076 };
6077 int status;
6078
6079 nfs_fattr_init(&locations->fattr);
6080 locations->server = server;
6081 locations->nlocations = 0;
6082
6083 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6084 nfs4_set_sequence_privileged(&args.seq_args);
6085 status = nfs4_call_sync_sequence(clnt, server, &msg,
6086 &args.seq_args, &res.seq_res);
6087 if (status == NFS4_OK &&
6088 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6089 status = -NFS4ERR_LEASE_MOVED;
6090 return status;
6091}
6092
6093#endif /* CONFIG_NFS_V4_1 */
6094
6095/**
6096 * nfs4_proc_get_locations - discover locations for a migrated FSID
6097 * @inode: inode on FSID that is migrating
6098 * @locations: result of query
6099 * @page: buffer
6100 * @cred: credential to use for this operation
6101 *
6102 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6103 * operation failed, or a negative errno if a local error occurred.
6104 *
6105 * On success, "locations" is filled in, but if the server has
6106 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6107 * asserted.
6108 *
6109 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6110 * from this client that require migration recovery.
6111 */
6112int nfs4_proc_get_locations(struct inode *inode,
6113 struct nfs4_fs_locations *locations,
6114 struct page *page, struct rpc_cred *cred)
6115{
6116 struct nfs_server *server = NFS_SERVER(inode);
6117 struct nfs_client *clp = server->nfs_client;
6118 const struct nfs4_mig_recovery_ops *ops =
6119 clp->cl_mvops->mig_recovery_ops;
6120 struct nfs4_exception exception = { };
6121 int status;
6122
6123 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6124 (unsigned long long)server->fsid.major,
6125 (unsigned long long)server->fsid.minor,
6126 clp->cl_hostname);
6127 nfs_display_fhandle(NFS_FH(inode), __func__);
6128
6129 do {
6130 status = ops->get_locations(inode, locations, page, cred);
6131 if (status != -NFS4ERR_DELAY)
6132 break;
6133 nfs4_handle_exception(server, status, &exception);
6134 } while (exception.retry);
6135 return status;
6136}
6137
Andy Adamson5ec16a82013-08-08 10:57:55 -04006138/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006139 * If 'use_integrity' is true and the state managment nfs_client
6140 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6141 * and the machine credential as per RFC3530bis and RFC5661 Security
6142 * Considerations sections. Otherwise, just use the user cred with the
6143 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04006144 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006145static 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 +00006146{
6147 int status;
6148 struct nfs4_secinfo_arg args = {
6149 .dir_fh = NFS_FH(dir),
6150 .name = name,
6151 };
6152 struct nfs4_secinfo_res res = {
6153 .flavors = flavors,
6154 };
6155 struct rpc_message msg = {
6156 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6157 .rpc_argp = &args,
6158 .rpc_resp = &res,
6159 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006160 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006161 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006162
6163 if (use_integrity) {
6164 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006165 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6166 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006167 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006168
6169 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006170
6171 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6172 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6173
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006174 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6175 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006176 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006177
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006178 if (cred)
6179 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006180
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006181 return status;
6182}
6183
Bryan Schumaker72de53e2012-04-27 13:27:40 -04006184int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6185 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006186{
6187 struct nfs4_exception exception = { };
6188 int err;
6189 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006190 err = -NFS4ERR_WRONGSEC;
6191
6192 /* try to use integrity protection with machine cred */
6193 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6194 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6195
6196 /*
6197 * if unable to use integrity protection, or SECINFO with
6198 * integrity protection returns NFS4ERR_WRONGSEC (which is
6199 * disallowed by spec, but exists in deployed servers) use
6200 * the current filesystem's rpc_client and the user cred.
6201 */
6202 if (err == -NFS4ERR_WRONGSEC)
6203 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6204
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006205 trace_nfs4_secinfo(dir, name, err);
6206 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006207 &exception);
6208 } while (exception.retry);
6209 return err;
6210}
6211
Andy Adamson557134a2009-04-01 09:21:53 -04006212#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04006213/*
Andy Adamson357f54d2010-12-14 10:11:57 -05006214 * Check the exchange flags returned by the server for invalid flags, having
6215 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6216 * DS flags set.
6217 */
6218static int nfs4_check_cl_exchange_flags(u32 flags)
6219{
6220 if (flags & ~EXCHGID4_FLAG_MASK_R)
6221 goto out_inval;
6222 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6223 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6224 goto out_inval;
6225 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6226 goto out_inval;
6227 return NFS_OK;
6228out_inval:
6229 return -NFS4ERR_INVAL;
6230}
6231
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006232static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006233nfs41_same_server_scope(struct nfs41_server_scope *a,
6234 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006235{
6236 if (a->server_scope_sz == b->server_scope_sz &&
6237 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6238 return true;
6239
6240 return false;
6241}
6242
Andy Adamson357f54d2010-12-14 10:11:57 -05006243/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006244 * nfs4_proc_bind_conn_to_session()
6245 *
6246 * The 4.1 client currently uses the same TCP connection for the
6247 * fore and backchannel.
6248 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006249int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006250{
6251 int status;
6252 struct nfs41_bind_conn_to_session_res res;
6253 struct rpc_message msg = {
6254 .rpc_proc =
6255 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
6256 .rpc_argp = clp,
6257 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006258 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006259 };
6260
6261 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006262
6263 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
6264 if (unlikely(res.session == NULL)) {
6265 status = -ENOMEM;
6266 goto out;
6267 }
6268
6269 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006270 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006271 if (status == 0) {
6272 if (memcmp(res.session->sess_id.data,
6273 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6274 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6275 status = -EIO;
6276 goto out_session;
6277 }
6278 if (res.dir != NFS4_CDFS4_BOTH) {
6279 dprintk("NFS: %s: Unexpected direction from server\n",
6280 __func__);
6281 status = -EIO;
6282 goto out_session;
6283 }
6284 if (res.use_conn_in_rdma_mode) {
6285 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6286 __func__);
6287 status = -EIO;
6288 goto out_session;
6289 }
6290 }
6291out_session:
6292 kfree(res.session);
6293out:
6294 dprintk("<-- %s status= %d\n", __func__, status);
6295 return status;
6296}
6297
6298/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006299 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6300 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04006301 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006302static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
6303 .how = SP4_MACH_CRED,
6304 .enforce.u.words = {
6305 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6306 1 << (OP_EXCHANGE_ID - 32) |
6307 1 << (OP_CREATE_SESSION - 32) |
6308 1 << (OP_DESTROY_SESSION - 32) |
6309 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006310 },
6311 .allow.u.words = {
6312 [0] = 1 << (OP_CLOSE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006313 1 << (OP_LOCKU) |
6314 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006315 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006316 1 << (OP_SECINFO_NO_NAME - 32) |
6317 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006318 1 << (OP_FREE_STATEID - 32) |
6319 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006320 }
6321};
6322
6323/*
6324 * Select the state protection mode for client `clp' given the server results
6325 * from exchange_id in `sp'.
6326 *
6327 * Returns 0 on success, negative errno otherwise.
6328 */
6329static int nfs4_sp4_select_mode(struct nfs_client *clp,
6330 struct nfs41_state_protection *sp)
6331{
6332 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
6333 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6334 1 << (OP_EXCHANGE_ID - 32) |
6335 1 << (OP_CREATE_SESSION - 32) |
6336 1 << (OP_DESTROY_SESSION - 32) |
6337 1 << (OP_DESTROY_CLIENTID - 32)
6338 };
6339 unsigned int i;
6340
6341 if (sp->how == SP4_MACH_CRED) {
6342 /* Print state protect result */
6343 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
6344 for (i = 0; i <= LAST_NFS4_OP; i++) {
6345 if (test_bit(i, sp->enforce.u.longs))
6346 dfprintk(MOUNT, " enforce op %d\n", i);
6347 if (test_bit(i, sp->allow.u.longs))
6348 dfprintk(MOUNT, " allow op %d\n", i);
6349 }
6350
6351 /* make sure nothing is on enforce list that isn't supported */
6352 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
6353 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
6354 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6355 return -EINVAL;
6356 }
6357 }
6358
6359 /*
6360 * Minimal mode - state operations are allowed to use machine
6361 * credential. Note this already happens by default, so the
6362 * client doesn't have to do anything more than the negotiation.
6363 *
6364 * NOTE: we don't care if EXCHANGE_ID is in the list -
6365 * we're already using the machine cred for exchange_id
6366 * and will never use a different cred.
6367 */
6368 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
6369 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
6370 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
6371 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
6372 dfprintk(MOUNT, "sp4_mach_cred:\n");
6373 dfprintk(MOUNT, " minimal mode enabled\n");
6374 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
6375 } else {
6376 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6377 return -EINVAL;
6378 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006379
6380 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
6381 test_bit(OP_LOCKU, sp->allow.u.longs)) {
6382 dfprintk(MOUNT, " cleanup mode enabled\n");
6383 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
6384 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006385
6386 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
6387 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
6388 dfprintk(MOUNT, " secinfo mode enabled\n");
6389 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
6390 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006391
6392 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
6393 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
6394 dfprintk(MOUNT, " stateid mode enabled\n");
6395 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
6396 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04006397
6398 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
6399 dfprintk(MOUNT, " write mode enabled\n");
6400 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
6401 }
6402
6403 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
6404 dfprintk(MOUNT, " commit mode enabled\n");
6405 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
6406 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006407 }
6408
6409 return 0;
6410}
6411
6412/*
6413 * _nfs4_proc_exchange_id()
6414 *
6415 * Wrapper for EXCHANGE_ID operation.
6416 */
6417static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
6418 u32 sp4_how)
Benny Halevy99fe60d2009-04-01 09:22:29 -04006419{
6420 nfs4_verifier verifier;
6421 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05006422 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04006423 .client = clp,
Trond Myklebust4f0b4292013-05-20 12:24:03 -04006424 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6425 EXCHGID4_FLAG_BIND_PRINC_STATEID,
Benny Halevy99fe60d2009-04-01 09:22:29 -04006426 };
6427 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04006428 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04006429 };
6430 int status;
6431 struct rpc_message msg = {
6432 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
6433 .rpc_argp = &args,
6434 .rpc_resp = &res,
6435 .rpc_cred = cred,
6436 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04006437
Chuck Leverf0920752012-05-21 22:45:41 -04006438 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04006439 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
6440 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006441 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
6442 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6443 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006444
Chuck Leveracdeb692012-05-21 22:46:16 -04006445 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006446 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04006447 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006448 status = -ENOMEM;
6449 goto out;
6450 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006451
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006452 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006453 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006454 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04006455 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04006456 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04006457 }
6458
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006459 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006460 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006461 status = -ENOMEM;
6462 goto out_server_scope;
6463 }
6464
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006465 switch (sp4_how) {
6466 case SP4_NONE:
6467 args.state_protect.how = SP4_NONE;
6468 break;
6469
6470 case SP4_MACH_CRED:
6471 args.state_protect = nfs4_sp4_mach_cred_request;
6472 break;
6473
6474 default:
6475 /* unsupported! */
6476 WARN_ON_ONCE(1);
6477 status = -EINVAL;
6478 goto out_server_scope;
6479 }
6480
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006481 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006482 trace_nfs4_exchange_id(clp, status);
Chuck Lever177313f2012-05-21 22:44:58 -04006483 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04006484 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006485
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006486 if (status == 0)
6487 status = nfs4_sp4_select_mode(clp, &res.state_protect);
6488
Chuck Lever177313f2012-05-21 22:44:58 -04006489 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04006490 clp->cl_clientid = res.clientid;
6491 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
6492 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
6493 clp->cl_seqid = res.seqid;
6494
Chuck Leveracdeb692012-05-21 22:46:16 -04006495 kfree(clp->cl_serverowner);
6496 clp->cl_serverowner = res.server_owner;
6497 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04006498
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006499 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04006500 kfree(clp->cl_implid);
6501 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006502
Chuck Lever177313f2012-05-21 22:44:58 -04006503 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006504 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006505 res.server_scope)) {
6506 dprintk("%s: server_scope mismatch detected\n",
6507 __func__);
6508 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006509 kfree(clp->cl_serverscope);
6510 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006511 }
6512
Chuck Lever177313f2012-05-21 22:44:58 -04006513 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006514 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006515 goto out;
6516 }
Trond Myklebust32b01312012-05-26 13:41:04 -04006517 } else
6518 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006519
Chuck Leveracdeb692012-05-21 22:46:16 -04006520out_server_owner:
6521 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006522out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006523 kfree(res.server_scope);
6524out:
Chuck Lever177313f2012-05-21 22:44:58 -04006525 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006526 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006527 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006528 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04006529 clp->cl_implid->date.seconds,
6530 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006531 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006532 return status;
6533}
6534
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006535/*
6536 * nfs4_proc_exchange_id()
6537 *
6538 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6539 *
6540 * Since the clientid has expired, all compounds using sessions
6541 * associated with the stale clientid will be returning
6542 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
6543 * be in some phase of session reset.
6544 *
6545 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
6546 */
6547int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
6548{
6549 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
6550 int status;
6551
6552 /* try SP4_MACH_CRED if krb5i/p */
6553 if (authflavor == RPC_AUTH_GSS_KRB5I ||
6554 authflavor == RPC_AUTH_GSS_KRB5P) {
6555 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
6556 if (!status)
6557 return 0;
6558 }
6559
6560 /* try SP4_NONE */
6561 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
6562}
6563
Trond Myklebust66245532012-05-25 17:18:09 -04006564static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
6565 struct rpc_cred *cred)
6566{
6567 struct rpc_message msg = {
6568 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
6569 .rpc_argp = clp,
6570 .rpc_cred = cred,
6571 };
6572 int status;
6573
6574 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006575 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04006576 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04006577 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04006578 "DESTROY_CLIENTID.", status, clp->cl_hostname);
6579 return status;
6580}
6581
6582static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
6583 struct rpc_cred *cred)
6584{
6585 unsigned int loop;
6586 int ret;
6587
6588 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
6589 ret = _nfs4_proc_destroy_clientid(clp, cred);
6590 switch (ret) {
6591 case -NFS4ERR_DELAY:
6592 case -NFS4ERR_CLIENTID_BUSY:
6593 ssleep(1);
6594 break;
6595 default:
6596 return ret;
6597 }
6598 }
6599 return 0;
6600}
6601
6602int nfs4_destroy_clientid(struct nfs_client *clp)
6603{
6604 struct rpc_cred *cred;
6605 int ret = 0;
6606
6607 if (clp->cl_mvops->minor_version < 1)
6608 goto out;
6609 if (clp->cl_exchange_flags == 0)
6610 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04006611 if (clp->cl_preserve_clid)
6612 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04006613 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04006614 ret = nfs4_proc_destroy_clientid(clp, cred);
6615 if (cred)
6616 put_rpccred(cred);
6617 switch (ret) {
6618 case 0:
6619 case -NFS4ERR_STALE_CLIENTID:
6620 clp->cl_exchange_flags = 0;
6621 }
6622out:
6623 return ret;
6624}
6625
Andy Adamson2050f0c2009-04-01 09:22:30 -04006626struct nfs4_get_lease_time_data {
6627 struct nfs4_get_lease_time_args *args;
6628 struct nfs4_get_lease_time_res *res;
6629 struct nfs_client *clp;
6630};
6631
6632static void nfs4_get_lease_time_prepare(struct rpc_task *task,
6633 void *calldata)
6634{
Andy Adamson2050f0c2009-04-01 09:22:30 -04006635 struct nfs4_get_lease_time_data *data =
6636 (struct nfs4_get_lease_time_data *)calldata;
6637
6638 dprintk("--> %s\n", __func__);
6639 /* just setup sequence, do not trigger session recovery
6640 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006641 nfs41_setup_sequence(data->clp->cl_session,
6642 &data->args->la_seq_args,
6643 &data->res->lr_seq_res,
6644 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006645 dprintk("<-- %s\n", __func__);
6646}
6647
6648/*
6649 * Called from nfs4_state_manager thread for session setup, so don't recover
6650 * from sequence operation or clientid errors.
6651 */
6652static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
6653{
6654 struct nfs4_get_lease_time_data *data =
6655 (struct nfs4_get_lease_time_data *)calldata;
6656
6657 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006658 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
6659 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04006660 switch (task->tk_status) {
6661 case -NFS4ERR_DELAY:
6662 case -NFS4ERR_GRACE:
6663 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
6664 rpc_delay(task, NFS4_POLL_RETRY_MIN);
6665 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006666 /* fall through */
6667 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006668 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006669 return;
6670 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04006671 dprintk("<-- %s\n", __func__);
6672}
6673
Trond Myklebust17280172012-03-11 13:11:00 -04006674static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04006675 .rpc_call_prepare = nfs4_get_lease_time_prepare,
6676 .rpc_call_done = nfs4_get_lease_time_done,
6677};
6678
6679int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
6680{
6681 struct rpc_task *task;
6682 struct nfs4_get_lease_time_args args;
6683 struct nfs4_get_lease_time_res res = {
6684 .lr_fsinfo = fsinfo,
6685 };
6686 struct nfs4_get_lease_time_data data = {
6687 .args = &args,
6688 .res = &res,
6689 .clp = clp,
6690 };
6691 struct rpc_message msg = {
6692 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
6693 .rpc_argp = &args,
6694 .rpc_resp = &res,
6695 };
6696 struct rpc_task_setup task_setup = {
6697 .rpc_client = clp->cl_rpcclient,
6698 .rpc_message = &msg,
6699 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006700 .callback_data = &data,
6701 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04006702 };
6703 int status;
6704
Chuck Levera9c92d62013-08-09 12:48:18 -04006705 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006706 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006707 dprintk("--> %s\n", __func__);
6708 task = rpc_run_task(&task_setup);
6709
6710 if (IS_ERR(task))
6711 status = PTR_ERR(task);
6712 else {
6713 status = task->tk_status;
6714 rpc_put_task(task);
6715 }
6716 dprintk("<-- %s return %d\n", __func__, status);
6717
6718 return status;
6719}
6720
Andy Adamsonfc931582009-04-01 09:22:31 -04006721/*
6722 * Initialize the values to be used by the client in CREATE_SESSION
6723 * If nfs4_init_session set the fore channel request and response sizes,
6724 * use them.
6725 *
6726 * Set the back channel max_resp_sz_cached to zero to force the client to
6727 * always set csa_cachethis to FALSE because the current implementation
6728 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
6729 */
6730static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
6731{
Andy Adamson18aad3d2013-06-26 12:21:49 -04006732 unsigned int max_rqst_sz, max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006733
Andy Adamson18aad3d2013-06-26 12:21:49 -04006734 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
6735 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
6736
Andy Adamsonfc931582009-04-01 09:22:31 -04006737 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04006738 args->fc_attrs.max_rqst_sz = max_rqst_sz;
6739 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006740 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05006741 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04006742
6743 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05006744 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04006745 __func__,
6746 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05006747 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04006748
6749 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04006750 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
6751 args->bc_attrs.max_resp_sz = PAGE_SIZE;
6752 args->bc_attrs.max_resp_sz_cached = 0;
6753 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
6754 args->bc_attrs.max_reqs = 1;
6755
6756 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
6757 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
6758 __func__,
6759 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
6760 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
6761 args->bc_attrs.max_reqs);
6762}
6763
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006764static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04006765{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006766 struct nfs4_channel_attrs *sent = &args->fc_attrs;
6767 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
6768
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006769 if (rcvd->max_resp_sz > sent->max_resp_sz)
6770 return -EINVAL;
6771 /*
6772 * Our requested max_ops is the minimum we need; we're not
6773 * prepared to break up compounds into smaller pieces than that.
6774 * So, no point even trying to continue if the server won't
6775 * cooperate:
6776 */
6777 if (rcvd->max_ops < sent->max_ops)
6778 return -EINVAL;
6779 if (rcvd->max_reqs == 0)
6780 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006781 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
6782 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006783 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04006784}
6785
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006786static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
6787{
6788 struct nfs4_channel_attrs *sent = &args->bc_attrs;
6789 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04006790
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006791 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
6792 return -EINVAL;
6793 if (rcvd->max_resp_sz < sent->max_resp_sz)
6794 return -EINVAL;
6795 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
6796 return -EINVAL;
6797 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006798 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006799 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006800 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006801 return -EINVAL;
6802 return 0;
6803}
Andy Adamson8d353012009-04-01 09:22:32 -04006804
Andy Adamson8d353012009-04-01 09:22:32 -04006805static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
6806 struct nfs4_session *session)
6807{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006808 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04006809
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006810 ret = nfs4_verify_fore_channel_attrs(args, session);
6811 if (ret)
6812 return ret;
6813 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04006814}
6815
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006816static int _nfs4_proc_create_session(struct nfs_client *clp,
6817 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04006818{
6819 struct nfs4_session *session = clp->cl_session;
6820 struct nfs41_create_session_args args = {
6821 .client = clp,
6822 .cb_program = NFS4_CALLBACK,
6823 };
6824 struct nfs41_create_session_res res = {
6825 .client = clp,
6826 };
6827 struct rpc_message msg = {
6828 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
6829 .rpc_argp = &args,
6830 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006831 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04006832 };
6833 int status;
6834
6835 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04006836 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04006837
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006838 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006839 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04006840
Trond Myklebust43095d32012-11-20 11:13:12 -05006841 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04006842 /* Verify the session's negotiated channel_attrs values */
6843 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04006844 /* Increment the clientid slot sequence id */
6845 clp->cl_seqid++;
6846 }
6847
6848 return status;
6849}
6850
6851/*
6852 * Issues a CREATE_SESSION operation to the server.
6853 * It is the responsibility of the caller to verify the session is
6854 * expired before calling this routine.
6855 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006856int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04006857{
6858 int status;
6859 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04006860 struct nfs4_session *session = clp->cl_session;
6861
6862 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
6863
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006864 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04006865 if (status)
6866 goto out;
6867
Andy Adamsonaacd5532011-11-09 13:58:21 -05006868 /* Init or reset the session slot tables */
6869 status = nfs4_setup_session_slot_tables(session);
6870 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04006871 if (status)
6872 goto out;
6873
6874 ptr = (unsigned *)&session->sess_id.data[0];
6875 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
6876 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04006877out:
6878 dprintk("<-- %s\n", __func__);
6879 return status;
6880}
6881
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006882/*
6883 * Issue the over-the-wire RPC DESTROY_SESSION.
6884 * The caller must serialize access to this routine.
6885 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006886int nfs4_proc_destroy_session(struct nfs4_session *session,
6887 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006888{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006889 struct rpc_message msg = {
6890 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
6891 .rpc_argp = session,
6892 .rpc_cred = cred,
6893 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006894 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006895
6896 dprintk("--> nfs4_proc_destroy_session\n");
6897
6898 /* session is still being setup */
6899 if (session->clp->cl_cons_state != NFS_CS_READY)
6900 return status;
6901
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006902 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006903 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006904
6905 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04006906 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006907 "Session has been destroyed regardless...\n", status);
6908
6909 dprintk("<-- nfs4_proc_destroy_session\n");
6910 return status;
6911}
6912
Trond Myklebust7b38c362012-05-23 13:23:31 -04006913/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006914 * Renew the cl_session lease.
6915 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006916struct nfs4_sequence_data {
6917 struct nfs_client *clp;
6918 struct nfs4_sequence_args args;
6919 struct nfs4_sequence_res res;
6920};
6921
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006922static void nfs41_sequence_release(void *data)
6923{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006924 struct nfs4_sequence_data *calldata = data;
6925 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006926
Alexandros Batsakis71358402010-02-05 03:45:05 -08006927 if (atomic_read(&clp->cl_count) > 1)
6928 nfs4_schedule_state_renewal(clp);
6929 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006930 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006931}
6932
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006933static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6934{
6935 switch(task->tk_status) {
6936 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006937 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6938 return -EAGAIN;
6939 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006940 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006941 }
6942 return 0;
6943}
6944
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006945static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006946{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006947 struct nfs4_sequence_data *calldata = data;
6948 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006949
Trond Myklebust14516c32010-07-31 14:29:06 -04006950 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6951 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006952
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006953 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006954 if (task->tk_status < 0) {
6955 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006956 if (atomic_read(&clp->cl_count) == 1)
6957 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006958
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006959 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6960 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006961 return;
6962 }
6963 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006964 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006965out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006966 dprintk("<-- %s\n", __func__);
6967}
6968
6969static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6970{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006971 struct nfs4_sequence_data *calldata = data;
6972 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006973 struct nfs4_sequence_args *args;
6974 struct nfs4_sequence_res *res;
6975
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006976 args = task->tk_msg.rpc_argp;
6977 res = task->tk_msg.rpc_resp;
6978
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006979 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006980}
6981
6982static const struct rpc_call_ops nfs41_sequence_ops = {
6983 .rpc_call_done = nfs41_sequence_call_done,
6984 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006985 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006986};
6987
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006988static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
6989 struct rpc_cred *cred,
6990 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006991{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006992 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006993 struct rpc_message msg = {
6994 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6995 .rpc_cred = cred,
6996 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006997 struct rpc_task_setup task_setup_data = {
6998 .rpc_client = clp->cl_rpcclient,
6999 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007000 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04007001 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007002 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007003
Alexandros Batsakis71358402010-02-05 03:45:05 -08007004 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007005 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04007006 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007007 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08007008 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007009 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007010 }
Chuck Levera9c92d62013-08-09 12:48:18 -04007011 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007012 if (is_privileged)
7013 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007014 msg.rpc_argp = &calldata->args;
7015 msg.rpc_resp = &calldata->res;
7016 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007017 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007018
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007019 return rpc_run_task(&task_setup_data);
7020}
7021
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007022static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007023{
7024 struct rpc_task *task;
7025 int ret = 0;
7026
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007027 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
7028 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007029 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007030 if (IS_ERR(task))
7031 ret = PTR_ERR(task);
7032 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08007033 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007034 dprintk("<-- %s status=%d\n", __func__, ret);
7035 return ret;
7036}
7037
7038static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
7039{
7040 struct rpc_task *task;
7041 int ret;
7042
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007043 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007044 if (IS_ERR(task)) {
7045 ret = PTR_ERR(task);
7046 goto out;
7047 }
7048 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05007049 if (!ret) {
7050 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
7051
7052 if (task->tk_status == 0)
7053 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007054 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05007055 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007056 rpc_put_task(task);
7057out:
7058 dprintk("<-- %s status=%d\n", __func__, ret);
7059 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007060}
7061
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007062struct nfs4_reclaim_complete_data {
7063 struct nfs_client *clp;
7064 struct nfs41_reclaim_complete_args arg;
7065 struct nfs41_reclaim_complete_res res;
7066};
7067
7068static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
7069{
7070 struct nfs4_reclaim_complete_data *calldata = data;
7071
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007072 nfs41_setup_sequence(calldata->clp->cl_session,
7073 &calldata->arg.seq_args,
7074 &calldata->res.seq_res,
7075 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007076}
7077
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007078static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7079{
7080 switch(task->tk_status) {
7081 case 0:
7082 case -NFS4ERR_COMPLETE_ALREADY:
7083 case -NFS4ERR_WRONG_CRED: /* What to do here? */
7084 break;
7085 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007086 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007087 /* fall through */
7088 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007089 return -EAGAIN;
7090 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007091 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007092 }
7093 return 0;
7094}
7095
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007096static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
7097{
7098 struct nfs4_reclaim_complete_data *calldata = data;
7099 struct nfs_client *clp = calldata->clp;
7100 struct nfs4_sequence_res *res = &calldata->res.seq_res;
7101
7102 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007103 if (!nfs41_sequence_done(task, res))
7104 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007105
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007106 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007107 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
7108 rpc_restart_call_prepare(task);
7109 return;
7110 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007111 dprintk("<-- %s\n", __func__);
7112}
7113
7114static void nfs4_free_reclaim_complete_data(void *data)
7115{
7116 struct nfs4_reclaim_complete_data *calldata = data;
7117
7118 kfree(calldata);
7119}
7120
7121static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
7122 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
7123 .rpc_call_done = nfs4_reclaim_complete_done,
7124 .rpc_release = nfs4_free_reclaim_complete_data,
7125};
7126
7127/*
7128 * Issue a global reclaim complete.
7129 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04007130static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
7131 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007132{
7133 struct nfs4_reclaim_complete_data *calldata;
7134 struct rpc_task *task;
7135 struct rpc_message msg = {
7136 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04007137 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007138 };
7139 struct rpc_task_setup task_setup_data = {
7140 .rpc_client = clp->cl_rpcclient,
7141 .rpc_message = &msg,
7142 .callback_ops = &nfs4_reclaim_complete_call_ops,
7143 .flags = RPC_TASK_ASYNC,
7144 };
7145 int status = -ENOMEM;
7146
7147 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04007148 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007149 if (calldata == NULL)
7150 goto out;
7151 calldata->clp = clp;
7152 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007153
Chuck Levera9c92d62013-08-09 12:48:18 -04007154 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007155 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007156 msg.rpc_argp = &calldata->arg;
7157 msg.rpc_resp = &calldata->res;
7158 task_setup_data.callback_data = calldata;
7159 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007160 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007161 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007162 goto out;
7163 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05007164 status = nfs4_wait_for_completion_rpc_task(task);
7165 if (status == 0)
7166 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007167 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007168 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007169out:
7170 dprintk("<-- %s status=%d\n", __func__, status);
7171 return status;
7172}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007173
7174static void
7175nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
7176{
7177 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00007178 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007179 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007180
7181 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00007182 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
7183 * right now covering the LAYOUTGET we are about to send.
7184 * However, that is not so catastrophic, and there seems
7185 * to be no way to prevent it completely.
7186 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007187 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05007188 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007189 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00007190 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
7191 NFS_I(lgp->args.inode)->layout,
7192 lgp->args.ctx->state)) {
7193 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00007194 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007195}
7196
7197static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
7198{
7199 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07007200 struct inode *inode = lgp->args.inode;
7201 struct nfs_server *server = NFS_SERVER(inode);
7202 struct pnfs_layout_hdr *lo;
7203 struct nfs4_state *state = NULL;
Weston Andros Adamson30005122013-02-28 20:30:10 -05007204 unsigned long timeo, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007205
7206 dprintk("--> %s\n", __func__);
7207
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007208 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07007209 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007210
7211 switch (task->tk_status) {
7212 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07007213 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007214 case -NFS4ERR_LAYOUTTRYLATER:
7215 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05007216 timeo = rpc_get_timeout(task->tk_client);
7217 giveup = lgp->args.timestamp + timeo;
7218 if (time_after(giveup, jiffies))
7219 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07007220 break;
7221 case -NFS4ERR_EXPIRED:
7222 case -NFS4ERR_BAD_STATEID:
7223 spin_lock(&inode->i_lock);
7224 lo = NFS_I(inode)->layout;
7225 if (!lo || list_empty(&lo->plh_segs)) {
7226 spin_unlock(&inode->i_lock);
7227 /* If the open stateid was bad, then recover it. */
7228 state = lgp->args.ctx->state;
7229 } else {
7230 LIST_HEAD(head);
7231
7232 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
7233 spin_unlock(&inode->i_lock);
7234 /* Mark the bad layout state as invalid, then
7235 * retry using the open stateid. */
7236 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007237 }
7238 }
Trond Myklebustee314c22012-10-01 17:25:48 -07007239 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
7240 rpc_restart_call_prepare(task);
7241out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007242 dprintk("<-- %s\n", __func__);
7243}
7244
Idan Kedar85541162012-08-02 11:47:10 +03007245static size_t max_response_pages(struct nfs_server *server)
7246{
7247 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
7248 return nfs_page_array_len(0, max_resp_sz);
7249}
7250
7251static void nfs4_free_pages(struct page **pages, size_t size)
7252{
7253 int i;
7254
7255 if (!pages)
7256 return;
7257
7258 for (i = 0; i < size; i++) {
7259 if (!pages[i])
7260 break;
7261 __free_page(pages[i]);
7262 }
7263 kfree(pages);
7264}
7265
7266static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
7267{
7268 struct page **pages;
7269 int i;
7270
7271 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
7272 if (!pages) {
7273 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
7274 return NULL;
7275 }
7276
7277 for (i = 0; i < size; i++) {
7278 pages[i] = alloc_page(gfp_flags);
7279 if (!pages[i]) {
7280 dprintk("%s: failed to allocate page\n", __func__);
7281 nfs4_free_pages(pages, size);
7282 return NULL;
7283 }
7284 }
7285
7286 return pages;
7287}
7288
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007289static void nfs4_layoutget_release(void *calldata)
7290{
7291 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007292 struct inode *inode = lgp->args.inode;
7293 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007294 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007295
7296 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03007297 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007298 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007299 put_nfs_open_context(lgp->args.ctx);
7300 kfree(calldata);
7301 dprintk("<-- %s\n", __func__);
7302}
7303
7304static const struct rpc_call_ops nfs4_layoutget_call_ops = {
7305 .rpc_call_prepare = nfs4_layoutget_prepare,
7306 .rpc_call_done = nfs4_layoutget_done,
7307 .rpc_release = nfs4_layoutget_release,
7308};
7309
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007310struct pnfs_layout_segment *
7311nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007312{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007313 struct inode *inode = lgp->args.inode;
7314 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007315 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007316 struct rpc_task *task;
7317 struct rpc_message msg = {
7318 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
7319 .rpc_argp = &lgp->args,
7320 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04007321 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007322 };
7323 struct rpc_task_setup task_setup_data = {
7324 .rpc_client = server->client,
7325 .rpc_message = &msg,
7326 .callback_ops = &nfs4_layoutget_call_ops,
7327 .callback_data = lgp,
7328 .flags = RPC_TASK_ASYNC,
7329 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007330 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007331 int status = 0;
7332
7333 dprintk("--> %s\n", __func__);
7334
Idan Kedar85541162012-08-02 11:47:10 +03007335 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
7336 if (!lgp->args.layout.pages) {
7337 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007338 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03007339 }
7340 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05007341 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03007342
Weston Andros Adamson35124a02011-03-24 16:48:21 -04007343 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007344 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04007345 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007346
7347 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
7348 pnfs_get_layout_hdr(NFS_I(inode)->layout);
7349
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007350 task = rpc_run_task(&task_setup_data);
7351 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007352 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007353 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00007354 if (status == 0)
7355 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007356 trace_nfs4_layoutget(lgp->args.ctx,
7357 &lgp->args.range,
7358 &lgp->res.range,
7359 status);
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05007360 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
7361 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007362 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007363 rpc_put_task(task);
7364 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007365 if (status)
7366 return ERR_PTR(status);
7367 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007368}
7369
Benny Halevycbe82602011-05-22 19:52:37 +03007370static void
7371nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
7372{
7373 struct nfs4_layoutreturn *lrp = calldata;
7374
7375 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007376 nfs41_setup_sequence(lrp->clp->cl_session,
7377 &lrp->args.seq_args,
7378 &lrp->res.seq_res,
7379 task);
Benny Halevycbe82602011-05-22 19:52:37 +03007380}
7381
7382static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
7383{
7384 struct nfs4_layoutreturn *lrp = calldata;
7385 struct nfs_server *server;
7386
7387 dprintk("--> %s\n", __func__);
7388
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007389 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03007390 return;
7391
7392 server = NFS_SERVER(lrp->args.inode);
7393 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007394 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03007395 return;
7396 }
Benny Halevycbe82602011-05-22 19:52:37 +03007397 dprintk("<-- %s\n", __func__);
7398}
7399
7400static void nfs4_layoutreturn_release(void *calldata)
7401{
7402 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04007403 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03007404
7405 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04007406 spin_lock(&lo->plh_inode->i_lock);
7407 if (lrp->res.lrs_present)
7408 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
7409 lo->plh_block_lgets--;
7410 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04007411 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03007412 kfree(calldata);
7413 dprintk("<-- %s\n", __func__);
7414}
7415
7416static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
7417 .rpc_call_prepare = nfs4_layoutreturn_prepare,
7418 .rpc_call_done = nfs4_layoutreturn_done,
7419 .rpc_release = nfs4_layoutreturn_release,
7420};
7421
7422int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
7423{
7424 struct rpc_task *task;
7425 struct rpc_message msg = {
7426 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
7427 .rpc_argp = &lrp->args,
7428 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04007429 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03007430 };
7431 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04007432 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03007433 .rpc_message = &msg,
7434 .callback_ops = &nfs4_layoutreturn_call_ops,
7435 .callback_data = lrp,
7436 };
7437 int status;
7438
7439 dprintk("--> %s\n", __func__);
Chuck Levera9c92d62013-08-09 12:48:18 -04007440 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03007441 task = rpc_run_task(&task_setup_data);
7442 if (IS_ERR(task))
7443 return PTR_ERR(task);
7444 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007445 trace_nfs4_layoutreturn(lrp->args.inode, status);
Benny Halevycbe82602011-05-22 19:52:37 +03007446 dprintk("<-- %s status=%d\n", __func__, status);
7447 rpc_put_task(task);
7448 return status;
7449}
7450
Andy Adamson7f11d8d2011-07-30 20:52:35 -04007451/*
7452 * Retrieve the list of Data Server devices from the MDS.
7453 */
7454static int _nfs4_getdevicelist(struct nfs_server *server,
7455 const struct nfs_fh *fh,
7456 struct pnfs_devicelist *devlist)
7457{
7458 struct nfs4_getdevicelist_args args = {
7459 .fh = fh,
7460 .layoutclass = server->pnfs_curr_ld->id,
7461 };
7462 struct nfs4_getdevicelist_res res = {
7463 .devlist = devlist,
7464 };
7465 struct rpc_message msg = {
7466 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
7467 .rpc_argp = &args,
7468 .rpc_resp = &res,
7469 };
7470 int status;
7471
7472 dprintk("--> %s\n", __func__);
7473 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
7474 &res.seq_res, 0);
7475 dprintk("<-- %s status=%d\n", __func__, status);
7476 return status;
7477}
7478
7479int nfs4_proc_getdevicelist(struct nfs_server *server,
7480 const struct nfs_fh *fh,
7481 struct pnfs_devicelist *devlist)
7482{
7483 struct nfs4_exception exception = { };
7484 int err;
7485
7486 do {
7487 err = nfs4_handle_exception(server,
7488 _nfs4_getdevicelist(server, fh, devlist),
7489 &exception);
7490 } while (exception.retry);
7491
7492 dprintk("%s: err=%d, num_devs=%u\n", __func__,
7493 err, devlist->num_devs);
7494
7495 return err;
7496}
7497EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
7498
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007499static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007500_nfs4_proc_getdeviceinfo(struct nfs_server *server,
7501 struct pnfs_device *pdev,
7502 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007503{
7504 struct nfs4_getdeviceinfo_args args = {
7505 .pdev = pdev,
7506 };
7507 struct nfs4_getdeviceinfo_res res = {
7508 .pdev = pdev,
7509 };
7510 struct rpc_message msg = {
7511 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
7512 .rpc_argp = &args,
7513 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007514 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007515 };
7516 int status;
7517
7518 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00007519 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007520 dprintk("<-- %s status=%d\n", __func__, status);
7521
7522 return status;
7523}
7524
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007525int nfs4_proc_getdeviceinfo(struct nfs_server *server,
7526 struct pnfs_device *pdev,
7527 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007528{
7529 struct nfs4_exception exception = { };
7530 int err;
7531
7532 do {
7533 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007534 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007535 &exception);
7536 } while (exception.retry);
7537 return err;
7538}
7539EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
7540
Andy Adamson863a3c62011-03-23 13:27:54 +00007541static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
7542{
7543 struct nfs4_layoutcommit_data *data = calldata;
7544 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007545 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00007546
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007547 nfs41_setup_sequence(session,
7548 &data->args.seq_args,
7549 &data->res.seq_res,
7550 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00007551}
7552
7553static void
7554nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
7555{
7556 struct nfs4_layoutcommit_data *data = calldata;
7557 struct nfs_server *server = NFS_SERVER(data->args.inode);
7558
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007559 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00007560 return;
7561
7562 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007563 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
7564 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
7565 case -NFS4ERR_BADLAYOUT: /* no layout */
7566 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00007567 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007568 break;
7569 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00007570 nfs_post_op_update_inode_force_wcc(data->args.inode,
7571 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007572 break;
7573 default:
7574 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
7575 rpc_restart_call_prepare(task);
7576 return;
7577 }
7578 }
Andy Adamson863a3c62011-03-23 13:27:54 +00007579}
7580
7581static void nfs4_layoutcommit_release(void *calldata)
7582{
7583 struct nfs4_layoutcommit_data *data = calldata;
7584
Andy Adamsondb29c082011-07-30 20:52:38 -04007585 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00007586 put_rpccred(data->cred);
7587 kfree(data);
7588}
7589
7590static const struct rpc_call_ops nfs4_layoutcommit_ops = {
7591 .rpc_call_prepare = nfs4_layoutcommit_prepare,
7592 .rpc_call_done = nfs4_layoutcommit_done,
7593 .rpc_release = nfs4_layoutcommit_release,
7594};
7595
7596int
Andy Adamsonef311532011-03-12 02:58:10 -05007597nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00007598{
7599 struct rpc_message msg = {
7600 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
7601 .rpc_argp = &data->args,
7602 .rpc_resp = &data->res,
7603 .rpc_cred = data->cred,
7604 };
7605 struct rpc_task_setup task_setup_data = {
7606 .task = &data->task,
7607 .rpc_client = NFS_CLIENT(data->args.inode),
7608 .rpc_message = &msg,
7609 .callback_ops = &nfs4_layoutcommit_ops,
7610 .callback_data = data,
7611 .flags = RPC_TASK_ASYNC,
7612 };
7613 struct rpc_task *task;
7614 int status = 0;
7615
7616 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
7617 "lbw: %llu inode %lu\n",
7618 data->task.tk_pid, sync,
7619 data->args.lastbytewritten,
7620 data->args.inode->i_ino);
7621
Chuck Levera9c92d62013-08-09 12:48:18 -04007622 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00007623 task = rpc_run_task(&task_setup_data);
7624 if (IS_ERR(task))
7625 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05007626 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00007627 goto out;
7628 status = nfs4_wait_for_completion_rpc_task(task);
7629 if (status != 0)
7630 goto out;
7631 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007632 trace_nfs4_layoutcommit(data->args.inode, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00007633out:
7634 dprintk("%s: status %d\n", __func__, status);
7635 rpc_put_task(task);
7636 return status;
7637}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007638
Andy Adamson97431202013-08-08 10:57:56 -04007639/**
7640 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
7641 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
7642 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007643static int
7644_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007645 struct nfs_fsinfo *info,
7646 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007647{
7648 struct nfs41_secinfo_no_name_args args = {
7649 .style = SECINFO_STYLE_CURRENT_FH,
7650 };
7651 struct nfs4_secinfo_res res = {
7652 .flavors = flavors,
7653 };
7654 struct rpc_message msg = {
7655 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
7656 .rpc_argp = &args,
7657 .rpc_resp = &res,
7658 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007659 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007660 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007661 int status;
7662
7663 if (use_integrity) {
7664 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007665 cred = nfs4_get_clid_cred(server->nfs_client);
7666 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007667 }
7668
7669 dprintk("--> %s\n", __func__);
7670 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
7671 &res.seq_res, 0);
7672 dprintk("<-- %s status=%d\n", __func__, status);
7673
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007674 if (cred)
7675 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007676
7677 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007678}
7679
7680static int
7681nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7682 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
7683{
7684 struct nfs4_exception exception = { };
7685 int err;
7686 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007687 /* first try using integrity protection */
7688 err = -NFS4ERR_WRONGSEC;
7689
7690 /* try to use integrity protection with machine cred */
7691 if (_nfs4_is_integrity_protected(server->nfs_client))
7692 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
7693 flavors, true);
7694
7695 /*
7696 * if unable to use integrity protection, or SECINFO with
7697 * integrity protection returns NFS4ERR_WRONGSEC (which is
7698 * disallowed by spec, but exists in deployed servers) use
7699 * the current filesystem's rpc_client and the user cred.
7700 */
7701 if (err == -NFS4ERR_WRONGSEC)
7702 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
7703 flavors, false);
7704
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007705 switch (err) {
7706 case 0:
7707 case -NFS4ERR_WRONGSEC:
7708 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007709 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007710 default:
7711 err = nfs4_handle_exception(server, err, &exception);
7712 }
7713 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007714out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007715 return err;
7716}
7717
7718static int
7719nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
7720 struct nfs_fsinfo *info)
7721{
7722 int err;
7723 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04007724 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007725 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04007726 struct nfs4_secinfo4 *secinfo;
7727 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007728
7729 page = alloc_page(GFP_KERNEL);
7730 if (!page) {
7731 err = -ENOMEM;
7732 goto out;
7733 }
7734
7735 flavors = page_address(page);
7736 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
7737
7738 /*
7739 * Fall back on "guess and check" method if
7740 * the server doesn't support SECINFO_NO_NAME
7741 */
7742 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
7743 err = nfs4_find_root_sec(server, fhandle, info);
7744 goto out_freepage;
7745 }
7746 if (err)
7747 goto out_freepage;
7748
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04007749 for (i = 0; i < flavors->num_flavors; i++) {
7750 secinfo = &flavors->flavors[i];
7751
7752 switch (secinfo->flavor) {
7753 case RPC_AUTH_NULL:
7754 case RPC_AUTH_UNIX:
7755 case RPC_AUTH_GSS:
7756 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
7757 &secinfo->flavor_info);
7758 break;
7759 default:
7760 flavor = RPC_AUTH_MAXFLAVOR;
7761 break;
7762 }
7763
7764 if (flavor != RPC_AUTH_MAXFLAVOR) {
7765 err = nfs4_lookup_root_sec(server, fhandle,
7766 info, flavor);
7767 if (!err)
7768 break;
7769 }
7770 }
7771
7772 if (flavor == RPC_AUTH_MAXFLAVOR)
7773 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007774
7775out_freepage:
7776 put_page(page);
7777 if (err == -EACCES)
7778 return -EPERM;
7779out:
7780 return err;
7781}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007782
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007783static int _nfs41_test_stateid(struct nfs_server *server,
7784 nfs4_stateid *stateid,
7785 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04007786{
7787 int status;
7788 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007789 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04007790 };
7791 struct nfs41_test_stateid_res res;
7792 struct rpc_message msg = {
7793 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
7794 .rpc_argp = &args,
7795 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007796 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04007797 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007798 struct rpc_clnt *rpc_client = server->client;
7799
7800 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
7801 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007802
Chuck Lever38527b12012-07-11 16:30:23 -04007803 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04007804 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007805 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007806 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007807 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04007808 if (status != NFS_OK) {
7809 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04007810 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04007811 }
7812 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04007813 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04007814}
7815
Chuck Lever38527b12012-07-11 16:30:23 -04007816/**
7817 * nfs41_test_stateid - perform a TEST_STATEID operation
7818 *
7819 * @server: server / transport on which to perform the operation
7820 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007821 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04007822 *
7823 * Returns NFS_OK if the server recognizes that "stateid" is valid.
7824 * Otherwise a negative NFS4ERR value is returned if the operation
7825 * failed or the state ID is not currently valid.
7826 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007827static int nfs41_test_stateid(struct nfs_server *server,
7828 nfs4_stateid *stateid,
7829 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04007830{
7831 struct nfs4_exception exception = { };
7832 int err;
7833 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007834 err = _nfs41_test_stateid(server, stateid, cred);
Chuck Lever377e5072012-07-11 16:29:45 -04007835 if (err != -NFS4ERR_DELAY)
7836 break;
7837 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04007838 } while (exception.retry);
7839 return err;
7840}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007841
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007842struct nfs_free_stateid_data {
7843 struct nfs_server *server;
7844 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007845 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007846};
7847
7848static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
7849{
7850 struct nfs_free_stateid_data *data = calldata;
7851 nfs41_setup_sequence(nfs4_get_session(data->server),
7852 &data->args.seq_args,
7853 &data->res.seq_res,
7854 task);
7855}
7856
7857static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
7858{
7859 struct nfs_free_stateid_data *data = calldata;
7860
7861 nfs41_sequence_done(task, &data->res.seq_res);
7862
7863 switch (task->tk_status) {
7864 case -NFS4ERR_DELAY:
7865 if (nfs4_async_handle_error(task, data->server, NULL) == -EAGAIN)
7866 rpc_restart_call_prepare(task);
7867 }
7868}
7869
7870static void nfs41_free_stateid_release(void *calldata)
7871{
7872 kfree(calldata);
7873}
7874
Trond Myklebust17f26b12013-08-21 15:48:42 -04007875static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007876 .rpc_call_prepare = nfs41_free_stateid_prepare,
7877 .rpc_call_done = nfs41_free_stateid_done,
7878 .rpc_release = nfs41_free_stateid_release,
7879};
7880
7881static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
7882 nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007883 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007884 bool privileged)
7885{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007886 struct rpc_message msg = {
7887 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007888 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007889 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007890 struct rpc_task_setup task_setup = {
7891 .rpc_client = server->client,
7892 .rpc_message = &msg,
7893 .callback_ops = &nfs41_free_stateid_ops,
7894 .flags = RPC_TASK_ASYNC,
7895 };
7896 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007897
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007898 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
7899 &task_setup.rpc_client, &msg);
7900
Chuck Lever38527b12012-07-11 16:30:23 -04007901 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007902 data = kmalloc(sizeof(*data), GFP_NOFS);
7903 if (!data)
7904 return ERR_PTR(-ENOMEM);
7905 data->server = server;
7906 nfs4_stateid_copy(&data->args.stateid, stateid);
7907
7908 task_setup.callback_data = data;
7909
7910 msg.rpc_argp = &data->args;
7911 msg.rpc_resp = &data->res;
Chuck Levera9c92d62013-08-09 12:48:18 -04007912 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007913 if (privileged)
7914 nfs4_set_sequence_privileged(&data->args.seq_args);
7915
7916 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007917}
7918
Chuck Lever38527b12012-07-11 16:30:23 -04007919/**
7920 * nfs41_free_stateid - perform a FREE_STATEID operation
7921 *
7922 * @server: server / transport on which to perform the operation
7923 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007924 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04007925 *
7926 * Returns NFS_OK if the server freed "stateid". Otherwise a
7927 * negative NFS4ERR value is returned.
7928 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007929static int nfs41_free_stateid(struct nfs_server *server,
7930 nfs4_stateid *stateid,
7931 struct rpc_cred *cred)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007932{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007933 struct rpc_task *task;
7934 int ret;
7935
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007936 task = _nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007937 if (IS_ERR(task))
7938 return PTR_ERR(task);
7939 ret = rpc_wait_for_completion_task(task);
7940 if (!ret)
7941 ret = task->tk_status;
7942 rpc_put_task(task);
7943 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007944}
Trond Myklebust36281ca2012-03-04 18:13:56 -05007945
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007946static int nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
7947{
7948 struct rpc_task *task;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007949 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007950
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007951 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007952 nfs4_free_lock_state(server, lsp);
7953 if (IS_ERR(task))
7954 return PTR_ERR(task);
7955 rpc_put_task(task);
7956 return 0;
7957}
7958
Trond Myklebust36281ca2012-03-04 18:13:56 -05007959static bool nfs41_match_stateid(const nfs4_stateid *s1,
7960 const nfs4_stateid *s2)
7961{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007962 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007963 return false;
7964
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007965 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007966 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007967 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007968 return true;
7969
7970 return false;
7971}
7972
Andy Adamson557134a2009-04-01 09:21:53 -04007973#endif /* CONFIG_NFS_V4_1 */
7974
Trond Myklebust36281ca2012-03-04 18:13:56 -05007975static bool nfs4_match_stateid(const nfs4_stateid *s1,
7976 const nfs4_stateid *s2)
7977{
Trond Myklebustf597c532012-03-04 18:13:56 -05007978 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05007979}
7980
7981
Trond Myklebust17280172012-03-11 13:11:00 -04007982static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007983 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007984 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007985 .recover_open = nfs4_open_reclaim,
7986 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04007987 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04007988 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007989};
7990
Andy Adamson591d71c2009-04-01 09:22:47 -04007991#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007992static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007993 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
7994 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
7995 .recover_open = nfs4_open_reclaim,
7996 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05007997 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007998 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04007999 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04008000};
8001#endif /* CONFIG_NFS_V4_1 */
8002
Trond Myklebust17280172012-03-11 13:11:00 -04008003static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008004 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008005 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008006 .recover_open = nfs4_open_expired,
8007 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04008008 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008009};
8010
Andy Adamson591d71c2009-04-01 09:22:47 -04008011#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008012static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008013 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8014 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04008015 .recover_open = nfs41_open_expired,
8016 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05008017 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04008018};
8019#endif /* CONFIG_NFS_V4_1 */
8020
Trond Myklebust17280172012-03-11 13:11:00 -04008021static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008022 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04008023 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008024 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04008025};
8026
8027#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008028static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008029 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04008030 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008031 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04008032};
8033#endif
8034
Chuck Leverec011fe2013-10-17 14:12:39 -04008035static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008036 .get_locations = _nfs40_proc_get_locations,
Chuck Leverec011fe2013-10-17 14:12:39 -04008037};
8038
8039#if defined(CONFIG_NFS_V4_1)
8040static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008041 .get_locations = _nfs41_proc_get_locations,
Chuck Leverec011fe2013-10-17 14:12:39 -04008042};
8043#endif /* CONFIG_NFS_V4_1 */
8044
Trond Myklebust97dc1352010-06-16 09:52:26 -04008045static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
8046 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008047 .init_caps = NFS_CAP_READDIRPLUS
8048 | NFS_CAP_ATOMIC_OPEN
8049 | NFS_CAP_CHANGE_ATTR
8050 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008051 .init_client = nfs40_init_client,
8052 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008053 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008054 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008055 .free_lock_state = nfs4_release_lockowner,
Chuck Lever9915ea72013-08-09 12:48:27 -04008056 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008057 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
8058 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
8059 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008060 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008061};
8062
8063#if defined(CONFIG_NFS_V4_1)
8064static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
8065 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008066 .init_caps = NFS_CAP_READDIRPLUS
8067 | NFS_CAP_ATOMIC_OPEN
8068 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04008069 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04008070 | NFS_CAP_STATEID_NFSV41
8071 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008072 .init_client = nfs41_init_client,
8073 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008074 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008075 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008076 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04008077 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008078 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8079 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8080 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008081 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008082};
8083#endif
8084
Steve Dickson42c2c422013-05-22 12:50:38 -04008085#if defined(CONFIG_NFS_V4_2)
8086static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
8087 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04008088 .init_caps = NFS_CAP_READDIRPLUS
8089 | NFS_CAP_ATOMIC_OPEN
8090 | NFS_CAP_CHANGE_ATTR
8091 | NFS_CAP_POSIX_LOCK
8092 | NFS_CAP_STATEID_NFSV41
8093 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008094 .init_client = nfs41_init_client,
8095 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04008096 .match_stateid = nfs41_match_stateid,
8097 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04008098 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04008099 .call_sync_ops = &nfs41_call_sync_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04008100 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8101 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8102 .state_renewal_ops = &nfs41_state_renewal_ops,
8103};
8104#endif
8105
Trond Myklebust97dc1352010-06-16 09:52:26 -04008106const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
8107 [0] = &nfs_v4_0_minor_ops,
8108#if defined(CONFIG_NFS_V4_1)
8109 [1] = &nfs_v4_1_minor_ops,
8110#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04008111#if defined(CONFIG_NFS_V4_2)
8112 [2] = &nfs_v4_2_minor_ops,
8113#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04008114};
8115
Trond Myklebust17f26b12013-08-21 15:48:42 -04008116static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04008117 .create = nfs_create,
8118 .lookup = nfs_lookup,
8119 .atomic_open = nfs_atomic_open,
8120 .link = nfs_link,
8121 .unlink = nfs_unlink,
8122 .symlink = nfs_symlink,
8123 .mkdir = nfs_mkdir,
8124 .rmdir = nfs_rmdir,
8125 .mknod = nfs_mknod,
8126 .rename = nfs_rename,
8127 .permission = nfs_permission,
8128 .getattr = nfs_getattr,
8129 .setattr = nfs_setattr,
8130 .getxattr = generic_getxattr,
8131 .setxattr = generic_setxattr,
8132 .listxattr = generic_listxattr,
8133 .removexattr = generic_removexattr,
8134};
8135
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08008136static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008137 .permission = nfs_permission,
8138 .getattr = nfs_getattr,
8139 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008140 .getxattr = generic_getxattr,
8141 .setxattr = generic_setxattr,
8142 .listxattr = generic_listxattr,
8143 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008144};
8145
David Howells509de812006-08-22 20:06:11 -04008146const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008147 .version = 4, /* protocol version */
8148 .dentry_ops = &nfs4_dentry_operations,
8149 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008150 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04008151 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008152 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04008153 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04008154 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008155 .getattr = nfs4_proc_getattr,
8156 .setattr = nfs4_proc_setattr,
8157 .lookup = nfs4_proc_lookup,
8158 .access = nfs4_proc_access,
8159 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008160 .create = nfs4_proc_create,
8161 .remove = nfs4_proc_remove,
8162 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04008163 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008164 .unlink_done = nfs4_proc_unlink_done,
8165 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04008166 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04008167 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04008168 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008169 .link = nfs4_proc_link,
8170 .symlink = nfs4_proc_symlink,
8171 .mkdir = nfs4_proc_mkdir,
8172 .rmdir = nfs4_proc_remove,
8173 .readdir = nfs4_proc_readdir,
8174 .mknod = nfs4_proc_mknod,
8175 .statfs = nfs4_proc_statfs,
8176 .fsinfo = nfs4_proc_fsinfo,
8177 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04008178 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008179 .decode_dirent = nfs4_decode_dirent,
8180 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04008181 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04008182 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05008183 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008184 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04008185 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04008186 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008187 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008188 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04008189 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008190 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008191 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00008192 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04008193 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04008194 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04008195 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04008196 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04008197 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00008198 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04008199 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04008200 .create_server = nfs4_create_server,
8201 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008202};
8203
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008204static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
8205 .prefix = XATTR_NAME_NFSV4_ACL,
8206 .list = nfs4_xattr_list_nfs4_acl,
8207 .get = nfs4_xattr_get_nfs4_acl,
8208 .set = nfs4_xattr_set_nfs4_acl,
8209};
8210
8211const struct xattr_handler *nfs4_xattr_handlers[] = {
8212 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04008213#ifdef CONFIG_NFS_V4_SECURITY_LABEL
8214 &nfs4_xattr_nfs4_label_handler,
8215#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008216 NULL
8217};
8218
Linus Torvalds1da177e2005-04-16 15:20:36 -07008219/*
8220 * Local variables:
8221 * c-basic-offset: 8
8222 * End:
8223 */