blob: cb56102ad40e7a2ce272ec3ce3929f84e8c1ec38 [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
842static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
843 struct nfs_server *server,
844 struct rpc_message *msg,
845 struct nfs4_sequence_args *args,
846 struct nfs4_sequence_res *res)
847{
848 int ret;
849 struct rpc_task *task;
850 struct nfs_client *clp = server->nfs_client;
851 struct nfs4_call_sync_data data = {
852 .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,
859 .callback_ops = clp->cl_mvops->call_sync_ops,
860 .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;
868 rpc_put_task(task);
869 }
870 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}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400884
Trond Myklebust38478b22006-05-25 01:40:57 -0400885static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
Trond Myklebust38478b22006-05-25 01:40:57 -0400887 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Trond Myklebust38478b22006-05-25 01:40:57 -0400889 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)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400892 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);
Trond Myklebust38478b22006-05-25 01:40:57 -0400895 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896}
897
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100898struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400899 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100900 struct nfs_openargs o_arg;
901 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100902 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;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100906 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400907 struct nfs4_label *f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100908 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400909 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100910 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400911 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100912 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100913 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400914 unsigned int rpc_done : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -0400915 unsigned int is_recover : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100916 int rpc_status;
917 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100918};
919
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400920static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
921 int err, struct nfs4_exception *exception)
922{
923 if (err != -EINVAL)
924 return false;
925 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
926 return false;
927 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
928 exception->retry = 1;
929 return true;
930}
931
932static enum open_claim_type4
933nfs4_map_atomic_open_claim(struct nfs_server *server,
934 enum open_claim_type4 claim)
935{
936 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
937 return claim;
938 switch (claim) {
939 default:
940 return claim;
941 case NFS4_OPEN_CLAIM_FH:
942 return NFS4_OPEN_CLAIM_NULL;
943 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
944 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
945 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
946 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
947 }
948}
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400949
950static void nfs4_init_opendata_res(struct nfs4_opendata *p)
951{
952 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400953 p->o_res.f_label = p->f_label;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400954 p->o_res.seqid = p->o_arg.seqid;
955 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400956 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400957 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400958 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500959 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400960}
961
Al Viro82a2c1b2011-06-22 18:30:55 -0400962static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500963 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400964 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -0400965 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400966 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400967 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100968{
Al Viro82a2c1b2011-06-22 18:30:55 -0400969 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100970 struct inode *dir = parent->d_inode;
971 struct nfs_server *server = NFS_SERVER(dir);
972 struct nfs4_opendata *p;
973
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400974 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100975 if (p == NULL)
976 goto err;
David Quigley14c43f72013-05-22 12:50:43 -0400977
978 p->f_label = nfs4_label_alloc(server, gfp_mask);
979 if (IS_ERR(p->f_label))
980 goto err_free_p;
981
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400982 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100983 if (p->o_arg.seqid == NULL)
David Quigley14c43f72013-05-22 12:50:43 -0400984 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -0400985 nfs_sb_active(dentry->d_sb);
986 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100987 p->dir = parent;
988 p->owner = sp;
989 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500990 p->o_arg.open_flags = flags;
991 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700992 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
993 * will return permission denied for all bits until close */
994 if (!(flags & O_EXCL)) {
995 /* ask server to check for all possible rights as results
996 * are cached */
997 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
998 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700999 }
David Howells7539bba2006-08-22 20:06:09 -04001000 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001001 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1002 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001003 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001004 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001005 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001006 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
David Quigley1775fd32013-05-22 12:50:42 -04001007 p->o_arg.label = label;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001008 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1009 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001010 case NFS4_OPEN_CLAIM_NULL:
1011 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1012 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1013 p->o_arg.fh = NFS_FH(dir);
1014 break;
1015 case NFS4_OPEN_CLAIM_PREVIOUS:
1016 case NFS4_OPEN_CLAIM_FH:
1017 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1018 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1019 p->o_arg.fh = NFS_FH(dentry->d_inode);
1020 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001021 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001022 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001023
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001024 p->o_arg.u.attrs = &p->attrs;
1025 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001026
1027 verf[0] = jiffies;
1028 verf[1] = current->pid;
1029 memcpy(p->o_arg.u.verifier.data, verf,
1030 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001031 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001032 p->c_arg.fh = &p->o_res.fh;
1033 p->c_arg.stateid = &p->o_res.stateid;
1034 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001035 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001036 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001037 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001038
1039err_free_label:
1040 nfs4_label_free(p->f_label);
1041err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001042 kfree(p);
1043err:
1044 dput(parent);
1045 return NULL;
1046}
1047
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001048static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001049{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001050 struct nfs4_opendata *p = container_of(kref,
1051 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001052 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001053
1054 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001055 if (p->state != NULL)
1056 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001057 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001058
1059 nfs4_label_free(p->f_label);
1060
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001061 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001062 dput(p->dentry);
1063 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001064 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001065 kfree(p);
1066}
1067
1068static void nfs4_opendata_put(struct nfs4_opendata *p)
1069{
1070 if (p != NULL)
1071 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001072}
1073
Trond Myklebust06f814a2006-01-03 09:55:07 +01001074static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1075{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001076 int ret;
1077
Trond Myklebust06f814a2006-01-03 09:55:07 +01001078 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001079 return ret;
1080}
1081
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001082static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001083{
1084 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001085
Trond Myklebust536e43d2012-01-17 22:04:26 -05001086 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001087 goto out;
1088 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001089 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001090 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1091 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001092 break;
1093 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001094 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1095 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001096 break;
1097 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001098 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1099 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001100 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001101out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001102 return ret;
1103}
1104
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001105static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001106{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001107 if (delegation == NULL)
1108 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001109 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001110 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -05001111 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -04001112 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001113 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1114 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -05001115 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001116 return 1;
1117}
1118
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001119static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001120{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001121 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001122 case FMODE_WRITE:
1123 state->n_wronly++;
1124 break;
1125 case FMODE_READ:
1126 state->n_rdonly++;
1127 break;
1128 case FMODE_READ|FMODE_WRITE:
1129 state->n_rdwr++;
1130 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001131 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001132}
1133
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001134static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001135{
1136 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001137 nfs4_stateid_copy(&state->stateid, stateid);
1138 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust92b40e92013-04-20 01:25:45 -04001139 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001140 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001141 case FMODE_READ:
1142 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1143 break;
1144 case FMODE_WRITE:
1145 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1146 break;
1147 case FMODE_READ|FMODE_WRITE:
1148 set_bit(NFS_O_RDWR_STATE, &state->flags);
1149 }
1150}
1151
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001152static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001153{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001154 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001155 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001156 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001157}
1158
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001159static 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 -07001160{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001161 /*
1162 * Protect the call to nfs4_state_set_mode_locked and
1163 * serialise the stateid update
1164 */
1165 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001166 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001167 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001168 set_bit(NFS_DELEGATED_STATE, &state->flags);
1169 }
1170 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001171 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001172 write_sequnlock(&state->seqlock);
1173 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001174 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001175 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176}
1177
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001178static 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 -05001179{
1180 struct nfs_inode *nfsi = NFS_I(state->inode);
1181 struct nfs_delegation *deleg_cur;
1182 int ret = 0;
1183
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001184 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001185
1186 rcu_read_lock();
1187 deleg_cur = rcu_dereference(nfsi->delegation);
1188 if (deleg_cur == NULL)
1189 goto no_delegation;
1190
1191 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001192 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001193 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001194 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001195 goto no_delegation_unlock;
1196
1197 if (delegation == NULL)
1198 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001199 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001200 goto no_delegation_unlock;
1201
Trond Myklebustb7391f42008-12-23 15:21:52 -05001202 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001203 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001204 ret = 1;
1205no_delegation_unlock:
1206 spin_unlock(&deleg_cur->lock);
1207no_delegation:
1208 rcu_read_unlock();
1209
1210 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001211 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001212 ret = 1;
1213 }
1214
1215 return ret;
1216}
1217
1218
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001219static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001220{
1221 struct nfs_delegation *delegation;
1222
1223 rcu_read_lock();
1224 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001225 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001226 rcu_read_unlock();
1227 return;
1228 }
1229 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001230 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001231}
1232
Trond Myklebust6ee41262007-07-08 14:11:36 -04001233static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001234{
1235 struct nfs4_state *state = opendata->state;
1236 struct nfs_inode *nfsi = NFS_I(state->inode);
1237 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001238 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001239 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001240 nfs4_stateid stateid;
1241 int ret = -EAGAIN;
1242
Trond Myklebustaac00a82007-07-05 19:02:21 -04001243 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001244 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001245 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001246 if (can_open_cached(state, fmode, open_mode)) {
1247 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001248 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001249 goto out_return_state;
1250 }
1251 spin_unlock(&state->owner->so_lock);
1252 }
Trond Myklebust34310432008-12-23 15:21:38 -05001253 rcu_read_lock();
1254 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001255 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001256 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001257 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001258 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001259 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001260 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001261 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001262 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001263 if (!opendata->is_recover) {
1264 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1265 if (ret != 0)
1266 goto out;
1267 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001268 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001269
1270 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001271 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001272 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001273 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001274out:
1275 return ERR_PTR(ret);
1276out_return_state:
1277 atomic_inc(&state->count);
1278 return state;
1279}
1280
Andy Adamsone23008e2012-10-02 21:07:32 -04001281static void
1282nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1283{
1284 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1285 struct nfs_delegation *delegation;
1286 int delegation_flags = 0;
1287
1288 rcu_read_lock();
1289 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1290 if (delegation)
1291 delegation_flags = delegation->flags;
1292 rcu_read_unlock();
1293 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1294 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1295 "returning a delegation for "
1296 "OPEN(CLAIM_DELEGATE_CUR)\n",
1297 clp->cl_hostname);
1298 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1299 nfs_inode_set_delegation(state->inode,
1300 data->owner->so_cred,
1301 &data->o_res);
1302 else
1303 nfs_inode_reclaim_delegation(state->inode,
1304 data->owner->so_cred,
1305 &data->o_res);
1306}
1307
1308/*
1309 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1310 * and update the nfs4_state.
1311 */
1312static struct nfs4_state *
1313_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1314{
1315 struct inode *inode = data->state->inode;
1316 struct nfs4_state *state = data->state;
1317 int ret;
1318
1319 if (!data->rpc_done) {
1320 ret = data->rpc_status;
1321 goto err;
1322 }
1323
1324 ret = -ESTALE;
1325 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1326 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1327 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1328 goto err;
1329
1330 ret = -ENOMEM;
1331 state = nfs4_get_open_state(inode, data->owner);
1332 if (state == NULL)
1333 goto err;
1334
1335 ret = nfs_refresh_inode(inode, &data->f_attr);
1336 if (ret)
1337 goto err;
1338
David Quigleyaa9c2662013-05-22 12:50:44 -04001339 nfs_setsecurity(inode, &data->f_attr, data->f_label);
1340
Andy Adamsone23008e2012-10-02 21:07:32 -04001341 if (data->o_res.delegation_type != 0)
1342 nfs4_opendata_check_deleg(data, state);
1343 update_open_stateid(state, &data->o_res.stateid, NULL,
1344 data->o_arg.fmode);
1345
1346 return state;
1347err:
1348 return ERR_PTR(ret);
1349
1350}
1351
1352static struct nfs4_state *
1353_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001354{
1355 struct inode *inode;
1356 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001357 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001358
Trond Myklebustaac00a82007-07-05 19:02:21 -04001359 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001360 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001361 goto out;
1362 }
1363
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001364 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001365 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001366 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001367 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001368 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001369 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001370 goto err;
1371 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001372 state = nfs4_get_open_state(inode, data->owner);
1373 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001374 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001375 if (data->o_res.delegation_type != 0)
1376 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001377 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001378 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001379 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001380out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001381 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001382 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001383err_put_inode:
1384 iput(inode);
1385err:
1386 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001387}
1388
Andy Adamsone23008e2012-10-02 21:07:32 -04001389static struct nfs4_state *
1390nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1391{
1392 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1393 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1394 return _nfs4_opendata_to_nfs4_state(data);
1395}
1396
Trond Myklebust864472e2006-01-03 09:55:15 +01001397static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1398{
1399 struct nfs_inode *nfsi = NFS_I(state->inode);
1400 struct nfs_open_context *ctx;
1401
1402 spin_lock(&state->inode->i_lock);
1403 list_for_each_entry(ctx, &nfsi->open_files, list) {
1404 if (ctx->state != state)
1405 continue;
1406 get_nfs_open_context(ctx);
1407 spin_unlock(&state->inode->i_lock);
1408 return ctx;
1409 }
1410 spin_unlock(&state->inode->i_lock);
1411 return ERR_PTR(-ENOENT);
1412}
1413
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001414static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1415 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001416{
1417 struct nfs4_opendata *opendata;
1418
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001419 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001420 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001421 if (opendata == NULL)
1422 return ERR_PTR(-ENOMEM);
1423 opendata->state = state;
1424 atomic_inc(&state->count);
1425 return opendata;
1426}
1427
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001428static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001429{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001430 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001431 int ret;
1432
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001433 opendata->o_arg.open_flags = 0;
1434 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001435 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1436 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1437 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001438 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001439 if (ret != 0)
1440 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001441 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001442 if (IS_ERR(newstate))
1443 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001444 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001445 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001446 return 0;
1447}
1448
1449static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1450{
Trond Myklebust864472e2006-01-03 09:55:15 +01001451 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001452 int ret;
1453
1454 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001455 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001456 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001457 smp_rmb();
1458 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001459 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001460 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001461 if (ret != 0)
1462 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001463 if (newstate != state)
1464 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001465 }
1466 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001467 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001468 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001469 if (ret != 0)
1470 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001471 if (newstate != state)
1472 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001473 }
1474 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001475 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001476 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001477 if (ret != 0)
1478 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001479 if (newstate != state)
1480 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001481 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001482 /*
1483 * We may have performed cached opens for all three recoveries.
1484 * Check if we need to update the current stateid.
1485 */
1486 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001487 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001488 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001489 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001490 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001491 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001492 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001493 return 0;
1494}
1495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496/*
1497 * OPEN_RECLAIM:
1498 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001500static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001502 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001503 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001504 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 int status;
1506
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001507 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1508 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001509 if (IS_ERR(opendata))
1510 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001511 rcu_read_lock();
1512 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001513 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001514 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001515 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001516 opendata->o_arg.u.delegation_type = delegation_type;
1517 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001518 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 return status;
1520}
1521
Trond Myklebust539cd032007-06-05 11:46:42 -04001522static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523{
1524 struct nfs_server *server = NFS_SERVER(state->inode);
1525 struct nfs4_exception exception = { };
1526 int err;
1527 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001528 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001529 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001530 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1531 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001532 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001533 break;
1534 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 } while (exception.retry);
1536 return err;
1537}
1538
Trond Myklebust864472e2006-01-03 09:55:15 +01001539static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1540{
1541 struct nfs_open_context *ctx;
1542 int ret;
1543
1544 ctx = nfs4_state_find_open_context(state);
1545 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001546 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001547 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001548 put_nfs_open_context(ctx);
1549 return ret;
1550}
1551
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001552static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001554 switch (err) {
1555 default:
1556 printk(KERN_ERR "NFS: %s: unhandled error "
1557 "%d.\n", __func__, err);
1558 case 0:
1559 case -ENOENT:
1560 case -ESTALE:
1561 break;
1562 case -NFS4ERR_BADSESSION:
1563 case -NFS4ERR_BADSLOT:
1564 case -NFS4ERR_BAD_HIGH_SLOT:
1565 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1566 case -NFS4ERR_DEADSESSION:
1567 set_bit(NFS_DELEGATED_STATE, &state->flags);
1568 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1569 return -EAGAIN;
1570 case -NFS4ERR_STALE_CLIENTID:
1571 case -NFS4ERR_STALE_STATEID:
1572 set_bit(NFS_DELEGATED_STATE, &state->flags);
1573 case -NFS4ERR_EXPIRED:
1574 /* Don't recall a delegation if it was lost */
1575 nfs4_schedule_lease_recovery(server->nfs_client);
1576 return -EAGAIN;
1577 case -NFS4ERR_DELEG_REVOKED:
1578 case -NFS4ERR_ADMIN_REVOKED:
1579 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001580 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001581 nfs_inode_find_state_and_recover(state->inode,
1582 stateid);
1583 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001584 return 0;
1585 case -NFS4ERR_DELAY:
1586 case -NFS4ERR_GRACE:
1587 set_bit(NFS_DELEGATED_STATE, &state->flags);
1588 ssleep(1);
1589 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001590 case -ENOMEM:
1591 case -NFS4ERR_DENIED:
1592 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1593 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 return err;
1596}
1597
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001598int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1599{
1600 struct nfs_server *server = NFS_SERVER(state->inode);
1601 struct nfs4_opendata *opendata;
1602 int err;
1603
1604 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1605 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1606 if (IS_ERR(opendata))
1607 return PTR_ERR(opendata);
1608 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1609 err = nfs4_open_recover(opendata, state);
1610 nfs4_opendata_put(opendata);
1611 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1612}
1613
Chuck Leverbe05c862013-08-09 12:49:47 -04001614static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1615{
1616 struct nfs4_opendata *data = calldata;
1617
1618 nfs40_setup_sequence(data->o_arg.server, &data->o_arg.seq_args,
1619 &data->o_res.seq_res, task);
1620}
1621
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001622static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1623{
1624 struct nfs4_opendata *data = calldata;
1625
Chuck Leverbe05c862013-08-09 12:49:47 -04001626 nfs40_sequence_done(task, &data->o_res.seq_res);
1627
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001628 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001629 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001630 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001631 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001632 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001633 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001634 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001635}
1636
1637static void nfs4_open_confirm_release(void *calldata)
1638{
1639 struct nfs4_opendata *data = calldata;
1640 struct nfs4_state *state = NULL;
1641
1642 /* If this request hasn't been cancelled, do nothing */
1643 if (data->cancelled == 0)
1644 goto out_free;
1645 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001646 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001647 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001648 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001649 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001650 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001651out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001652 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001653}
1654
1655static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04001656 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001657 .rpc_call_done = nfs4_open_confirm_done,
1658 .rpc_release = nfs4_open_confirm_release,
1659};
1660
1661/*
1662 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1663 */
1664static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1665{
1666 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1667 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001668 struct rpc_message msg = {
1669 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1670 .rpc_argp = &data->c_arg,
1671 .rpc_resp = &data->c_res,
1672 .rpc_cred = data->owner->so_cred,
1673 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001674 struct rpc_task_setup task_setup_data = {
1675 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001676 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001677 .callback_ops = &nfs4_open_confirm_ops,
1678 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001679 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001680 .flags = RPC_TASK_ASYNC,
1681 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 int status;
1683
Chuck Leverbe05c862013-08-09 12:49:47 -04001684 nfs4_init_sequence(&data->o_arg.seq_args, &data->o_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001685 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001686 data->rpc_done = 0;
1687 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001688 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001689 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001690 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001691 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001692 status = nfs4_wait_for_completion_rpc_task(task);
1693 if (status != 0) {
1694 data->cancelled = 1;
1695 smp_wmb();
1696 } else
1697 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001698 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 return status;
1700}
1701
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001702static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001704 struct nfs4_opendata *data = calldata;
1705 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001706 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001707
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001708 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001709 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001710 /*
1711 * Check if we still need to send an OPEN call, or if we can use
1712 * a delegation instead.
1713 */
1714 if (data->state != NULL) {
1715 struct nfs_delegation *delegation;
1716
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001717 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001718 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001719 rcu_read_lock();
1720 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001721 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
Trond Myklebustcd4c9be2013-04-23 14:46:25 -04001722 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
Trond Myklebust652f89f2011-12-09 19:05:58 -05001723 can_open_delegated(delegation, data->o_arg.fmode))
1724 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001725 rcu_read_unlock();
1726 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001727 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04001728 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust8188df12013-04-23 14:31:19 -04001729 switch (data->o_arg.claim) {
1730 case NFS4_OPEN_CLAIM_PREVIOUS:
1731 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1732 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001733 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04001734 case NFS4_OPEN_CLAIM_FH:
1735 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001736 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1737 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001738 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001739 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001740 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001741 &data->o_res.seq_res,
1742 task) != 0)
1743 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04001744
1745 /* Set the create mode (note dependency on the session type) */
1746 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1747 if (data->o_arg.open_flags & O_EXCL) {
1748 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1749 if (nfs4_has_persistent_session(clp))
1750 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1751 else if (clp->cl_mvops->minor_version > 0)
1752 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1753 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04001754 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001755unlock_no_action:
1756 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001757out_no_action:
1758 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001759out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001760 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001761}
1762
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001763static void nfs4_open_done(struct rpc_task *task, void *calldata)
1764{
1765 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001767 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001768
Trond Myklebust14516c32010-07-31 14:29:06 -04001769 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1770 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001771
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001772 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001773 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1774 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001775 case S_IFREG:
1776 break;
1777 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001778 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001779 break;
1780 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001781 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001782 break;
1783 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001784 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001785 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001786 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001787 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001788 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1789 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001790 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001791 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001792}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001793
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001794static void nfs4_open_release(void *calldata)
1795{
1796 struct nfs4_opendata *data = calldata;
1797 struct nfs4_state *state = NULL;
1798
1799 /* If this request hasn't been cancelled, do nothing */
1800 if (data->cancelled == 0)
1801 goto out_free;
1802 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001803 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001804 goto out_free;
1805 /* In case we need an open_confirm, no cleanup! */
1806 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1807 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001808 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001809 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001810 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001811out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001812 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001813}
1814
1815static const struct rpc_call_ops nfs4_open_ops = {
1816 .rpc_call_prepare = nfs4_open_prepare,
1817 .rpc_call_done = nfs4_open_done,
1818 .rpc_release = nfs4_open_release,
1819};
1820
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001821static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001822{
1823 struct inode *dir = data->dir->d_inode;
1824 struct nfs_server *server = NFS_SERVER(dir);
1825 struct nfs_openargs *o_arg = &data->o_arg;
1826 struct nfs_openres *o_res = &data->o_res;
1827 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001828 struct rpc_message msg = {
1829 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1830 .rpc_argp = o_arg,
1831 .rpc_resp = o_res,
1832 .rpc_cred = data->owner->so_cred,
1833 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001834 struct rpc_task_setup task_setup_data = {
1835 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001836 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001837 .callback_ops = &nfs4_open_ops,
1838 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001839 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001840 .flags = RPC_TASK_ASYNC,
1841 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001842 int status;
1843
Chuck Levera9c92d62013-08-09 12:48:18 -04001844 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001845 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001846 data->rpc_done = 0;
1847 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001848 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001849 data->is_recover = 0;
1850 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001851 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001852 data->is_recover = 1;
1853 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04001854 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001855 if (IS_ERR(task))
1856 return PTR_ERR(task);
1857 status = nfs4_wait_for_completion_rpc_task(task);
1858 if (status != 0) {
1859 data->cancelled = 1;
1860 smp_wmb();
1861 } else
1862 status = data->rpc_status;
1863 rpc_put_task(task);
1864
1865 return status;
1866}
1867
1868static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1869{
1870 struct inode *dir = data->dir->d_inode;
1871 struct nfs_openres *o_res = &data->o_res;
1872 int status;
1873
1874 status = nfs4_run_open_task(data, 1);
1875 if (status != 0 || !data->rpc_done)
1876 return status;
1877
Trond Myklebust6926afd2012-01-07 13:22:46 -05001878 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1879
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001880 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1881 status = _nfs4_proc_open_confirm(data);
1882 if (status != 0)
1883 return status;
1884 }
1885
1886 return status;
1887}
1888
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001889static int nfs4_opendata_access(struct rpc_cred *cred,
1890 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001891 struct nfs4_state *state, fmode_t fmode,
1892 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001893{
1894 struct nfs_access_entry cache;
1895 u32 mask;
1896
1897 /* access call failed or for some reason the server doesn't
1898 * support any access modes -- defer access call until later */
1899 if (opendata->o_res.access_supported == 0)
1900 return 0;
1901
1902 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001903 /* don't check MAY_WRITE - a newly created file may not have
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001904 * write mode bits, but POSIX allows the creating process to write.
1905 * use openflags to check for exec, because fmode won't
1906 * always have FMODE_EXEC set when file open for exec. */
1907 if (openflags & __FMODE_EXEC) {
1908 /* ONLY check for exec rights */
1909 mask = MAY_EXEC;
1910 } else if (fmode & FMODE_READ)
1911 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001912
1913 cache.cred = cred;
1914 cache.jiffies = jiffies;
1915 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1916 nfs_access_add_cache(state->inode, &cache);
1917
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001918 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001919 return 0;
1920
1921 /* even though OPEN succeeded, access is denied. Close the file */
1922 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001923 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001924}
1925
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001926/*
1927 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1928 */
1929static int _nfs4_proc_open(struct nfs4_opendata *data)
1930{
1931 struct inode *dir = data->dir->d_inode;
1932 struct nfs_server *server = NFS_SERVER(dir);
1933 struct nfs_openargs *o_arg = &data->o_arg;
1934 struct nfs_openres *o_res = &data->o_res;
1935 int status;
1936
1937 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001938 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001939 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001940 if (status != 0) {
1941 if (status == -NFS4ERR_BADNAME &&
1942 !(o_arg->open_flags & O_CREAT))
1943 return -ENOENT;
1944 return status;
1945 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001946
Trond Myklebust6926afd2012-01-07 13:22:46 -05001947 nfs_fattr_map_and_free_names(server, &data->f_attr);
1948
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001949 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001950 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001951 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1952 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001954 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001956 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 }
1958 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
David Quigley1775fd32013-05-22 12:50:42 -04001959 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001960 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961}
1962
Andy Adamsond83217c2011-03-01 01:34:17 +00001963static int nfs4_recover_expired_lease(struct nfs_server *server)
1964{
1965 return nfs4_client_recover_expired_lease(server->nfs_client);
1966}
1967
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968/*
1969 * OPEN_EXPIRED:
1970 * reclaim state on the server after a network partition.
1971 * Assumes caller holds the appropriate lock
1972 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001973static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001975 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001976 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001978 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001979 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001980 if (IS_ERR(opendata))
1981 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001982 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001983 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001984 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001985 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001986 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987}
1988
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001989static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001990{
Trond Myklebust539cd032007-06-05 11:46:42 -04001991 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001992 struct nfs4_exception exception = { };
1993 int err;
1994
1995 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001996 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001997 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001998 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1999 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002000 switch (err) {
2001 default:
2002 goto out;
2003 case -NFS4ERR_GRACE:
2004 case -NFS4ERR_DELAY:
2005 nfs4_handle_exception(server, err, &exception);
2006 err = 0;
2007 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002008 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002009out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002010 return err;
2011}
2012
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2014{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002016 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Trond Myklebust864472e2006-01-03 09:55:15 +01002018 ctx = nfs4_state_find_open_context(state);
2019 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002020 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002021 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002022 put_nfs_open_context(ctx);
2023 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024}
2025
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002026#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002027static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002028{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002029 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002030 nfs4_stateid *stateid = &state->stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002031 struct nfs_delegation *delegation;
2032 struct rpc_cred *cred = NULL;
2033 int status = -NFS4ERR_BAD_STATEID;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002034
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002035 /* If a state reset has been done, test_stateid is unneeded */
2036 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2037 return;
2038
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002039 /* Get the delegation credential for use by test/free_stateid */
2040 rcu_read_lock();
2041 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2042 if (delegation != NULL &&
2043 nfs4_stateid_match(&delegation->stateid, stateid)) {
2044 cred = get_rpccred(delegation->cred);
2045 rcu_read_unlock();
2046 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002047 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002048 } else
2049 rcu_read_unlock();
2050
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002051 if (status != NFS_OK) {
2052 /* Free the stateid unless the server explicitly
2053 * informs us the stateid is unrecognized. */
2054 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002055 nfs41_free_stateid(server, stateid, cred);
Bryan Schumaker69388672012-09-26 15:25:52 -04002056 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002057
Bryan Schumaker69388672012-09-26 15:25:52 -04002058 write_seqlock(&state->seqlock);
2059 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2060 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002061 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2062 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002063
2064 if (cred != NULL)
2065 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002066}
2067
2068/**
2069 * nfs41_check_open_stateid - possibly free an open stateid
2070 *
2071 * @state: NFSv4 state for an inode
2072 *
2073 * Returns NFS_OK if recovery for this stateid is now finished.
2074 * Otherwise a negative NFS4ERR value is returned.
2075 */
2076static int nfs41_check_open_stateid(struct nfs4_state *state)
2077{
2078 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002079 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002080 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002081 int status;
2082
2083 /* If a state reset has been done, test_stateid is unneeded */
2084 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2085 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2086 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2087 return -NFS4ERR_BAD_STATEID;
2088
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002089 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002090 trace_nfs4_test_open_stateid(state, NULL, status);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002091 if (status != NFS_OK) {
2092 /* Free the stateid unless the server explicitly
2093 * informs us the stateid is unrecognized. */
2094 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002095 nfs41_free_stateid(server, stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002096
2097 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2098 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2099 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002100 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002101 }
2102 return status;
2103}
2104
2105static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2106{
Chuck Levereb64cf92012-07-11 16:30:05 -04002107 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002108
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002109 nfs41_clear_delegation_stateid(state);
2110 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002111 if (status != NFS_OK)
2112 status = nfs4_open_expired(sp, state);
2113 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002114}
2115#endif
2116
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002118 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2119 * fields corresponding to attributes that were used to store the verifier.
2120 * Make sure we clobber those fields in the later setattr call
2121 */
2122static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2123{
2124 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2125 !(sattr->ia_valid & ATTR_ATIME_SET))
2126 sattr->ia_valid |= ATTR_ATIME;
2127
2128 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2129 !(sattr->ia_valid & ATTR_MTIME_SET))
2130 sattr->ia_valid |= ATTR_MTIME;
2131}
2132
Trond Myklebustc21443c2013-02-07 14:26:21 -05002133static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2134 fmode_t fmode,
2135 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002136 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002137{
2138 struct nfs4_state_owner *sp = opendata->owner;
2139 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002140 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002141 struct nfs4_state *state;
2142 unsigned int seq;
2143 int ret;
2144
2145 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2146
2147 ret = _nfs4_proc_open(opendata);
2148 if (ret != 0)
2149 goto out;
2150
2151 state = nfs4_opendata_to_nfs4_state(opendata);
2152 ret = PTR_ERR(state);
2153 if (IS_ERR(state))
2154 goto out;
2155 if (server->caps & NFS_CAP_POSIX_LOCK)
2156 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2157
Trond Myklebust275bb302013-05-29 13:11:28 -04002158 dentry = opendata->dentry;
2159 if (dentry->d_inode == NULL) {
2160 /* FIXME: Is this d_drop() ever needed? */
2161 d_drop(dentry);
2162 dentry = d_add_unique(dentry, igrab(state->inode));
2163 if (dentry == NULL) {
2164 dentry = opendata->dentry;
2165 } else if (dentry != ctx->dentry) {
2166 dput(ctx->dentry);
2167 ctx->dentry = dget(dentry);
2168 }
2169 nfs_set_verifier(dentry,
2170 nfs_save_change_attribute(opendata->dir->d_inode));
2171 }
2172
Trond Myklebustc21443c2013-02-07 14:26:21 -05002173 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2174 if (ret != 0)
2175 goto out;
2176
Trond Myklebust3efb9722013-05-29 13:17:04 -04002177 ctx->state = state;
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002178 if (dentry->d_inode == state->inode) {
2179 nfs_inode_attach_open_context(ctx);
2180 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2181 nfs4_schedule_stateid_recovery(server, state);
2182 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002183out:
2184 return ret;
2185}
2186
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002187/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002188 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 */
Andy Adamson82be4172012-05-23 05:02:35 -04002190static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002191 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002192 int flags,
2193 struct iattr *sattr,
Trond Myklebust959d9212013-06-28 16:29:51 -04002194 struct nfs4_label *label)
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 }
Andy Adamson82be4172012-05-23 05:02:35 -04002264
2265 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2266 *ctx_th = opendata->f_attr.mdsthreshold;
2267 else
2268 kfree(opendata->f_attr.mdsthreshold);
2269 opendata->f_attr.mdsthreshold = NULL;
2270
David Quigley14c43f72013-05-22 12:50:43 -04002271 nfs4_label_free(olabel);
2272
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002273 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002276err_free_label:
2277 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002278err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002279 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002280 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002281err_put_state_owner:
2282 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 return status;
2285}
2286
2287
Andy Adamson82be4172012-05-23 05:02:35 -04002288static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002289 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002290 int flags,
2291 struct iattr *sattr,
Trond Myklebust959d9212013-06-28 16:29:51 -04002292 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002294 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 struct nfs4_exception exception = { };
2296 struct nfs4_state *res;
2297 int status;
2298
2299 do {
Trond Myklebust959d9212013-06-28 16:29:51 -04002300 status = _nfs4_do_open(dir, ctx, flags, sattr, label);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002301 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002302 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 if (status == 0)
2304 break;
2305 /* NOTE: BAD_SEQID means the server and client disagree about the
2306 * book-keeping w.r.t. state-changing operations
2307 * (OPEN/CLOSE/LOCK/LOCKU...)
2308 * It is actually a sign of a bug on the client or on the server.
2309 *
2310 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002311 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 * have unhashed the old state_owner for us, and that we can
2313 * therefore safely retry using a new one. We should still warn
2314 * the user though...
2315 */
2316 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002317 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002318 " returned a bad sequence-id error!\n",
2319 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 exception.retry = 1;
2321 continue;
2322 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002323 /*
2324 * BAD_STATEID on OPEN means that the server cancelled our
2325 * state before it received the OPEN_CONFIRM.
2326 * Recover by retrying the request as per the discussion
2327 * on Page 181 of RFC3530.
2328 */
2329 if (status == -NFS4ERR_BAD_STATEID) {
2330 exception.retry = 1;
2331 continue;
2332 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002333 if (status == -EAGAIN) {
2334 /* We must have found a delegation */
2335 exception.retry = 1;
2336 continue;
2337 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002338 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2339 continue;
2340 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 status, &exception));
2342 } while (exception.retry);
2343 return res;
2344}
2345
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002346static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2347 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002348 struct nfs4_state *state, struct nfs4_label *ilabel,
2349 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002351 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002353 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 .iap = sattr,
2355 .server = server,
2356 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002357 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 };
2359 struct nfs_setattrres res = {
2360 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002361 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 .server = server,
2363 };
2364 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002365 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2366 .rpc_argp = &arg,
2367 .rpc_resp = &res,
2368 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002370 unsigned long timestamp = jiffies;
Trond Myklebustee3ae842013-04-29 10:35:36 -04002371 fmode_t fmode;
2372 bool truncate;
Trond Myklebust65e43082005-08-16 11:49:44 -04002373 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
David Quigleyaa9c2662013-05-22 12:50:44 -04002375 arg.bitmask = nfs4_bitmask(server, ilabel);
2376 if (ilabel)
2377 arg.bitmask = nfs4_bitmask(server, olabel);
2378
Trond Myklebust0e574af2005-10-27 22:12:38 -04002379 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
Trond Myklebustee3ae842013-04-29 10:35:36 -04002381 /* Servers should only apply open mode checks for file size changes */
2382 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2383 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2384
2385 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2386 /* Use that stateid */
2387 } else if (truncate && state != NULL && nfs4_valid_open_stateid(state)) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002388 struct nfs_lockowner lockowner = {
2389 .l_owner = current->files,
2390 .l_pid = current->tgid,
2391 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002392 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002393 &lockowner);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002394 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002395 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
Bryan Schumaker7c513052011-03-24 17:12:24 +00002397 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002398 if (status == 0 && state != NULL)
2399 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002400 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401}
2402
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002403static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2404 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002405 struct nfs4_state *state, struct nfs4_label *ilabel,
2406 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002408 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002409 struct nfs4_exception exception = {
2410 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002411 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002412 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 int err;
2414 do {
David Quigley1775fd32013-05-22 12:50:42 -04002415 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
Trond Myklebustc1578b72013-08-12 16:58:42 -04002416 trace_nfs4_setattr(inode, err);
Trond Myklebust451146b2012-04-18 16:29:11 -04002417 switch (err) {
2418 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002419 if (!(sattr->ia_valid & ATTR_SIZE)) {
2420 pr_warn_once("NFSv4: server %s is incorrectly "
2421 "applying open mode checks to "
2422 "a SETATTR that is not "
2423 "changing file size.\n",
2424 server->nfs_client->cl_hostname);
2425 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002426 if (state && !(state->state & FMODE_WRITE)) {
2427 err = -EBADF;
2428 if (sattr->ia_valid & ATTR_OPEN)
2429 err = -EACCES;
2430 goto out;
2431 }
2432 }
2433 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002435out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 return err;
2437}
2438
2439struct nfs4_closedata {
2440 struct inode *inode;
2441 struct nfs4_state *state;
2442 struct nfs_closeargs arg;
2443 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002444 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002445 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002446 bool roc;
2447 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448};
2449
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002450static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002451{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002452 struct nfs4_closedata *calldata = data;
2453 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002454 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002455
Fred Isamanf7e89172011-01-06 11:36:32 +00002456 if (calldata->roc)
2457 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002458 nfs4_put_open_state(calldata->state);
2459 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002460 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002461 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002462 kfree(calldata);
2463}
2464
Trond Myklebust88069f72009-12-08 08:33:16 -05002465static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2466 fmode_t fmode)
2467{
2468 spin_lock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002469 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002470 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
2471 case FMODE_WRITE:
2472 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2473 break;
2474 case FMODE_READ:
2475 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2476 break;
2477 case 0:
2478 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2479 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2480 clear_bit(NFS_OPEN_STATE, &state->flags);
2481 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002482 spin_unlock(&state->owner->so_lock);
2483}
2484
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002485static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002487 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 struct nfs_server *server = NFS_SERVER(calldata->inode);
2490
Chuck Levera3ca5652012-03-01 17:00:40 -05002491 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002492 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2493 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002494 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 /* hmm. we are done with the inode, and in the process of freeing
2496 * the state_owner. we keep this around to process errors
2497 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 switch (task->tk_status) {
2499 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002500 if (calldata->roc)
2501 pnfs_roc_set_barrier(state->inode,
2502 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002503 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002504 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002505 nfs4_close_clear_stateid_flags(state,
2506 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 break;
2508 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002509 case -NFS4ERR_OLD_STATEID:
2510 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002512 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002513 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002515 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2516 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002518 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002519 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002520 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521}
2522
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002523static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002525 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002526 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002527 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002528 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002529
Chuck Levera3ca5652012-03-01 17:00:40 -05002530 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002531 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002532 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002533
Trond Myklebust88069f72009-12-08 08:33:16 -05002534 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2535 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002536 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002537 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002538 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002539 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002540 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2541 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2542 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002543 }
2544 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002545 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2546 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2547 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002548 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002549 }
Trond Myklebust5d422302013-03-14 16:57:48 -04002550 if (!nfs4_valid_open_stateid(state))
2551 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002552 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002553
2554 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002555 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002556 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002557 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002558
Fred Isamanf7e89172011-01-06 11:36:32 +00002559 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002560 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002561 if (calldata->roc &&
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002562 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2563 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002564 goto out_wait;
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002565 }
Fred Isamanf7e89172011-01-06 11:36:32 +00002566 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002567
Trond Myklebust516a6af2005-10-27 22:12:41 -04002568 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002569 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002570 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002571 &calldata->arg.seq_args,
2572 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002573 task) != 0)
2574 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002575 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002576 return;
2577out_no_action:
2578 task->tk_action = NULL;
2579out_wait:
2580 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581}
2582
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002583static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002584 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002585 .rpc_call_done = nfs4_close_done,
2586 .rpc_release = nfs4_free_closedata,
2587};
2588
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589/*
2590 * It is possible for data to be read/written from a mem-mapped file
2591 * after the sys_close call (which hits the vfs layer as a flush).
2592 * This means that we can't safely call nfsv4 close on a file until
2593 * the inode is cleared. This in turn means that we are not good
2594 * NFSv4 citizens - we do not indicate to the server to update the file's
2595 * share state even when we are done with one of the three share
2596 * stateid's in the inode.
2597 *
2598 * NOTE: Caller must be holding the sp->so_owner semaphore!
2599 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002600int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002602 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002604 struct nfs4_state_owner *sp = state->owner;
2605 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002606 struct rpc_message msg = {
2607 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2608 .rpc_cred = state->owner->so_cred,
2609 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002610 struct rpc_task_setup task_setup_data = {
2611 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002612 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002613 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002614 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002615 .flags = RPC_TASK_ASYNC,
2616 };
Trond Myklebust95121352005-10-18 14:20:12 -07002617 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04002619 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
2620 &task_setup_data.rpc_client, &msg);
2621
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002622 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002624 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04002625 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002626 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002628 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002629 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002631 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002632 if (calldata->arg.seqid == NULL)
2633 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002634 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002635 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002636 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002637 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002638 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002639 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002640 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002641
Trond Myklebust1174dd12010-12-21 10:52:24 -05002642 msg.rpc_argp = &calldata->arg;
2643 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002644 task_setup_data.callback_data = calldata;
2645 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002646 if (IS_ERR(task))
2647 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002648 status = 0;
2649 if (wait)
2650 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002651 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002652 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002653out_free_calldata:
2654 kfree(calldata);
2655out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002656 nfs4_put_open_state(state);
2657 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002658 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659}
2660
Trond Myklebust2b484292010-09-17 10:56:51 -04002661static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002662nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04002665 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2666
2667 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668
Trond Myklebust565277f2007-10-15 18:17:53 -04002669 /* Protect against concurrent sillydeletes */
Trond Myklebust959d9212013-06-28 16:29:51 -04002670 state = nfs4_do_open(dir, ctx, open_flags, attr, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04002671
2672 nfs4_label_release_security(label);
2673
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002674 if (IS_ERR(state))
2675 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04002676 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677}
2678
Trond Myklebust1185a552009-12-03 15:54:02 -05002679static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002680{
2681 if (ctx->state == NULL)
2682 return;
2683 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002684 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002685 else
Al Viro643168c2011-06-22 18:20:23 -04002686 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002687}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688
2689static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2690{
Benny Halevy43652ad2009-04-01 09:21:54 -04002691 struct nfs4_server_caps_arg args = {
2692 .fhandle = fhandle,
2693 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 struct nfs4_server_caps_res res = {};
2695 struct rpc_message msg = {
2696 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002697 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 .rpc_resp = &res,
2699 };
2700 int status;
2701
Bryan Schumaker7c513052011-03-24 17:12:24 +00002702 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 if (status == 0) {
2704 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002705 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2706 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2707 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2708 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2709 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2711 server->caps |= NFS_CAP_ACLS;
2712 if (res.has_links != 0)
2713 server->caps |= NFS_CAP_HARDLINKS;
2714 if (res.has_symlinks != 0)
2715 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002716 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2717 server->caps |= NFS_CAP_FILEID;
2718 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2719 server->caps |= NFS_CAP_MODE;
2720 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2721 server->caps |= NFS_CAP_NLINK;
2722 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2723 server->caps |= NFS_CAP_OWNER;
2724 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2725 server->caps |= NFS_CAP_OWNER_GROUP;
2726 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2727 server->caps |= NFS_CAP_ATIME;
2728 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2729 server->caps |= NFS_CAP_CTIME;
2730 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2731 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04002732#ifdef CONFIG_NFS_V4_SECURITY_LABEL
2733 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
2734 server->caps |= NFS_CAP_SECURITY_LABEL;
2735#endif
2736 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
2737 sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002738
David Quigleyaa9c2662013-05-22 12:50:44 -04002739 if (server->caps & NFS_CAP_SECURITY_LABEL) {
2740 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2741 res.attr_bitmask[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2742 }
Trond Myklebusta65318b2009-03-11 14:10:28 -04002743 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2744 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2745 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002747 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002749
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 return status;
2751}
2752
Trond Myklebust55a97592006-06-09 09:34:19 -04002753int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754{
2755 struct nfs4_exception exception = { };
2756 int err;
2757 do {
2758 err = nfs4_handle_exception(server,
2759 _nfs4_server_capabilities(server, fhandle),
2760 &exception);
2761 } while (exception.retry);
2762 return err;
2763}
2764
2765static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2766 struct nfs_fsinfo *info)
2767{
David Quigleyaa9c2662013-05-22 12:50:44 -04002768 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04002770 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 };
2772 struct nfs4_lookup_res res = {
2773 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002774 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 .fh = fhandle,
2776 };
2777 struct rpc_message msg = {
2778 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2779 .rpc_argp = &args,
2780 .rpc_resp = &res,
2781 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002782
David Quigleyaa9c2662013-05-22 12:50:44 -04002783 bitmask[0] = nfs4_fattr_bitmap[0];
2784 bitmask[1] = nfs4_fattr_bitmap[1];
2785 /*
2786 * Process the label in the upcoming getfattr
2787 */
2788 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
2789
Trond Myklebust0e574af2005-10-27 22:12:38 -04002790 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002791 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792}
2793
2794static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2795 struct nfs_fsinfo *info)
2796{
2797 struct nfs4_exception exception = { };
2798 int err;
2799 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002800 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04002801 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002802 switch (err) {
2803 case 0:
2804 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002805 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002806 default:
2807 err = nfs4_handle_exception(server, err, &exception);
2808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002810out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 return err;
2812}
2813
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002814static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2815 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2816{
Trond Myklebustc2190662013-08-26 19:23:04 -04002817 struct rpc_auth_create_args auth_args = {
2818 .pseudoflavor = flavor,
2819 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002820 struct rpc_auth *auth;
2821 int ret;
2822
Trond Myklebustc2190662013-08-26 19:23:04 -04002823 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002824 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04002825 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002826 goto out;
2827 }
2828 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002829out:
2830 return ret;
2831}
2832
Chuck Lever9a744ba2013-03-16 15:56:02 -04002833/*
2834 * Retry pseudoroot lookup with various security flavors. We do this when:
2835 *
2836 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2837 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2838 *
2839 * Returns zero on success, or a negative NFS4ERR value, or a
2840 * negative errno value.
2841 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002842static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002843 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844{
Chuck Lever9a744ba2013-03-16 15:56:02 -04002845 /* Per 3530bis 15.33.5 */
2846 static const rpc_authflavor_t flav_array[] = {
2847 RPC_AUTH_GSS_KRB5P,
2848 RPC_AUTH_GSS_KRB5I,
2849 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04002850 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04002851 RPC_AUTH_NULL,
2852 };
2853 int status = -EPERM;
2854 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855
Chuck Lever9a744ba2013-03-16 15:56:02 -04002856 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002857 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002858 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002859 continue;
2860 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002861 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04002862
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002863 /*
2864 * -EACCESS could mean that the user doesn't have correct permissions
2865 * to access the mount. It could also mean that we tried to mount
2866 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2867 * existing mount programs don't handle -EACCES very well so it should
2868 * be mapped to -EPERM instead.
2869 */
2870 if (status == -EACCES)
2871 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002872 return status;
2873}
2874
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002875static int nfs4_do_find_root_sec(struct nfs_server *server,
2876 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
2877{
2878 int mv = server->nfs_client->cl_minorversion;
2879 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
2880}
2881
2882/**
2883 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
2884 * @server: initialized nfs_server handle
2885 * @fhandle: we fill in the pseudo-fs root file handle
2886 * @info: we fill in an FSINFO struct
2887 *
2888 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002889 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002890int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2891 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002892{
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002893 int status;
2894
2895 status = nfs4_lookup_root(server, fhandle, info);
2896 if ((status == -NFS4ERR_WRONGSEC) &&
2897 !(server->flags & NFS_MOUNT_SECFLAVOUR))
2898 status = nfs4_do_find_root_sec(server, fhandle, info);
2899
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 if (status == 0)
2901 status = nfs4_server_capabilities(server, fhandle);
2902 if (status == 0)
2903 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002904
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002905 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906}
2907
Bryan Schumakerbae36242012-05-10 15:07:31 -04002908static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2909 struct nfs_fsinfo *info)
2910{
2911 int error;
2912 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04002913 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04002914
2915 error = nfs4_server_capabilities(server, mntfh);
2916 if (error < 0) {
2917 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2918 return error;
2919 }
2920
David Quigley14c43f72013-05-22 12:50:43 -04002921 label = nfs4_label_alloc(server, GFP_KERNEL);
2922 if (IS_ERR(label))
2923 return PTR_ERR(label);
2924
David Quigley1775fd32013-05-22 12:50:42 -04002925 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04002926 if (error < 0) {
2927 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04002928 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04002929 }
2930
2931 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2932 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2933 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2934
David Quigley14c43f72013-05-22 12:50:43 -04002935err_free_label:
2936 nfs4_label_free(label);
2937
Bryan Schumakerbae36242012-05-10 15:07:31 -04002938 return error;
2939}
2940
Manoj Naik6b97fd32006-06-09 09:34:29 -04002941/*
2942 * Get locations and (maybe) other attributes of a referral.
2943 * Note that we'll actually follow the referral later when
2944 * we detect fsid mismatch in inode revalidation
2945 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002946static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2947 const struct qstr *name, struct nfs_fattr *fattr,
2948 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002949{
2950 int status = -ENOMEM;
2951 struct page *page = NULL;
2952 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002953
2954 page = alloc_page(GFP_KERNEL);
2955 if (page == NULL)
2956 goto out;
2957 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2958 if (locations == NULL)
2959 goto out;
2960
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002961 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002962 if (status != 0)
2963 goto out;
2964 /* Make sure server returned a different fsid for the referral */
2965 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002966 dprintk("%s: server did not return a different fsid for"
2967 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002968 status = -EIO;
2969 goto out;
2970 }
Andy Adamson533eb462011-06-13 18:25:56 -04002971 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2972 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002973
Andy Adamson533eb462011-06-13 18:25:56 -04002974 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002975 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002976 memset(fhandle, 0, sizeof(struct nfs_fh));
2977out:
2978 if (page)
2979 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002980 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002981 return status;
2982}
2983
David Quigley1775fd32013-05-22 12:50:42 -04002984static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
2985 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986{
2987 struct nfs4_getattr_arg args = {
2988 .fh = fhandle,
2989 .bitmask = server->attr_bitmask,
2990 };
2991 struct nfs4_getattr_res res = {
2992 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002993 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 .server = server,
2995 };
2996 struct rpc_message msg = {
2997 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2998 .rpc_argp = &args,
2999 .rpc_resp = &res,
3000 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003001
3002 args.bitmask = nfs4_bitmask(server, label);
3003
Trond Myklebust0e574af2005-10-27 22:12:38 -04003004 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003005 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006}
3007
David Quigley1775fd32013-05-22 12:50:42 -04003008static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3009 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010{
3011 struct nfs4_exception exception = { };
3012 int err;
3013 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003014 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3015 trace_nfs4_getattr(server, fhandle, fattr, err);
3016 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 &exception);
3018 } while (exception.retry);
3019 return err;
3020}
3021
3022/*
3023 * The file is not closed if it is opened due to the a request to change
3024 * the size of the file. The open call will not be needed once the
3025 * VFS layer lookup-intents are implemented.
3026 *
3027 * Close is called when the inode is destroyed.
3028 * If we haven't opened the file for O_WRONLY, we
3029 * need to in the size_change case to obtain a stateid.
3030 *
3031 * Got race?
3032 * Because OPEN is always done by name in nfsv4, it is
3033 * possible that we opened a different file by the same
3034 * name. We can recognize this race condition, but we
3035 * can't do anything about it besides returning an error.
3036 *
3037 * This will be fixed with VFS changes (lookup-intent).
3038 */
3039static int
3040nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3041 struct iattr *sattr)
3042{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003043 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003044 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003045 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003046 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 int status;
3048
Benny Halevy8a1636c2010-07-14 15:43:57 -04003049 if (pnfs_ld_layoutret_on_setattr(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003050 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003051
Trond Myklebust0e574af2005-10-27 22:12:38 -04003052 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053
Andy Adamson26699402012-05-30 16:12:24 -04003054 /* Deal with open(O_TRUNC) */
3055 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003056 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003057
3058 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003059 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003060 return 0;
3061
Trond Myklebustd5308382005-11-04 15:33:38 -05003062 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003063 if (sattr->ia_valid & ATTR_FILE) {
3064 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003065
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003066 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003067 if (ctx) {
3068 cred = ctx->cred;
3069 state = ctx->state;
3070 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003071 }
3072
David Quigley14c43f72013-05-22 12:50:43 -04003073 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3074 if (IS_ERR(label))
3075 return PTR_ERR(label);
3076
3077 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003078 if (status == 0) {
Trond Myklebust65e43082005-08-16 11:49:44 -04003079 nfs_setattr_update_inode(inode, sattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003080 nfs_setsecurity(inode, fattr, label);
3081 }
David Quigley14c43f72013-05-22 12:50:43 -04003082 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 return status;
3084}
3085
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003086static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3087 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003088 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003089{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003090 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003091 int status;
3092 struct nfs4_lookup_arg args = {
3093 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003094 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003095 .name = name,
3096 };
3097 struct nfs4_lookup_res res = {
3098 .server = server,
3099 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003100 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003101 .fh = fhandle,
3102 };
3103 struct rpc_message msg = {
3104 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3105 .rpc_argp = &args,
3106 .rpc_resp = &res,
3107 };
3108
David Quigleyaa9c2662013-05-22 12:50:44 -04003109 args.bitmask = nfs4_bitmask(server, label);
3110
David Howells2b3de442006-08-22 20:06:09 -04003111 nfs_fattr_init(fattr);
3112
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003114 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 dprintk("NFS reply lookup: %d\n", status);
3116 return status;
3117}
3118
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003119static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003120{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003121 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003122 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003123 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3124 fattr->nlink = 2;
3125}
3126
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003127static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3128 struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003129 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003130{
3131 struct nfs4_exception exception = { };
3132 struct rpc_clnt *client = *clnt;
3133 int err;
3134 do {
David Quigley1775fd32013-05-22 12:50:42 -04003135 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003136 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003137 switch (err) {
3138 case -NFS4ERR_BADNAME:
3139 err = -ENOENT;
3140 goto out;
3141 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003142 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003143 goto out;
3144 case -NFS4ERR_WRONGSEC:
3145 err = -EPERM;
3146 if (client != *clnt)
3147 goto out;
3148
3149 client = nfs4_create_sec_client(client, dir, name);
3150 if (IS_ERR(client))
3151 return PTR_ERR(client);
3152
3153 exception.retry = 1;
3154 break;
3155 default:
3156 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3157 }
3158 } while (exception.retry);
3159
3160out:
3161 if (err == 0)
3162 *clnt = client;
3163 else if (client != *clnt)
3164 rpc_shutdown_client(client);
3165
3166 return err;
3167}
3168
Bryan Schumaker80a16b22012-04-27 13:27:46 -04003169static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003170 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3171 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003173 int status;
3174 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003175
David Quigley1775fd32013-05-22 12:50:42 -04003176 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003177 if (client != NFS_CLIENT(dir)) {
3178 rpc_shutdown_client(client);
3179 nfs_fixup_secinfo_attributes(fattr);
3180 }
3181 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182}
3183
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003184struct rpc_clnt *
3185nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3186 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3187{
3188 int status;
3189 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
3190
David Quigley1775fd32013-05-22 12:50:42 -04003191 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003192 if (status < 0) {
3193 rpc_shutdown_client(client);
3194 return ERR_PTR(status);
3195 }
3196 return client;
3197}
3198
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3200{
Trond Myklebust76b32992007-08-10 17:45:11 -04003201 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 struct nfs4_accessargs args = {
3203 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003204 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003206 struct nfs4_accessres res = {
3207 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003208 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 struct rpc_message msg = {
3210 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3211 .rpc_argp = &args,
3212 .rpc_resp = &res,
3213 .rpc_cred = entry->cred,
3214 };
3215 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003216 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217
3218 /*
3219 * Determine which access bits we want to ask for...
3220 */
3221 if (mode & MAY_READ)
3222 args.access |= NFS4_ACCESS_READ;
3223 if (S_ISDIR(inode->i_mode)) {
3224 if (mode & MAY_WRITE)
3225 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3226 if (mode & MAY_EXEC)
3227 args.access |= NFS4_ACCESS_LOOKUP;
3228 } else {
3229 if (mode & MAY_WRITE)
3230 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3231 if (mode & MAY_EXEC)
3232 args.access |= NFS4_ACCESS_EXECUTE;
3233 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003234
3235 res.fattr = nfs_alloc_fattr();
3236 if (res.fattr == NULL)
3237 return -ENOMEM;
3238
Bryan Schumaker7c513052011-03-24 17:12:24 +00003239 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003241 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003242 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003244 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 return status;
3246}
3247
3248static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3249{
3250 struct nfs4_exception exception = { };
3251 int err;
3252 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003253 err = _nfs4_proc_access(inode, entry);
3254 trace_nfs4_access(inode, err);
3255 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 &exception);
3257 } while (exception.retry);
3258 return err;
3259}
3260
3261/*
3262 * TODO: For the time being, we don't try to get any attributes
3263 * along with any of the zero-copy operations READ, READDIR,
3264 * READLINK, WRITE.
3265 *
3266 * In the case of the first three, we want to put the GETATTR
3267 * after the read-type operation -- this is because it is hard
3268 * to predict the length of a GETATTR response in v4, and thus
3269 * align the READ data correctly. This means that the GETATTR
3270 * may end up partially falling into the page cache, and we should
3271 * shift it into the 'tail' of the xdr_buf before processing.
3272 * To do this efficiently, we need to know the total length
3273 * of data received, which doesn't seem to be available outside
3274 * of the RPC layer.
3275 *
3276 * In the case of WRITE, we also want to put the GETATTR after
3277 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003278 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 *
3280 * Both of these changes to the XDR layer would in fact be quite
3281 * minor, but I decided to leave them for a subsequent patch.
3282 */
3283static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3284 unsigned int pgbase, unsigned int pglen)
3285{
3286 struct nfs4_readlink args = {
3287 .fh = NFS_FH(inode),
3288 .pgbase = pgbase,
3289 .pglen = pglen,
3290 .pages = &page,
3291 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003292 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 struct rpc_message msg = {
3294 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3295 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003296 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 };
3298
Bryan Schumaker7c513052011-03-24 17:12:24 +00003299 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 -07003300}
3301
3302static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3303 unsigned int pgbase, unsigned int pglen)
3304{
3305 struct nfs4_exception exception = { };
3306 int err;
3307 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003308 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3309 trace_nfs4_readlink(inode, err);
3310 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 &exception);
3312 } while (exception.retry);
3313 return err;
3314}
3315
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003317 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319static int
3320nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003321 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322{
David Quigleyaa9c2662013-05-22 12:50:44 -04003323 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003324 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 int status = 0;
3327
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003328 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3329 if (IS_ERR(ctx))
3330 return PTR_ERR(ctx);
3331
David Quigleyaa9c2662013-05-22 12:50:44 -04003332 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3333
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003334 sattr->ia_mode &= ~current_umask();
Trond Myklebust959d9212013-06-28 16:29:51 -04003335 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 if (IS_ERR(state)) {
3337 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003338 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003341 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003342 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 return status;
3344}
3345
3346static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3347{
Trond Myklebust16e42952005-10-27 22:12:44 -04003348 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003349 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003351 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003353 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003354 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003355 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003357 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3358 .rpc_argp = &args,
3359 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003361 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003362
Bryan Schumaker7c513052011-03-24 17:12:24 +00003363 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003364 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003365 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 return status;
3367}
3368
3369static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3370{
3371 struct nfs4_exception exception = { };
3372 int err;
3373 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003374 err = _nfs4_proc_remove(dir, name);
3375 trace_nfs4_remove(dir, name, err);
3376 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 &exception);
3378 } while (exception.retry);
3379 return err;
3380}
3381
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003382static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003384 struct nfs_server *server = NFS_SERVER(dir);
3385 struct nfs_removeargs *args = msg->rpc_argp;
3386 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003388 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04003390 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003391
3392 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393}
3394
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003395static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3396{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003397 nfs4_setup_sequence(NFS_SERVER(data->dir),
3398 &data->args.seq_args,
3399 &data->res.seq_res,
3400 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401}
3402
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003403static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003405 struct nfs_unlinkdata *data = task->tk_calldata;
3406 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003407
Trond Myklebust14516c32010-07-31 14:29:06 -04003408 if (!nfs4_sequence_done(task, &res->seq_res))
3409 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003410 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003411 return 0;
3412 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003413 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414}
3415
Jeff Laytond3d41522010-09-17 17:31:57 -04003416static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3417{
3418 struct nfs_server *server = NFS_SERVER(dir);
3419 struct nfs_renameargs *arg = msg->rpc_argp;
3420 struct nfs_renameres *res = msg->rpc_resp;
3421
3422 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003423 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04003424 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003425}
3426
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003427static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3428{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003429 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3430 &data->args.seq_args,
3431 &data->res.seq_res,
3432 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003433}
3434
3435static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3436 struct inode *new_dir)
3437{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003438 struct nfs_renamedata *data = task->tk_calldata;
3439 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04003440
3441 if (!nfs4_sequence_done(task, &res->seq_res))
3442 return 0;
3443 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3444 return 0;
3445
3446 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003447 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003448 return 1;
3449}
3450
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3452 struct inode *new_dir, struct qstr *new_name)
3453{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003454 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003455 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 .old_dir = NFS_FH(old_dir),
3457 .new_dir = NFS_FH(new_dir),
3458 .old_name = old_name,
3459 .new_name = new_name,
3460 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003461 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003462 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003463 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 struct rpc_message msg = {
3465 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3466 .rpc_argp = &arg,
3467 .rpc_resp = &res,
3468 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003469 int status = -ENOMEM;
David Quigley14c43f72013-05-22 12:50:43 -04003470
Bryan Schumaker7c513052011-03-24 17:12:24 +00003471 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 if (!status) {
3473 update_changeattr(old_dir, &res.old_cinfo);
3474 update_changeattr(new_dir, &res.new_cinfo);
3475 }
3476 return status;
3477}
3478
3479static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3480 struct inode *new_dir, struct qstr *new_name)
3481{
3482 struct nfs4_exception exception = { };
3483 int err;
3484 do {
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003485 err = _nfs4_proc_rename(old_dir, old_name,
3486 new_dir, new_name);
3487 trace_nfs4_rename(old_dir, old_name, new_dir, new_name, err);
3488 err = nfs4_handle_exception(NFS_SERVER(old_dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 &exception);
3490 } while (exception.retry);
3491 return err;
3492}
3493
3494static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3495{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003496 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 struct nfs4_link_arg arg = {
3498 .fh = NFS_FH(inode),
3499 .dir_fh = NFS_FH(dir),
3500 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003501 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003503 struct nfs4_link_res res = {
3504 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003505 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003506 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 struct rpc_message msg = {
3508 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3509 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003510 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003512 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513
Trond Myklebust136f2622010-04-16 16:22:49 -04003514 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003515 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003516 goto out;
3517
David Quigley14c43f72013-05-22 12:50:43 -04003518 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3519 if (IS_ERR(res.label)) {
3520 status = PTR_ERR(res.label);
3521 goto out;
3522 }
David Quigleyaa9c2662013-05-22 12:50:44 -04003523 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04003524
Bryan Schumaker7c513052011-03-24 17:12:24 +00003525 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003526 if (!status) {
3527 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04003528 status = nfs_post_op_update_inode(inode, res.fattr);
3529 if (!status)
3530 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003531 }
David Quigley14c43f72013-05-22 12:50:43 -04003532
3533
3534 nfs4_label_free(res.label);
3535
Trond Myklebust136f2622010-04-16 16:22:49 -04003536out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003537 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 return status;
3539}
3540
3541static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3542{
3543 struct nfs4_exception exception = { };
3544 int err;
3545 do {
3546 err = nfs4_handle_exception(NFS_SERVER(inode),
3547 _nfs4_proc_link(inode, dir, name),
3548 &exception);
3549 } while (exception.retry);
3550 return err;
3551}
3552
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003553struct nfs4_createdata {
3554 struct rpc_message msg;
3555 struct nfs4_create_arg arg;
3556 struct nfs4_create_res res;
3557 struct nfs_fh fh;
3558 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003559 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003560};
3561
3562static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3563 struct qstr *name, struct iattr *sattr, u32 ftype)
3564{
3565 struct nfs4_createdata *data;
3566
3567 data = kzalloc(sizeof(*data), GFP_KERNEL);
3568 if (data != NULL) {
3569 struct nfs_server *server = NFS_SERVER(dir);
3570
David Quigley14c43f72013-05-22 12:50:43 -04003571 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3572 if (IS_ERR(data->label))
3573 goto out_free;
3574
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003575 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3576 data->msg.rpc_argp = &data->arg;
3577 data->msg.rpc_resp = &data->res;
3578 data->arg.dir_fh = NFS_FH(dir);
3579 data->arg.server = server;
3580 data->arg.name = name;
3581 data->arg.attrs = sattr;
3582 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04003583 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003584 data->res.server = server;
3585 data->res.fh = &data->fh;
3586 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003587 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003588 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003589 }
3590 return data;
David Quigley14c43f72013-05-22 12:50:43 -04003591out_free:
3592 kfree(data);
3593 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003594}
3595
3596static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3597{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003598 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003599 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003600 if (status == 0) {
3601 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04003602 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003603 }
3604 return status;
3605}
3606
3607static void nfs4_free_createdata(struct nfs4_createdata *data)
3608{
David Quigley14c43f72013-05-22 12:50:43 -04003609 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003610 kfree(data);
3611}
3612
Chuck Lever4f390c12006-08-22 20:06:22 -04003613static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003614 struct page *page, unsigned int len, struct iattr *sattr,
3615 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003617 struct nfs4_createdata *data;
3618 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619
Chuck Lever94a6d752006-08-22 20:06:23 -04003620 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003621 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003622
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003623 status = -ENOMEM;
3624 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3625 if (data == NULL)
3626 goto out;
3627
3628 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3629 data->arg.u.symlink.pages = &page;
3630 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04003631 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003633 status = nfs4_do_create(dir, dentry, data);
3634
3635 nfs4_free_createdata(data);
3636out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 return status;
3638}
3639
Chuck Lever4f390c12006-08-22 20:06:22 -04003640static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003641 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642{
3643 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003644 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04003646
3647 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3648
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003650 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3651 trace_nfs4_symlink(dir, &dentry->d_name, err);
3652 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 &exception);
3654 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003655
3656 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 return err;
3658}
3659
3660static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003661 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003663 struct nfs4_createdata *data;
3664 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003666 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3667 if (data == NULL)
3668 goto out;
3669
David Quigley1775fd32013-05-22 12:50:42 -04003670 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003671 status = nfs4_do_create(dir, dentry, data);
3672
3673 nfs4_free_createdata(data);
3674out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 return status;
3676}
3677
3678static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3679 struct iattr *sattr)
3680{
3681 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003682 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003684
David Quigleyaa9c2662013-05-22 12:50:44 -04003685 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3686
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003687 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003689 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3690 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3691 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 &exception);
3693 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003694 nfs4_label_release_security(label);
3695
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 return err;
3697}
3698
3699static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003700 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701{
3702 struct inode *dir = dentry->d_inode;
3703 struct nfs4_readdir_arg args = {
3704 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003705 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 .pgbase = 0,
3707 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003708 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003709 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 };
3711 struct nfs4_readdir_res res;
3712 struct rpc_message msg = {
3713 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3714 .rpc_argp = &args,
3715 .rpc_resp = &res,
3716 .rpc_cred = cred,
3717 };
3718 int status;
3719
Harvey Harrison3110ff82008-05-02 13:42:44 -07003720 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003721 dentry->d_parent->d_name.name,
3722 dentry->d_name.name,
3723 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003724 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003726 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 -05003727 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003728 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003729 status += args.pgbase;
3730 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003731
3732 nfs_invalidate_atime(dir);
3733
Harvey Harrison3110ff82008-05-02 13:42:44 -07003734 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 return status;
3736}
3737
3738static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003739 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740{
3741 struct nfs4_exception exception = { };
3742 int err;
3743 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003744 err = _nfs4_proc_readdir(dentry, cred, cookie,
3745 pages, count, plus);
3746 trace_nfs4_readdir(dentry->d_inode, err);
3747 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 &exception);
3749 } while (exception.retry);
3750 return err;
3751}
3752
3753static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04003754 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003756 struct nfs4_createdata *data;
3757 int mode = sattr->ia_mode;
3758 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003760 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3761 if (data == NULL)
3762 goto out;
3763
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003765 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003767 data->arg.ftype = NF4BLK;
3768 data->arg.u.device.specdata1 = MAJOR(rdev);
3769 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 }
3771 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003772 data->arg.ftype = NF4CHR;
3773 data->arg.u.device.specdata1 = MAJOR(rdev);
3774 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003775 } else if (!S_ISSOCK(mode)) {
3776 status = -EINVAL;
3777 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 }
David Quigley1775fd32013-05-22 12:50:42 -04003779
David Quigleyaa9c2662013-05-22 12:50:44 -04003780 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003781 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003782out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003783 nfs4_free_createdata(data);
3784out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 return status;
3786}
3787
3788static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3789 struct iattr *sattr, dev_t rdev)
3790{
3791 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003792 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003794
David Quigleyaa9c2662013-05-22 12:50:44 -04003795 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3796
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003797 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003799 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
3800 trace_nfs4_mknod(dir, &dentry->d_name, err);
3801 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 &exception);
3803 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003804
3805 nfs4_label_release_security(label);
3806
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 return err;
3808}
3809
3810static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3811 struct nfs_fsstat *fsstat)
3812{
3813 struct nfs4_statfs_arg args = {
3814 .fh = fhandle,
3815 .bitmask = server->attr_bitmask,
3816 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003817 struct nfs4_statfs_res res = {
3818 .fsstat = fsstat,
3819 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 struct rpc_message msg = {
3821 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3822 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003823 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 };
3825
Trond Myklebust0e574af2005-10-27 22:12:38 -04003826 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003827 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828}
3829
3830static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3831{
3832 struct nfs4_exception exception = { };
3833 int err;
3834 do {
3835 err = nfs4_handle_exception(server,
3836 _nfs4_proc_statfs(server, fhandle, fsstat),
3837 &exception);
3838 } while (exception.retry);
3839 return err;
3840}
3841
3842static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3843 struct nfs_fsinfo *fsinfo)
3844{
3845 struct nfs4_fsinfo_arg args = {
3846 .fh = fhandle,
3847 .bitmask = server->attr_bitmask,
3848 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003849 struct nfs4_fsinfo_res res = {
3850 .fsinfo = fsinfo,
3851 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 struct rpc_message msg = {
3853 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3854 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003855 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 };
3857
Bryan Schumaker7c513052011-03-24 17:12:24 +00003858 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859}
3860
3861static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3862{
3863 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04003864 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 int err;
3866
3867 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04003868 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003869 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04003870 if (err == 0) {
3871 struct nfs_client *clp = server->nfs_client;
3872
3873 spin_lock(&clp->cl_lock);
3874 clp->cl_lease_time = fsinfo->lease_time * HZ;
3875 clp->cl_last_renewal = now;
3876 spin_unlock(&clp->cl_lock);
3877 break;
3878 }
3879 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 } while (exception.retry);
3881 return err;
3882}
3883
3884static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3885{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003886 int error;
3887
Trond Myklebust0e574af2005-10-27 22:12:38 -04003888 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003889 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003890 if (error == 0) {
3891 /* block layout checks this! */
3892 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003893 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003894 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003895
3896 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897}
3898
3899static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3900 struct nfs_pathconf *pathconf)
3901{
3902 struct nfs4_pathconf_arg args = {
3903 .fh = fhandle,
3904 .bitmask = server->attr_bitmask,
3905 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003906 struct nfs4_pathconf_res res = {
3907 .pathconf = pathconf,
3908 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 struct rpc_message msg = {
3910 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3911 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003912 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 };
3914
3915 /* None of the pathconf attributes are mandatory to implement */
3916 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3917 memset(pathconf, 0, sizeof(*pathconf));
3918 return 0;
3919 }
3920
Trond Myklebust0e574af2005-10-27 22:12:38 -04003921 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003922 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923}
3924
3925static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3926 struct nfs_pathconf *pathconf)
3927{
3928 struct nfs4_exception exception = { };
3929 int err;
3930
3931 do {
3932 err = nfs4_handle_exception(server,
3933 _nfs4_proc_pathconf(server, fhandle, pathconf),
3934 &exception);
3935 } while (exception.retry);
3936 return err;
3937}
3938
Trond Myklebust5521abf2013-03-16 20:54:34 -04003939int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04003940 const struct nfs_open_context *ctx,
3941 const struct nfs_lock_context *l_ctx,
3942 fmode_t fmode)
3943{
3944 const struct nfs_lockowner *lockowner = NULL;
3945
3946 if (l_ctx != NULL)
3947 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003948 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04003949}
3950EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
3951
Trond Myklebust5521abf2013-03-16 20:54:34 -04003952static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
3953 const struct nfs_open_context *ctx,
3954 const struct nfs_lock_context *l_ctx,
3955 fmode_t fmode)
3956{
3957 nfs4_stateid current_stateid;
3958
3959 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode))
3960 return false;
3961 return nfs4_stateid_match(stateid, &current_stateid);
3962}
3963
3964static bool nfs4_error_stateid_expired(int err)
3965{
3966 switch (err) {
3967 case -NFS4ERR_DELEG_REVOKED:
3968 case -NFS4ERR_ADMIN_REVOKED:
3969 case -NFS4ERR_BAD_STATEID:
3970 case -NFS4ERR_STALE_STATEID:
3971 case -NFS4ERR_OLD_STATEID:
3972 case -NFS4ERR_OPENMODE:
3973 case -NFS4ERR_EXPIRED:
3974 return true;
3975 }
3976 return false;
3977}
3978
Benny Halevyd20581a2011-05-22 19:52:03 +03003979void __nfs4_read_done_cb(struct nfs_read_data *data)
3980{
Fred Isamancd841602012-04-20 14:47:44 -04003981 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003982}
3983
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003984static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985{
Fred Isamancd841602012-04-20 14:47:44 -04003986 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987
Trond Myklebustcc668ab2013-08-14 15:31:28 -04003988 trace_nfs4_read(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003989 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003990 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003991 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003993
Benny Halevyd20581a2011-05-22 19:52:03 +03003994 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003996 renew_lease(server, data->timestamp);
3997 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998}
3999
Trond Myklebust5521abf2013-03-16 20:54:34 -04004000static bool nfs4_read_stateid_changed(struct rpc_task *task,
4001 struct nfs_readargs *args)
4002{
4003
4004 if (!nfs4_error_stateid_expired(task->tk_status) ||
4005 nfs4_stateid_is_current(&args->stateid,
4006 args->context,
4007 args->lock_context,
4008 FMODE_READ))
4009 return false;
4010 rpc_restart_call_prepare(task);
4011 return true;
4012}
4013
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004014static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
4015{
4016
4017 dprintk("--> %s\n", __func__);
4018
4019 if (!nfs4_sequence_done(task, &data->res.seq_res))
4020 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004021 if (nfs4_read_stateid_changed(task, &data->args))
4022 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03004023 return data->read_done_cb ? data->read_done_cb(task, data) :
4024 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004025}
4026
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004027static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004030 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004031 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Chuck Levera9c92d62013-08-09 12:48:18 -04004032 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033}
4034
NeilBrownef1820f2013-09-04 17:04:49 +10004035static int nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004036{
Trond Myklebust9b206142013-03-17 15:52:00 -04004037 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004038 &data->args.seq_args,
4039 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004040 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004041 return 0;
4042 if (nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4043 data->args.lock_context, FMODE_READ) == -EIO)
4044 return -EIO;
4045 if (unlikely(test_bit(NFS_CONTEXT_BAD, &data->args.context->flags)))
4046 return -EIO;
4047 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048}
4049
Fred Isamanb029bc92011-03-03 15:13:42 +00004050static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051{
Fred Isamancd841602012-04-20 14:47:44 -04004052 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004054 trace_nfs4_write(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004055 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004056 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004057 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004059 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04004061 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004062 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004063 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064}
4065
Trond Myklebust5521abf2013-03-16 20:54:34 -04004066static bool nfs4_write_stateid_changed(struct rpc_task *task,
4067 struct nfs_writeargs *args)
4068{
4069
4070 if (!nfs4_error_stateid_expired(task->tk_status) ||
4071 nfs4_stateid_is_current(&args->stateid,
4072 args->context,
4073 args->lock_context,
4074 FMODE_WRITE))
4075 return false;
4076 rpc_restart_call_prepare(task);
4077 return true;
4078}
4079
Fred Isamanb029bc92011-03-03 15:13:42 +00004080static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
4081{
4082 if (!nfs4_sequence_done(task, &data->res.seq_res))
4083 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004084 if (nfs4_write_stateid_changed(task, &data->args))
4085 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03004086 return data->write_done_cb ? data->write_done_cb(task, data) :
4087 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00004088}
4089
Trond Myklebust5a37f852012-04-28 14:55:16 -04004090static
4091bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00004092{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004093 const struct nfs_pgio_header *hdr = data->header;
4094
4095 /* Don't request attributes for pNFS or O_DIRECT writes */
4096 if (data->ds_clp != NULL || hdr->dreq != NULL)
4097 return false;
4098 /* Otherwise, request attributes if and only if we don't hold
4099 * a delegation
4100 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004101 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004102}
Fred Isamana69aef12011-03-03 15:13:47 +00004103
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004104static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105{
Fred Isamancd841602012-04-20 14:47:44 -04004106 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004107
Trond Myklebust5a37f852012-04-28 14:55:16 -04004108 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00004109 data->args.bitmask = NULL;
4110 data->res.fattr = NULL;
4111 } else
4112 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004113
Fred Isamanb029bc92011-03-03 15:13:42 +00004114 if (!data->write_done_cb)
4115 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004116 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 data->timestamp = jiffies;
4118
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004119 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004120 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121}
4122
NeilBrownef1820f2013-09-04 17:04:49 +10004123static int nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04004124{
Trond Myklebust9b206142013-03-17 15:52:00 -04004125 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004126 &data->args.seq_args,
4127 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004128 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004129 return 0;
4130 if (nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4131 data->args.lock_context, FMODE_WRITE) == -EIO)
4132 return -EIO;
4133 if (unlikely(test_bit(NFS_CONTEXT_BAD, &data->args.context->flags)))
4134 return -EIO;
4135 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136}
4137
Fred Isaman0b7c0152012-04-20 14:47:39 -04004138static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4139{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004140 nfs4_setup_sequence(NFS_SERVER(data->inode),
4141 &data->args.seq_args,
4142 &data->res.seq_res,
4143 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144}
4145
Fred Isaman0b7c0152012-04-20 14:47:39 -04004146static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004149
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004150 trace_nfs4_commit(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004151 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004152 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004153 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004155 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156}
4157
Fred Isaman0b7c0152012-04-20 14:47:39 -04004158static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004159{
4160 if (!nfs4_sequence_done(task, &data->res.seq_res))
4161 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004162 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004163}
4164
Fred Isaman0b7c0152012-04-20 14:47:39 -04004165static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004167 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004168
Fred Isaman0b7c0152012-04-20 14:47:39 -04004169 if (data->commit_done_cb == NULL)
4170 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004171 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004172 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004173 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174}
4175
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004176struct nfs4_renewdata {
4177 struct nfs_client *client;
4178 unsigned long timestamp;
4179};
4180
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181/*
4182 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4183 * standalone procedure for queueing an asynchronous RENEW.
4184 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004185static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004186{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004187 struct nfs4_renewdata *data = calldata;
4188 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004189
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004190 if (atomic_read(&clp->cl_count) > 1)
4191 nfs4_schedule_state_renewal(clp);
4192 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004193 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004194}
4195
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004196static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004198 struct nfs4_renewdata *data = calldata;
4199 struct nfs_client *clp = data->client;
4200 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004202 trace_nfs4_renew_async(clp, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04004204 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004205 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4206 return;
4207 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004208 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004209 return;
4210 }
4211 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004213 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214}
4215
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004216static const struct rpc_call_ops nfs4_renew_ops = {
4217 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004218 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004219};
4220
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004221static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222{
4223 struct rpc_message msg = {
4224 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4225 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004226 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004228 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004230 if (renew_flags == 0)
4231 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004232 if (!atomic_inc_not_zero(&clp->cl_count))
4233 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004234 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004235 if (data == NULL)
4236 return -ENOMEM;
4237 data->client = clp;
4238 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004239 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004240 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241}
4242
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004243static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244{
4245 struct rpc_message msg = {
4246 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4247 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004248 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 };
4250 unsigned long now = jiffies;
4251 int status;
4252
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004253 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 if (status < 0)
4255 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004256 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 return 0;
4258}
4259
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004260static inline int nfs4_server_supports_acls(struct nfs_server *server)
4261{
4262 return (server->caps & NFS_CAP_ACLS)
4263 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
4264 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
4265}
4266
Trond Myklebust21f498c2012-08-24 10:59:25 -04004267/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4268 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004269 * the stack.
4270 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004271#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004272
Neil Hormane9e3d722011-03-04 19:26:03 -05004273static int buf_to_pages_noslab(const void *buf, size_t buflen,
4274 struct page **pages, unsigned int *pgbase)
4275{
4276 struct page *newpage, **spages;
4277 int rc = 0;
4278 size_t len;
4279 spages = pages;
4280
4281 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004282 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004283 newpage = alloc_page(GFP_KERNEL);
4284
4285 if (newpage == NULL)
4286 goto unwind;
4287 memcpy(page_address(newpage), buf, len);
4288 buf += len;
4289 buflen -= len;
4290 *pages++ = newpage;
4291 rc++;
4292 } while (buflen != 0);
4293
4294 return rc;
4295
4296unwind:
4297 for(; rc > 0; rc--)
4298 __free_page(spages[rc-1]);
4299 return -ENOMEM;
4300}
4301
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004302struct nfs4_cached_acl {
4303 int cached;
4304 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004305 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004306};
4307
4308static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004309{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004310 struct nfs_inode *nfsi = NFS_I(inode);
4311
4312 spin_lock(&inode->i_lock);
4313 kfree(nfsi->nfs4_acl);
4314 nfsi->nfs4_acl = acl;
4315 spin_unlock(&inode->i_lock);
4316}
4317
4318static void nfs4_zap_acl_attr(struct inode *inode)
4319{
4320 nfs4_set_cached_acl(inode, NULL);
4321}
4322
4323static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4324{
4325 struct nfs_inode *nfsi = NFS_I(inode);
4326 struct nfs4_cached_acl *acl;
4327 int ret = -ENOENT;
4328
4329 spin_lock(&inode->i_lock);
4330 acl = nfsi->nfs4_acl;
4331 if (acl == NULL)
4332 goto out;
4333 if (buf == NULL) /* user is just asking for length */
4334 goto out_len;
4335 if (acl->cached == 0)
4336 goto out;
4337 ret = -ERANGE; /* see getxattr(2) man page */
4338 if (acl->len > buflen)
4339 goto out;
4340 memcpy(buf, acl->data, acl->len);
4341out_len:
4342 ret = acl->len;
4343out:
4344 spin_unlock(&inode->i_lock);
4345 return ret;
4346}
4347
Sachin Prabhu5794d212012-04-17 14:36:40 +01004348static 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 +00004349{
4350 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004351 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004352
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004353 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004354 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004355 if (acl == NULL)
4356 goto out;
4357 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004358 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004359 } else {
4360 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4361 if (acl == NULL)
4362 goto out;
4363 acl->cached = 0;
4364 }
4365 acl->len = acl_len;
4366out:
4367 nfs4_set_cached_acl(inode, acl);
4368}
4369
Andy Adamsonbf118a32011-12-07 11:55:27 -05004370/*
4371 * The getxattr API returns the required buffer length when called with a
4372 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4373 * the required buf. On a NULL buf, we send a page of data to the server
4374 * guessing that the ACL request can be serviced by a page. If so, we cache
4375 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4376 * the cache. If not so, we throw away the page, and cache the required
4377 * length. The next getxattr call will then produce another round trip to
4378 * the server, this time with the input buf of the required size.
4379 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004380static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004381{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004382 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004383 struct nfs_getaclargs args = {
4384 .fh = NFS_FH(inode),
4385 .acl_pages = pages,
4386 .acl_len = buflen,
4387 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004388 struct nfs_getaclres res = {
4389 .acl_len = buflen,
4390 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004391 struct rpc_message msg = {
4392 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4393 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004394 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004395 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004396 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4397 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004398
Andy Adamsonbf118a32011-12-07 11:55:27 -05004399 /* As long as we're doing a round trip to the server anyway,
4400 * let's be prepared for a page of acl data. */
4401 if (npages == 0)
4402 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004403 if (npages > ARRAY_SIZE(pages))
4404 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004405
Andy Adamsonbf118a32011-12-07 11:55:27 -05004406 for (i = 0; i < npages; i++) {
4407 pages[i] = alloc_page(GFP_KERNEL);
4408 if (!pages[i])
4409 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004410 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004411
4412 /* for decoding across pages */
4413 res.acl_scratch = alloc_page(GFP_KERNEL);
4414 if (!res.acl_scratch)
4415 goto out_free;
4416
Andy Adamsonbf118a32011-12-07 11:55:27 -05004417 args.acl_len = npages * PAGE_SIZE;
4418 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004419
Peng Taode040be2012-01-10 22:42:47 +08004420 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004421 __func__, buf, buflen, npages, args.acl_len);
4422 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4423 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004424 if (ret)
4425 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004426
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004427 /* Handle the case where the passed-in buffer is too short */
4428 if (res.acl_flags & NFS4_ACL_TRUNC) {
4429 /* Did the user only issue a request for the acl length? */
4430 if (buf == NULL)
4431 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004432 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004433 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004434 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004435 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004436 if (buf) {
4437 if (res.acl_len > buflen) {
4438 ret = -ERANGE;
4439 goto out_free;
4440 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004441 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004442 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004443out_ok:
4444 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004445out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004446 for (i = 0; i < npages; i++)
4447 if (pages[i])
4448 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004449 if (res.acl_scratch)
4450 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004451 return ret;
4452}
4453
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004454static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4455{
4456 struct nfs4_exception exception = { };
4457 ssize_t ret;
4458 do {
4459 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04004460 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004461 if (ret >= 0)
4462 break;
4463 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4464 } while (exception.retry);
4465 return ret;
4466}
4467
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004468static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4469{
4470 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004471 int ret;
4472
4473 if (!nfs4_server_supports_acls(server))
4474 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004475 ret = nfs_revalidate_inode(server, inode);
4476 if (ret < 0)
4477 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004478 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4479 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004480 ret = nfs4_read_cached_acl(inode, buf, buflen);
4481 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004482 /* -ENOENT is returned if there is no ACL or if there is an ACL
4483 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004484 return ret;
4485 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004486}
4487
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004488static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004489{
4490 struct nfs_server *server = NFS_SERVER(inode);
4491 struct page *pages[NFS4ACL_MAXPAGES];
4492 struct nfs_setaclargs arg = {
4493 .fh = NFS_FH(inode),
4494 .acl_pages = pages,
4495 .acl_len = buflen,
4496 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004497 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004498 struct rpc_message msg = {
4499 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4500 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004501 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004502 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004503 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004504 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004505
4506 if (!nfs4_server_supports_acls(server))
4507 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004508 if (npages > ARRAY_SIZE(pages))
4509 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004510 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4511 if (i < 0)
4512 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004513 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004514 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004515
4516 /*
4517 * Free each page after tx, so the only ref left is
4518 * held by the network stack
4519 */
4520 for (; i > 0; i--)
4521 put_page(pages[i-1]);
4522
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004523 /*
4524 * Acl update can result in inode attribute update.
4525 * so mark the attribute cache invalid.
4526 */
4527 spin_lock(&inode->i_lock);
4528 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4529 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004530 nfs_access_zap_cache(inode);
4531 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004532 return ret;
4533}
4534
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004535static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4536{
4537 struct nfs4_exception exception = { };
4538 int err;
4539 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004540 err = __nfs4_proc_set_acl(inode, buf, buflen);
4541 trace_nfs4_set_acl(inode, err);
4542 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004543 &exception);
4544 } while (exception.retry);
4545 return err;
4546}
4547
David Quigleyaa9c2662013-05-22 12:50:44 -04004548#ifdef CONFIG_NFS_V4_SECURITY_LABEL
4549static int _nfs4_get_security_label(struct inode *inode, void *buf,
4550 size_t buflen)
4551{
4552 struct nfs_server *server = NFS_SERVER(inode);
4553 struct nfs_fattr fattr;
4554 struct nfs4_label label = {0, 0, buflen, buf};
4555
4556 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4557 struct nfs4_getattr_arg args = {
4558 .fh = NFS_FH(inode),
4559 .bitmask = bitmask,
4560 };
4561 struct nfs4_getattr_res res = {
4562 .fattr = &fattr,
4563 .label = &label,
4564 .server = server,
4565 };
4566 struct rpc_message msg = {
4567 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4568 .rpc_argp = &args,
4569 .rpc_resp = &res,
4570 };
4571 int ret;
4572
4573 nfs_fattr_init(&fattr);
4574
4575 ret = rpc_call_sync(server->client, &msg, 0);
4576 if (ret)
4577 return ret;
4578 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4579 return -ENOENT;
4580 if (buflen < label.len)
4581 return -ERANGE;
4582 return 0;
4583}
4584
4585static int nfs4_get_security_label(struct inode *inode, void *buf,
4586 size_t buflen)
4587{
4588 struct nfs4_exception exception = { };
4589 int err;
4590
4591 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4592 return -EOPNOTSUPP;
4593
4594 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004595 err = _nfs4_get_security_label(inode, buf, buflen);
4596 trace_nfs4_get_security_label(inode, err);
4597 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004598 &exception);
4599 } while (exception.retry);
4600 return err;
4601}
4602
4603static int _nfs4_do_set_security_label(struct inode *inode,
4604 struct nfs4_label *ilabel,
4605 struct nfs_fattr *fattr,
4606 struct nfs4_label *olabel)
4607{
4608
4609 struct iattr sattr = {0};
4610 struct nfs_server *server = NFS_SERVER(inode);
4611 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4612 struct nfs_setattrargs args = {
4613 .fh = NFS_FH(inode),
4614 .iap = &sattr,
4615 .server = server,
4616 .bitmask = bitmask,
4617 .label = ilabel,
4618 };
4619 struct nfs_setattrres res = {
4620 .fattr = fattr,
4621 .label = olabel,
4622 .server = server,
4623 };
4624 struct rpc_message msg = {
4625 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
4626 .rpc_argp = &args,
4627 .rpc_resp = &res,
4628 };
4629 int status;
4630
4631 nfs4_stateid_copy(&args.stateid, &zero_stateid);
4632
4633 status = rpc_call_sync(server->client, &msg, 0);
4634 if (status)
4635 dprintk("%s failed: %d\n", __func__, status);
4636
4637 return status;
4638}
4639
4640static int nfs4_do_set_security_label(struct inode *inode,
4641 struct nfs4_label *ilabel,
4642 struct nfs_fattr *fattr,
4643 struct nfs4_label *olabel)
4644{
4645 struct nfs4_exception exception = { };
4646 int err;
4647
4648 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004649 err = _nfs4_do_set_security_label(inode, ilabel,
4650 fattr, olabel);
4651 trace_nfs4_set_security_label(inode, err);
4652 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004653 &exception);
4654 } while (exception.retry);
4655 return err;
4656}
4657
4658static int
4659nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4660{
4661 struct nfs4_label ilabel, *olabel = NULL;
4662 struct nfs_fattr fattr;
4663 struct rpc_cred *cred;
4664 struct inode *inode = dentry->d_inode;
4665 int status;
4666
4667 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4668 return -EOPNOTSUPP;
4669
4670 nfs_fattr_init(&fattr);
4671
4672 ilabel.pi = 0;
4673 ilabel.lfs = 0;
4674 ilabel.label = (char *)buf;
4675 ilabel.len = buflen;
4676
4677 cred = rpc_lookup_cred();
4678 if (IS_ERR(cred))
4679 return PTR_ERR(cred);
4680
4681 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4682 if (IS_ERR(olabel)) {
4683 status = -PTR_ERR(olabel);
4684 goto out;
4685 }
4686
4687 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4688 if (status == 0)
4689 nfs_setsecurity(inode, &fattr, olabel);
4690
4691 nfs4_label_free(olabel);
4692out:
4693 put_rpccred(cred);
4694 return status;
4695}
4696#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4697
4698
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004700nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004702 struct nfs_client *clp = server->nfs_client;
4703
4704 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705 return 0;
4706 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004707 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004708 case -NFS4ERR_ADMIN_REVOKED:
4709 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004710 if (state == NULL)
4711 break;
4712 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004713 case -NFS4ERR_OPENMODE:
4714 if (state == NULL)
4715 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004716 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4717 goto stateid_invalid;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004718 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004719 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004720 if (state != NULL) {
4721 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4722 goto stateid_invalid;
4723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004725 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004726 nfs4_schedule_lease_recovery(clp);
4727 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004728#if defined(CONFIG_NFS_V4_1)
4729 case -NFS4ERR_BADSESSION:
4730 case -NFS4ERR_BADSLOT:
4731 case -NFS4ERR_BAD_HIGH_SLOT:
4732 case -NFS4ERR_DEADSESSION:
4733 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4734 case -NFS4ERR_SEQ_FALSE_RETRY:
4735 case -NFS4ERR_SEQ_MISORDERED:
4736 dprintk("%s ERROR %d, Reset session\n", __func__,
4737 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004738 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004739 task->tk_status = 0;
4740 return -EAGAIN;
4741#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004743 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004744 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4746 task->tk_status = 0;
4747 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004748 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 case -NFS4ERR_OLD_STATEID:
4750 task->tk_status = 0;
4751 return -EAGAIN;
4752 }
4753 task->tk_status = nfs4_map_errors(task->tk_status);
4754 return 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004755stateid_invalid:
4756 task->tk_status = -EIO;
4757 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004758wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004759 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004760 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4761 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4762 task->tk_status = 0;
4763 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764}
4765
Chuck Leverf0920752012-05-21 22:45:41 -04004766static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4767 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004768{
4769 __be32 verf[2];
4770
Chuck Lever2c820d92012-05-21 22:45:33 -04004771 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4772 /* An impossible timestamp guarantees this value
4773 * will never match a generated boot time. */
4774 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04004775 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04004776 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004777 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04004778 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
4779 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04004780 }
Chuck Levercd937102012-03-02 17:14:31 -05004781 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4782}
4783
Chuck Levere984a552012-09-14 17:24:21 -04004784static unsigned int
4785nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4786 char *buf, size_t len)
4787{
4788 unsigned int result;
4789
4790 rcu_read_lock();
4791 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4792 clp->cl_ipaddr,
4793 rpc_peeraddr2str(clp->cl_rpcclient,
4794 RPC_DISPLAY_ADDR),
4795 rpc_peeraddr2str(clp->cl_rpcclient,
4796 RPC_DISPLAY_PROTO));
4797 rcu_read_unlock();
4798 return result;
4799}
4800
4801static unsigned int
4802nfs4_init_uniform_client_string(const struct nfs_client *clp,
4803 char *buf, size_t len)
4804{
Trond Myklebust55b59292013-07-24 16:36:35 -04004805 const char *nodename = clp->cl_rpcclient->cl_nodename;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004806
4807 if (nfs4_client_id_uniquifier[0] != '\0')
Trond Myklebust55b59292013-07-24 16:36:35 -04004808 return scnprintf(buf, len, "Linux NFSv%u.%u %s/%s",
4809 clp->rpc_ops->version,
4810 clp->cl_minorversion,
4811 nfs4_client_id_uniquifier,
4812 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004813 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4814 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004815 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004816}
4817
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004818/**
4819 * nfs4_proc_setclientid - Negotiate client ID
4820 * @clp: state data structure
4821 * @program: RPC program for NFSv4 callback service
4822 * @port: IP port number for NFS4 callback service
4823 * @cred: RPC credential to use for this call
4824 * @res: where to place the result
4825 *
4826 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4827 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004828int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4829 unsigned short port, struct rpc_cred *cred,
4830 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831{
4832 nfs4_verifier sc_verifier;
4833 struct nfs4_setclientid setclientid = {
4834 .sc_verifier = &sc_verifier,
4835 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004836 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837 };
4838 struct rpc_message msg = {
4839 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4840 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004841 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004842 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004844 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845
Chuck Leverde734832012-07-11 16:30:50 -04004846 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004847 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004848 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4849 setclientid.sc_name_len =
4850 nfs4_init_uniform_client_string(clp,
4851 setclientid.sc_name,
4852 sizeof(setclientid.sc_name));
4853 else
4854 setclientid.sc_name_len =
4855 nfs4_init_nonuniform_client_string(clp,
4856 setclientid.sc_name,
4857 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004858 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004859 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004860 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Djalal Harounife2d5392013-06-18 17:58:12 +01004861 sizeof(setclientid.sc_netid), "%s",
Chuck Leverd4d3c502007-12-10 14:57:09 -05004862 rpc_peeraddr2str(clp->cl_rpcclient,
4863 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004864 rcu_read_unlock();
4865 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004866 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 clp->cl_ipaddr, port >> 8, port & 255);
4868
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004869 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4870 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4871 setclientid.sc_name_len, setclientid.sc_name);
4872 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004873 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004874 dprintk("NFS reply setclientid: %d\n", status);
4875 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876}
4877
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004878/**
4879 * nfs4_proc_setclientid_confirm - Confirm client ID
4880 * @clp: state data structure
4881 * @res: result of a previous SETCLIENTID
4882 * @cred: RPC credential to use for this call
4883 *
4884 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4885 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004886int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004887 struct nfs4_setclientid_res *arg,
4888 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890 struct rpc_message msg = {
4891 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004892 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004893 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 int status;
4896
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004897 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4898 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4899 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004900 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004901 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004902 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 return status;
4904}
4905
Trond Myklebustfe650402006-01-03 09:55:18 +01004906struct nfs4_delegreturndata {
4907 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004908 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004909 struct nfs_fh fh;
4910 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004911 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004912 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004913 int rpc_status;
4914};
4915
Trond Myklebustfe650402006-01-03 09:55:18 +01004916static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4917{
4918 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004919
Trond Myklebust14516c32010-07-31 14:29:06 -04004920 if (!nfs4_sequence_done(task, &data->res.seq_res))
4921 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004922
Trond Myklebustca8acf82013-08-13 10:36:56 -04004923 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004924 switch (task->tk_status) {
4925 case -NFS4ERR_STALE_STATEID:
4926 case -NFS4ERR_EXPIRED:
4927 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004928 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004929 break;
4930 default:
4931 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4932 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004933 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004934 return;
4935 }
4936 }
4937 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004938}
4939
4940static void nfs4_delegreturn_release(void *calldata)
4941{
Trond Myklebustfe650402006-01-03 09:55:18 +01004942 kfree(calldata);
4943}
4944
Andy Adamson938e1012009-04-01 09:22:28 -04004945static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4946{
4947 struct nfs4_delegreturndata *d_data;
4948
4949 d_data = (struct nfs4_delegreturndata *)data;
4950
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004951 nfs4_setup_sequence(d_data->res.server,
4952 &d_data->args.seq_args,
4953 &d_data->res.seq_res,
4954 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004955}
Andy Adamson938e1012009-04-01 09:22:28 -04004956
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004957static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004958 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01004959 .rpc_call_done = nfs4_delegreturn_done,
4960 .rpc_release = nfs4_delegreturn_release,
4961};
4962
Trond Myklebuste6f81072008-01-24 18:14:34 -05004963static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004964{
4965 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004966 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004967 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004968 struct rpc_message msg = {
4969 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4970 .rpc_cred = cred,
4971 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004972 struct rpc_task_setup task_setup_data = {
4973 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004974 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004975 .callback_ops = &nfs4_delegreturn_ops,
4976 .flags = RPC_TASK_ASYNC,
4977 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004978 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004979
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004980 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004981 if (data == NULL)
4982 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04004983 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004984 data->args.fhandle = &data->fh;
4985 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004986 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004987 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004988 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004989 data->res.fattr = &data->fattr;
4990 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004991 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004992 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004993 data->rpc_status = 0;
4994
Trond Myklebustc970aa82007-07-14 15:39:59 -04004995 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004996 msg.rpc_argp = &data->args;
4997 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004998 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004999 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005000 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005001 if (!issync)
5002 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005003 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005004 if (status != 0)
5005 goto out;
5006 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005007 if (status == 0)
5008 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5009 else
5010 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005011out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005012 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005013 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014}
5015
Trond Myklebuste6f81072008-01-24 18:14:34 -05005016int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017{
5018 struct nfs_server *server = NFS_SERVER(inode);
5019 struct nfs4_exception exception = { };
5020 int err;
5021 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005022 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Trond Myklebustca8acf82013-08-13 10:36:56 -04005023 trace_nfs4_delegreturn(inode, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024 switch (err) {
5025 case -NFS4ERR_STALE_STATEID:
5026 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 case 0:
5028 return 0;
5029 }
5030 err = nfs4_handle_exception(server, err, &exception);
5031 } while (exception.retry);
5032 return err;
5033}
5034
5035#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5036#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5037
5038/*
5039 * sleep, with exponential backoff, and retry the LOCK operation.
5040 */
5041static unsigned long
5042nfs4_set_lock_task_retry(unsigned long timeout)
5043{
Colin Cross416ad3c2013-05-06 23:50:06 +00005044 freezable_schedule_timeout_killable_unsafe(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045 timeout <<= 1;
5046 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5047 return NFS4_LOCK_MAXTIMEOUT;
5048 return timeout;
5049}
5050
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5052{
5053 struct inode *inode = state->inode;
5054 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005055 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005056 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005058 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005060 struct nfs_lockt_res res = {
5061 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062 };
5063 struct rpc_message msg = {
5064 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5065 .rpc_argp = &arg,
5066 .rpc_resp = &res,
5067 .rpc_cred = state->owner->so_cred,
5068 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 struct nfs4_lock_state *lsp;
5070 int status;
5071
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005072 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005073 status = nfs4_set_lock_state(state, request);
5074 if (status != 0)
5075 goto out;
5076 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005077 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005078 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005079 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005080 switch (status) {
5081 case 0:
5082 request->fl_type = F_UNLCK;
5083 break;
5084 case -NFS4ERR_DENIED:
5085 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005087 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005088out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089 return status;
5090}
5091
5092static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5093{
5094 struct nfs4_exception exception = { };
5095 int err;
5096
5097 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005098 err = _nfs4_proc_getlk(state, cmd, request);
5099 trace_nfs4_get_lock(request, state, cmd, err);
5100 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 &exception);
5102 } while (exception.retry);
5103 return err;
5104}
5105
5106static int do_vfs_lock(struct file *file, struct file_lock *fl)
5107{
5108 int res = 0;
5109 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5110 case FL_POSIX:
5111 res = posix_lock_file_wait(file, fl);
5112 break;
5113 case FL_FLOCK:
5114 res = flock_lock_file_wait(file, fl);
5115 break;
5116 default:
5117 BUG();
5118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119 return res;
5120}
5121
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005122struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005123 struct nfs_locku_args arg;
5124 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005125 struct nfs4_lock_state *lsp;
5126 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005127 struct file_lock fl;
5128 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005129 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005130};
5131
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005132static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5133 struct nfs_open_context *ctx,
5134 struct nfs4_lock_state *lsp,
5135 struct nfs_seqid *seqid)
5136{
5137 struct nfs4_unlockdata *p;
5138 struct inode *inode = lsp->ls_state->inode;
5139
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005140 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005141 if (p == NULL)
5142 return NULL;
5143 p->arg.fh = NFS_FH(inode);
5144 p->arg.fl = &p->fl;
5145 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005146 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005147 p->arg.stateid = &lsp->ls_stateid;
5148 p->lsp = lsp;
5149 atomic_inc(&lsp->ls_count);
5150 /* Ensure we don't close file until we're done freeing locks! */
5151 p->ctx = get_nfs_open_context(ctx);
5152 memcpy(&p->fl, fl, sizeof(p->fl));
5153 p->server = NFS_SERVER(inode);
5154 return p;
5155}
5156
Trond Myklebust06f814a2006-01-03 09:55:07 +01005157static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005158{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005159 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005160 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005161 nfs4_put_lock_state(calldata->lsp);
5162 put_nfs_open_context(calldata->ctx);
5163 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005164}
5165
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005166static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005167{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005168 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005169
Trond Myklebust14516c32010-07-31 14:29:06 -04005170 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5171 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005172 switch (task->tk_status) {
5173 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05005174 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
5175 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005176 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005177 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005178 case -NFS4ERR_BAD_STATEID:
5179 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005180 case -NFS4ERR_STALE_STATEID:
5181 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005182 break;
5183 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005184 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005185 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005186 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005187 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005188}
5189
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005190static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005191{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005192 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005194 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005195 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04005196 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005197 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005198 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005199 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005200 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005201 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005202 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005203 &calldata->res.seq_res,
5204 task) != 0)
5205 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005206 return;
5207out_no_action:
5208 task->tk_action = NULL;
5209out_wait:
5210 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211}
5212
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005213static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005214 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005215 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005216 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005217};
5218
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005219static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5220 struct nfs_open_context *ctx,
5221 struct nfs4_lock_state *lsp,
5222 struct nfs_seqid *seqid)
5223{
5224 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005225 struct rpc_message msg = {
5226 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5227 .rpc_cred = ctx->cred,
5228 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005229 struct rpc_task_setup task_setup_data = {
5230 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005231 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005232 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005233 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005234 .flags = RPC_TASK_ASYNC,
5235 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005236
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005237 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5238 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5239
Frank Filz137d6ac2007-07-09 15:32:29 -07005240 /* Ensure this is an unlock - when canceling a lock, the
5241 * canceled lock is passed in, and it won't be an unlock.
5242 */
5243 fl->fl_type = F_UNLCK;
5244
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005245 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5246 if (data == NULL) {
5247 nfs_free_seqid(seqid);
5248 return ERR_PTR(-ENOMEM);
5249 }
5250
Chuck Levera9c92d62013-08-09 12:48:18 -04005251 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005252 msg.rpc_argp = &data->arg;
5253 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005254 task_setup_data.callback_data = data;
5255 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005256}
5257
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5259{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005260 struct inode *inode = state->inode;
5261 struct nfs4_state_owner *sp = state->owner;
5262 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005263 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005264 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005265 struct rpc_task *task;
5266 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005267 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268
Trond Myklebust9b073572006-06-29 16:38:34 -04005269 status = nfs4_set_lock_state(state, request);
5270 /* Unlock _before_ we do the RPC call */
5271 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005272 /* Exclude nfs_delegation_claim_locks() */
5273 mutex_lock(&sp->so_delegreturn_mutex);
5274 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005275 down_read(&nfsi->rwsem);
5276 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
5277 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005278 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005279 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005280 }
5281 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005282 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005283 if (status != 0)
5284 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005285 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005286 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005287 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5288 goto out;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005289 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005290 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005291 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04005292 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005293 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005294 status = PTR_ERR(task);
5295 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005296 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005297 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005298 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005299out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005300 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005301 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005302 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303}
5304
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005305struct nfs4_lockdata {
5306 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005307 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005308 struct nfs4_lock_state *lsp;
5309 struct nfs_open_context *ctx;
5310 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005311 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005312 int rpc_status;
5313 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005314 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005315};
5316
5317static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005318 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5319 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005320{
5321 struct nfs4_lockdata *p;
5322 struct inode *inode = lsp->ls_state->inode;
5323 struct nfs_server *server = NFS_SERVER(inode);
5324
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005325 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005326 if (p == NULL)
5327 return NULL;
5328
5329 p->arg.fh = NFS_FH(inode);
5330 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005331 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005332 if (p->arg.open_seqid == NULL)
5333 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005334 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005335 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005336 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005337 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04005338 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005339 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005340 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005341 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005342 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005343 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005344 atomic_inc(&lsp->ls_count);
5345 p->ctx = get_nfs_open_context(ctx);
5346 memcpy(&p->fl, fl, sizeof(p->fl));
5347 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005348out_free_seqid:
5349 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005350out_free:
5351 kfree(p);
5352 return NULL;
5353}
5354
5355static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5356{
5357 struct nfs4_lockdata *data = calldata;
5358 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359
Harvey Harrison3110ff82008-05-02 13:42:44 -07005360 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005361 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005362 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005363 /* Do we need to do an open_to_lock_owner? */
5364 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005365 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005366 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005367 }
Trond Myklebust8e472f32013-04-20 01:30:53 -04005368 data->arg.open_stateid = &state->open_stateid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005369 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005370 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005371 } else
5372 data->arg.new_lock_owner = 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04005373 if (!nfs4_valid_open_stateid(state)) {
5374 data->rpc_status = -EBADF;
5375 task->tk_action = NULL;
5376 goto out_release_open_seqid;
5377 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005378 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005379 if (nfs4_setup_sequence(data->server,
5380 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005381 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005382 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04005383 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04005384out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005385 nfs_release_seqid(data->arg.open_seqid);
5386out_release_lock_seqid:
5387 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005388out_wait:
5389 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005390 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005391}
5392
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005393static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5394{
5395 struct nfs4_lockdata *data = calldata;
5396
Harvey Harrison3110ff82008-05-02 13:42:44 -07005397 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005398
Trond Myklebust14516c32010-07-31 14:29:06 -04005399 if (!nfs4_sequence_done(task, &data->res.seq_res))
5400 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04005401
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005402 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005403 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005404 if (data->rpc_status == 0)
5405 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
5406 else
5407 goto out;
5408 }
5409 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05005410 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005411 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04005412 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005413 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005414out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07005415 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005416}
5417
5418static void nfs4_lock_release(void *calldata)
5419{
5420 struct nfs4_lockdata *data = calldata;
5421
Harvey Harrison3110ff82008-05-02 13:42:44 -07005422 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005423 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005424 if (data->cancelled != 0) {
5425 struct rpc_task *task;
5426 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5427 data->arg.lock_seqid);
5428 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08005429 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005430 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005431 } else
5432 nfs_free_seqid(data->arg.lock_seqid);
5433 nfs4_put_lock_state(data->lsp);
5434 put_nfs_open_context(data->ctx);
5435 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005436 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005437}
5438
5439static const struct rpc_call_ops nfs4_lock_ops = {
5440 .rpc_call_prepare = nfs4_lock_prepare,
5441 .rpc_call_done = nfs4_lock_done,
5442 .rpc_release = nfs4_lock_release,
5443};
5444
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005445static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5446{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005447 switch (error) {
5448 case -NFS4ERR_ADMIN_REVOKED:
5449 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05005450 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005451 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04005452 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05005453 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005454 break;
5455 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005456 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05005457 case -NFS4ERR_EXPIRED:
5458 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005459 };
5460}
5461
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005462static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005463{
5464 struct nfs4_lockdata *data;
5465 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005466 struct rpc_message msg = {
5467 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5468 .rpc_cred = state->owner->so_cred,
5469 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005470 struct rpc_task_setup task_setup_data = {
5471 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005472 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005473 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005474 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005475 .flags = RPC_TASK_ASYNC,
5476 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005477 int ret;
5478
Harvey Harrison3110ff82008-05-02 13:42:44 -07005479 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005480 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005481 fl->fl_u.nfs4_fl.owner,
5482 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005483 if (data == NULL)
5484 return -ENOMEM;
5485 if (IS_SETLKW(cmd))
5486 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04005487 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005488 msg.rpc_argp = &data->arg;
5489 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005490 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005491 if (recovery_type > NFS_LOCK_NEW) {
5492 if (recovery_type == NFS_LOCK_RECLAIM)
5493 data->arg.reclaim = NFS_LOCK_RECLAIM;
5494 nfs4_set_sequence_privileged(&data->arg.seq_args);
5495 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04005496 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005497 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005498 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005499 ret = nfs4_wait_for_completion_rpc_task(task);
5500 if (ret == 0) {
5501 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005502 if (ret)
5503 nfs4_handle_setlk_error(data->server, data->lsp,
5504 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005505 } else
5506 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005507 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005508 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005509 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510}
5511
5512static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5513{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005514 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005515 struct nfs4_exception exception = {
5516 .inode = state->inode,
5517 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005518 int err;
5519
5520 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005521 /* Cache the lock if possible... */
5522 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5523 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005524 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005525 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
Trond Myklebust168667c2010-10-19 19:47:49 -04005526 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00005527 break;
5528 nfs4_handle_exception(server, err, &exception);
5529 } while (exception.retry);
5530 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531}
5532
5533static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5534{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005535 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005536 struct nfs4_exception exception = {
5537 .inode = state->inode,
5538 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005539 int err;
5540
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005541 err = nfs4_set_lock_state(state, request);
5542 if (err != 0)
5543 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04005544 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10005545 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
5546 return 0;
5547 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005548 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005549 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5550 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005551 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005552 trace_nfs4_lock_expired(request, state, F_SETLK, err);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005553 switch (err) {
5554 default:
5555 goto out;
5556 case -NFS4ERR_GRACE:
5557 case -NFS4ERR_DELAY:
5558 nfs4_handle_exception(server, err, &exception);
5559 err = 0;
5560 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005561 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005562out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00005563 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564}
5565
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005566#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005567/**
5568 * nfs41_check_expired_locks - possibly free a lock stateid
5569 *
5570 * @state: NFSv4 state for an inode
5571 *
5572 * Returns NFS_OK if recovery for this stateid is now finished.
5573 * Otherwise a negative NFS4ERR value is returned.
5574 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005575static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005576{
Chuck Levereb64cf92012-07-11 16:30:05 -04005577 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005578 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005579 struct nfs_server *server = NFS_SERVER(state->inode);
5580
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005581 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04005582 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005583 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
5584
5585 status = nfs41_test_stateid(server,
5586 &lsp->ls_stateid,
5587 cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04005588 trace_nfs4_test_lock_stateid(state, lsp, status);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005589 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005590 /* Free the stateid unless the server
5591 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04005592 if (status != -NFS4ERR_BAD_STATEID)
5593 nfs41_free_stateid(server,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005594 &lsp->ls_stateid,
5595 cred);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005596 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005597 ret = status;
5598 }
5599 }
5600 };
5601
5602 return ret;
5603}
5604
5605static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5606{
5607 int status = NFS_OK;
5608
5609 if (test_bit(LK_STATE_IN_USE, &state->flags))
5610 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005611 if (status != NFS_OK)
5612 status = nfs4_lock_expired(state, request);
5613 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005614}
5615#endif
5616
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5618{
Trond Myklebust9a99af492013-02-04 20:17:49 -05005619 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005620 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005621 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005622 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005623 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005625 if ((fl_flags & FL_POSIX) &&
5626 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5627 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005628 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005629 status = nfs4_set_lock_state(state, request);
5630 if (status != 0)
5631 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005632 request->fl_flags |= FL_ACCESS;
5633 status = do_vfs_lock(request->fl_file, request);
5634 if (status < 0)
5635 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005636 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005637 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005638 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005639 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005640 request->fl_flags = fl_flags & ~FL_SLEEP;
5641 status = do_vfs_lock(request->fl_file, request);
5642 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005643 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05005644 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5645 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005646 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005647 if (status != 0)
Trond Myklebust9a99af492013-02-04 20:17:49 -05005648 goto out;
5649 down_read(&nfsi->rwsem);
5650 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5651 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005652 goto out_unlock;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005653 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005654 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005655 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005656 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005657 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5658 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005659out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005660 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005661out:
5662 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663 return status;
5664}
5665
5666static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5667{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005668 struct nfs4_exception exception = {
5669 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005670 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005671 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672 int err;
5673
5674 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005675 err = _nfs4_proc_setlk(state, cmd, request);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005676 trace_nfs4_set_lock(request, state, cmd, err);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005677 if (err == -NFS4ERR_DENIED)
5678 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005680 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681 } while (exception.retry);
5682 return err;
5683}
5684
5685static int
5686nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5687{
5688 struct nfs_open_context *ctx;
5689 struct nfs4_state *state;
5690 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5691 int status;
5692
5693 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005694 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695 state = ctx->state;
5696
5697 if (request->fl_start < 0 || request->fl_end < 0)
5698 return -EINVAL;
5699
Trond Myklebustd9531262009-07-21 19:22:38 -04005700 if (IS_GETLK(cmd)) {
5701 if (state != NULL)
5702 return nfs4_proc_getlk(state, F_GETLK, request);
5703 return 0;
5704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705
5706 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5707 return -EINVAL;
5708
Trond Myklebustd9531262009-07-21 19:22:38 -04005709 if (request->fl_type == F_UNLCK) {
5710 if (state != NULL)
5711 return nfs4_proc_unlck(state, cmd, request);
5712 return 0;
5713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714
Trond Myklebustd9531262009-07-21 19:22:38 -04005715 if (state == NULL)
5716 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005717 /*
5718 * Don't rely on the VFS having checked the file open mode,
5719 * since it won't do this for flock() locks.
5720 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005721 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005722 case F_RDLCK:
5723 if (!(filp->f_mode & FMODE_READ))
5724 return -EBADF;
5725 break;
5726 case F_WRLCK:
5727 if (!(filp->f_mode & FMODE_WRITE))
5728 return -EBADF;
5729 }
5730
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731 do {
5732 status = nfs4_proc_setlk(state, cmd, request);
5733 if ((status != -EAGAIN) || IS_SETLK(cmd))
5734 break;
5735 timeout = nfs4_set_lock_task_retry(timeout);
5736 status = -ERESTARTSYS;
5737 if (signalled())
5738 break;
5739 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740 return status;
5741}
5742
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005743int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05005744{
5745 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05005746 int err;
5747
5748 err = nfs4_set_lock_state(state, fl);
5749 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005750 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04005751 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005752 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05005753}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005754
Trond Myklebustcf470c32012-03-07 13:49:12 -05005755struct nfs_release_lockowner_data {
5756 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005757 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005758 struct nfs_release_lockowner_args args;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005759 struct nfs4_sequence_args seq_args;
5760 struct nfs4_sequence_res seq_res;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005761};
5762
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005763static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
5764{
5765 struct nfs_release_lockowner_data *data = calldata;
5766 nfs40_setup_sequence(data->server,
5767 &data->seq_args, &data->seq_res, task);
5768}
5769
5770static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
5771{
5772 struct nfs_release_lockowner_data *data = calldata;
5773 nfs40_sequence_done(task, &data->seq_res);
5774}
5775
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005776static void nfs4_release_lockowner_release(void *calldata)
5777{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005778 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005779 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005780 kfree(calldata);
5781}
5782
Trond Myklebust17280172012-03-11 13:11:00 -04005783static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005784 .rpc_call_prepare = nfs4_release_lockowner_prepare,
5785 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005786 .rpc_release = nfs4_release_lockowner_release,
5787};
5788
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04005789static int nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005790{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005791 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005792 struct rpc_message msg = {
5793 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5794 };
5795
5796 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005797 return -EINVAL;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005798
Trond Myklebustcf470c32012-03-07 13:49:12 -05005799 data = kmalloc(sizeof(*data), GFP_NOFS);
5800 if (!data)
5801 return -ENOMEM;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005802 nfs4_init_sequence(&data->seq_args, &data->seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05005803 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005804 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005805 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5806 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5807 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005808
Trond Myklebustcf470c32012-03-07 13:49:12 -05005809 msg.rpc_argp = &data->args;
5810 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5811 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005812}
5813
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005814#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5815
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005816static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5817 const void *buf, size_t buflen,
5818 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005819{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005820 if (strcmp(key, "") != 0)
5821 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005822
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005823 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005824}
5825
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005826static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5827 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005828{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005829 if (strcmp(key, "") != 0)
5830 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005831
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005832 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005833}
5834
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005835static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5836 size_t list_len, const char *name,
5837 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005838{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005839 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005840
J. Bruce Fields096455a2006-03-20 23:23:42 -05005841 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5842 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005843
5844 if (list && len <= list_len)
5845 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005846 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005847}
5848
David Quigleyc9bccef2013-05-22 12:50:45 -04005849#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5850static inline int nfs4_server_supports_labels(struct nfs_server *server)
5851{
5852 return server->caps & NFS_CAP_SECURITY_LABEL;
5853}
5854
5855static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
5856 const void *buf, size_t buflen,
5857 int flags, int type)
5858{
5859 if (security_ismaclabel(key))
5860 return nfs4_set_security_label(dentry, buf, buflen);
5861
5862 return -EOPNOTSUPP;
5863}
5864
5865static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
5866 void *buf, size_t buflen, int type)
5867{
5868 if (security_ismaclabel(key))
5869 return nfs4_get_security_label(dentry->d_inode, buf, buflen);
5870 return -EOPNOTSUPP;
5871}
5872
5873static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
5874 size_t list_len, const char *name,
5875 size_t name_len, int type)
5876{
5877 size_t len = 0;
5878
5879 if (nfs_server_capable(dentry->d_inode, NFS_CAP_SECURITY_LABEL)) {
5880 len = security_inode_listsecurity(dentry->d_inode, NULL, 0);
5881 if (list && len <= list_len)
5882 security_inode_listsecurity(dentry->d_inode, list, len);
5883 }
5884 return len;
5885}
5886
5887static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
5888 .prefix = XATTR_SECURITY_PREFIX,
5889 .list = nfs4_xattr_list_nfs4_label,
5890 .get = nfs4_xattr_get_nfs4_label,
5891 .set = nfs4_xattr_set_nfs4_label,
5892};
5893#endif
5894
5895
Andy Adamson533eb462011-06-13 18:25:56 -04005896/*
5897 * nfs_fhget will use either the mounted_on_fileid or the fileid
5898 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005899static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5900{
Andy Adamson533eb462011-06-13 18:25:56 -04005901 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5902 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5903 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005904 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005905 return;
5906
5907 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005908 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005909 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5910 fattr->nlink = 2;
5911}
5912
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005913static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5914 const struct qstr *name,
5915 struct nfs4_fs_locations *fs_locations,
5916 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005917{
5918 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04005919 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005920 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005921 };
5922 struct nfs4_fs_locations_arg args = {
5923 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005924 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005925 .page = page,
5926 .bitmask = bitmask,
5927 };
Benny Halevy22958462009-04-01 09:22:02 -04005928 struct nfs4_fs_locations_res res = {
5929 .fs_locations = fs_locations,
5930 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005931 struct rpc_message msg = {
5932 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5933 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005934 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005935 };
5936 int status;
5937
Harvey Harrison3110ff82008-05-02 13:42:44 -07005938 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005939
5940 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5941 * is not supported */
5942 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5943 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5944 else
5945 bitmask[0] |= FATTR4_WORD0_FILEID;
5946
Trond Myklebustc228fd32007-01-13 02:28:11 -05005947 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005948 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005949 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005950 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005951 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005952 return status;
5953}
5954
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005955int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5956 const struct qstr *name,
5957 struct nfs4_fs_locations *fs_locations,
5958 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005959{
5960 struct nfs4_exception exception = { };
5961 int err;
5962 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04005963 err = _nfs4_proc_fs_locations(client, dir, name,
5964 fs_locations, page);
5965 trace_nfs4_get_fs_locations(dir, name, err);
5966 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005967 &exception);
5968 } while (exception.retry);
5969 return err;
5970}
5971
Andy Adamson5ec16a82013-08-08 10:57:55 -04005972/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04005973 * If 'use_integrity' is true and the state managment nfs_client
5974 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
5975 * and the machine credential as per RFC3530bis and RFC5661 Security
5976 * Considerations sections. Otherwise, just use the user cred with the
5977 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04005978 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04005979static 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 +00005980{
5981 int status;
5982 struct nfs4_secinfo_arg args = {
5983 .dir_fh = NFS_FH(dir),
5984 .name = name,
5985 };
5986 struct nfs4_secinfo_res res = {
5987 .flavors = flavors,
5988 };
5989 struct rpc_message msg = {
5990 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5991 .rpc_argp = &args,
5992 .rpc_resp = &res,
5993 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04005994 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
5995
5996 if (use_integrity) {
5997 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
5998 msg.rpc_cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
5999 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006000
6001 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006002
6003 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6004 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6005
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006006 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6007 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006008 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006009
6010 if (msg.rpc_cred)
6011 put_rpccred(msg.rpc_cred);
6012
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006013 return status;
6014}
6015
Bryan Schumaker72de53e2012-04-27 13:27:40 -04006016int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6017 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006018{
6019 struct nfs4_exception exception = { };
6020 int err;
6021 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006022 err = -NFS4ERR_WRONGSEC;
6023
6024 /* try to use integrity protection with machine cred */
6025 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6026 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6027
6028 /*
6029 * if unable to use integrity protection, or SECINFO with
6030 * integrity protection returns NFS4ERR_WRONGSEC (which is
6031 * disallowed by spec, but exists in deployed servers) use
6032 * the current filesystem's rpc_client and the user cred.
6033 */
6034 if (err == -NFS4ERR_WRONGSEC)
6035 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6036
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006037 trace_nfs4_secinfo(dir, name, err);
6038 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006039 &exception);
6040 } while (exception.retry);
6041 return err;
6042}
6043
Andy Adamson557134a2009-04-01 09:21:53 -04006044#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04006045/*
Andy Adamson357f54d2010-12-14 10:11:57 -05006046 * Check the exchange flags returned by the server for invalid flags, having
6047 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6048 * DS flags set.
6049 */
6050static int nfs4_check_cl_exchange_flags(u32 flags)
6051{
6052 if (flags & ~EXCHGID4_FLAG_MASK_R)
6053 goto out_inval;
6054 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6055 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6056 goto out_inval;
6057 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6058 goto out_inval;
6059 return NFS_OK;
6060out_inval:
6061 return -NFS4ERR_INVAL;
6062}
6063
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006064static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006065nfs41_same_server_scope(struct nfs41_server_scope *a,
6066 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006067{
6068 if (a->server_scope_sz == b->server_scope_sz &&
6069 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6070 return true;
6071
6072 return false;
6073}
6074
Andy Adamson357f54d2010-12-14 10:11:57 -05006075/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006076 * nfs4_proc_bind_conn_to_session()
6077 *
6078 * The 4.1 client currently uses the same TCP connection for the
6079 * fore and backchannel.
6080 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006081int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006082{
6083 int status;
6084 struct nfs41_bind_conn_to_session_res res;
6085 struct rpc_message msg = {
6086 .rpc_proc =
6087 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
6088 .rpc_argp = clp,
6089 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006090 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006091 };
6092
6093 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006094
6095 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
6096 if (unlikely(res.session == NULL)) {
6097 status = -ENOMEM;
6098 goto out;
6099 }
6100
6101 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006102 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006103 if (status == 0) {
6104 if (memcmp(res.session->sess_id.data,
6105 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6106 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6107 status = -EIO;
6108 goto out_session;
6109 }
6110 if (res.dir != NFS4_CDFS4_BOTH) {
6111 dprintk("NFS: %s: Unexpected direction from server\n",
6112 __func__);
6113 status = -EIO;
6114 goto out_session;
6115 }
6116 if (res.use_conn_in_rdma_mode) {
6117 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6118 __func__);
6119 status = -EIO;
6120 goto out_session;
6121 }
6122 }
6123out_session:
6124 kfree(res.session);
6125out:
6126 dprintk("<-- %s status= %d\n", __func__, status);
6127 return status;
6128}
6129
6130/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006131 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6132 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04006133 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006134static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
6135 .how = SP4_MACH_CRED,
6136 .enforce.u.words = {
6137 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6138 1 << (OP_EXCHANGE_ID - 32) |
6139 1 << (OP_CREATE_SESSION - 32) |
6140 1 << (OP_DESTROY_SESSION - 32) |
6141 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006142 },
6143 .allow.u.words = {
6144 [0] = 1 << (OP_CLOSE) |
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006145 1 << (OP_LOCKU),
6146 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006147 1 << (OP_SECINFO_NO_NAME - 32) |
6148 1 << (OP_TEST_STATEID - 32) |
6149 1 << (OP_FREE_STATEID - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006150 }
6151};
6152
6153/*
6154 * Select the state protection mode for client `clp' given the server results
6155 * from exchange_id in `sp'.
6156 *
6157 * Returns 0 on success, negative errno otherwise.
6158 */
6159static int nfs4_sp4_select_mode(struct nfs_client *clp,
6160 struct nfs41_state_protection *sp)
6161{
6162 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
6163 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6164 1 << (OP_EXCHANGE_ID - 32) |
6165 1 << (OP_CREATE_SESSION - 32) |
6166 1 << (OP_DESTROY_SESSION - 32) |
6167 1 << (OP_DESTROY_CLIENTID - 32)
6168 };
6169 unsigned int i;
6170
6171 if (sp->how == SP4_MACH_CRED) {
6172 /* Print state protect result */
6173 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
6174 for (i = 0; i <= LAST_NFS4_OP; i++) {
6175 if (test_bit(i, sp->enforce.u.longs))
6176 dfprintk(MOUNT, " enforce op %d\n", i);
6177 if (test_bit(i, sp->allow.u.longs))
6178 dfprintk(MOUNT, " allow op %d\n", i);
6179 }
6180
6181 /* make sure nothing is on enforce list that isn't supported */
6182 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
6183 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
6184 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6185 return -EINVAL;
6186 }
6187 }
6188
6189 /*
6190 * Minimal mode - state operations are allowed to use machine
6191 * credential. Note this already happens by default, so the
6192 * client doesn't have to do anything more than the negotiation.
6193 *
6194 * NOTE: we don't care if EXCHANGE_ID is in the list -
6195 * we're already using the machine cred for exchange_id
6196 * and will never use a different cred.
6197 */
6198 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
6199 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
6200 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
6201 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
6202 dfprintk(MOUNT, "sp4_mach_cred:\n");
6203 dfprintk(MOUNT, " minimal mode enabled\n");
6204 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
6205 } else {
6206 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6207 return -EINVAL;
6208 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006209
6210 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
6211 test_bit(OP_LOCKU, sp->allow.u.longs)) {
6212 dfprintk(MOUNT, " cleanup mode enabled\n");
6213 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
6214 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006215
6216 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
6217 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
6218 dfprintk(MOUNT, " secinfo mode enabled\n");
6219 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
6220 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006221
6222 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
6223 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
6224 dfprintk(MOUNT, " stateid mode enabled\n");
6225 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
6226 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04006227
6228 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
6229 dfprintk(MOUNT, " write mode enabled\n");
6230 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
6231 }
6232
6233 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
6234 dfprintk(MOUNT, " commit mode enabled\n");
6235 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
6236 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006237 }
6238
6239 return 0;
6240}
6241
6242/*
6243 * _nfs4_proc_exchange_id()
6244 *
6245 * Wrapper for EXCHANGE_ID operation.
6246 */
6247static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
6248 u32 sp4_how)
Benny Halevy99fe60d2009-04-01 09:22:29 -04006249{
6250 nfs4_verifier verifier;
6251 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05006252 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04006253 .client = clp,
Trond Myklebust4f0b4292013-05-20 12:24:03 -04006254 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6255 EXCHGID4_FLAG_BIND_PRINC_STATEID,
Benny Halevy99fe60d2009-04-01 09:22:29 -04006256 };
6257 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04006258 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04006259 };
6260 int status;
6261 struct rpc_message msg = {
6262 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
6263 .rpc_argp = &args,
6264 .rpc_resp = &res,
6265 .rpc_cred = cred,
6266 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04006267
Chuck Leverf0920752012-05-21 22:45:41 -04006268 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04006269 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
6270 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006271 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
6272 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6273 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006274
Chuck Leveracdeb692012-05-21 22:46:16 -04006275 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006276 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04006277 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006278 status = -ENOMEM;
6279 goto out;
6280 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006281
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006282 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006283 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006284 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04006285 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04006286 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04006287 }
6288
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006289 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006290 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006291 status = -ENOMEM;
6292 goto out_server_scope;
6293 }
6294
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006295 switch (sp4_how) {
6296 case SP4_NONE:
6297 args.state_protect.how = SP4_NONE;
6298 break;
6299
6300 case SP4_MACH_CRED:
6301 args.state_protect = nfs4_sp4_mach_cred_request;
6302 break;
6303
6304 default:
6305 /* unsupported! */
6306 WARN_ON_ONCE(1);
6307 status = -EINVAL;
6308 goto out_server_scope;
6309 }
6310
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006311 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006312 trace_nfs4_exchange_id(clp, status);
Chuck Lever177313f2012-05-21 22:44:58 -04006313 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04006314 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006315
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006316 if (status == 0)
6317 status = nfs4_sp4_select_mode(clp, &res.state_protect);
6318
Chuck Lever177313f2012-05-21 22:44:58 -04006319 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04006320 clp->cl_clientid = res.clientid;
6321 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
6322 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
6323 clp->cl_seqid = res.seqid;
6324
Chuck Leveracdeb692012-05-21 22:46:16 -04006325 kfree(clp->cl_serverowner);
6326 clp->cl_serverowner = res.server_owner;
6327 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04006328
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006329 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04006330 kfree(clp->cl_implid);
6331 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006332
Chuck Lever177313f2012-05-21 22:44:58 -04006333 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006334 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006335 res.server_scope)) {
6336 dprintk("%s: server_scope mismatch detected\n",
6337 __func__);
6338 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006339 kfree(clp->cl_serverscope);
6340 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006341 }
6342
Chuck Lever177313f2012-05-21 22:44:58 -04006343 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006344 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006345 goto out;
6346 }
Trond Myklebust32b01312012-05-26 13:41:04 -04006347 } else
6348 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006349
Chuck Leveracdeb692012-05-21 22:46:16 -04006350out_server_owner:
6351 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006352out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006353 kfree(res.server_scope);
6354out:
Chuck Lever177313f2012-05-21 22:44:58 -04006355 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006356 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006357 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006358 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04006359 clp->cl_implid->date.seconds,
6360 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006361 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006362 return status;
6363}
6364
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006365/*
6366 * nfs4_proc_exchange_id()
6367 *
6368 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6369 *
6370 * Since the clientid has expired, all compounds using sessions
6371 * associated with the stale clientid will be returning
6372 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
6373 * be in some phase of session reset.
6374 *
6375 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
6376 */
6377int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
6378{
6379 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
6380 int status;
6381
6382 /* try SP4_MACH_CRED if krb5i/p */
6383 if (authflavor == RPC_AUTH_GSS_KRB5I ||
6384 authflavor == RPC_AUTH_GSS_KRB5P) {
6385 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
6386 if (!status)
6387 return 0;
6388 }
6389
6390 /* try SP4_NONE */
6391 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
6392}
6393
Trond Myklebust66245532012-05-25 17:18:09 -04006394static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
6395 struct rpc_cred *cred)
6396{
6397 struct rpc_message msg = {
6398 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
6399 .rpc_argp = clp,
6400 .rpc_cred = cred,
6401 };
6402 int status;
6403
6404 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006405 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04006406 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04006407 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04006408 "DESTROY_CLIENTID.", status, clp->cl_hostname);
6409 return status;
6410}
6411
6412static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
6413 struct rpc_cred *cred)
6414{
6415 unsigned int loop;
6416 int ret;
6417
6418 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
6419 ret = _nfs4_proc_destroy_clientid(clp, cred);
6420 switch (ret) {
6421 case -NFS4ERR_DELAY:
6422 case -NFS4ERR_CLIENTID_BUSY:
6423 ssleep(1);
6424 break;
6425 default:
6426 return ret;
6427 }
6428 }
6429 return 0;
6430}
6431
6432int nfs4_destroy_clientid(struct nfs_client *clp)
6433{
6434 struct rpc_cred *cred;
6435 int ret = 0;
6436
6437 if (clp->cl_mvops->minor_version < 1)
6438 goto out;
6439 if (clp->cl_exchange_flags == 0)
6440 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04006441 if (clp->cl_preserve_clid)
6442 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04006443 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04006444 ret = nfs4_proc_destroy_clientid(clp, cred);
6445 if (cred)
6446 put_rpccred(cred);
6447 switch (ret) {
6448 case 0:
6449 case -NFS4ERR_STALE_CLIENTID:
6450 clp->cl_exchange_flags = 0;
6451 }
6452out:
6453 return ret;
6454}
6455
Andy Adamson2050f0c2009-04-01 09:22:30 -04006456struct nfs4_get_lease_time_data {
6457 struct nfs4_get_lease_time_args *args;
6458 struct nfs4_get_lease_time_res *res;
6459 struct nfs_client *clp;
6460};
6461
6462static void nfs4_get_lease_time_prepare(struct rpc_task *task,
6463 void *calldata)
6464{
Andy Adamson2050f0c2009-04-01 09:22:30 -04006465 struct nfs4_get_lease_time_data *data =
6466 (struct nfs4_get_lease_time_data *)calldata;
6467
6468 dprintk("--> %s\n", __func__);
6469 /* just setup sequence, do not trigger session recovery
6470 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006471 nfs41_setup_sequence(data->clp->cl_session,
6472 &data->args->la_seq_args,
6473 &data->res->lr_seq_res,
6474 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006475 dprintk("<-- %s\n", __func__);
6476}
6477
6478/*
6479 * Called from nfs4_state_manager thread for session setup, so don't recover
6480 * from sequence operation or clientid errors.
6481 */
6482static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
6483{
6484 struct nfs4_get_lease_time_data *data =
6485 (struct nfs4_get_lease_time_data *)calldata;
6486
6487 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006488 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
6489 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04006490 switch (task->tk_status) {
6491 case -NFS4ERR_DELAY:
6492 case -NFS4ERR_GRACE:
6493 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
6494 rpc_delay(task, NFS4_POLL_RETRY_MIN);
6495 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006496 /* fall through */
6497 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006498 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006499 return;
6500 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04006501 dprintk("<-- %s\n", __func__);
6502}
6503
Trond Myklebust17280172012-03-11 13:11:00 -04006504static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04006505 .rpc_call_prepare = nfs4_get_lease_time_prepare,
6506 .rpc_call_done = nfs4_get_lease_time_done,
6507};
6508
6509int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
6510{
6511 struct rpc_task *task;
6512 struct nfs4_get_lease_time_args args;
6513 struct nfs4_get_lease_time_res res = {
6514 .lr_fsinfo = fsinfo,
6515 };
6516 struct nfs4_get_lease_time_data data = {
6517 .args = &args,
6518 .res = &res,
6519 .clp = clp,
6520 };
6521 struct rpc_message msg = {
6522 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
6523 .rpc_argp = &args,
6524 .rpc_resp = &res,
6525 };
6526 struct rpc_task_setup task_setup = {
6527 .rpc_client = clp->cl_rpcclient,
6528 .rpc_message = &msg,
6529 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006530 .callback_data = &data,
6531 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04006532 };
6533 int status;
6534
Chuck Levera9c92d62013-08-09 12:48:18 -04006535 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006536 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006537 dprintk("--> %s\n", __func__);
6538 task = rpc_run_task(&task_setup);
6539
6540 if (IS_ERR(task))
6541 status = PTR_ERR(task);
6542 else {
6543 status = task->tk_status;
6544 rpc_put_task(task);
6545 }
6546 dprintk("<-- %s return %d\n", __func__, status);
6547
6548 return status;
6549}
6550
Andy Adamsonfc931582009-04-01 09:22:31 -04006551/*
6552 * Initialize the values to be used by the client in CREATE_SESSION
6553 * If nfs4_init_session set the fore channel request and response sizes,
6554 * use them.
6555 *
6556 * Set the back channel max_resp_sz_cached to zero to force the client to
6557 * always set csa_cachethis to FALSE because the current implementation
6558 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
6559 */
6560static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
6561{
Andy Adamson18aad3d2013-06-26 12:21:49 -04006562 unsigned int max_rqst_sz, max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006563
Andy Adamson18aad3d2013-06-26 12:21:49 -04006564 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
6565 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
6566
Andy Adamsonfc931582009-04-01 09:22:31 -04006567 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04006568 args->fc_attrs.max_rqst_sz = max_rqst_sz;
6569 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006570 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05006571 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04006572
6573 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05006574 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04006575 __func__,
6576 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05006577 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04006578
6579 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04006580 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
6581 args->bc_attrs.max_resp_sz = PAGE_SIZE;
6582 args->bc_attrs.max_resp_sz_cached = 0;
6583 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
6584 args->bc_attrs.max_reqs = 1;
6585
6586 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
6587 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
6588 __func__,
6589 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
6590 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
6591 args->bc_attrs.max_reqs);
6592}
6593
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006594static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04006595{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006596 struct nfs4_channel_attrs *sent = &args->fc_attrs;
6597 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
6598
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006599 if (rcvd->max_resp_sz > sent->max_resp_sz)
6600 return -EINVAL;
6601 /*
6602 * Our requested max_ops is the minimum we need; we're not
6603 * prepared to break up compounds into smaller pieces than that.
6604 * So, no point even trying to continue if the server won't
6605 * cooperate:
6606 */
6607 if (rcvd->max_ops < sent->max_ops)
6608 return -EINVAL;
6609 if (rcvd->max_reqs == 0)
6610 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006611 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
6612 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006613 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04006614}
6615
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006616static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
6617{
6618 struct nfs4_channel_attrs *sent = &args->bc_attrs;
6619 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04006620
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006621 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
6622 return -EINVAL;
6623 if (rcvd->max_resp_sz < sent->max_resp_sz)
6624 return -EINVAL;
6625 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
6626 return -EINVAL;
6627 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006628 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006629 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006630 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006631 return -EINVAL;
6632 return 0;
6633}
Andy Adamson8d353012009-04-01 09:22:32 -04006634
Andy Adamson8d353012009-04-01 09:22:32 -04006635static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
6636 struct nfs4_session *session)
6637{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006638 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04006639
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006640 ret = nfs4_verify_fore_channel_attrs(args, session);
6641 if (ret)
6642 return ret;
6643 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04006644}
6645
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006646static int _nfs4_proc_create_session(struct nfs_client *clp,
6647 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04006648{
6649 struct nfs4_session *session = clp->cl_session;
6650 struct nfs41_create_session_args args = {
6651 .client = clp,
6652 .cb_program = NFS4_CALLBACK,
6653 };
6654 struct nfs41_create_session_res res = {
6655 .client = clp,
6656 };
6657 struct rpc_message msg = {
6658 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
6659 .rpc_argp = &args,
6660 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006661 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04006662 };
6663 int status;
6664
6665 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04006666 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04006667
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006668 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006669 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04006670
Trond Myklebust43095d32012-11-20 11:13:12 -05006671 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04006672 /* Verify the session's negotiated channel_attrs values */
6673 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04006674 /* Increment the clientid slot sequence id */
6675 clp->cl_seqid++;
6676 }
6677
6678 return status;
6679}
6680
6681/*
6682 * Issues a CREATE_SESSION operation to the server.
6683 * It is the responsibility of the caller to verify the session is
6684 * expired before calling this routine.
6685 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006686int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04006687{
6688 int status;
6689 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04006690 struct nfs4_session *session = clp->cl_session;
6691
6692 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
6693
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006694 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04006695 if (status)
6696 goto out;
6697
Andy Adamsonaacd5532011-11-09 13:58:21 -05006698 /* Init or reset the session slot tables */
6699 status = nfs4_setup_session_slot_tables(session);
6700 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04006701 if (status)
6702 goto out;
6703
6704 ptr = (unsigned *)&session->sess_id.data[0];
6705 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
6706 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04006707out:
6708 dprintk("<-- %s\n", __func__);
6709 return status;
6710}
6711
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006712/*
6713 * Issue the over-the-wire RPC DESTROY_SESSION.
6714 * The caller must serialize access to this routine.
6715 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006716int nfs4_proc_destroy_session(struct nfs4_session *session,
6717 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006718{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006719 struct rpc_message msg = {
6720 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
6721 .rpc_argp = session,
6722 .rpc_cred = cred,
6723 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006724 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006725
6726 dprintk("--> nfs4_proc_destroy_session\n");
6727
6728 /* session is still being setup */
6729 if (session->clp->cl_cons_state != NFS_CS_READY)
6730 return status;
6731
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006732 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006733 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006734
6735 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04006736 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006737 "Session has been destroyed regardless...\n", status);
6738
6739 dprintk("<-- nfs4_proc_destroy_session\n");
6740 return status;
6741}
6742
Trond Myklebust7b38c362012-05-23 13:23:31 -04006743/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006744 * Renew the cl_session lease.
6745 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006746struct nfs4_sequence_data {
6747 struct nfs_client *clp;
6748 struct nfs4_sequence_args args;
6749 struct nfs4_sequence_res res;
6750};
6751
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006752static void nfs41_sequence_release(void *data)
6753{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006754 struct nfs4_sequence_data *calldata = data;
6755 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006756
Alexandros Batsakis71358402010-02-05 03:45:05 -08006757 if (atomic_read(&clp->cl_count) > 1)
6758 nfs4_schedule_state_renewal(clp);
6759 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006760 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006761}
6762
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006763static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6764{
6765 switch(task->tk_status) {
6766 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006767 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6768 return -EAGAIN;
6769 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006770 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006771 }
6772 return 0;
6773}
6774
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006775static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006776{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006777 struct nfs4_sequence_data *calldata = data;
6778 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006779
Trond Myklebust14516c32010-07-31 14:29:06 -04006780 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6781 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006782
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006783 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006784 if (task->tk_status < 0) {
6785 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006786 if (atomic_read(&clp->cl_count) == 1)
6787 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006788
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006789 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6790 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006791 return;
6792 }
6793 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006794 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006795out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006796 dprintk("<-- %s\n", __func__);
6797}
6798
6799static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6800{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006801 struct nfs4_sequence_data *calldata = data;
6802 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006803 struct nfs4_sequence_args *args;
6804 struct nfs4_sequence_res *res;
6805
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006806 args = task->tk_msg.rpc_argp;
6807 res = task->tk_msg.rpc_resp;
6808
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006809 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006810}
6811
6812static const struct rpc_call_ops nfs41_sequence_ops = {
6813 .rpc_call_done = nfs41_sequence_call_done,
6814 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006815 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006816};
6817
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006818static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
6819 struct rpc_cred *cred,
6820 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006821{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006822 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006823 struct rpc_message msg = {
6824 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6825 .rpc_cred = cred,
6826 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006827 struct rpc_task_setup task_setup_data = {
6828 .rpc_client = clp->cl_rpcclient,
6829 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006830 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04006831 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006832 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006833
Alexandros Batsakis71358402010-02-05 03:45:05 -08006834 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006835 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006836 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006837 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006838 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006839 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006840 }
Chuck Levera9c92d62013-08-09 12:48:18 -04006841 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006842 if (is_privileged)
6843 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006844 msg.rpc_argp = &calldata->args;
6845 msg.rpc_resp = &calldata->res;
6846 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006847 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006848
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006849 return rpc_run_task(&task_setup_data);
6850}
6851
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006852static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006853{
6854 struct rpc_task *task;
6855 int ret = 0;
6856
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006857 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6858 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006859 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006860 if (IS_ERR(task))
6861 ret = PTR_ERR(task);
6862 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006863 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006864 dprintk("<-- %s status=%d\n", __func__, ret);
6865 return ret;
6866}
6867
6868static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6869{
6870 struct rpc_task *task;
6871 int ret;
6872
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006873 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006874 if (IS_ERR(task)) {
6875 ret = PTR_ERR(task);
6876 goto out;
6877 }
6878 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006879 if (!ret) {
6880 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6881
6882 if (task->tk_status == 0)
6883 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006884 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006885 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006886 rpc_put_task(task);
6887out:
6888 dprintk("<-- %s status=%d\n", __func__, ret);
6889 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006890}
6891
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006892struct nfs4_reclaim_complete_data {
6893 struct nfs_client *clp;
6894 struct nfs41_reclaim_complete_args arg;
6895 struct nfs41_reclaim_complete_res res;
6896};
6897
6898static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6899{
6900 struct nfs4_reclaim_complete_data *calldata = data;
6901
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006902 nfs41_setup_sequence(calldata->clp->cl_session,
6903 &calldata->arg.seq_args,
6904 &calldata->res.seq_res,
6905 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006906}
6907
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006908static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6909{
6910 switch(task->tk_status) {
6911 case 0:
6912 case -NFS4ERR_COMPLETE_ALREADY:
6913 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6914 break;
6915 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006916 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006917 /* fall through */
6918 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006919 return -EAGAIN;
6920 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006921 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006922 }
6923 return 0;
6924}
6925
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006926static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6927{
6928 struct nfs4_reclaim_complete_data *calldata = data;
6929 struct nfs_client *clp = calldata->clp;
6930 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6931
6932 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006933 if (!nfs41_sequence_done(task, res))
6934 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006935
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006936 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006937 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6938 rpc_restart_call_prepare(task);
6939 return;
6940 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006941 dprintk("<-- %s\n", __func__);
6942}
6943
6944static void nfs4_free_reclaim_complete_data(void *data)
6945{
6946 struct nfs4_reclaim_complete_data *calldata = data;
6947
6948 kfree(calldata);
6949}
6950
6951static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6952 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6953 .rpc_call_done = nfs4_reclaim_complete_done,
6954 .rpc_release = nfs4_free_reclaim_complete_data,
6955};
6956
6957/*
6958 * Issue a global reclaim complete.
6959 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04006960static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
6961 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006962{
6963 struct nfs4_reclaim_complete_data *calldata;
6964 struct rpc_task *task;
6965 struct rpc_message msg = {
6966 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04006967 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006968 };
6969 struct rpc_task_setup task_setup_data = {
6970 .rpc_client = clp->cl_rpcclient,
6971 .rpc_message = &msg,
6972 .callback_ops = &nfs4_reclaim_complete_call_ops,
6973 .flags = RPC_TASK_ASYNC,
6974 };
6975 int status = -ENOMEM;
6976
6977 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006978 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006979 if (calldata == NULL)
6980 goto out;
6981 calldata->clp = clp;
6982 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006983
Chuck Levera9c92d62013-08-09 12:48:18 -04006984 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006985 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006986 msg.rpc_argp = &calldata->arg;
6987 msg.rpc_resp = &calldata->res;
6988 task_setup_data.callback_data = calldata;
6989 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006990 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006991 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006992 goto out;
6993 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006994 status = nfs4_wait_for_completion_rpc_task(task);
6995 if (status == 0)
6996 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006997 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006998 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006999out:
7000 dprintk("<-- %s status=%d\n", __func__, status);
7001 return status;
7002}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007003
7004static void
7005nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
7006{
7007 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00007008 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007009 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007010
7011 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00007012 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
7013 * right now covering the LAYOUTGET we are about to send.
7014 * However, that is not so catastrophic, and there seems
7015 * to be no way to prevent it completely.
7016 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007017 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05007018 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007019 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00007020 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
7021 NFS_I(lgp->args.inode)->layout,
7022 lgp->args.ctx->state)) {
7023 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00007024 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007025}
7026
7027static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
7028{
7029 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07007030 struct inode *inode = lgp->args.inode;
7031 struct nfs_server *server = NFS_SERVER(inode);
7032 struct pnfs_layout_hdr *lo;
7033 struct nfs4_state *state = NULL;
Weston Andros Adamson30005122013-02-28 20:30:10 -05007034 unsigned long timeo, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007035
7036 dprintk("--> %s\n", __func__);
7037
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007038 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07007039 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007040
7041 switch (task->tk_status) {
7042 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07007043 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007044 case -NFS4ERR_LAYOUTTRYLATER:
7045 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05007046 timeo = rpc_get_timeout(task->tk_client);
7047 giveup = lgp->args.timestamp + timeo;
7048 if (time_after(giveup, jiffies))
7049 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07007050 break;
7051 case -NFS4ERR_EXPIRED:
7052 case -NFS4ERR_BAD_STATEID:
7053 spin_lock(&inode->i_lock);
7054 lo = NFS_I(inode)->layout;
7055 if (!lo || list_empty(&lo->plh_segs)) {
7056 spin_unlock(&inode->i_lock);
7057 /* If the open stateid was bad, then recover it. */
7058 state = lgp->args.ctx->state;
7059 } else {
7060 LIST_HEAD(head);
7061
7062 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
7063 spin_unlock(&inode->i_lock);
7064 /* Mark the bad layout state as invalid, then
7065 * retry using the open stateid. */
7066 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007067 }
7068 }
Trond Myklebustee314c22012-10-01 17:25:48 -07007069 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
7070 rpc_restart_call_prepare(task);
7071out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007072 dprintk("<-- %s\n", __func__);
7073}
7074
Idan Kedar85541162012-08-02 11:47:10 +03007075static size_t max_response_pages(struct nfs_server *server)
7076{
7077 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
7078 return nfs_page_array_len(0, max_resp_sz);
7079}
7080
7081static void nfs4_free_pages(struct page **pages, size_t size)
7082{
7083 int i;
7084
7085 if (!pages)
7086 return;
7087
7088 for (i = 0; i < size; i++) {
7089 if (!pages[i])
7090 break;
7091 __free_page(pages[i]);
7092 }
7093 kfree(pages);
7094}
7095
7096static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
7097{
7098 struct page **pages;
7099 int i;
7100
7101 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
7102 if (!pages) {
7103 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
7104 return NULL;
7105 }
7106
7107 for (i = 0; i < size; i++) {
7108 pages[i] = alloc_page(gfp_flags);
7109 if (!pages[i]) {
7110 dprintk("%s: failed to allocate page\n", __func__);
7111 nfs4_free_pages(pages, size);
7112 return NULL;
7113 }
7114 }
7115
7116 return pages;
7117}
7118
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007119static void nfs4_layoutget_release(void *calldata)
7120{
7121 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007122 struct inode *inode = lgp->args.inode;
7123 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007124 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007125
7126 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03007127 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007128 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007129 put_nfs_open_context(lgp->args.ctx);
7130 kfree(calldata);
7131 dprintk("<-- %s\n", __func__);
7132}
7133
7134static const struct rpc_call_ops nfs4_layoutget_call_ops = {
7135 .rpc_call_prepare = nfs4_layoutget_prepare,
7136 .rpc_call_done = nfs4_layoutget_done,
7137 .rpc_release = nfs4_layoutget_release,
7138};
7139
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007140struct pnfs_layout_segment *
7141nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007142{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007143 struct inode *inode = lgp->args.inode;
7144 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007145 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007146 struct rpc_task *task;
7147 struct rpc_message msg = {
7148 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
7149 .rpc_argp = &lgp->args,
7150 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04007151 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007152 };
7153 struct rpc_task_setup task_setup_data = {
7154 .rpc_client = server->client,
7155 .rpc_message = &msg,
7156 .callback_ops = &nfs4_layoutget_call_ops,
7157 .callback_data = lgp,
7158 .flags = RPC_TASK_ASYNC,
7159 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007160 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007161 int status = 0;
7162
7163 dprintk("--> %s\n", __func__);
7164
Idan Kedar85541162012-08-02 11:47:10 +03007165 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
7166 if (!lgp->args.layout.pages) {
7167 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007168 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03007169 }
7170 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05007171 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03007172
Weston Andros Adamson35124a02011-03-24 16:48:21 -04007173 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007174 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04007175 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007176
7177 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
7178 pnfs_get_layout_hdr(NFS_I(inode)->layout);
7179
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007180 task = rpc_run_task(&task_setup_data);
7181 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007182 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007183 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00007184 if (status == 0)
7185 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007186 trace_nfs4_layoutget(lgp->args.ctx,
7187 &lgp->args.range,
7188 &lgp->res.range,
7189 status);
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05007190 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
7191 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007192 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007193 rpc_put_task(task);
7194 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007195 if (status)
7196 return ERR_PTR(status);
7197 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007198}
7199
Benny Halevycbe82602011-05-22 19:52:37 +03007200static void
7201nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
7202{
7203 struct nfs4_layoutreturn *lrp = calldata;
7204
7205 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007206 nfs41_setup_sequence(lrp->clp->cl_session,
7207 &lrp->args.seq_args,
7208 &lrp->res.seq_res,
7209 task);
Benny Halevycbe82602011-05-22 19:52:37 +03007210}
7211
7212static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
7213{
7214 struct nfs4_layoutreturn *lrp = calldata;
7215 struct nfs_server *server;
7216
7217 dprintk("--> %s\n", __func__);
7218
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007219 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03007220 return;
7221
7222 server = NFS_SERVER(lrp->args.inode);
7223 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007224 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03007225 return;
7226 }
Benny Halevycbe82602011-05-22 19:52:37 +03007227 dprintk("<-- %s\n", __func__);
7228}
7229
7230static void nfs4_layoutreturn_release(void *calldata)
7231{
7232 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04007233 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03007234
7235 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04007236 spin_lock(&lo->plh_inode->i_lock);
7237 if (lrp->res.lrs_present)
7238 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
7239 lo->plh_block_lgets--;
7240 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04007241 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03007242 kfree(calldata);
7243 dprintk("<-- %s\n", __func__);
7244}
7245
7246static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
7247 .rpc_call_prepare = nfs4_layoutreturn_prepare,
7248 .rpc_call_done = nfs4_layoutreturn_done,
7249 .rpc_release = nfs4_layoutreturn_release,
7250};
7251
7252int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
7253{
7254 struct rpc_task *task;
7255 struct rpc_message msg = {
7256 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
7257 .rpc_argp = &lrp->args,
7258 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04007259 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03007260 };
7261 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04007262 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03007263 .rpc_message = &msg,
7264 .callback_ops = &nfs4_layoutreturn_call_ops,
7265 .callback_data = lrp,
7266 };
7267 int status;
7268
7269 dprintk("--> %s\n", __func__);
Chuck Levera9c92d62013-08-09 12:48:18 -04007270 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03007271 task = rpc_run_task(&task_setup_data);
7272 if (IS_ERR(task))
7273 return PTR_ERR(task);
7274 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007275 trace_nfs4_layoutreturn(lrp->args.inode, status);
Benny Halevycbe82602011-05-22 19:52:37 +03007276 dprintk("<-- %s status=%d\n", __func__, status);
7277 rpc_put_task(task);
7278 return status;
7279}
7280
Andy Adamson7f11d8d2011-07-30 20:52:35 -04007281/*
7282 * Retrieve the list of Data Server devices from the MDS.
7283 */
7284static int _nfs4_getdevicelist(struct nfs_server *server,
7285 const struct nfs_fh *fh,
7286 struct pnfs_devicelist *devlist)
7287{
7288 struct nfs4_getdevicelist_args args = {
7289 .fh = fh,
7290 .layoutclass = server->pnfs_curr_ld->id,
7291 };
7292 struct nfs4_getdevicelist_res res = {
7293 .devlist = devlist,
7294 };
7295 struct rpc_message msg = {
7296 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
7297 .rpc_argp = &args,
7298 .rpc_resp = &res,
7299 };
7300 int status;
7301
7302 dprintk("--> %s\n", __func__);
7303 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
7304 &res.seq_res, 0);
7305 dprintk("<-- %s status=%d\n", __func__, status);
7306 return status;
7307}
7308
7309int nfs4_proc_getdevicelist(struct nfs_server *server,
7310 const struct nfs_fh *fh,
7311 struct pnfs_devicelist *devlist)
7312{
7313 struct nfs4_exception exception = { };
7314 int err;
7315
7316 do {
7317 err = nfs4_handle_exception(server,
7318 _nfs4_getdevicelist(server, fh, devlist),
7319 &exception);
7320 } while (exception.retry);
7321
7322 dprintk("%s: err=%d, num_devs=%u\n", __func__,
7323 err, devlist->num_devs);
7324
7325 return err;
7326}
7327EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
7328
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007329static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007330_nfs4_proc_getdeviceinfo(struct nfs_server *server,
7331 struct pnfs_device *pdev,
7332 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007333{
7334 struct nfs4_getdeviceinfo_args args = {
7335 .pdev = pdev,
7336 };
7337 struct nfs4_getdeviceinfo_res res = {
7338 .pdev = pdev,
7339 };
7340 struct rpc_message msg = {
7341 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
7342 .rpc_argp = &args,
7343 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007344 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007345 };
7346 int status;
7347
7348 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00007349 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007350 dprintk("<-- %s status=%d\n", __func__, status);
7351
7352 return status;
7353}
7354
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007355int nfs4_proc_getdeviceinfo(struct nfs_server *server,
7356 struct pnfs_device *pdev,
7357 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007358{
7359 struct nfs4_exception exception = { };
7360 int err;
7361
7362 do {
7363 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007364 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007365 &exception);
7366 } while (exception.retry);
7367 return err;
7368}
7369EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
7370
Andy Adamson863a3c62011-03-23 13:27:54 +00007371static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
7372{
7373 struct nfs4_layoutcommit_data *data = calldata;
7374 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007375 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00007376
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007377 nfs41_setup_sequence(session,
7378 &data->args.seq_args,
7379 &data->res.seq_res,
7380 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00007381}
7382
7383static void
7384nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
7385{
7386 struct nfs4_layoutcommit_data *data = calldata;
7387 struct nfs_server *server = NFS_SERVER(data->args.inode);
7388
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007389 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00007390 return;
7391
7392 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007393 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
7394 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
7395 case -NFS4ERR_BADLAYOUT: /* no layout */
7396 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00007397 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007398 break;
7399 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00007400 nfs_post_op_update_inode_force_wcc(data->args.inode,
7401 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007402 break;
7403 default:
7404 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
7405 rpc_restart_call_prepare(task);
7406 return;
7407 }
7408 }
Andy Adamson863a3c62011-03-23 13:27:54 +00007409}
7410
7411static void nfs4_layoutcommit_release(void *calldata)
7412{
7413 struct nfs4_layoutcommit_data *data = calldata;
7414
Andy Adamsondb29c082011-07-30 20:52:38 -04007415 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00007416 put_rpccred(data->cred);
7417 kfree(data);
7418}
7419
7420static const struct rpc_call_ops nfs4_layoutcommit_ops = {
7421 .rpc_call_prepare = nfs4_layoutcommit_prepare,
7422 .rpc_call_done = nfs4_layoutcommit_done,
7423 .rpc_release = nfs4_layoutcommit_release,
7424};
7425
7426int
Andy Adamsonef311532011-03-12 02:58:10 -05007427nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00007428{
7429 struct rpc_message msg = {
7430 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
7431 .rpc_argp = &data->args,
7432 .rpc_resp = &data->res,
7433 .rpc_cred = data->cred,
7434 };
7435 struct rpc_task_setup task_setup_data = {
7436 .task = &data->task,
7437 .rpc_client = NFS_CLIENT(data->args.inode),
7438 .rpc_message = &msg,
7439 .callback_ops = &nfs4_layoutcommit_ops,
7440 .callback_data = data,
7441 .flags = RPC_TASK_ASYNC,
7442 };
7443 struct rpc_task *task;
7444 int status = 0;
7445
7446 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
7447 "lbw: %llu inode %lu\n",
7448 data->task.tk_pid, sync,
7449 data->args.lastbytewritten,
7450 data->args.inode->i_ino);
7451
Chuck Levera9c92d62013-08-09 12:48:18 -04007452 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00007453 task = rpc_run_task(&task_setup_data);
7454 if (IS_ERR(task))
7455 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05007456 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00007457 goto out;
7458 status = nfs4_wait_for_completion_rpc_task(task);
7459 if (status != 0)
7460 goto out;
7461 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007462 trace_nfs4_layoutcommit(data->args.inode, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00007463out:
7464 dprintk("%s: status %d\n", __func__, status);
7465 rpc_put_task(task);
7466 return status;
7467}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007468
Andy Adamson97431202013-08-08 10:57:56 -04007469/**
7470 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
7471 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
7472 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007473static int
7474_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7475 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
7476{
7477 struct nfs41_secinfo_no_name_args args = {
7478 .style = SECINFO_STYLE_CURRENT_FH,
7479 };
7480 struct nfs4_secinfo_res res = {
7481 .flavors = flavors,
7482 };
7483 struct rpc_message msg = {
7484 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
7485 .rpc_argp = &args,
7486 .rpc_resp = &res,
7487 };
Andy Adamson97431202013-08-08 10:57:56 -04007488 return nfs4_call_sync(server->nfs_client->cl_rpcclient, server, &msg,
7489 &args.seq_args, &res.seq_res, 0);
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007490}
7491
7492static int
7493nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7494 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
7495{
7496 struct nfs4_exception exception = { };
7497 int err;
7498 do {
7499 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
7500 switch (err) {
7501 case 0:
7502 case -NFS4ERR_WRONGSEC:
7503 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007504 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007505 default:
7506 err = nfs4_handle_exception(server, err, &exception);
7507 }
7508 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007509out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007510 return err;
7511}
7512
7513static int
7514nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
7515 struct nfs_fsinfo *info)
7516{
7517 int err;
7518 struct page *page;
7519 rpc_authflavor_t flavor;
7520 struct nfs4_secinfo_flavors *flavors;
7521
7522 page = alloc_page(GFP_KERNEL);
7523 if (!page) {
7524 err = -ENOMEM;
7525 goto out;
7526 }
7527
7528 flavors = page_address(page);
7529 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
7530
7531 /*
7532 * Fall back on "guess and check" method if
7533 * the server doesn't support SECINFO_NO_NAME
7534 */
7535 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
7536 err = nfs4_find_root_sec(server, fhandle, info);
7537 goto out_freepage;
7538 }
7539 if (err)
7540 goto out_freepage;
7541
7542 flavor = nfs_find_best_sec(flavors);
7543 if (err == 0)
7544 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
7545
7546out_freepage:
7547 put_page(page);
7548 if (err == -EACCES)
7549 return -EPERM;
7550out:
7551 return err;
7552}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007553
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007554static int _nfs41_test_stateid(struct nfs_server *server,
7555 nfs4_stateid *stateid,
7556 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04007557{
7558 int status;
7559 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007560 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04007561 };
7562 struct nfs41_test_stateid_res res;
7563 struct rpc_message msg = {
7564 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
7565 .rpc_argp = &args,
7566 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007567 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04007568 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007569 struct rpc_clnt *rpc_client = server->client;
7570
7571 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
7572 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007573
Chuck Lever38527b12012-07-11 16:30:23 -04007574 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04007575 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007576 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007577 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007578 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04007579 if (status != NFS_OK) {
7580 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04007581 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04007582 }
7583 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04007584 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04007585}
7586
Chuck Lever38527b12012-07-11 16:30:23 -04007587/**
7588 * nfs41_test_stateid - perform a TEST_STATEID operation
7589 *
7590 * @server: server / transport on which to perform the operation
7591 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007592 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04007593 *
7594 * Returns NFS_OK if the server recognizes that "stateid" is valid.
7595 * Otherwise a negative NFS4ERR value is returned if the operation
7596 * failed or the state ID is not currently valid.
7597 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007598static int nfs41_test_stateid(struct nfs_server *server,
7599 nfs4_stateid *stateid,
7600 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04007601{
7602 struct nfs4_exception exception = { };
7603 int err;
7604 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007605 err = _nfs41_test_stateid(server, stateid, cred);
Chuck Lever377e5072012-07-11 16:29:45 -04007606 if (err != -NFS4ERR_DELAY)
7607 break;
7608 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04007609 } while (exception.retry);
7610 return err;
7611}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007612
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007613struct nfs_free_stateid_data {
7614 struct nfs_server *server;
7615 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007616 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007617};
7618
7619static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
7620{
7621 struct nfs_free_stateid_data *data = calldata;
7622 nfs41_setup_sequence(nfs4_get_session(data->server),
7623 &data->args.seq_args,
7624 &data->res.seq_res,
7625 task);
7626}
7627
7628static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
7629{
7630 struct nfs_free_stateid_data *data = calldata;
7631
7632 nfs41_sequence_done(task, &data->res.seq_res);
7633
7634 switch (task->tk_status) {
7635 case -NFS4ERR_DELAY:
7636 if (nfs4_async_handle_error(task, data->server, NULL) == -EAGAIN)
7637 rpc_restart_call_prepare(task);
7638 }
7639}
7640
7641static void nfs41_free_stateid_release(void *calldata)
7642{
7643 kfree(calldata);
7644}
7645
Trond Myklebust17f26b12013-08-21 15:48:42 -04007646static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007647 .rpc_call_prepare = nfs41_free_stateid_prepare,
7648 .rpc_call_done = nfs41_free_stateid_done,
7649 .rpc_release = nfs41_free_stateid_release,
7650};
7651
7652static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
7653 nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007654 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007655 bool privileged)
7656{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007657 struct rpc_message msg = {
7658 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007659 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007660 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007661 struct rpc_task_setup task_setup = {
7662 .rpc_client = server->client,
7663 .rpc_message = &msg,
7664 .callback_ops = &nfs41_free_stateid_ops,
7665 .flags = RPC_TASK_ASYNC,
7666 };
7667 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007668
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007669 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
7670 &task_setup.rpc_client, &msg);
7671
Chuck Lever38527b12012-07-11 16:30:23 -04007672 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007673 data = kmalloc(sizeof(*data), GFP_NOFS);
7674 if (!data)
7675 return ERR_PTR(-ENOMEM);
7676 data->server = server;
7677 nfs4_stateid_copy(&data->args.stateid, stateid);
7678
7679 task_setup.callback_data = data;
7680
7681 msg.rpc_argp = &data->args;
7682 msg.rpc_resp = &data->res;
Chuck Levera9c92d62013-08-09 12:48:18 -04007683 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007684 if (privileged)
7685 nfs4_set_sequence_privileged(&data->args.seq_args);
7686
7687 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007688}
7689
Chuck Lever38527b12012-07-11 16:30:23 -04007690/**
7691 * nfs41_free_stateid - perform a FREE_STATEID operation
7692 *
7693 * @server: server / transport on which to perform the operation
7694 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007695 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04007696 *
7697 * Returns NFS_OK if the server freed "stateid". Otherwise a
7698 * negative NFS4ERR value is returned.
7699 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007700static int nfs41_free_stateid(struct nfs_server *server,
7701 nfs4_stateid *stateid,
7702 struct rpc_cred *cred)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007703{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007704 struct rpc_task *task;
7705 int ret;
7706
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007707 task = _nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007708 if (IS_ERR(task))
7709 return PTR_ERR(task);
7710 ret = rpc_wait_for_completion_task(task);
7711 if (!ret)
7712 ret = task->tk_status;
7713 rpc_put_task(task);
7714 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007715}
Trond Myklebust36281ca2012-03-04 18:13:56 -05007716
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007717static int nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
7718{
7719 struct rpc_task *task;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007720 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007721
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007722 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007723 nfs4_free_lock_state(server, lsp);
7724 if (IS_ERR(task))
7725 return PTR_ERR(task);
7726 rpc_put_task(task);
7727 return 0;
7728}
7729
Trond Myklebust36281ca2012-03-04 18:13:56 -05007730static bool nfs41_match_stateid(const nfs4_stateid *s1,
7731 const nfs4_stateid *s2)
7732{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007733 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007734 return false;
7735
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007736 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007737 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007738 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007739 return true;
7740
7741 return false;
7742}
7743
Andy Adamson557134a2009-04-01 09:21:53 -04007744#endif /* CONFIG_NFS_V4_1 */
7745
Trond Myklebust36281ca2012-03-04 18:13:56 -05007746static bool nfs4_match_stateid(const nfs4_stateid *s1,
7747 const nfs4_stateid *s2)
7748{
Trond Myklebustf597c532012-03-04 18:13:56 -05007749 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05007750}
7751
7752
Trond Myklebust17280172012-03-11 13:11:00 -04007753static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007754 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007755 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007756 .recover_open = nfs4_open_reclaim,
7757 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04007758 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04007759 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007760};
7761
Andy Adamson591d71c2009-04-01 09:22:47 -04007762#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007763static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007764 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
7765 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
7766 .recover_open = nfs4_open_reclaim,
7767 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05007768 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007769 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04007770 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04007771};
7772#endif /* CONFIG_NFS_V4_1 */
7773
Trond Myklebust17280172012-03-11 13:11:00 -04007774static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007775 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007776 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007777 .recover_open = nfs4_open_expired,
7778 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04007779 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007780};
7781
Andy Adamson591d71c2009-04-01 09:22:47 -04007782#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007783static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007784 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
7785 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007786 .recover_open = nfs41_open_expired,
7787 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05007788 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04007789};
7790#endif /* CONFIG_NFS_V4_1 */
7791
Trond Myklebust17280172012-03-11 13:11:00 -04007792static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007793 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04007794 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007795 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04007796};
7797
7798#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007799static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007800 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04007801 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007802 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04007803};
7804#endif
7805
Trond Myklebust97dc1352010-06-16 09:52:26 -04007806static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
7807 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04007808 .init_caps = NFS_CAP_READDIRPLUS
7809 | NFS_CAP_ATOMIC_OPEN
7810 | NFS_CAP_CHANGE_ATTR
7811 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04007812 .init_client = nfs40_init_client,
7813 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007814 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007815 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007816 .free_lock_state = nfs4_release_lockowner,
Chuck Lever9915ea72013-08-09 12:48:27 -04007817 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007818 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
7819 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
7820 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007821};
7822
7823#if defined(CONFIG_NFS_V4_1)
7824static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
7825 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04007826 .init_caps = NFS_CAP_READDIRPLUS
7827 | NFS_CAP_ATOMIC_OPEN
7828 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04007829 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04007830 | NFS_CAP_STATEID_NFSV41
7831 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04007832 .init_client = nfs41_init_client,
7833 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007834 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007835 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007836 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04007837 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007838 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7839 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7840 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007841};
7842#endif
7843
Steve Dickson42c2c422013-05-22 12:50:38 -04007844#if defined(CONFIG_NFS_V4_2)
7845static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
7846 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04007847 .init_caps = NFS_CAP_READDIRPLUS
7848 | NFS_CAP_ATOMIC_OPEN
7849 | NFS_CAP_CHANGE_ATTR
7850 | NFS_CAP_POSIX_LOCK
7851 | NFS_CAP_STATEID_NFSV41
7852 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04007853 .init_client = nfs41_init_client,
7854 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04007855 .match_stateid = nfs41_match_stateid,
7856 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04007857 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04007858 .call_sync_ops = &nfs41_call_sync_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04007859 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7860 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7861 .state_renewal_ops = &nfs41_state_renewal_ops,
7862};
7863#endif
7864
Trond Myklebust97dc1352010-06-16 09:52:26 -04007865const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
7866 [0] = &nfs_v4_0_minor_ops,
7867#if defined(CONFIG_NFS_V4_1)
7868 [1] = &nfs_v4_1_minor_ops,
7869#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04007870#if defined(CONFIG_NFS_V4_2)
7871 [2] = &nfs_v4_2_minor_ops,
7872#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04007873};
7874
Trond Myklebust17f26b12013-08-21 15:48:42 -04007875static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04007876 .create = nfs_create,
7877 .lookup = nfs_lookup,
7878 .atomic_open = nfs_atomic_open,
7879 .link = nfs_link,
7880 .unlink = nfs_unlink,
7881 .symlink = nfs_symlink,
7882 .mkdir = nfs_mkdir,
7883 .rmdir = nfs_rmdir,
7884 .mknod = nfs_mknod,
7885 .rename = nfs_rename,
7886 .permission = nfs_permission,
7887 .getattr = nfs_getattr,
7888 .setattr = nfs_setattr,
7889 .getxattr = generic_getxattr,
7890 .setxattr = generic_setxattr,
7891 .listxattr = generic_listxattr,
7892 .removexattr = generic_removexattr,
7893};
7894
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08007895static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007896 .permission = nfs_permission,
7897 .getattr = nfs_getattr,
7898 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007899 .getxattr = generic_getxattr,
7900 .setxattr = generic_setxattr,
7901 .listxattr = generic_listxattr,
7902 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007903};
7904
David Howells509de812006-08-22 20:06:11 -04007905const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007906 .version = 4, /* protocol version */
7907 .dentry_ops = &nfs4_dentry_operations,
7908 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007909 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04007910 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007911 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04007912 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04007913 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007914 .getattr = nfs4_proc_getattr,
7915 .setattr = nfs4_proc_setattr,
7916 .lookup = nfs4_proc_lookup,
7917 .access = nfs4_proc_access,
7918 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007919 .create = nfs4_proc_create,
7920 .remove = nfs4_proc_remove,
7921 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04007922 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007923 .unlink_done = nfs4_proc_unlink_done,
7924 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04007925 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04007926 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04007927 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007928 .link = nfs4_proc_link,
7929 .symlink = nfs4_proc_symlink,
7930 .mkdir = nfs4_proc_mkdir,
7931 .rmdir = nfs4_proc_remove,
7932 .readdir = nfs4_proc_readdir,
7933 .mknod = nfs4_proc_mknod,
7934 .statfs = nfs4_proc_statfs,
7935 .fsinfo = nfs4_proc_fsinfo,
7936 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04007937 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007938 .decode_dirent = nfs4_decode_dirent,
7939 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04007940 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04007941 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05007942 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007943 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04007944 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04007945 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007946 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007947 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04007948 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007949 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007950 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00007951 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04007952 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04007953 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04007954 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04007955 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04007956 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00007957 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04007958 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04007959 .create_server = nfs4_create_server,
7960 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007961};
7962
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007963static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7964 .prefix = XATTR_NAME_NFSV4_ACL,
7965 .list = nfs4_xattr_list_nfs4_acl,
7966 .get = nfs4_xattr_get_nfs4_acl,
7967 .set = nfs4_xattr_set_nfs4_acl,
7968};
7969
7970const struct xattr_handler *nfs4_xattr_handlers[] = {
7971 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04007972#ifdef CONFIG_NFS_V4_SECURITY_LABEL
7973 &nfs4_xattr_nfs4_label_handler,
7974#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007975 NULL
7976};
7977
Linus Torvalds1da177e2005-04-16 15:20:36 -07007978/*
7979 * Local variables:
7980 * c-basic-offset: 8
7981 * End:
7982 */