blob: d3c4255be6e02119735147822947e34a7fa6da43 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Trond Myklebust6926afd2012-01-07 13:22:46 -050054#include <linux/nfs_idmap.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000055#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050056#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010057#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050066#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000067#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Trond Myklebustc6d01c62013-08-09 11:51:26 -040069#include "nfs4trace.h"
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define NFSDBG_FACILITY NFSDBG_PROC
72
Trond Myklebust2066fe82006-09-15 08:30:46 -040073#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define NFS4_POLL_RETRY_MAX (15*HZ)
75
Trond Myklebustcdd4e682006-01-03 09:55:12 +010076struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010077static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080078static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050080static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Chuck Lever81934dd2012-03-01 17:01:57 -050081static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040082static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
83static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040084static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
85 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -040086 struct nfs4_state *state, struct nfs4_label *ilabel,
87 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040088#ifdef CONFIG_NFS_V4_1
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040089static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
90 struct rpc_cred *);
91static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *,
92 struct rpc_cred *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040093#endif
David Quigleyaa9c2662013-05-22 12:50:44 -040094
95#ifdef CONFIG_NFS_V4_SECURITY_LABEL
96static inline struct nfs4_label *
97nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
98 struct iattr *sattr, struct nfs4_label *label)
99{
100 int err;
101
102 if (label == NULL)
103 return NULL;
104
105 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
106 return NULL;
107
108 if (NFS_SERVER(dir)->nfs_client->cl_minorversion < 2)
109 return NULL;
110
111 err = security_dentry_init_security(dentry, sattr->ia_mode,
112 &dentry->d_name, (void **)&label->label, &label->len);
113 if (err == 0)
114 return label;
115
116 return NULL;
117}
118static inline void
119nfs4_label_release_security(struct nfs4_label *label)
120{
121 if (label)
122 security_release_secctx(label->label, label->len);
123}
124static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
125{
126 if (label)
127 return server->attr_bitmask;
128
129 return server->attr_bitmask_nl;
130}
131#else
132static inline struct nfs4_label *
133nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
134 struct iattr *sattr, struct nfs4_label *l)
135{ return NULL; }
136static inline void
137nfs4_label_release_security(struct nfs4_label *label)
138{ return; }
139static inline u32 *
140nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
141{ return server->attr_bitmask; }
142#endif
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500145static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Trond Myklebust52567b02009-10-23 14:46:42 -0400147 if (err >= -1000)
148 return err;
149 switch (err) {
150 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500151 case -NFS4ERR_LAYOUTTRYLATER:
152 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400153 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000154 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400155 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000156 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800157 case -NFS4ERR_BADOWNER:
158 case -NFS4ERR_BADNAME:
159 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400160 case -NFS4ERR_SHARE_DENIED:
161 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400162 case -NFS4ERR_MINOR_VERS_MISMATCH:
163 return -EPROTONOSUPPORT;
Weston Andros Adamson6168f622012-09-10 14:00:46 -0400164 case -NFS4ERR_ACCESS:
165 return -EACCES;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400166 case -NFS4ERR_FILE_OPEN:
167 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400168 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700170 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400171 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400173 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
175
176/*
177 * This is our standard bitmap for GETATTR requests.
178 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400179const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 FATTR4_WORD0_TYPE
181 | FATTR4_WORD0_CHANGE
182 | FATTR4_WORD0_SIZE
183 | FATTR4_WORD0_FSID
184 | FATTR4_WORD0_FILEID,
185 FATTR4_WORD1_MODE
186 | FATTR4_WORD1_NUMLINKS
187 | FATTR4_WORD1_OWNER
188 | FATTR4_WORD1_OWNER_GROUP
189 | FATTR4_WORD1_RAWDEV
190 | FATTR4_WORD1_SPACE_USED
191 | FATTR4_WORD1_TIME_ACCESS
192 | FATTR4_WORD1_TIME_METADATA
David Quigleyaa9c2662013-05-22 12:50:44 -0400193 | FATTR4_WORD1_TIME_MODIFY,
194#ifdef CONFIG_NFS_V4_SECURITY_LABEL
195 FATTR4_WORD2_SECURITY_LABEL
196#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197};
198
Trond Myklebust1549210f2012-06-05 09:16:47 -0400199static const u32 nfs4_pnfs_open_bitmap[3] = {
200 FATTR4_WORD0_TYPE
201 | FATTR4_WORD0_CHANGE
202 | FATTR4_WORD0_SIZE
203 | FATTR4_WORD0_FSID
204 | FATTR4_WORD0_FILEID,
205 FATTR4_WORD1_MODE
206 | FATTR4_WORD1_NUMLINKS
207 | FATTR4_WORD1_OWNER
208 | FATTR4_WORD1_OWNER_GROUP
209 | FATTR4_WORD1_RAWDEV
210 | FATTR4_WORD1_SPACE_USED
211 | FATTR4_WORD1_TIME_ACCESS
212 | FATTR4_WORD1_TIME_METADATA
213 | FATTR4_WORD1_TIME_MODIFY,
214 FATTR4_WORD2_MDSTHRESHOLD
215};
216
Andy Adamsone23008e2012-10-02 21:07:32 -0400217static const u32 nfs4_open_noattr_bitmap[3] = {
218 FATTR4_WORD0_TYPE
219 | FATTR4_WORD0_CHANGE
220 | FATTR4_WORD0_FILEID,
221};
222
David Quigleya09df2c2013-05-22 12:50:41 -0400223const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 FATTR4_WORD0_FILES_AVAIL
225 | FATTR4_WORD0_FILES_FREE
226 | FATTR4_WORD0_FILES_TOTAL,
227 FATTR4_WORD1_SPACE_AVAIL
228 | FATTR4_WORD1_SPACE_FREE
229 | FATTR4_WORD1_SPACE_TOTAL
230};
231
David Quigleya09df2c2013-05-22 12:50:41 -0400232const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 FATTR4_WORD0_MAXLINK
234 | FATTR4_WORD0_MAXNAME,
235 0
236};
237
Fred Isamandae100c2011-07-30 20:52:37 -0400238const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 | FATTR4_WORD0_MAXREAD
240 | FATTR4_WORD0_MAXWRITE
241 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700242 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400243 | FATTR4_WORD1_FS_LAYOUT_TYPES,
244 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245};
246
David Quigleya09df2c2013-05-22 12:50:41 -0400247const u32 nfs4_fs_locations_bitmap[3] = {
Manoj Naik830b8e32006-06-09 09:34:25 -0400248 FATTR4_WORD0_TYPE
249 | FATTR4_WORD0_CHANGE
250 | FATTR4_WORD0_SIZE
251 | FATTR4_WORD0_FSID
252 | FATTR4_WORD0_FILEID
253 | FATTR4_WORD0_FS_LOCATIONS,
254 FATTR4_WORD1_MODE
255 | FATTR4_WORD1_NUMLINKS
256 | FATTR4_WORD1_OWNER
257 | FATTR4_WORD1_OWNER_GROUP
258 | FATTR4_WORD1_RAWDEV
259 | FATTR4_WORD1_SPACE_USED
260 | FATTR4_WORD1_TIME_ACCESS
261 | FATTR4_WORD1_TIME_METADATA
262 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400263 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400264};
265
Al Virobc4785c2006-10-19 23:28:51 -0700266static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 struct nfs4_readdir_arg *readdir)
268{
Al Viro0dbb4c62006-10-19 23:28:49 -0700269 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000272 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
274 return;
275 }
276
277 readdir->cookie = 0;
278 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
279 if (cookie == 2)
280 return;
281
282 /*
283 * NFSv4 servers do not return entries for '.' and '..'
284 * Therefore, we fake these entries here. We let '.'
285 * have cookie 0 and '..' have cookie 1. Note that
286 * when talking to the server, we always send cookie 0
287 * instead of 1 or 2.
288 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800289 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 if (cookie == 0) {
292 *p++ = xdr_one; /* next */
293 *p++ = xdr_zero; /* cookie, first word */
294 *p++ = xdr_one; /* cookie, second word */
295 *p++ = xdr_one; /* entry len */
296 memcpy(p, ".\0\0\0", 4); /* entry */
297 p++;
298 *p++ = xdr_one; /* bitmap length */
299 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
300 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400301 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
303
304 *p++ = xdr_one; /* next */
305 *p++ = xdr_zero; /* cookie, first word */
306 *p++ = xdr_two; /* cookie, second word */
307 *p++ = xdr_two; /* entry len */
308 memcpy(p, "..\0\0", 4); /* entry */
309 p++;
310 *p++ = xdr_one; /* bitmap length */
311 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
312 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400313 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 readdir->pgbase = (char *)p - (char *)start;
316 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800317 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
319
Trond Myklebust65de8722008-12-23 15:21:44 -0500320static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
321{
322 int res = 0;
323
324 might_sleep();
325
326 if (*timeout <= 0)
327 *timeout = NFS4_POLL_RETRY_MIN;
328 if (*timeout > NFS4_POLL_RETRY_MAX)
329 *timeout = NFS4_POLL_RETRY_MAX;
Colin Cross416ad3c2013-05-06 23:50:06 +0000330 freezable_schedule_timeout_killable_unsafe(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500331 if (fatal_signal_pending(current))
332 res = -ERESTARTSYS;
333 *timeout <<= 1;
334 return res;
335}
336
337/* This is the error handling routine for processes that are allowed
338 * to sleep.
339 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800340static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500341{
342 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500343 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500344 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500345 int ret = errorcode;
346
347 exception->retry = 0;
348 switch(errorcode) {
349 case 0:
350 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500351 case -NFS4ERR_OPENMODE:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400352 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400353 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500354 exception->retry = 1;
355 return 0;
356 }
357 if (state == NULL)
358 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400359 ret = nfs4_schedule_stateid_recovery(server, state);
360 if (ret < 0)
361 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500362 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500363 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500364 case -NFS4ERR_ADMIN_REVOKED:
365 case -NFS4ERR_BAD_STATEID:
Trond Myklebustb570a972013-04-12 15:04:51 -0400366 if (inode != NULL && nfs4_have_delegation(inode, FMODE_READ)) {
367 nfs_remove_bad_delegation(inode);
368 exception->retry = 1;
369 break;
370 }
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500371 if (state == NULL)
372 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400373 ret = nfs4_schedule_stateid_recovery(server, state);
374 if (ret < 0)
375 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500376 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400377 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -0400378 if (state != NULL) {
379 ret = nfs4_schedule_stateid_recovery(server, state);
380 if (ret < 0)
381 break;
382 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500383 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500384 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500385 nfs4_schedule_lease_recovery(clp);
386 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500387#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400388 case -NFS4ERR_BADSESSION:
389 case -NFS4ERR_BADSLOT:
390 case -NFS4ERR_BAD_HIGH_SLOT:
391 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
392 case -NFS4ERR_DEADSESSION:
393 case -NFS4ERR_SEQ_FALSE_RETRY:
394 case -NFS4ERR_SEQ_MISORDERED:
395 dprintk("%s ERROR: %d Reset session\n", __func__,
396 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400397 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400398 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500399#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500400 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500401 if (exception->timeout > HZ) {
402 /* We have retried a decent amount, time to
403 * fail
404 */
405 ret = -EBUSY;
406 break;
407 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500408 case -NFS4ERR_GRACE:
409 case -NFS4ERR_DELAY:
410 ret = nfs4_delay(server->client, &exception->timeout);
411 if (ret != 0)
412 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400413 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500414 case -NFS4ERR_OLD_STATEID:
415 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800416 break;
417 case -NFS4ERR_BADOWNER:
418 /* The following works around a Linux server bug! */
419 case -NFS4ERR_BADNAME:
420 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
421 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
422 exception->retry = 1;
423 printk(KERN_WARNING "NFS: v4 server %s "
424 "does not accept raw "
425 "uid/gids. "
426 "Reenabling the idmapper.\n",
427 server->nfs_client->cl_hostname);
428 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500429 }
430 /* We failed to handle the error */
431 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500432wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500433 ret = nfs4_wait_clnt_recover(clp);
434 if (ret == 0)
435 exception->retry = 1;
436 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500437}
438
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400439/*
440 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
441 * or 'false' otherwise.
442 */
443static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
444{
445 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
446
447 if (flavor == RPC_AUTH_GSS_KRB5I ||
448 flavor == RPC_AUTH_GSS_KRB5P)
449 return true;
450
451 return false;
452}
Trond Myklebust65de8722008-12-23 15:21:44 -0500453
Trond Myklebust452e9352010-07-31 14:29:06 -0400454static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 spin_lock(&clp->cl_lock);
457 if (time_before(clp->cl_last_renewal,timestamp))
458 clp->cl_last_renewal = timestamp;
459 spin_unlock(&clp->cl_lock);
460}
461
Trond Myklebust452e9352010-07-31 14:29:06 -0400462static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
463{
464 do_renew_lease(server->nfs_client, timestamp);
465}
466
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400467struct nfs4_call_sync_data {
468 const struct nfs_server *seq_server;
469 struct nfs4_sequence_args *seq_args;
470 struct nfs4_sequence_res *seq_res;
471};
472
Chuck Levera9c92d62013-08-09 12:48:18 -0400473static void nfs4_init_sequence(struct nfs4_sequence_args *args,
474 struct nfs4_sequence_res *res, int cache_reply)
475{
476 args->sa_slot = NULL;
477 args->sa_cache_this = cache_reply;
478 args->sa_privileged = 0;
479
480 res->sr_slot = NULL;
481}
482
483static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
484{
485 args->sa_privileged = 1;
486}
487
Chuck Lever3bd23842013-08-09 12:49:19 -0400488static int nfs40_setup_sequence(const struct nfs_server *server,
489 struct nfs4_sequence_args *args,
490 struct nfs4_sequence_res *res,
491 struct rpc_task *task)
492{
493 struct nfs4_slot_table *tbl = server->nfs_client->cl_slot_tbl;
494 struct nfs4_slot *slot;
495
496 /* slot already allocated? */
497 if (res->sr_slot != NULL)
498 goto out_start;
499
500 spin_lock(&tbl->slot_tbl_lock);
501 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
502 goto out_sleep;
503
504 slot = nfs4_alloc_slot(tbl);
505 if (IS_ERR(slot)) {
506 if (slot == ERR_PTR(-ENOMEM))
507 task->tk_timeout = HZ >> 2;
508 goto out_sleep;
509 }
510 spin_unlock(&tbl->slot_tbl_lock);
511
512 args->sa_slot = slot;
513 res->sr_slot = slot;
514
515out_start:
516 rpc_call_start(task);
517 return 0;
518
519out_sleep:
520 if (args->sa_privileged)
521 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
522 NULL, RPC_PRIORITY_PRIVILEGED);
523 else
524 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
525 spin_unlock(&tbl->slot_tbl_lock);
526 return -EAGAIN;
527}
528
529static int nfs40_sequence_done(struct rpc_task *task,
530 struct nfs4_sequence_res *res)
531{
532 struct nfs4_slot *slot = res->sr_slot;
533 struct nfs4_slot_table *tbl;
534
535 if (!RPC_WAS_SENT(task))
536 goto out;
537
538 tbl = slot->table;
539 spin_lock(&tbl->slot_tbl_lock);
540 if (!nfs41_wake_and_assign_slot(tbl, slot))
541 nfs4_free_slot(tbl, slot);
542 spin_unlock(&tbl->slot_tbl_lock);
543
544 res->sr_slot = NULL;
545out:
546 return 1;
547}
548
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400549#if defined(CONFIG_NFS_V4_1)
550
Trond Myklebustd185a332010-06-16 09:52:25 -0400551static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400552{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500553 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400554 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500555 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400556
Benny Halevydfb4f3092010-09-24 09:17:01 -0400557 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400558 /* just wake up the next guy waiting since
559 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500560 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500561 return;
Andy Adamson13615872009-04-01 09:22:17 -0400562 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500563 tbl = res->sr_slot->table;
564 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500565
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500566 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500567 /* Be nice to the server: try to ensure that the last transmitted
568 * value for highest_user_slotid <= target_highest_slotid
569 */
570 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
571 send_new_highest_used_slotid = true;
572
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500573 if (nfs41_wake_and_assign_slot(tbl, res->sr_slot)) {
574 send_new_highest_used_slotid = false;
575 goto out_unlock;
576 }
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500577 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500578
579 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
580 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500581out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500582 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400583 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500584 if (send_new_highest_used_slotid)
585 nfs41_server_notify_highest_slotid_update(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400586}
587
Trond Myklebust14516c32010-07-31 14:29:06 -0400588static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400589{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500590 struct nfs4_session *session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500591 struct nfs4_slot *slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400592 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500593 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500594 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400595
Bryan Schumaker468f8612011-04-18 15:57:32 -0400596 /* don't increment the sequence number if the task wasn't sent */
597 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400598 goto out;
599
Trond Myklebust933602e2012-11-16 12:12:38 -0500600 slot = res->sr_slot;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500601 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500602
Trond Myklebustac20d162012-12-15 15:36:07 -0500603 if (slot->interrupted) {
604 slot->interrupted = 0;
605 interrupted = true;
606 }
607
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400608 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500609 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400610 switch (res->sr_status) {
611 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400612 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500613 ++slot->seq_nr;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500614 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500615 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500616 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500617 if (res->sr_status_flags != 0)
618 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500619 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400620 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500621 case 1:
622 /*
623 * sr_status remains 1 if an RPC level error occurred.
624 * The server may or may not have processed the sequence
625 * operation..
626 * Mark the slot as having hosted an interrupted RPC call.
627 */
628 slot->interrupted = 1;
629 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400630 case -NFS4ERR_DELAY:
631 /* The server detected a resend of the RPC call and
632 * returned NFS4ERR_DELAY as per Section 2.10.6.2
633 * of RFC5661.
634 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500635 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400636 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500637 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500638 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400639 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500640 case -NFS4ERR_BADSLOT:
641 /*
642 * The slot id we used was probably retired. Try again
643 * using a different slot id.
644 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500645 goto retry_nowait;
646 case -NFS4ERR_SEQ_MISORDERED:
647 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500648 * Was the last operation on this sequence interrupted?
649 * If so, retry after bumping the sequence number.
650 */
651 if (interrupted) {
652 ++slot->seq_nr;
653 goto retry_nowait;
654 }
655 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500656 * Could this slot have been previously retired?
657 * If so, then the server may be expecting seq_nr = 1!
658 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500659 if (slot->seq_nr != 1) {
660 slot->seq_nr = 1;
661 goto retry_nowait;
662 }
663 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500664 case -NFS4ERR_SEQ_FALSE_RETRY:
665 ++slot->seq_nr;
666 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400667 default:
668 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500669 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400670 }
671out:
672 /* The session may be reset by one of the error handlers. */
673 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400674 nfs41_sequence_free_slot(res);
Trond Myklebust85563072012-12-11 10:31:12 -0500675 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500676retry_nowait:
677 if (rpc_restart_call_prepare(task)) {
678 task->tk_status = 0;
679 ret = 0;
680 }
681 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400682out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400683 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400684 goto out;
685 rpc_delay(task, NFS4_POLL_RETRY_MAX);
686 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400687}
688
Trond Myklebust14516c32010-07-31 14:29:06 -0400689static int nfs4_sequence_done(struct rpc_task *task,
690 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400691{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500692 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400693 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400694 if (!res->sr_slot->table->session)
695 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400696 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400697}
698
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000699int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400700 struct nfs4_sequence_args *args,
701 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400702 struct rpc_task *task)
703{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400704 struct nfs4_slot *slot;
705 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400706
707 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400708 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400709 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400710 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400711
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400712 tbl = &session->fc_slot_table;
713
Trond Myklebust69d206b2012-11-22 13:21:02 -0500714 task->tk_timeout = 0;
715
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400716 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400717 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400718 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500719 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500720 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400721 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400722 }
723
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500724 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500725 if (IS_ERR(slot)) {
726 /* If out of memory, try again in 1/4 second */
727 if (slot == ERR_PTR(-ENOMEM))
728 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400729 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400730 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400731 }
732 spin_unlock(&tbl->slot_tbl_lock);
733
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500734 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400735
Chuck Levere8d92382013-08-09 12:47:51 -0400736 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500737 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400738
Benny Halevydfb4f3092010-09-24 09:17:01 -0400739 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500740 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400741 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400742 /*
743 * sr_status is only set in decode_sequence, and so will remain
744 * set to 1 if an rpc level failure occurs.
745 */
746 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400747 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400748out_success:
749 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400750 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400751out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400752 /* Privileged tasks are queued with top priority */
753 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400754 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
755 NULL, RPC_PRIORITY_PRIVILEGED);
756 else
757 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400758 spin_unlock(&tbl->slot_tbl_lock);
759 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400760}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000761EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400762
Chuck Lever5a580e02013-08-09 12:48:09 -0400763static int nfs4_setup_sequence(const struct nfs_server *server,
764 struct nfs4_sequence_args *args,
765 struct nfs4_sequence_res *res,
766 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400767{
Trond Myklebust035168a2010-06-16 09:52:26 -0400768 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400769 int ret = 0;
770
Chuck Lever3bd23842013-08-09 12:49:19 -0400771 if (!session)
772 return nfs40_setup_sequence(server, args, res, task);
Trond Myklebust035168a2010-06-16 09:52:26 -0400773
Chuck Levere8d92382013-08-09 12:47:51 -0400774 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400775 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400776 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168a2010-06-16 09:52:26 -0400777
Trond Myklebust9d12b212012-01-17 22:04:25 -0500778 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400779
Andy Adamsonce5039c2009-04-01 09:22:13 -0400780 dprintk("<-- %s status=%d\n", __func__, ret);
781 return ret;
782}
783
Andy Adamsonce5039c2009-04-01 09:22:13 -0400784static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
785{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400786 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400787 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400788
Trond Myklebust035168a2010-06-16 09:52:26 -0400789 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
790
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400791 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400792}
793
Andy Adamson69ab40c2009-04-01 09:22:19 -0400794static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
795{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400796 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400797
Trond Myklebust14516c32010-07-31 14:29:06 -0400798 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400799}
800
Trond Myklebust17280172012-03-11 13:11:00 -0400801static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400802 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400803 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400804};
805
Chuck Lever3bd23842013-08-09 12:49:19 -0400806#else /* !CONFIG_NFS_V4_1 */
807
Chuck Lever5a580e02013-08-09 12:48:09 -0400808static int nfs4_setup_sequence(const struct nfs_server *server,
809 struct nfs4_sequence_args *args,
810 struct nfs4_sequence_res *res,
811 struct rpc_task *task)
812{
Chuck Lever3bd23842013-08-09 12:49:19 -0400813 return nfs40_setup_sequence(server, args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -0400814}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400815
Trond Myklebust14516c32010-07-31 14:29:06 -0400816static int nfs4_sequence_done(struct rpc_task *task,
817 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400818{
Chuck Lever3bd23842013-08-09 12:49:19 -0400819 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400820}
Chuck Lever3bd23842013-08-09 12:49:19 -0400821
822#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400823
Chuck Lever9915ea72013-08-09 12:48:27 -0400824static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
825{
826 struct nfs4_call_sync_data *data = calldata;
827 nfs4_setup_sequence(data->seq_server,
828 data->seq_args, data->seq_res, task);
829}
830
831static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
832{
833 struct nfs4_call_sync_data *data = calldata;
834 nfs4_sequence_done(task, data->seq_res);
835}
836
837static const struct rpc_call_ops nfs40_call_sync_ops = {
838 .rpc_call_prepare = nfs40_call_sync_prepare,
839 .rpc_call_done = nfs40_call_sync_done,
840};
841
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400842static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -0400843 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500844 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100845 struct nfs4_sequence_args *args,
846 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -0400847{
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100848 int ret;
849 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -0400850 struct nfs_client *clp = server->nfs_client;
851 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100852 .seq_server = server,
853 .seq_args = args,
854 .seq_res = res,
855 };
856 struct rpc_task_setup task_setup = {
857 .rpc_client = clnt,
858 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -0400859 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100860 .callback_data = &data
861 };
862
863 task = rpc_run_task(&task_setup);
864 if (IS_ERR(task))
865 ret = PTR_ERR(task);
866 else {
867 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -0400868 rpc_put_task(task);
869 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100870 return ret;
871}
872
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400873static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000874int nfs4_call_sync(struct rpc_clnt *clnt,
875 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000876 struct rpc_message *msg,
877 struct nfs4_sequence_args *args,
878 struct nfs4_sequence_res *res,
879 int cache_reply)
880{
Chuck Levera9c92d62013-08-09 12:48:18 -0400881 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -0400882 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000883}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
886{
887 struct nfs_inode *nfsi = NFS_I(dir);
888
889 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400890 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700891 if (!cinfo->atomic || cinfo->before != dir->i_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700893 dir->i_version = cinfo->after;
David Howellsde242c02012-12-20 21:52:38 +0000894 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 spin_unlock(&dir->i_lock);
896}
897
898struct nfs4_opendata {
899 struct kref kref;
900 struct nfs_openargs o_arg;
901 struct nfs_openres o_res;
902 struct nfs_open_confirmargs c_arg;
903 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500904 struct nfs4_string owner_name;
905 struct nfs4_string group_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400907 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400909 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 struct nfs4_state_owner *owner;
911 struct nfs4_state *state;
912 struct iattr attrs;
913 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400914 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -0400915 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -0400916 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400917 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 int cancelled;
919};
Trond Myklebustdecf4912005-10-27 22:12:39 -0400920
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400921static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
922 int err, struct nfs4_exception *exception)
923{
924 if (err != -EINVAL)
925 return false;
926 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
927 return false;
928 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
929 exception->retry = 1;
930 return true;
931}
932
933static enum open_claim_type4
934nfs4_map_atomic_open_claim(struct nfs_server *server,
935 enum open_claim_type4 claim)
936{
937 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
938 return claim;
939 switch (claim) {
940 default:
941 return claim;
942 case NFS4_OPEN_CLAIM_FH:
943 return NFS4_OPEN_CLAIM_NULL;
944 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
945 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
946 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
947 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
948 }
949}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100952{
953 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400954 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100955 p->o_res.seqid = p->o_arg.seqid;
956 p->c_res.seqid = p->c_arg.seqid;
957 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400958 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100959 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500960 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100961}
962
Al Viro82a2c1b2011-06-22 18:30:55 -0400963static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100964 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400965 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -0400966 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400967 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400968 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100969{
Al Viro82a2c1b2011-06-22 18:30:55 -0400970 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100971 struct inode *dir = parent->d_inode;
972 struct nfs_server *server = NFS_SERVER(dir);
973 struct nfs4_opendata *p;
974
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400975 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100976 if (p == NULL)
977 goto err;
David Quigley14c43f72013-05-22 12:50:43 -0400978
979 p->f_label = nfs4_label_alloc(server, gfp_mask);
980 if (IS_ERR(p->f_label))
981 goto err_free_p;
982
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400983 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100984 if (p->o_arg.seqid == NULL)
David Quigley14c43f72013-05-22 12:50:43 -0400985 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -0400986 nfs_sb_active(dentry->d_sb);
987 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100988 p->dir = parent;
989 p->owner = sp;
990 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500991 p->o_arg.open_flags = flags;
992 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700993 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
994 * will return permission denied for all bits until close */
995 if (!(flags & O_EXCL)) {
996 /* ask server to check for all possible rights as results
997 * are cached */
998 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
999 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001000 }
David Howells7539bba2006-08-22 20:06:09 -04001001 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001002 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1003 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001004 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001005 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001006 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001007 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
David Quigley1775fd32013-05-22 12:50:42 -04001008 p->o_arg.label = label;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001009 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1010 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001011 case NFS4_OPEN_CLAIM_NULL:
1012 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1013 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1014 p->o_arg.fh = NFS_FH(dir);
1015 break;
1016 case NFS4_OPEN_CLAIM_PREVIOUS:
1017 case NFS4_OPEN_CLAIM_FH:
1018 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1019 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1020 p->o_arg.fh = NFS_FH(dentry->d_inode);
1021 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001022 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001023 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001024
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001025 p->o_arg.u.attrs = &p->attrs;
1026 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001027
1028 verf[0] = jiffies;
1029 verf[1] = current->pid;
1030 memcpy(p->o_arg.u.verifier.data, verf,
1031 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001032 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001033 p->c_arg.fh = &p->o_res.fh;
1034 p->c_arg.stateid = &p->o_res.stateid;
1035 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001036 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001037 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001038 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001039
1040err_free_label:
1041 nfs4_label_free(p->f_label);
1042err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001043 kfree(p);
1044err:
1045 dput(parent);
1046 return NULL;
1047}
1048
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001049static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001050{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001051 struct nfs4_opendata *p = container_of(kref,
1052 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001053 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001054
1055 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001056 if (p->state != NULL)
1057 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001058 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001059
1060 nfs4_label_free(p->f_label);
1061
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001062 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001063 dput(p->dentry);
1064 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001065 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001066 kfree(p);
1067}
1068
1069static void nfs4_opendata_put(struct nfs4_opendata *p)
1070{
1071 if (p != NULL)
1072 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001073}
1074
Trond Myklebust06f814a2006-01-03 09:55:07 +01001075static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1076{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001077 int ret;
1078
Trond Myklebust06f814a2006-01-03 09:55:07 +01001079 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001080 return ret;
1081}
1082
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001083static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001084{
1085 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001086
Trond Myklebust536e43d2012-01-17 22:04:26 -05001087 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001088 goto out;
1089 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001090 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001091 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1092 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001093 break;
1094 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001095 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1096 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001097 break;
1098 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001099 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1100 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001101 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001102out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001103 return ret;
1104}
1105
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001106static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001107{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001108 if (delegation == NULL)
1109 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001110 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001111 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -05001112 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -04001113 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001114 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1115 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -05001116 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001117 return 1;
1118}
1119
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001120static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001121{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001122 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001123 case FMODE_WRITE:
1124 state->n_wronly++;
1125 break;
1126 case FMODE_READ:
1127 state->n_rdonly++;
1128 break;
1129 case FMODE_READ|FMODE_WRITE:
1130 state->n_rdwr++;
1131 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001132 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001133}
1134
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001135static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001136{
1137 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001138 nfs4_stateid_copy(&state->stateid, stateid);
1139 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust92b40e92013-04-20 01:25:45 -04001140 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001141 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001142 case FMODE_READ:
1143 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1144 break;
1145 case FMODE_WRITE:
1146 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1147 break;
1148 case FMODE_READ|FMODE_WRITE:
1149 set_bit(NFS_O_RDWR_STATE, &state->flags);
1150 }
1151}
1152
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001153static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001154{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001155 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001156 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001157 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001158}
1159
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001160static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001162 /*
1163 * Protect the call to nfs4_state_set_mode_locked and
1164 * serialise the stateid update
1165 */
1166 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001167 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001168 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001169 set_bit(NFS_DELEGATED_STATE, &state->flags);
1170 }
1171 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001172 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001173 write_sequnlock(&state->seqlock);
1174 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001175 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001176 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177}
1178
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001179static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001180{
1181 struct nfs_inode *nfsi = NFS_I(state->inode);
1182 struct nfs_delegation *deleg_cur;
1183 int ret = 0;
1184
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001185 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001186
1187 rcu_read_lock();
1188 deleg_cur = rcu_dereference(nfsi->delegation);
1189 if (deleg_cur == NULL)
1190 goto no_delegation;
1191
1192 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001193 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001194 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001195 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001196 goto no_delegation_unlock;
1197
1198 if (delegation == NULL)
1199 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001200 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001201 goto no_delegation_unlock;
1202
Trond Myklebustb7391f42008-12-23 15:21:52 -05001203 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001204 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001205 ret = 1;
1206no_delegation_unlock:
1207 spin_unlock(&deleg_cur->lock);
1208no_delegation:
1209 rcu_read_unlock();
1210
1211 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001212 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001213 ret = 1;
1214 }
1215
1216 return ret;
1217}
1218
1219
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001220static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001221{
1222 struct nfs_delegation *delegation;
1223
1224 rcu_read_lock();
1225 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001226 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001227 rcu_read_unlock();
1228 return;
1229 }
1230 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001231 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001232}
1233
Trond Myklebust6ee41262007-07-08 14:11:36 -04001234static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001235{
1236 struct nfs4_state *state = opendata->state;
1237 struct nfs_inode *nfsi = NFS_I(state->inode);
1238 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001239 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001240 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001241 nfs4_stateid stateid;
1242 int ret = -EAGAIN;
1243
Trond Myklebustaac00a82007-07-05 19:02:21 -04001244 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001245 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001246 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001247 if (can_open_cached(state, fmode, open_mode)) {
1248 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001249 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001250 goto out_return_state;
1251 }
1252 spin_unlock(&state->owner->so_lock);
1253 }
Trond Myklebust34310432008-12-23 15:21:38 -05001254 rcu_read_lock();
1255 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001256 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001257 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001258 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001259 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001260 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001261 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001262 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001263 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001264 if (!opendata->is_recover) {
1265 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1266 if (ret != 0)
1267 goto out;
1268 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001269 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001270
1271 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001272 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001273 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001274 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001275out:
1276 return ERR_PTR(ret);
1277out_return_state:
1278 atomic_inc(&state->count);
1279 return state;
1280}
1281
Andy Adamsone23008e2012-10-02 21:07:32 -04001282static void
1283nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1284{
1285 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1286 struct nfs_delegation *delegation;
1287 int delegation_flags = 0;
1288
1289 rcu_read_lock();
1290 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1291 if (delegation)
1292 delegation_flags = delegation->flags;
1293 rcu_read_unlock();
1294 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1295 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1296 "returning a delegation for "
1297 "OPEN(CLAIM_DELEGATE_CUR)\n",
1298 clp->cl_hostname);
1299 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1300 nfs_inode_set_delegation(state->inode,
1301 data->owner->so_cred,
1302 &data->o_res);
1303 else
1304 nfs_inode_reclaim_delegation(state->inode,
1305 data->owner->so_cred,
1306 &data->o_res);
1307}
1308
1309/*
1310 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1311 * and update the nfs4_state.
1312 */
1313static struct nfs4_state *
1314_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1315{
1316 struct inode *inode = data->state->inode;
1317 struct nfs4_state *state = data->state;
1318 int ret;
1319
Weston Andros Adamsonf494a602013-10-21 13:10:10 -04001320 /* allow cached opens (!rpc_done && !rpc_status) */
1321 if (!data->rpc_done && data->rpc_status) {
Andy Adamsone23008e2012-10-02 21:07:32 -04001322 ret = data->rpc_status;
1323 goto err;
1324 }
1325
Andy Adamsone23008e2012-10-02 21:07:32 -04001326 ret = nfs_refresh_inode(inode, &data->f_attr);
1327 if (ret)
1328 goto err;
1329
1330 if (data->o_res.delegation_type != 0)
1331 nfs4_opendata_check_deleg(data, state);
1332 update_open_stateid(state, &data->o_res.stateid, NULL,
1333 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001334 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001335
1336 return state;
1337err:
1338 return ERR_PTR(ret);
1339
1340}
1341
1342static struct nfs4_state *
1343_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001344{
1345 struct inode *inode;
1346 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001347 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001348
Trond Myklebustaac00a82007-07-05 19:02:21 -04001349 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001350 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001351 goto out;
1352 }
1353
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001354 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001355 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001356 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001357 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001358 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001359 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001360 goto err;
1361 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001362 state = nfs4_get_open_state(inode, data->owner);
1363 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001364 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001365 if (data->o_res.delegation_type != 0)
1366 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001367 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001368 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001369 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001370out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001371 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001372 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001373err_put_inode:
1374 iput(inode);
1375err:
1376 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001377}
1378
Andy Adamsone23008e2012-10-02 21:07:32 -04001379static struct nfs4_state *
1380nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1381{
1382 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1383 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1384 return _nfs4_opendata_to_nfs4_state(data);
1385}
1386
Trond Myklebust864472e2006-01-03 09:55:15 +01001387static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1388{
1389 struct nfs_inode *nfsi = NFS_I(state->inode);
1390 struct nfs_open_context *ctx;
1391
1392 spin_lock(&state->inode->i_lock);
1393 list_for_each_entry(ctx, &nfsi->open_files, list) {
1394 if (ctx->state != state)
1395 continue;
1396 get_nfs_open_context(ctx);
1397 spin_unlock(&state->inode->i_lock);
1398 return ctx;
1399 }
1400 spin_unlock(&state->inode->i_lock);
1401 return ERR_PTR(-ENOENT);
1402}
1403
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001404static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1405 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001406{
1407 struct nfs4_opendata *opendata;
1408
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001409 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001410 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001411 if (opendata == NULL)
1412 return ERR_PTR(-ENOMEM);
1413 opendata->state = state;
1414 atomic_inc(&state->count);
1415 return opendata;
1416}
1417
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001418static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001419{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001420 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001421 int ret;
1422
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001423 opendata->o_arg.open_flags = 0;
1424 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001425 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1426 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1427 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001428 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001429 if (ret != 0)
1430 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001431 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001432 if (IS_ERR(newstate))
1433 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001434 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001435 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001436 return 0;
1437}
1438
1439static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1440{
Trond Myklebust864472e2006-01-03 09:55:15 +01001441 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001442 int ret;
1443
1444 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001445 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001446 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001447 smp_rmb();
1448 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001449 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001450 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001451 if (ret != 0)
1452 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001453 if (newstate != state)
1454 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001455 }
1456 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001457 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001458 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001459 if (ret != 0)
1460 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001461 if (newstate != state)
1462 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001463 }
1464 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001465 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001466 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001467 if (ret != 0)
1468 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001469 if (newstate != state)
1470 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001471 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001472 /*
1473 * We may have performed cached opens for all three recoveries.
1474 * Check if we need to update the current stateid.
1475 */
1476 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001477 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001478 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001479 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001480 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001481 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001482 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001483 return 0;
1484}
1485
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486/*
1487 * OPEN_RECLAIM:
1488 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001490static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001492 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001493 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001494 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 int status;
1496
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001497 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1498 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001499 if (IS_ERR(opendata))
1500 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001501 rcu_read_lock();
1502 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001503 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001504 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001505 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001506 opendata->o_arg.u.delegation_type = delegation_type;
1507 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001508 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 return status;
1510}
1511
Trond Myklebust539cd032007-06-05 11:46:42 -04001512static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513{
1514 struct nfs_server *server = NFS_SERVER(state->inode);
1515 struct nfs4_exception exception = { };
1516 int err;
1517 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001518 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001519 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001520 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1521 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001522 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001523 break;
1524 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 } while (exception.retry);
1526 return err;
1527}
1528
Trond Myklebust864472e2006-01-03 09:55:15 +01001529static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1530{
1531 struct nfs_open_context *ctx;
1532 int ret;
1533
1534 ctx = nfs4_state_find_open_context(state);
1535 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001536 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001537 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001538 put_nfs_open_context(ctx);
1539 return ret;
1540}
1541
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001542static 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 -07001543{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001544 switch (err) {
1545 default:
1546 printk(KERN_ERR "NFS: %s: unhandled error "
1547 "%d.\n", __func__, err);
1548 case 0:
1549 case -ENOENT:
1550 case -ESTALE:
1551 break;
1552 case -NFS4ERR_BADSESSION:
1553 case -NFS4ERR_BADSLOT:
1554 case -NFS4ERR_BAD_HIGH_SLOT:
1555 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1556 case -NFS4ERR_DEADSESSION:
1557 set_bit(NFS_DELEGATED_STATE, &state->flags);
1558 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1559 return -EAGAIN;
1560 case -NFS4ERR_STALE_CLIENTID:
1561 case -NFS4ERR_STALE_STATEID:
1562 set_bit(NFS_DELEGATED_STATE, &state->flags);
1563 case -NFS4ERR_EXPIRED:
1564 /* Don't recall a delegation if it was lost */
1565 nfs4_schedule_lease_recovery(server->nfs_client);
1566 return -EAGAIN;
1567 case -NFS4ERR_DELEG_REVOKED:
1568 case -NFS4ERR_ADMIN_REVOKED:
1569 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001570 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001571 nfs_inode_find_state_and_recover(state->inode,
1572 stateid);
1573 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001574 return 0;
1575 case -NFS4ERR_DELAY:
1576 case -NFS4ERR_GRACE:
1577 set_bit(NFS_DELEGATED_STATE, &state->flags);
1578 ssleep(1);
1579 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001580 case -ENOMEM:
1581 case -NFS4ERR_DENIED:
1582 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1583 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 return err;
1586}
1587
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001588int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1589{
1590 struct nfs_server *server = NFS_SERVER(state->inode);
1591 struct nfs4_opendata *opendata;
1592 int err;
1593
1594 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1595 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1596 if (IS_ERR(opendata))
1597 return PTR_ERR(opendata);
1598 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1599 err = nfs4_open_recover(opendata, state);
1600 nfs4_opendata_put(opendata);
1601 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1602}
1603
Chuck Leverbe05c862013-08-09 12:49:47 -04001604static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1605{
1606 struct nfs4_opendata *data = calldata;
1607
1608 nfs40_setup_sequence(data->o_arg.server, &data->o_arg.seq_args,
1609 &data->o_res.seq_res, task);
1610}
1611
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001612static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1613{
1614 struct nfs4_opendata *data = calldata;
1615
Chuck Leverbe05c862013-08-09 12:49:47 -04001616 nfs40_sequence_done(task, &data->o_res.seq_res);
1617
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001618 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001619 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001620 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001621 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001622 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001623 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001624 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001625}
1626
1627static void nfs4_open_confirm_release(void *calldata)
1628{
1629 struct nfs4_opendata *data = calldata;
1630 struct nfs4_state *state = NULL;
1631
1632 /* If this request hasn't been cancelled, do nothing */
1633 if (data->cancelled == 0)
1634 goto out_free;
1635 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001636 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001637 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001638 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001639 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001640 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001641out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001642 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001643}
1644
1645static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04001646 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001647 .rpc_call_done = nfs4_open_confirm_done,
1648 .rpc_release = nfs4_open_confirm_release,
1649};
1650
1651/*
1652 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1653 */
1654static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1655{
1656 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1657 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001658 struct rpc_message msg = {
1659 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1660 .rpc_argp = &data->c_arg,
1661 .rpc_resp = &data->c_res,
1662 .rpc_cred = data->owner->so_cred,
1663 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001664 struct rpc_task_setup task_setup_data = {
1665 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001666 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001667 .callback_ops = &nfs4_open_confirm_ops,
1668 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001669 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001670 .flags = RPC_TASK_ASYNC,
1671 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 int status;
1673
Chuck Leverbe05c862013-08-09 12:49:47 -04001674 nfs4_init_sequence(&data->o_arg.seq_args, &data->o_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001675 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001676 data->rpc_done = 0;
1677 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001678 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001679 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001680 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001681 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001682 status = nfs4_wait_for_completion_rpc_task(task);
1683 if (status != 0) {
1684 data->cancelled = 1;
1685 smp_wmb();
1686 } else
1687 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001688 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 return status;
1690}
1691
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001692static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001694 struct nfs4_opendata *data = calldata;
1695 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001696 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001697
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001698 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001699 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001700 /*
1701 * Check if we still need to send an OPEN call, or if we can use
1702 * a delegation instead.
1703 */
1704 if (data->state != NULL) {
1705 struct nfs_delegation *delegation;
1706
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001707 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001708 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001709 rcu_read_lock();
1710 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001711 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
Trond Myklebustcd4c9be2013-04-23 14:46:25 -04001712 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
Trond Myklebust652f89f2011-12-09 19:05:58 -05001713 can_open_delegated(delegation, data->o_arg.fmode))
1714 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001715 rcu_read_unlock();
1716 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001717 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04001718 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust8188df12013-04-23 14:31:19 -04001719 switch (data->o_arg.claim) {
1720 case NFS4_OPEN_CLAIM_PREVIOUS:
1721 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1722 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001723 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04001724 case NFS4_OPEN_CLAIM_FH:
1725 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001726 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1727 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001728 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001729 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001730 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001731 &data->o_res.seq_res,
1732 task) != 0)
1733 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04001734
1735 /* Set the create mode (note dependency on the session type) */
1736 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1737 if (data->o_arg.open_flags & O_EXCL) {
1738 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1739 if (nfs4_has_persistent_session(clp))
1740 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1741 else if (clp->cl_mvops->minor_version > 0)
1742 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1743 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04001744 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001745unlock_no_action:
1746 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001747out_no_action:
1748 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001749out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001750 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001751}
1752
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001753static void nfs4_open_done(struct rpc_task *task, void *calldata)
1754{
1755 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001757 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001758
Trond Myklebust14516c32010-07-31 14:29:06 -04001759 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1760 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001761
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001762 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001763 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1764 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001765 case S_IFREG:
1766 break;
1767 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001768 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001769 break;
1770 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001771 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001772 break;
1773 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001774 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001775 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001776 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001777 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001778 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1779 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001780 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001781 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001782}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001783
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001784static void nfs4_open_release(void *calldata)
1785{
1786 struct nfs4_opendata *data = calldata;
1787 struct nfs4_state *state = NULL;
1788
1789 /* If this request hasn't been cancelled, do nothing */
1790 if (data->cancelled == 0)
1791 goto out_free;
1792 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001793 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001794 goto out_free;
1795 /* In case we need an open_confirm, no cleanup! */
1796 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1797 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001798 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001799 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001800 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001801out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001802 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001803}
1804
1805static const struct rpc_call_ops nfs4_open_ops = {
1806 .rpc_call_prepare = nfs4_open_prepare,
1807 .rpc_call_done = nfs4_open_done,
1808 .rpc_release = nfs4_open_release,
1809};
1810
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001811static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001812{
1813 struct inode *dir = data->dir->d_inode;
1814 struct nfs_server *server = NFS_SERVER(dir);
1815 struct nfs_openargs *o_arg = &data->o_arg;
1816 struct nfs_openres *o_res = &data->o_res;
1817 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001818 struct rpc_message msg = {
1819 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1820 .rpc_argp = o_arg,
1821 .rpc_resp = o_res,
1822 .rpc_cred = data->owner->so_cred,
1823 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001824 struct rpc_task_setup task_setup_data = {
1825 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001826 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001827 .callback_ops = &nfs4_open_ops,
1828 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001829 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001830 .flags = RPC_TASK_ASYNC,
1831 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001832 int status;
1833
Chuck Levera9c92d62013-08-09 12:48:18 -04001834 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001835 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001836 data->rpc_done = 0;
1837 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001838 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001839 data->is_recover = 0;
1840 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001841 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001842 data->is_recover = 1;
1843 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04001844 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001845 if (IS_ERR(task))
1846 return PTR_ERR(task);
1847 status = nfs4_wait_for_completion_rpc_task(task);
1848 if (status != 0) {
1849 data->cancelled = 1;
1850 smp_wmb();
1851 } else
1852 status = data->rpc_status;
1853 rpc_put_task(task);
1854
1855 return status;
1856}
1857
1858static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1859{
1860 struct inode *dir = data->dir->d_inode;
1861 struct nfs_openres *o_res = &data->o_res;
1862 int status;
1863
1864 status = nfs4_run_open_task(data, 1);
1865 if (status != 0 || !data->rpc_done)
1866 return status;
1867
Trond Myklebust6926afd2012-01-07 13:22:46 -05001868 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1869
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001870 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1871 status = _nfs4_proc_open_confirm(data);
1872 if (status != 0)
1873 return status;
1874 }
1875
1876 return status;
1877}
1878
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001879static int nfs4_opendata_access(struct rpc_cred *cred,
1880 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001881 struct nfs4_state *state, fmode_t fmode,
1882 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001883{
1884 struct nfs_access_entry cache;
1885 u32 mask;
1886
1887 /* access call failed or for some reason the server doesn't
1888 * support any access modes -- defer access call until later */
1889 if (opendata->o_res.access_supported == 0)
1890 return 0;
1891
1892 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001893 /* don't check MAY_WRITE - a newly created file may not have
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001894 * write mode bits, but POSIX allows the creating process to write.
1895 * use openflags to check for exec, because fmode won't
1896 * always have FMODE_EXEC set when file open for exec. */
1897 if (openflags & __FMODE_EXEC) {
1898 /* ONLY check for exec rights */
1899 mask = MAY_EXEC;
1900 } else if (fmode & FMODE_READ)
1901 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001902
1903 cache.cred = cred;
1904 cache.jiffies = jiffies;
1905 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1906 nfs_access_add_cache(state->inode, &cache);
1907
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001908 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001909 return 0;
1910
1911 /* even though OPEN succeeded, access is denied. Close the file */
1912 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001913 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001914}
1915
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001916/*
1917 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1918 */
1919static int _nfs4_proc_open(struct nfs4_opendata *data)
1920{
1921 struct inode *dir = data->dir->d_inode;
1922 struct nfs_server *server = NFS_SERVER(dir);
1923 struct nfs_openargs *o_arg = &data->o_arg;
1924 struct nfs_openres *o_res = &data->o_res;
1925 int status;
1926
1927 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001928 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001929 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001930 if (status != 0) {
1931 if (status == -NFS4ERR_BADNAME &&
1932 !(o_arg->open_flags & O_CREAT))
1933 return -ENOENT;
1934 return status;
1935 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001936
Trond Myklebust6926afd2012-01-07 13:22:46 -05001937 nfs_fattr_map_and_free_names(server, &data->f_attr);
1938
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001939 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001940 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001941 if (o_arg->open_flags & O_EXCL)
1942 data->file_created = 1;
1943 else if (o_res->cinfo.before != o_res->cinfo.after)
1944 data->file_created = 1;
1945 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001946 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1947 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001949 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001951 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 }
1953 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
David Quigley1775fd32013-05-22 12:50:42 -04001954 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001955 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956}
1957
Andy Adamsond83217c2011-03-01 01:34:17 +00001958static int nfs4_recover_expired_lease(struct nfs_server *server)
1959{
1960 return nfs4_client_recover_expired_lease(server->nfs_client);
1961}
1962
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963/*
1964 * OPEN_EXPIRED:
1965 * reclaim state on the server after a network partition.
1966 * Assumes caller holds the appropriate lock
1967 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001968static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001970 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001971 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001973 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001974 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001975 if (IS_ERR(opendata))
1976 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001977 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001978 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001979 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001980 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001981 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982}
1983
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001984static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001985{
Trond Myklebust539cd032007-06-05 11:46:42 -04001986 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001987 struct nfs4_exception exception = { };
1988 int err;
1989
1990 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001991 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001992 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001993 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1994 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001995 switch (err) {
1996 default:
1997 goto out;
1998 case -NFS4ERR_GRACE:
1999 case -NFS4ERR_DELAY:
2000 nfs4_handle_exception(server, err, &exception);
2001 err = 0;
2002 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002003 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002004out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002005 return err;
2006}
2007
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2009{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002011 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
Trond Myklebust864472e2006-01-03 09:55:15 +01002013 ctx = nfs4_state_find_open_context(state);
2014 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002015 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002016 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002017 put_nfs_open_context(ctx);
2018 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019}
2020
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002021#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002022static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002023{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002024 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002025 nfs4_stateid *stateid = &state->stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002026 struct nfs_delegation *delegation;
2027 struct rpc_cred *cred = NULL;
2028 int status = -NFS4ERR_BAD_STATEID;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002029
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002030 /* If a state reset has been done, test_stateid is unneeded */
2031 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2032 return;
2033
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002034 /* Get the delegation credential for use by test/free_stateid */
2035 rcu_read_lock();
2036 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2037 if (delegation != NULL &&
2038 nfs4_stateid_match(&delegation->stateid, stateid)) {
2039 cred = get_rpccred(delegation->cred);
2040 rcu_read_unlock();
2041 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002042 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002043 } else
2044 rcu_read_unlock();
2045
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002046 if (status != NFS_OK) {
2047 /* Free the stateid unless the server explicitly
2048 * informs us the stateid is unrecognized. */
2049 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002050 nfs41_free_stateid(server, stateid, cred);
Bryan Schumaker69388672012-09-26 15:25:52 -04002051 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002052
Bryan Schumaker69388672012-09-26 15:25:52 -04002053 write_seqlock(&state->seqlock);
2054 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2055 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002056 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2057 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002058
2059 if (cred != NULL)
2060 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002061}
2062
2063/**
2064 * nfs41_check_open_stateid - possibly free an open stateid
2065 *
2066 * @state: NFSv4 state for an inode
2067 *
2068 * Returns NFS_OK if recovery for this stateid is now finished.
2069 * Otherwise a negative NFS4ERR value is returned.
2070 */
2071static int nfs41_check_open_stateid(struct nfs4_state *state)
2072{
2073 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002074 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002075 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002076 int status;
2077
2078 /* If a state reset has been done, test_stateid is unneeded */
2079 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2080 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2081 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2082 return -NFS4ERR_BAD_STATEID;
2083
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002084 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002085 trace_nfs4_test_open_stateid(state, NULL, status);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002086 if (status != NFS_OK) {
2087 /* Free the stateid unless the server explicitly
2088 * informs us the stateid is unrecognized. */
2089 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002090 nfs41_free_stateid(server, stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002091
2092 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2093 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2094 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002095 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002096 }
2097 return status;
2098}
2099
2100static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2101{
Chuck Levereb64cf92012-07-11 16:30:05 -04002102 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002103
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002104 nfs41_clear_delegation_stateid(state);
2105 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002106 if (status != NFS_OK)
2107 status = nfs4_open_expired(sp, state);
2108 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002109}
2110#endif
2111
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002113 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2114 * fields corresponding to attributes that were used to store the verifier.
2115 * Make sure we clobber those fields in the later setattr call
2116 */
2117static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2118{
2119 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2120 !(sattr->ia_valid & ATTR_ATIME_SET))
2121 sattr->ia_valid |= ATTR_ATIME;
2122
2123 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2124 !(sattr->ia_valid & ATTR_MTIME_SET))
2125 sattr->ia_valid |= ATTR_MTIME;
2126}
2127
Trond Myklebustc21443c2013-02-07 14:26:21 -05002128static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2129 fmode_t fmode,
2130 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002131 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002132{
2133 struct nfs4_state_owner *sp = opendata->owner;
2134 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002135 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002136 struct nfs4_state *state;
2137 unsigned int seq;
2138 int ret;
2139
2140 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2141
2142 ret = _nfs4_proc_open(opendata);
2143 if (ret != 0)
2144 goto out;
2145
2146 state = nfs4_opendata_to_nfs4_state(opendata);
2147 ret = PTR_ERR(state);
2148 if (IS_ERR(state))
2149 goto out;
2150 if (server->caps & NFS_CAP_POSIX_LOCK)
2151 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2152
Trond Myklebust275bb302013-05-29 13:11:28 -04002153 dentry = opendata->dentry;
2154 if (dentry->d_inode == NULL) {
2155 /* FIXME: Is this d_drop() ever needed? */
2156 d_drop(dentry);
2157 dentry = d_add_unique(dentry, igrab(state->inode));
2158 if (dentry == NULL) {
2159 dentry = opendata->dentry;
2160 } else if (dentry != ctx->dentry) {
2161 dput(ctx->dentry);
2162 ctx->dentry = dget(dentry);
2163 }
2164 nfs_set_verifier(dentry,
2165 nfs_save_change_attribute(opendata->dir->d_inode));
2166 }
2167
Trond Myklebustc21443c2013-02-07 14:26:21 -05002168 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2169 if (ret != 0)
2170 goto out;
2171
Trond Myklebust3efb9722013-05-29 13:17:04 -04002172 ctx->state = state;
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002173 if (dentry->d_inode == state->inode) {
2174 nfs_inode_attach_open_context(ctx);
2175 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2176 nfs4_schedule_stateid_recovery(server, state);
2177 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002178out:
2179 return ret;
2180}
2181
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002182/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002183 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 */
Andy Adamson82be4172012-05-23 05:02:35 -04002185static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002186 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002187 int flags,
2188 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002189 struct nfs4_label *label,
2190 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191{
2192 struct nfs4_state_owner *sp;
2193 struct nfs4_state *state = NULL;
2194 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002195 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002196 struct dentry *dentry = ctx->dentry;
2197 struct rpc_cred *cred = ctx->cred;
2198 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2199 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002200 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002201 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002202 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
2204 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002206 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2207 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2209 goto out_err;
2210 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002211 status = nfs4_recover_expired_lease(server);
2212 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002213 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04002214 if (dentry->d_inode != NULL)
2215 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002216 status = -ENOMEM;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002217 if (dentry->d_inode)
2218 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002219 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002220 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002221 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002222 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
David Quigley14c43f72013-05-22 12:50:43 -04002224 if (label) {
2225 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2226 if (IS_ERR(olabel)) {
2227 status = PTR_ERR(olabel);
2228 goto err_opendata_put;
2229 }
2230 }
2231
Andy Adamson82be4172012-05-23 05:02:35 -04002232 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2233 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2234 if (!opendata->f_attr.mdsthreshold)
David Quigley14c43f72013-05-22 12:50:43 -04002235 goto err_free_label;
Trond Myklebust1549210f2012-06-05 09:16:47 -04002236 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002237 }
Al Viro82a2c1b2011-06-22 18:30:55 -04002238 if (dentry->d_inode != NULL)
2239 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002240
Trond Myklebust3efb9722013-05-29 13:17:04 -04002241 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002242 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002243 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002244 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002245
Trond Myklebust549b19c2013-04-16 18:42:34 -04002246 if ((opendata->o_arg.open_flags & O_EXCL) &&
2247 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002248 nfs4_exclusive_attrset(opendata, sattr);
2249
2250 nfs_fattr_init(opendata->o_res.f_attr);
2251 status = nfs4_do_setattr(state->inode, cred,
2252 opendata->o_res.f_attr, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002253 state, label, olabel);
2254 if (status == 0) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002255 nfs_setattr_update_inode(state->inode, sattr);
David Quigley1775fd32013-05-22 12:50:42 -04002256 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
David Quigleyaa9c2662013-05-22 12:50:44 -04002257 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
David Quigley1775fd32013-05-22 12:50:42 -04002258 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002259 }
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002260 if (opendata->file_created)
2261 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002262
2263 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2264 *ctx_th = opendata->f_attr.mdsthreshold;
2265 else
2266 kfree(opendata->f_attr.mdsthreshold);
2267 opendata->f_attr.mdsthreshold = NULL;
2268
David Quigley14c43f72013-05-22 12:50:43 -04002269 nfs4_label_free(olabel);
2270
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002271 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002274err_free_label:
2275 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002276err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002277 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002278 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002279err_put_state_owner:
2280 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 return status;
2283}
2284
2285
Andy Adamson82be4172012-05-23 05:02:35 -04002286static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002287 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002288 int flags,
2289 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002290 struct nfs4_label *label,
2291 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002293 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 struct nfs4_exception exception = { };
2295 struct nfs4_state *res;
2296 int status;
2297
2298 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002299 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002300 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002301 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 if (status == 0)
2303 break;
2304 /* NOTE: BAD_SEQID means the server and client disagree about the
2305 * book-keeping w.r.t. state-changing operations
2306 * (OPEN/CLOSE/LOCK/LOCKU...)
2307 * It is actually a sign of a bug on the client or on the server.
2308 *
2309 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002310 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 * have unhashed the old state_owner for us, and that we can
2312 * therefore safely retry using a new one. We should still warn
2313 * the user though...
2314 */
2315 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002316 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002317 " returned a bad sequence-id error!\n",
2318 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 exception.retry = 1;
2320 continue;
2321 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002322 /*
2323 * BAD_STATEID on OPEN means that the server cancelled our
2324 * state before it received the OPEN_CONFIRM.
2325 * Recover by retrying the request as per the discussion
2326 * on Page 181 of RFC3530.
2327 */
2328 if (status == -NFS4ERR_BAD_STATEID) {
2329 exception.retry = 1;
2330 continue;
2331 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002332 if (status == -EAGAIN) {
2333 /* We must have found a delegation */
2334 exception.retry = 1;
2335 continue;
2336 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002337 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2338 continue;
2339 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 status, &exception));
2341 } while (exception.retry);
2342 return res;
2343}
2344
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002345static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2346 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002347 struct nfs4_state *state, struct nfs4_label *ilabel,
2348 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002350 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002352 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 .iap = sattr,
2354 .server = server,
2355 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002356 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 };
2358 struct nfs_setattrres res = {
2359 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002360 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 .server = server,
2362 };
2363 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002364 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2365 .rpc_argp = &arg,
2366 .rpc_resp = &res,
2367 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002369 unsigned long timestamp = jiffies;
Trond Myklebustee3ae842013-04-29 10:35:36 -04002370 fmode_t fmode;
2371 bool truncate;
Trond Myklebust65e43082005-08-16 11:49:44 -04002372 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373
David Quigleyaa9c2662013-05-22 12:50:44 -04002374 arg.bitmask = nfs4_bitmask(server, ilabel);
2375 if (ilabel)
2376 arg.bitmask = nfs4_bitmask(server, olabel);
2377
Trond Myklebust0e574af2005-10-27 22:12:38 -04002378 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
Trond Myklebustee3ae842013-04-29 10:35:36 -04002380 /* Servers should only apply open mode checks for file size changes */
2381 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2382 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2383
2384 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2385 /* Use that stateid */
2386 } else if (truncate && state != NULL && nfs4_valid_open_stateid(state)) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002387 struct nfs_lockowner lockowner = {
2388 .l_owner = current->files,
2389 .l_pid = current->tgid,
2390 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002391 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002392 &lockowner);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002393 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002394 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
Bryan Schumaker7c513052011-03-24 17:12:24 +00002396 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002397 if (status == 0 && state != NULL)
2398 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002399 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400}
2401
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002402static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2403 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002404 struct nfs4_state *state, struct nfs4_label *ilabel,
2405 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002407 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002408 struct nfs4_exception exception = {
2409 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002410 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002411 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 int err;
2413 do {
David Quigley1775fd32013-05-22 12:50:42 -04002414 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
Trond Myklebustc1578b72013-08-12 16:58:42 -04002415 trace_nfs4_setattr(inode, err);
Trond Myklebust451146b2012-04-18 16:29:11 -04002416 switch (err) {
2417 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002418 if (!(sattr->ia_valid & ATTR_SIZE)) {
2419 pr_warn_once("NFSv4: server %s is incorrectly "
2420 "applying open mode checks to "
2421 "a SETATTR that is not "
2422 "changing file size.\n",
2423 server->nfs_client->cl_hostname);
2424 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002425 if (state && !(state->state & FMODE_WRITE)) {
2426 err = -EBADF;
2427 if (sattr->ia_valid & ATTR_OPEN)
2428 err = -EACCES;
2429 goto out;
2430 }
2431 }
2432 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002434out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 return err;
2436}
2437
2438struct nfs4_closedata {
2439 struct inode *inode;
2440 struct nfs4_state *state;
2441 struct nfs_closeargs arg;
2442 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002443 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002444 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002445 bool roc;
2446 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447};
2448
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002449static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002450{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002451 struct nfs4_closedata *calldata = data;
2452 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002453 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002454
Fred Isamanf7e89172011-01-06 11:36:32 +00002455 if (calldata->roc)
2456 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002457 nfs4_put_open_state(calldata->state);
2458 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002459 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002460 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002461 kfree(calldata);
2462}
2463
Trond Myklebust88069f72009-12-08 08:33:16 -05002464static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2465 fmode_t fmode)
2466{
2467 spin_lock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002468 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002469 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
2470 case FMODE_WRITE:
2471 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2472 break;
2473 case FMODE_READ:
2474 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2475 break;
2476 case 0:
2477 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2478 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2479 clear_bit(NFS_OPEN_STATE, &state->flags);
2480 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002481 spin_unlock(&state->owner->so_lock);
2482}
2483
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002484static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002486 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 struct nfs_server *server = NFS_SERVER(calldata->inode);
2489
Chuck Levera3ca5652012-03-01 17:00:40 -05002490 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002491 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2492 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002493 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 /* hmm. we are done with the inode, and in the process of freeing
2495 * the state_owner. we keep this around to process errors
2496 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 switch (task->tk_status) {
2498 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002499 if (calldata->roc)
2500 pnfs_roc_set_barrier(state->inode,
2501 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002502 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002503 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002504 nfs4_close_clear_stateid_flags(state,
2505 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 break;
2507 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002508 case -NFS4ERR_OLD_STATEID:
2509 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002511 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002512 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002514 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2515 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002517 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002518 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002519 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520}
2521
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002522static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002524 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002525 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002526 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002527 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002528
Chuck Levera3ca5652012-03-01 17:00:40 -05002529 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002530 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002531 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002532
Trond Myklebust88069f72009-12-08 08:33:16 -05002533 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2534 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002535 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002536 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002537 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002538 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002539 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2540 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2541 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002542 }
2543 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002544 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2545 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2546 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002547 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002548 }
Trond Myklebust5d422302013-03-14 16:57:48 -04002549 if (!nfs4_valid_open_stateid(state))
2550 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002551 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002552
2553 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002554 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002555 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002556 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002557
Fred Isamanf7e89172011-01-06 11:36:32 +00002558 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002559 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002560 if (calldata->roc &&
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002561 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2562 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002563 goto out_wait;
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002564 }
Fred Isamanf7e89172011-01-06 11:36:32 +00002565 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002566
Trond Myklebust516a6af2005-10-27 22:12:41 -04002567 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002568 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002569 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002570 &calldata->arg.seq_args,
2571 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002572 task) != 0)
2573 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002574 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002575 return;
2576out_no_action:
2577 task->tk_action = NULL;
2578out_wait:
2579 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580}
2581
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002582static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002583 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002584 .rpc_call_done = nfs4_close_done,
2585 .rpc_release = nfs4_free_closedata,
2586};
2587
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588/*
2589 * It is possible for data to be read/written from a mem-mapped file
2590 * after the sys_close call (which hits the vfs layer as a flush).
2591 * This means that we can't safely call nfsv4 close on a file until
2592 * the inode is cleared. This in turn means that we are not good
2593 * NFSv4 citizens - we do not indicate to the server to update the file's
2594 * share state even when we are done with one of the three share
2595 * stateid's in the inode.
2596 *
2597 * NOTE: Caller must be holding the sp->so_owner semaphore!
2598 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002599int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002601 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002603 struct nfs4_state_owner *sp = state->owner;
2604 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002605 struct rpc_message msg = {
2606 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2607 .rpc_cred = state->owner->so_cred,
2608 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002609 struct rpc_task_setup task_setup_data = {
2610 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002611 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002612 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002613 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002614 .flags = RPC_TASK_ASYNC,
2615 };
Trond Myklebust95121352005-10-18 14:20:12 -07002616 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04002618 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
2619 &task_setup_data.rpc_client, &msg);
2620
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002621 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002623 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04002624 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002625 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002627 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002628 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002630 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002631 if (calldata->arg.seqid == NULL)
2632 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002633 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002634 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002635 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002636 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002637 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002638 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002639 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002640
Trond Myklebust1174dd12010-12-21 10:52:24 -05002641 msg.rpc_argp = &calldata->arg;
2642 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002643 task_setup_data.callback_data = calldata;
2644 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002645 if (IS_ERR(task))
2646 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002647 status = 0;
2648 if (wait)
2649 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002650 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002651 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002652out_free_calldata:
2653 kfree(calldata);
2654out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002655 nfs4_put_open_state(state);
2656 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002657 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658}
2659
Trond Myklebust2b484292010-09-17 10:56:51 -04002660static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002661nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
2662 int open_flags, struct iattr *attr, int *opened)
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 Myklebust5bc2afc2013-09-23 18:01:28 -04002670 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
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
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002887 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002888 *
2889 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002890 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002891int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002892 struct nfs_fsinfo *info,
2893 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002894{
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002895 int status;
2896
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002897 switch (auth_probe) {
2898 case false:
2899 status = nfs4_lookup_root(server, fhandle, info);
2900 if (status != -NFS4ERR_WRONGSEC)
2901 break;
2902 /* Did user force a 'sec=' mount option? */
2903 if (server->flags & NFS_MOUNT_SECFLAVOUR)
2904 break;
2905 default:
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002906 status = nfs4_do_find_root_sec(server, fhandle, info);
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002907 }
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002908
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 if (status == 0)
2910 status = nfs4_server_capabilities(server, fhandle);
2911 if (status == 0)
2912 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002913
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002914 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915}
2916
Bryan Schumakerbae36242012-05-10 15:07:31 -04002917static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2918 struct nfs_fsinfo *info)
2919{
2920 int error;
2921 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04002922 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04002923
2924 error = nfs4_server_capabilities(server, mntfh);
2925 if (error < 0) {
2926 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2927 return error;
2928 }
2929
David Quigley14c43f72013-05-22 12:50:43 -04002930 label = nfs4_label_alloc(server, GFP_KERNEL);
2931 if (IS_ERR(label))
2932 return PTR_ERR(label);
2933
David Quigley1775fd32013-05-22 12:50:42 -04002934 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04002935 if (error < 0) {
2936 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04002937 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04002938 }
2939
2940 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2941 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2942 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2943
David Quigley14c43f72013-05-22 12:50:43 -04002944err_free_label:
2945 nfs4_label_free(label);
2946
Bryan Schumakerbae36242012-05-10 15:07:31 -04002947 return error;
2948}
2949
Manoj Naik6b97fd32006-06-09 09:34:29 -04002950/*
2951 * Get locations and (maybe) other attributes of a referral.
2952 * Note that we'll actually follow the referral later when
2953 * we detect fsid mismatch in inode revalidation
2954 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002955static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2956 const struct qstr *name, struct nfs_fattr *fattr,
2957 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002958{
2959 int status = -ENOMEM;
2960 struct page *page = NULL;
2961 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002962
2963 page = alloc_page(GFP_KERNEL);
2964 if (page == NULL)
2965 goto out;
2966 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2967 if (locations == NULL)
2968 goto out;
2969
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002970 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002971 if (status != 0)
2972 goto out;
2973 /* Make sure server returned a different fsid for the referral */
2974 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002975 dprintk("%s: server did not return a different fsid for"
2976 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002977 status = -EIO;
2978 goto out;
2979 }
Andy Adamson533eb462011-06-13 18:25:56 -04002980 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2981 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002982
Andy Adamson533eb462011-06-13 18:25:56 -04002983 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002984 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002985 memset(fhandle, 0, sizeof(struct nfs_fh));
2986out:
2987 if (page)
2988 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002989 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002990 return status;
2991}
2992
David Quigley1775fd32013-05-22 12:50:42 -04002993static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
2994 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995{
2996 struct nfs4_getattr_arg args = {
2997 .fh = fhandle,
2998 .bitmask = server->attr_bitmask,
2999 };
3000 struct nfs4_getattr_res res = {
3001 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003002 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 .server = server,
3004 };
3005 struct rpc_message msg = {
3006 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3007 .rpc_argp = &args,
3008 .rpc_resp = &res,
3009 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003010
3011 args.bitmask = nfs4_bitmask(server, label);
3012
Trond Myklebust0e574af2005-10-27 22:12:38 -04003013 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003014 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015}
3016
David Quigley1775fd32013-05-22 12:50:42 -04003017static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3018 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019{
3020 struct nfs4_exception exception = { };
3021 int err;
3022 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003023 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3024 trace_nfs4_getattr(server, fhandle, fattr, err);
3025 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 &exception);
3027 } while (exception.retry);
3028 return err;
3029}
3030
3031/*
3032 * The file is not closed if it is opened due to the a request to change
3033 * the size of the file. The open call will not be needed once the
3034 * VFS layer lookup-intents are implemented.
3035 *
3036 * Close is called when the inode is destroyed.
3037 * If we haven't opened the file for O_WRONLY, we
3038 * need to in the size_change case to obtain a stateid.
3039 *
3040 * Got race?
3041 * Because OPEN is always done by name in nfsv4, it is
3042 * possible that we opened a different file by the same
3043 * name. We can recognize this race condition, but we
3044 * can't do anything about it besides returning an error.
3045 *
3046 * This will be fixed with VFS changes (lookup-intent).
3047 */
3048static int
3049nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3050 struct iattr *sattr)
3051{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003052 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003053 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003054 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003055 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 int status;
3057
Benny Halevy8a1636c2010-07-14 15:43:57 -04003058 if (pnfs_ld_layoutret_on_setattr(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003059 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003060
Trond Myklebust0e574af2005-10-27 22:12:38 -04003061 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062
Andy Adamson26699402012-05-30 16:12:24 -04003063 /* Deal with open(O_TRUNC) */
3064 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003065 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003066
3067 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003068 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003069 return 0;
3070
Trond Myklebustd5308382005-11-04 15:33:38 -05003071 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003072 if (sattr->ia_valid & ATTR_FILE) {
3073 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003074
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003075 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003076 if (ctx) {
3077 cred = ctx->cred;
3078 state = ctx->state;
3079 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003080 }
3081
David Quigley14c43f72013-05-22 12:50:43 -04003082 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3083 if (IS_ERR(label))
3084 return PTR_ERR(label);
3085
3086 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003087 if (status == 0) {
Trond Myklebust65e43082005-08-16 11:49:44 -04003088 nfs_setattr_update_inode(inode, sattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003089 nfs_setsecurity(inode, fattr, label);
3090 }
David Quigley14c43f72013-05-22 12:50:43 -04003091 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 return status;
3093}
3094
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003095static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3096 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003097 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003098{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003099 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003100 int status;
3101 struct nfs4_lookup_arg args = {
3102 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003103 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003104 .name = name,
3105 };
3106 struct nfs4_lookup_res res = {
3107 .server = server,
3108 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003109 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003110 .fh = fhandle,
3111 };
3112 struct rpc_message msg = {
3113 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3114 .rpc_argp = &args,
3115 .rpc_resp = &res,
3116 };
3117
David Quigleyaa9c2662013-05-22 12:50:44 -04003118 args.bitmask = nfs4_bitmask(server, label);
3119
David Howells2b3de442006-08-22 20:06:09 -04003120 nfs_fattr_init(fattr);
3121
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003123 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 dprintk("NFS reply lookup: %d\n", status);
3125 return status;
3126}
3127
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003128static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003129{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003130 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003131 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003132 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3133 fattr->nlink = 2;
3134}
3135
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003136static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3137 struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003138 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003139{
3140 struct nfs4_exception exception = { };
3141 struct rpc_clnt *client = *clnt;
3142 int err;
3143 do {
David Quigley1775fd32013-05-22 12:50:42 -04003144 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003145 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003146 switch (err) {
3147 case -NFS4ERR_BADNAME:
3148 err = -ENOENT;
3149 goto out;
3150 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003151 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003152 goto out;
3153 case -NFS4ERR_WRONGSEC:
3154 err = -EPERM;
3155 if (client != *clnt)
3156 goto out;
Trond Myklebust41d058c2013-09-07 17:49:05 -04003157 /* No security negotiation if the user specified 'sec=' */
3158 if (NFS_SERVER(dir)->flags & NFS_MOUNT_SECFLAVOUR)
3159 goto out;
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003160 client = nfs4_create_sec_client(client, dir, name);
3161 if (IS_ERR(client))
3162 return PTR_ERR(client);
3163
3164 exception.retry = 1;
3165 break;
3166 default:
3167 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3168 }
3169 } while (exception.retry);
3170
3171out:
3172 if (err == 0)
3173 *clnt = client;
3174 else if (client != *clnt)
3175 rpc_shutdown_client(client);
3176
3177 return err;
3178}
3179
Bryan Schumaker80a16b22012-04-27 13:27:46 -04003180static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003181 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3182 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003184 int status;
3185 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003186
David Quigley1775fd32013-05-22 12:50:42 -04003187 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003188 if (client != NFS_CLIENT(dir)) {
3189 rpc_shutdown_client(client);
3190 nfs_fixup_secinfo_attributes(fattr);
3191 }
3192 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193}
3194
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003195struct rpc_clnt *
3196nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3197 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3198{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003199 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003200 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003201
David Quigley1775fd32013-05-22 12:50:42 -04003202 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003203 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003204 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003205 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003206}
3207
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3209{
Trond Myklebust76b32992007-08-10 17:45:11 -04003210 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 struct nfs4_accessargs args = {
3212 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003213 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003215 struct nfs4_accessres res = {
3216 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003217 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 struct rpc_message msg = {
3219 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3220 .rpc_argp = &args,
3221 .rpc_resp = &res,
3222 .rpc_cred = entry->cred,
3223 };
3224 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003225 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226
3227 /*
3228 * Determine which access bits we want to ask for...
3229 */
3230 if (mode & MAY_READ)
3231 args.access |= NFS4_ACCESS_READ;
3232 if (S_ISDIR(inode->i_mode)) {
3233 if (mode & MAY_WRITE)
3234 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3235 if (mode & MAY_EXEC)
3236 args.access |= NFS4_ACCESS_LOOKUP;
3237 } else {
3238 if (mode & MAY_WRITE)
3239 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3240 if (mode & MAY_EXEC)
3241 args.access |= NFS4_ACCESS_EXECUTE;
3242 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003243
3244 res.fattr = nfs_alloc_fattr();
3245 if (res.fattr == NULL)
3246 return -ENOMEM;
3247
Bryan Schumaker7c513052011-03-24 17:12:24 +00003248 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003250 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003251 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003253 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 return status;
3255}
3256
3257static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3258{
3259 struct nfs4_exception exception = { };
3260 int err;
3261 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003262 err = _nfs4_proc_access(inode, entry);
3263 trace_nfs4_access(inode, err);
3264 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 &exception);
3266 } while (exception.retry);
3267 return err;
3268}
3269
3270/*
3271 * TODO: For the time being, we don't try to get any attributes
3272 * along with any of the zero-copy operations READ, READDIR,
3273 * READLINK, WRITE.
3274 *
3275 * In the case of the first three, we want to put the GETATTR
3276 * after the read-type operation -- this is because it is hard
3277 * to predict the length of a GETATTR response in v4, and thus
3278 * align the READ data correctly. This means that the GETATTR
3279 * may end up partially falling into the page cache, and we should
3280 * shift it into the 'tail' of the xdr_buf before processing.
3281 * To do this efficiently, we need to know the total length
3282 * of data received, which doesn't seem to be available outside
3283 * of the RPC layer.
3284 *
3285 * In the case of WRITE, we also want to put the GETATTR after
3286 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003287 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 *
3289 * Both of these changes to the XDR layer would in fact be quite
3290 * minor, but I decided to leave them for a subsequent patch.
3291 */
3292static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3293 unsigned int pgbase, unsigned int pglen)
3294{
3295 struct nfs4_readlink args = {
3296 .fh = NFS_FH(inode),
3297 .pgbase = pgbase,
3298 .pglen = pglen,
3299 .pages = &page,
3300 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003301 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 struct rpc_message msg = {
3303 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3304 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003305 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 };
3307
Bryan Schumaker7c513052011-03-24 17:12:24 +00003308 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 -07003309}
3310
3311static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3312 unsigned int pgbase, unsigned int pglen)
3313{
3314 struct nfs4_exception exception = { };
3315 int err;
3316 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003317 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3318 trace_nfs4_readlink(inode, err);
3319 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 &exception);
3321 } while (exception.retry);
3322 return err;
3323}
3324
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003326 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328static int
3329nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003330 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331{
David Quigleyaa9c2662013-05-22 12:50:44 -04003332 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003333 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 struct nfs4_state *state;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003335 int opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 int status = 0;
3337
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003338 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3339 if (IS_ERR(ctx))
3340 return PTR_ERR(ctx);
3341
David Quigleyaa9c2662013-05-22 12:50:44 -04003342 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3343
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003344 sattr->ia_mode &= ~current_umask();
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003345 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, &opened);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 if (IS_ERR(state)) {
3347 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003348 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003351 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003352 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 return status;
3354}
3355
3356static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3357{
Trond Myklebust16e42952005-10-27 22:12:44 -04003358 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003359 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003361 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003363 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003364 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003365 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003367 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3368 .rpc_argp = &args,
3369 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003371 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003372
Bryan Schumaker7c513052011-03-24 17:12:24 +00003373 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003374 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003375 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 return status;
3377}
3378
3379static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3380{
3381 struct nfs4_exception exception = { };
3382 int err;
3383 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003384 err = _nfs4_proc_remove(dir, name);
3385 trace_nfs4_remove(dir, name, err);
3386 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 &exception);
3388 } while (exception.retry);
3389 return err;
3390}
3391
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003392static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003394 struct nfs_server *server = NFS_SERVER(dir);
3395 struct nfs_removeargs *args = msg->rpc_argp;
3396 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003398 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04003400 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003401
3402 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403}
3404
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003405static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3406{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003407 nfs4_setup_sequence(NFS_SERVER(data->dir),
3408 &data->args.seq_args,
3409 &data->res.seq_res,
3410 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411}
3412
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003413static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003415 struct nfs_unlinkdata *data = task->tk_calldata;
3416 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003417
Trond Myklebust14516c32010-07-31 14:29:06 -04003418 if (!nfs4_sequence_done(task, &res->seq_res))
3419 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003420 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003421 return 0;
3422 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003423 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424}
3425
Jeff Laytond3d41522010-09-17 17:31:57 -04003426static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3427{
3428 struct nfs_server *server = NFS_SERVER(dir);
3429 struct nfs_renameargs *arg = msg->rpc_argp;
3430 struct nfs_renameres *res = msg->rpc_resp;
3431
3432 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003433 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04003434 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003435}
3436
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003437static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3438{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003439 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3440 &data->args.seq_args,
3441 &data->res.seq_res,
3442 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003443}
3444
3445static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3446 struct inode *new_dir)
3447{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003448 struct nfs_renamedata *data = task->tk_calldata;
3449 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04003450
3451 if (!nfs4_sequence_done(task, &res->seq_res))
3452 return 0;
3453 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3454 return 0;
3455
3456 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003457 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003458 return 1;
3459}
3460
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3462 struct inode *new_dir, struct qstr *new_name)
3463{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003464 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003465 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 .old_dir = NFS_FH(old_dir),
3467 .new_dir = NFS_FH(new_dir),
3468 .old_name = old_name,
3469 .new_name = new_name,
3470 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003471 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003472 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003473 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 struct rpc_message msg = {
3475 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3476 .rpc_argp = &arg,
3477 .rpc_resp = &res,
3478 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003479 int status = -ENOMEM;
David Quigley14c43f72013-05-22 12:50:43 -04003480
Bryan Schumaker7c513052011-03-24 17:12:24 +00003481 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 if (!status) {
3483 update_changeattr(old_dir, &res.old_cinfo);
3484 update_changeattr(new_dir, &res.new_cinfo);
3485 }
3486 return status;
3487}
3488
3489static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3490 struct inode *new_dir, struct qstr *new_name)
3491{
3492 struct nfs4_exception exception = { };
3493 int err;
3494 do {
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003495 err = _nfs4_proc_rename(old_dir, old_name,
3496 new_dir, new_name);
3497 trace_nfs4_rename(old_dir, old_name, new_dir, new_name, err);
3498 err = nfs4_handle_exception(NFS_SERVER(old_dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 &exception);
3500 } while (exception.retry);
3501 return err;
3502}
3503
3504static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3505{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003506 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 struct nfs4_link_arg arg = {
3508 .fh = NFS_FH(inode),
3509 .dir_fh = NFS_FH(dir),
3510 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003511 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003513 struct nfs4_link_res res = {
3514 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003515 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003516 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 struct rpc_message msg = {
3518 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3519 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003520 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003522 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523
Trond Myklebust136f2622010-04-16 16:22:49 -04003524 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003525 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003526 goto out;
3527
David Quigley14c43f72013-05-22 12:50:43 -04003528 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3529 if (IS_ERR(res.label)) {
3530 status = PTR_ERR(res.label);
3531 goto out;
3532 }
David Quigleyaa9c2662013-05-22 12:50:44 -04003533 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04003534
Bryan Schumaker7c513052011-03-24 17:12:24 +00003535 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003536 if (!status) {
3537 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04003538 status = nfs_post_op_update_inode(inode, res.fattr);
3539 if (!status)
3540 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003541 }
David Quigley14c43f72013-05-22 12:50:43 -04003542
3543
3544 nfs4_label_free(res.label);
3545
Trond Myklebust136f2622010-04-16 16:22:49 -04003546out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003547 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 return status;
3549}
3550
3551static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3552{
3553 struct nfs4_exception exception = { };
3554 int err;
3555 do {
3556 err = nfs4_handle_exception(NFS_SERVER(inode),
3557 _nfs4_proc_link(inode, dir, name),
3558 &exception);
3559 } while (exception.retry);
3560 return err;
3561}
3562
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003563struct nfs4_createdata {
3564 struct rpc_message msg;
3565 struct nfs4_create_arg arg;
3566 struct nfs4_create_res res;
3567 struct nfs_fh fh;
3568 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003569 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003570};
3571
3572static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3573 struct qstr *name, struct iattr *sattr, u32 ftype)
3574{
3575 struct nfs4_createdata *data;
3576
3577 data = kzalloc(sizeof(*data), GFP_KERNEL);
3578 if (data != NULL) {
3579 struct nfs_server *server = NFS_SERVER(dir);
3580
David Quigley14c43f72013-05-22 12:50:43 -04003581 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3582 if (IS_ERR(data->label))
3583 goto out_free;
3584
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003585 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3586 data->msg.rpc_argp = &data->arg;
3587 data->msg.rpc_resp = &data->res;
3588 data->arg.dir_fh = NFS_FH(dir);
3589 data->arg.server = server;
3590 data->arg.name = name;
3591 data->arg.attrs = sattr;
3592 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04003593 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003594 data->res.server = server;
3595 data->res.fh = &data->fh;
3596 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003597 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003598 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003599 }
3600 return data;
David Quigley14c43f72013-05-22 12:50:43 -04003601out_free:
3602 kfree(data);
3603 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003604}
3605
3606static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3607{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003608 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003609 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003610 if (status == 0) {
3611 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04003612 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003613 }
3614 return status;
3615}
3616
3617static void nfs4_free_createdata(struct nfs4_createdata *data)
3618{
David Quigley14c43f72013-05-22 12:50:43 -04003619 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003620 kfree(data);
3621}
3622
Chuck Lever4f390c12006-08-22 20:06:22 -04003623static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003624 struct page *page, unsigned int len, struct iattr *sattr,
3625 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003627 struct nfs4_createdata *data;
3628 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629
Chuck Lever94a6d752006-08-22 20:06:23 -04003630 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003631 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003632
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003633 status = -ENOMEM;
3634 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3635 if (data == NULL)
3636 goto out;
3637
3638 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3639 data->arg.u.symlink.pages = &page;
3640 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04003641 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003643 status = nfs4_do_create(dir, dentry, data);
3644
3645 nfs4_free_createdata(data);
3646out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 return status;
3648}
3649
Chuck Lever4f390c12006-08-22 20:06:22 -04003650static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003651 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652{
3653 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003654 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04003656
3657 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3658
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003660 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3661 trace_nfs4_symlink(dir, &dentry->d_name, err);
3662 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 &exception);
3664 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003665
3666 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 return err;
3668}
3669
3670static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003671 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003673 struct nfs4_createdata *data;
3674 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003676 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3677 if (data == NULL)
3678 goto out;
3679
David Quigley1775fd32013-05-22 12:50:42 -04003680 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003681 status = nfs4_do_create(dir, dentry, data);
3682
3683 nfs4_free_createdata(data);
3684out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 return status;
3686}
3687
3688static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3689 struct iattr *sattr)
3690{
3691 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003692 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003694
David Quigleyaa9c2662013-05-22 12:50:44 -04003695 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3696
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003697 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003699 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3700 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3701 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 &exception);
3703 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003704 nfs4_label_release_security(label);
3705
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 return err;
3707}
3708
3709static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003710 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711{
3712 struct inode *dir = dentry->d_inode;
3713 struct nfs4_readdir_arg args = {
3714 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003715 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 .pgbase = 0,
3717 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003718 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003719 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 };
3721 struct nfs4_readdir_res res;
3722 struct rpc_message msg = {
3723 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3724 .rpc_argp = &args,
3725 .rpc_resp = &res,
3726 .rpc_cred = cred,
3727 };
3728 int status;
3729
Harvey Harrison3110ff82008-05-02 13:42:44 -07003730 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003731 dentry->d_parent->d_name.name,
3732 dentry->d_name.name,
3733 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003734 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003736 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 -05003737 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003738 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003739 status += args.pgbase;
3740 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003741
3742 nfs_invalidate_atime(dir);
3743
Harvey Harrison3110ff82008-05-02 13:42:44 -07003744 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 return status;
3746}
3747
3748static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003749 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750{
3751 struct nfs4_exception exception = { };
3752 int err;
3753 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003754 err = _nfs4_proc_readdir(dentry, cred, cookie,
3755 pages, count, plus);
3756 trace_nfs4_readdir(dentry->d_inode, err);
3757 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 &exception);
3759 } while (exception.retry);
3760 return err;
3761}
3762
3763static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04003764 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003766 struct nfs4_createdata *data;
3767 int mode = sattr->ia_mode;
3768 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003770 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3771 if (data == NULL)
3772 goto out;
3773
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003775 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003777 data->arg.ftype = NF4BLK;
3778 data->arg.u.device.specdata1 = MAJOR(rdev);
3779 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 }
3781 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003782 data->arg.ftype = NF4CHR;
3783 data->arg.u.device.specdata1 = MAJOR(rdev);
3784 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003785 } else if (!S_ISSOCK(mode)) {
3786 status = -EINVAL;
3787 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 }
David Quigley1775fd32013-05-22 12:50:42 -04003789
David Quigleyaa9c2662013-05-22 12:50:44 -04003790 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003791 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003792out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003793 nfs4_free_createdata(data);
3794out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 return status;
3796}
3797
3798static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3799 struct iattr *sattr, dev_t rdev)
3800{
3801 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003802 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003804
David Quigleyaa9c2662013-05-22 12:50:44 -04003805 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3806
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003807 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003809 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
3810 trace_nfs4_mknod(dir, &dentry->d_name, err);
3811 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 &exception);
3813 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003814
3815 nfs4_label_release_security(label);
3816
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 return err;
3818}
3819
3820static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3821 struct nfs_fsstat *fsstat)
3822{
3823 struct nfs4_statfs_arg args = {
3824 .fh = fhandle,
3825 .bitmask = server->attr_bitmask,
3826 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003827 struct nfs4_statfs_res res = {
3828 .fsstat = fsstat,
3829 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 struct rpc_message msg = {
3831 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3832 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003833 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 };
3835
Trond Myklebust0e574af2005-10-27 22:12:38 -04003836 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003837 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838}
3839
3840static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3841{
3842 struct nfs4_exception exception = { };
3843 int err;
3844 do {
3845 err = nfs4_handle_exception(server,
3846 _nfs4_proc_statfs(server, fhandle, fsstat),
3847 &exception);
3848 } while (exception.retry);
3849 return err;
3850}
3851
3852static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3853 struct nfs_fsinfo *fsinfo)
3854{
3855 struct nfs4_fsinfo_arg args = {
3856 .fh = fhandle,
3857 .bitmask = server->attr_bitmask,
3858 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003859 struct nfs4_fsinfo_res res = {
3860 .fsinfo = fsinfo,
3861 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 struct rpc_message msg = {
3863 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3864 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003865 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 };
3867
Bryan Schumaker7c513052011-03-24 17:12:24 +00003868 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869}
3870
3871static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3872{
3873 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04003874 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 int err;
3876
3877 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04003878 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003879 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04003880 if (err == 0) {
3881 struct nfs_client *clp = server->nfs_client;
3882
3883 spin_lock(&clp->cl_lock);
3884 clp->cl_lease_time = fsinfo->lease_time * HZ;
3885 clp->cl_last_renewal = now;
3886 spin_unlock(&clp->cl_lock);
3887 break;
3888 }
3889 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 } while (exception.retry);
3891 return err;
3892}
3893
3894static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3895{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003896 int error;
3897
Trond Myklebust0e574af2005-10-27 22:12:38 -04003898 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003899 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003900 if (error == 0) {
3901 /* block layout checks this! */
3902 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003903 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003904 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003905
3906 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907}
3908
3909static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3910 struct nfs_pathconf *pathconf)
3911{
3912 struct nfs4_pathconf_arg args = {
3913 .fh = fhandle,
3914 .bitmask = server->attr_bitmask,
3915 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003916 struct nfs4_pathconf_res res = {
3917 .pathconf = pathconf,
3918 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 struct rpc_message msg = {
3920 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3921 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003922 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 };
3924
3925 /* None of the pathconf attributes are mandatory to implement */
3926 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3927 memset(pathconf, 0, sizeof(*pathconf));
3928 return 0;
3929 }
3930
Trond Myklebust0e574af2005-10-27 22:12:38 -04003931 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003932 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933}
3934
3935static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3936 struct nfs_pathconf *pathconf)
3937{
3938 struct nfs4_exception exception = { };
3939 int err;
3940
3941 do {
3942 err = nfs4_handle_exception(server,
3943 _nfs4_proc_pathconf(server, fhandle, pathconf),
3944 &exception);
3945 } while (exception.retry);
3946 return err;
3947}
3948
Trond Myklebust5521abf2013-03-16 20:54:34 -04003949int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04003950 const struct nfs_open_context *ctx,
3951 const struct nfs_lock_context *l_ctx,
3952 fmode_t fmode)
3953{
3954 const struct nfs_lockowner *lockowner = NULL;
3955
3956 if (l_ctx != NULL)
3957 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003958 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04003959}
3960EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
3961
Trond Myklebust5521abf2013-03-16 20:54:34 -04003962static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
3963 const struct nfs_open_context *ctx,
3964 const struct nfs_lock_context *l_ctx,
3965 fmode_t fmode)
3966{
3967 nfs4_stateid current_stateid;
3968
3969 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode))
3970 return false;
3971 return nfs4_stateid_match(stateid, &current_stateid);
3972}
3973
3974static bool nfs4_error_stateid_expired(int err)
3975{
3976 switch (err) {
3977 case -NFS4ERR_DELEG_REVOKED:
3978 case -NFS4ERR_ADMIN_REVOKED:
3979 case -NFS4ERR_BAD_STATEID:
3980 case -NFS4ERR_STALE_STATEID:
3981 case -NFS4ERR_OLD_STATEID:
3982 case -NFS4ERR_OPENMODE:
3983 case -NFS4ERR_EXPIRED:
3984 return true;
3985 }
3986 return false;
3987}
3988
Benny Halevyd20581a2011-05-22 19:52:03 +03003989void __nfs4_read_done_cb(struct nfs_read_data *data)
3990{
Fred Isamancd841602012-04-20 14:47:44 -04003991 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003992}
3993
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003994static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995{
Fred Isamancd841602012-04-20 14:47:44 -04003996 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997
Trond Myklebustcc668ab2013-08-14 15:31:28 -04003998 trace_nfs4_read(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003999 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004000 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05004001 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004003
Benny Halevyd20581a2011-05-22 19:52:03 +03004004 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05004006 renew_lease(server, data->timestamp);
4007 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008}
4009
Trond Myklebust5521abf2013-03-16 20:54:34 -04004010static bool nfs4_read_stateid_changed(struct rpc_task *task,
4011 struct nfs_readargs *args)
4012{
4013
4014 if (!nfs4_error_stateid_expired(task->tk_status) ||
4015 nfs4_stateid_is_current(&args->stateid,
4016 args->context,
4017 args->lock_context,
4018 FMODE_READ))
4019 return false;
4020 rpc_restart_call_prepare(task);
4021 return true;
4022}
4023
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004024static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
4025{
4026
4027 dprintk("--> %s\n", __func__);
4028
4029 if (!nfs4_sequence_done(task, &data->res.seq_res))
4030 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004031 if (nfs4_read_stateid_changed(task, &data->args))
4032 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03004033 return data->read_done_cb ? data->read_done_cb(task, data) :
4034 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004035}
4036
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004037static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004040 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004041 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Chuck Levera9c92d62013-08-09 12:48:18 -04004042 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043}
4044
NeilBrownef1820f2013-09-04 17:04:49 +10004045static int nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004046{
Trond Myklebust9b206142013-03-17 15:52:00 -04004047 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004048 &data->args.seq_args,
4049 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004050 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004051 return 0;
4052 if (nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4053 data->args.lock_context, FMODE_READ) == -EIO)
4054 return -EIO;
4055 if (unlikely(test_bit(NFS_CONTEXT_BAD, &data->args.context->flags)))
4056 return -EIO;
4057 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058}
4059
Fred Isamanb029bc92011-03-03 15:13:42 +00004060static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061{
Fred Isamancd841602012-04-20 14:47:44 -04004062 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004064 trace_nfs4_write(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004065 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004066 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004067 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004069 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04004071 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004072 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004073 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074}
4075
Trond Myklebust5521abf2013-03-16 20:54:34 -04004076static bool nfs4_write_stateid_changed(struct rpc_task *task,
4077 struct nfs_writeargs *args)
4078{
4079
4080 if (!nfs4_error_stateid_expired(task->tk_status) ||
4081 nfs4_stateid_is_current(&args->stateid,
4082 args->context,
4083 args->lock_context,
4084 FMODE_WRITE))
4085 return false;
4086 rpc_restart_call_prepare(task);
4087 return true;
4088}
4089
Fred Isamanb029bc92011-03-03 15:13:42 +00004090static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
4091{
4092 if (!nfs4_sequence_done(task, &data->res.seq_res))
4093 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004094 if (nfs4_write_stateid_changed(task, &data->args))
4095 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03004096 return data->write_done_cb ? data->write_done_cb(task, data) :
4097 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00004098}
4099
Trond Myklebust5a37f852012-04-28 14:55:16 -04004100static
4101bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00004102{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004103 const struct nfs_pgio_header *hdr = data->header;
4104
4105 /* Don't request attributes for pNFS or O_DIRECT writes */
4106 if (data->ds_clp != NULL || hdr->dreq != NULL)
4107 return false;
4108 /* Otherwise, request attributes if and only if we don't hold
4109 * a delegation
4110 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004111 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004112}
Fred Isamana69aef12011-03-03 15:13:47 +00004113
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004114static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115{
Fred Isamancd841602012-04-20 14:47:44 -04004116 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004117
Trond Myklebust5a37f852012-04-28 14:55:16 -04004118 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00004119 data->args.bitmask = NULL;
4120 data->res.fattr = NULL;
4121 } else
4122 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004123
Fred Isamanb029bc92011-03-03 15:13:42 +00004124 if (!data->write_done_cb)
4125 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004126 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 data->timestamp = jiffies;
4128
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004129 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004130 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131}
4132
NeilBrownef1820f2013-09-04 17:04:49 +10004133static int nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04004134{
Trond Myklebust9b206142013-03-17 15:52:00 -04004135 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004136 &data->args.seq_args,
4137 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004138 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004139 return 0;
4140 if (nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4141 data->args.lock_context, FMODE_WRITE) == -EIO)
4142 return -EIO;
4143 if (unlikely(test_bit(NFS_CONTEXT_BAD, &data->args.context->flags)))
4144 return -EIO;
4145 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146}
4147
Fred Isaman0b7c0152012-04-20 14:47:39 -04004148static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4149{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004150 nfs4_setup_sequence(NFS_SERVER(data->inode),
4151 &data->args.seq_args,
4152 &data->res.seq_res,
4153 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154}
4155
Fred Isaman0b7c0152012-04-20 14:47:39 -04004156static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004159
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004160 trace_nfs4_commit(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004161 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004162 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004163 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004165 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166}
4167
Fred Isaman0b7c0152012-04-20 14:47:39 -04004168static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004169{
4170 if (!nfs4_sequence_done(task, &data->res.seq_res))
4171 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004172 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004173}
4174
Fred Isaman0b7c0152012-04-20 14:47:39 -04004175static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004177 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004178
Fred Isaman0b7c0152012-04-20 14:47:39 -04004179 if (data->commit_done_cb == NULL)
4180 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004181 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004182 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004183 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184}
4185
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004186struct nfs4_renewdata {
4187 struct nfs_client *client;
4188 unsigned long timestamp;
4189};
4190
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191/*
4192 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4193 * standalone procedure for queueing an asynchronous RENEW.
4194 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004195static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004196{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004197 struct nfs4_renewdata *data = calldata;
4198 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004199
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004200 if (atomic_read(&clp->cl_count) > 1)
4201 nfs4_schedule_state_renewal(clp);
4202 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004203 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004204}
4205
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004206static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004208 struct nfs4_renewdata *data = calldata;
4209 struct nfs_client *clp = data->client;
4210 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004212 trace_nfs4_renew_async(clp, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04004214 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004215 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4216 return;
4217 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004218 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004219 return;
4220 }
4221 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004223 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224}
4225
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004226static const struct rpc_call_ops nfs4_renew_ops = {
4227 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004228 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004229};
4230
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004231static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232{
4233 struct rpc_message msg = {
4234 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4235 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004236 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004238 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004240 if (renew_flags == 0)
4241 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004242 if (!atomic_inc_not_zero(&clp->cl_count))
4243 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004244 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004245 if (data == NULL)
4246 return -ENOMEM;
4247 data->client = clp;
4248 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004249 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004250 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251}
4252
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004253static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254{
4255 struct rpc_message msg = {
4256 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4257 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004258 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 };
4260 unsigned long now = jiffies;
4261 int status;
4262
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004263 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 if (status < 0)
4265 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004266 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267 return 0;
4268}
4269
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004270static inline int nfs4_server_supports_acls(struct nfs_server *server)
4271{
4272 return (server->caps & NFS_CAP_ACLS)
4273 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
4274 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
4275}
4276
Trond Myklebust21f498c2012-08-24 10:59:25 -04004277/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4278 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004279 * the stack.
4280 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004281#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004282
Neil Hormane9e3d722011-03-04 19:26:03 -05004283static int buf_to_pages_noslab(const void *buf, size_t buflen,
4284 struct page **pages, unsigned int *pgbase)
4285{
4286 struct page *newpage, **spages;
4287 int rc = 0;
4288 size_t len;
4289 spages = pages;
4290
4291 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004292 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004293 newpage = alloc_page(GFP_KERNEL);
4294
4295 if (newpage == NULL)
4296 goto unwind;
4297 memcpy(page_address(newpage), buf, len);
4298 buf += len;
4299 buflen -= len;
4300 *pages++ = newpage;
4301 rc++;
4302 } while (buflen != 0);
4303
4304 return rc;
4305
4306unwind:
4307 for(; rc > 0; rc--)
4308 __free_page(spages[rc-1]);
4309 return -ENOMEM;
4310}
4311
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004312struct nfs4_cached_acl {
4313 int cached;
4314 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004315 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004316};
4317
4318static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004319{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004320 struct nfs_inode *nfsi = NFS_I(inode);
4321
4322 spin_lock(&inode->i_lock);
4323 kfree(nfsi->nfs4_acl);
4324 nfsi->nfs4_acl = acl;
4325 spin_unlock(&inode->i_lock);
4326}
4327
4328static void nfs4_zap_acl_attr(struct inode *inode)
4329{
4330 nfs4_set_cached_acl(inode, NULL);
4331}
4332
4333static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4334{
4335 struct nfs_inode *nfsi = NFS_I(inode);
4336 struct nfs4_cached_acl *acl;
4337 int ret = -ENOENT;
4338
4339 spin_lock(&inode->i_lock);
4340 acl = nfsi->nfs4_acl;
4341 if (acl == NULL)
4342 goto out;
4343 if (buf == NULL) /* user is just asking for length */
4344 goto out_len;
4345 if (acl->cached == 0)
4346 goto out;
4347 ret = -ERANGE; /* see getxattr(2) man page */
4348 if (acl->len > buflen)
4349 goto out;
4350 memcpy(buf, acl->data, acl->len);
4351out_len:
4352 ret = acl->len;
4353out:
4354 spin_unlock(&inode->i_lock);
4355 return ret;
4356}
4357
Sachin Prabhu5794d212012-04-17 14:36:40 +01004358static 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 +00004359{
4360 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004361 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004362
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004363 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004364 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004365 if (acl == NULL)
4366 goto out;
4367 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004368 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004369 } else {
4370 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4371 if (acl == NULL)
4372 goto out;
4373 acl->cached = 0;
4374 }
4375 acl->len = acl_len;
4376out:
4377 nfs4_set_cached_acl(inode, acl);
4378}
4379
Andy Adamsonbf118a32011-12-07 11:55:27 -05004380/*
4381 * The getxattr API returns the required buffer length when called with a
4382 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4383 * the required buf. On a NULL buf, we send a page of data to the server
4384 * guessing that the ACL request can be serviced by a page. If so, we cache
4385 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4386 * the cache. If not so, we throw away the page, and cache the required
4387 * length. The next getxattr call will then produce another round trip to
4388 * the server, this time with the input buf of the required size.
4389 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004390static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004391{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004392 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004393 struct nfs_getaclargs args = {
4394 .fh = NFS_FH(inode),
4395 .acl_pages = pages,
4396 .acl_len = buflen,
4397 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004398 struct nfs_getaclres res = {
4399 .acl_len = buflen,
4400 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004401 struct rpc_message msg = {
4402 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4403 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004404 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004405 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004406 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4407 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004408
Andy Adamsonbf118a32011-12-07 11:55:27 -05004409 /* As long as we're doing a round trip to the server anyway,
4410 * let's be prepared for a page of acl data. */
4411 if (npages == 0)
4412 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004413 if (npages > ARRAY_SIZE(pages))
4414 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004415
Andy Adamsonbf118a32011-12-07 11:55:27 -05004416 for (i = 0; i < npages; i++) {
4417 pages[i] = alloc_page(GFP_KERNEL);
4418 if (!pages[i])
4419 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004420 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004421
4422 /* for decoding across pages */
4423 res.acl_scratch = alloc_page(GFP_KERNEL);
4424 if (!res.acl_scratch)
4425 goto out_free;
4426
Andy Adamsonbf118a32011-12-07 11:55:27 -05004427 args.acl_len = npages * PAGE_SIZE;
4428 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004429
Peng Taode040be2012-01-10 22:42:47 +08004430 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004431 __func__, buf, buflen, npages, args.acl_len);
4432 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4433 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004434 if (ret)
4435 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004436
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004437 /* Handle the case where the passed-in buffer is too short */
4438 if (res.acl_flags & NFS4_ACL_TRUNC) {
4439 /* Did the user only issue a request for the acl length? */
4440 if (buf == NULL)
4441 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004442 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004443 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004444 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004445 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004446 if (buf) {
4447 if (res.acl_len > buflen) {
4448 ret = -ERANGE;
4449 goto out_free;
4450 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004451 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004452 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004453out_ok:
4454 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004455out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004456 for (i = 0; i < npages; i++)
4457 if (pages[i])
4458 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004459 if (res.acl_scratch)
4460 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004461 return ret;
4462}
4463
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004464static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4465{
4466 struct nfs4_exception exception = { };
4467 ssize_t ret;
4468 do {
4469 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04004470 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004471 if (ret >= 0)
4472 break;
4473 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4474 } while (exception.retry);
4475 return ret;
4476}
4477
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004478static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4479{
4480 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004481 int ret;
4482
4483 if (!nfs4_server_supports_acls(server))
4484 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004485 ret = nfs_revalidate_inode(server, inode);
4486 if (ret < 0)
4487 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004488 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4489 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004490 ret = nfs4_read_cached_acl(inode, buf, buflen);
4491 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004492 /* -ENOENT is returned if there is no ACL or if there is an ACL
4493 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004494 return ret;
4495 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004496}
4497
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004498static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004499{
4500 struct nfs_server *server = NFS_SERVER(inode);
4501 struct page *pages[NFS4ACL_MAXPAGES];
4502 struct nfs_setaclargs arg = {
4503 .fh = NFS_FH(inode),
4504 .acl_pages = pages,
4505 .acl_len = buflen,
4506 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004507 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004508 struct rpc_message msg = {
4509 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4510 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004511 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004512 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004513 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004514 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004515
4516 if (!nfs4_server_supports_acls(server))
4517 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004518 if (npages > ARRAY_SIZE(pages))
4519 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004520 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4521 if (i < 0)
4522 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004523 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004524 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004525
4526 /*
4527 * Free each page after tx, so the only ref left is
4528 * held by the network stack
4529 */
4530 for (; i > 0; i--)
4531 put_page(pages[i-1]);
4532
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004533 /*
4534 * Acl update can result in inode attribute update.
4535 * so mark the attribute cache invalid.
4536 */
4537 spin_lock(&inode->i_lock);
4538 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4539 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004540 nfs_access_zap_cache(inode);
4541 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004542 return ret;
4543}
4544
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004545static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4546{
4547 struct nfs4_exception exception = { };
4548 int err;
4549 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004550 err = __nfs4_proc_set_acl(inode, buf, buflen);
4551 trace_nfs4_set_acl(inode, err);
4552 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004553 &exception);
4554 } while (exception.retry);
4555 return err;
4556}
4557
David Quigleyaa9c2662013-05-22 12:50:44 -04004558#ifdef CONFIG_NFS_V4_SECURITY_LABEL
4559static int _nfs4_get_security_label(struct inode *inode, void *buf,
4560 size_t buflen)
4561{
4562 struct nfs_server *server = NFS_SERVER(inode);
4563 struct nfs_fattr fattr;
4564 struct nfs4_label label = {0, 0, buflen, buf};
4565
4566 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4567 struct nfs4_getattr_arg args = {
4568 .fh = NFS_FH(inode),
4569 .bitmask = bitmask,
4570 };
4571 struct nfs4_getattr_res res = {
4572 .fattr = &fattr,
4573 .label = &label,
4574 .server = server,
4575 };
4576 struct rpc_message msg = {
4577 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4578 .rpc_argp = &args,
4579 .rpc_resp = &res,
4580 };
4581 int ret;
4582
4583 nfs_fattr_init(&fattr);
4584
4585 ret = rpc_call_sync(server->client, &msg, 0);
4586 if (ret)
4587 return ret;
4588 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4589 return -ENOENT;
4590 if (buflen < label.len)
4591 return -ERANGE;
4592 return 0;
4593}
4594
4595static int nfs4_get_security_label(struct inode *inode, void *buf,
4596 size_t buflen)
4597{
4598 struct nfs4_exception exception = { };
4599 int err;
4600
4601 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4602 return -EOPNOTSUPP;
4603
4604 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004605 err = _nfs4_get_security_label(inode, buf, buflen);
4606 trace_nfs4_get_security_label(inode, err);
4607 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004608 &exception);
4609 } while (exception.retry);
4610 return err;
4611}
4612
4613static int _nfs4_do_set_security_label(struct inode *inode,
4614 struct nfs4_label *ilabel,
4615 struct nfs_fattr *fattr,
4616 struct nfs4_label *olabel)
4617{
4618
4619 struct iattr sattr = {0};
4620 struct nfs_server *server = NFS_SERVER(inode);
4621 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4622 struct nfs_setattrargs args = {
4623 .fh = NFS_FH(inode),
4624 .iap = &sattr,
4625 .server = server,
4626 .bitmask = bitmask,
4627 .label = ilabel,
4628 };
4629 struct nfs_setattrres res = {
4630 .fattr = fattr,
4631 .label = olabel,
4632 .server = server,
4633 };
4634 struct rpc_message msg = {
4635 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
4636 .rpc_argp = &args,
4637 .rpc_resp = &res,
4638 };
4639 int status;
4640
4641 nfs4_stateid_copy(&args.stateid, &zero_stateid);
4642
4643 status = rpc_call_sync(server->client, &msg, 0);
4644 if (status)
4645 dprintk("%s failed: %d\n", __func__, status);
4646
4647 return status;
4648}
4649
4650static int nfs4_do_set_security_label(struct inode *inode,
4651 struct nfs4_label *ilabel,
4652 struct nfs_fattr *fattr,
4653 struct nfs4_label *olabel)
4654{
4655 struct nfs4_exception exception = { };
4656 int err;
4657
4658 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004659 err = _nfs4_do_set_security_label(inode, ilabel,
4660 fattr, olabel);
4661 trace_nfs4_set_security_label(inode, err);
4662 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004663 &exception);
4664 } while (exception.retry);
4665 return err;
4666}
4667
4668static int
4669nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4670{
4671 struct nfs4_label ilabel, *olabel = NULL;
4672 struct nfs_fattr fattr;
4673 struct rpc_cred *cred;
4674 struct inode *inode = dentry->d_inode;
4675 int status;
4676
4677 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4678 return -EOPNOTSUPP;
4679
4680 nfs_fattr_init(&fattr);
4681
4682 ilabel.pi = 0;
4683 ilabel.lfs = 0;
4684 ilabel.label = (char *)buf;
4685 ilabel.len = buflen;
4686
4687 cred = rpc_lookup_cred();
4688 if (IS_ERR(cred))
4689 return PTR_ERR(cred);
4690
4691 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4692 if (IS_ERR(olabel)) {
4693 status = -PTR_ERR(olabel);
4694 goto out;
4695 }
4696
4697 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4698 if (status == 0)
4699 nfs_setsecurity(inode, &fattr, olabel);
4700
4701 nfs4_label_free(olabel);
4702out:
4703 put_rpccred(cred);
4704 return status;
4705}
4706#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4707
4708
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004710nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004712 struct nfs_client *clp = server->nfs_client;
4713
4714 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 return 0;
4716 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004717 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004718 case -NFS4ERR_ADMIN_REVOKED:
4719 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004720 if (state == NULL)
4721 break;
4722 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004723 case -NFS4ERR_OPENMODE:
4724 if (state == NULL)
4725 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004726 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4727 goto stateid_invalid;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004728 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004729 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004730 if (state != NULL) {
4731 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4732 goto stateid_invalid;
4733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004735 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004736 nfs4_schedule_lease_recovery(clp);
4737 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004738#if defined(CONFIG_NFS_V4_1)
4739 case -NFS4ERR_BADSESSION:
4740 case -NFS4ERR_BADSLOT:
4741 case -NFS4ERR_BAD_HIGH_SLOT:
4742 case -NFS4ERR_DEADSESSION:
4743 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4744 case -NFS4ERR_SEQ_FALSE_RETRY:
4745 case -NFS4ERR_SEQ_MISORDERED:
4746 dprintk("%s ERROR %d, Reset session\n", __func__,
4747 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004748 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004749 task->tk_status = 0;
4750 return -EAGAIN;
4751#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004753 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004754 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4756 task->tk_status = 0;
4757 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004758 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 case -NFS4ERR_OLD_STATEID:
4760 task->tk_status = 0;
4761 return -EAGAIN;
4762 }
4763 task->tk_status = nfs4_map_errors(task->tk_status);
4764 return 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004765stateid_invalid:
4766 task->tk_status = -EIO;
4767 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004768wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004769 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004770 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4771 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4772 task->tk_status = 0;
4773 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774}
4775
Chuck Leverf0920752012-05-21 22:45:41 -04004776static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4777 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004778{
4779 __be32 verf[2];
4780
Chuck Lever2c820d92012-05-21 22:45:33 -04004781 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4782 /* An impossible timestamp guarantees this value
4783 * will never match a generated boot time. */
4784 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04004785 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04004786 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004787 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04004788 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
4789 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04004790 }
Chuck Levercd937102012-03-02 17:14:31 -05004791 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4792}
4793
Chuck Levere984a552012-09-14 17:24:21 -04004794static unsigned int
4795nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4796 char *buf, size_t len)
4797{
4798 unsigned int result;
4799
4800 rcu_read_lock();
4801 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4802 clp->cl_ipaddr,
4803 rpc_peeraddr2str(clp->cl_rpcclient,
4804 RPC_DISPLAY_ADDR),
4805 rpc_peeraddr2str(clp->cl_rpcclient,
4806 RPC_DISPLAY_PROTO));
4807 rcu_read_unlock();
4808 return result;
4809}
4810
4811static unsigned int
4812nfs4_init_uniform_client_string(const struct nfs_client *clp,
4813 char *buf, size_t len)
4814{
Trond Myklebust55b59292013-07-24 16:36:35 -04004815 const char *nodename = clp->cl_rpcclient->cl_nodename;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004816
4817 if (nfs4_client_id_uniquifier[0] != '\0')
Trond Myklebust55b59292013-07-24 16:36:35 -04004818 return scnprintf(buf, len, "Linux NFSv%u.%u %s/%s",
4819 clp->rpc_ops->version,
4820 clp->cl_minorversion,
4821 nfs4_client_id_uniquifier,
4822 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004823 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4824 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004825 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004826}
4827
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004828/**
4829 * nfs4_proc_setclientid - Negotiate client ID
4830 * @clp: state data structure
4831 * @program: RPC program for NFSv4 callback service
4832 * @port: IP port number for NFS4 callback service
4833 * @cred: RPC credential to use for this call
4834 * @res: where to place the result
4835 *
4836 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4837 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004838int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4839 unsigned short port, struct rpc_cred *cred,
4840 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841{
4842 nfs4_verifier sc_verifier;
4843 struct nfs4_setclientid setclientid = {
4844 .sc_verifier = &sc_verifier,
4845 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004846 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 };
4848 struct rpc_message msg = {
4849 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4850 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004851 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004852 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004854 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855
Chuck Leverde734832012-07-11 16:30:50 -04004856 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004857 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004858 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4859 setclientid.sc_name_len =
4860 nfs4_init_uniform_client_string(clp,
4861 setclientid.sc_name,
4862 sizeof(setclientid.sc_name));
4863 else
4864 setclientid.sc_name_len =
4865 nfs4_init_nonuniform_client_string(clp,
4866 setclientid.sc_name,
4867 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004868 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004869 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004870 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Djalal Harounife2d5392013-06-18 17:58:12 +01004871 sizeof(setclientid.sc_netid), "%s",
Chuck Leverd4d3c502007-12-10 14:57:09 -05004872 rpc_peeraddr2str(clp->cl_rpcclient,
4873 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004874 rcu_read_unlock();
4875 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004876 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877 clp->cl_ipaddr, port >> 8, port & 255);
4878
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004879 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4880 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4881 setclientid.sc_name_len, setclientid.sc_name);
4882 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004883 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004884 dprintk("NFS reply setclientid: %d\n", status);
4885 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886}
4887
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004888/**
4889 * nfs4_proc_setclientid_confirm - Confirm client ID
4890 * @clp: state data structure
4891 * @res: result of a previous SETCLIENTID
4892 * @cred: RPC credential to use for this call
4893 *
4894 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4895 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004896int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004897 struct nfs4_setclientid_res *arg,
4898 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900 struct rpc_message msg = {
4901 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004902 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004903 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 int status;
4906
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004907 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4908 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4909 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004910 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004911 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004912 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913 return status;
4914}
4915
Trond Myklebustfe650402006-01-03 09:55:18 +01004916struct nfs4_delegreturndata {
4917 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004918 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004919 struct nfs_fh fh;
4920 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004921 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004922 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004923 int rpc_status;
4924};
4925
Trond Myklebustfe650402006-01-03 09:55:18 +01004926static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4927{
4928 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004929
Trond Myklebust14516c32010-07-31 14:29:06 -04004930 if (!nfs4_sequence_done(task, &data->res.seq_res))
4931 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004932
Trond Myklebustca8acf82013-08-13 10:36:56 -04004933 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004934 switch (task->tk_status) {
4935 case -NFS4ERR_STALE_STATEID:
4936 case -NFS4ERR_EXPIRED:
4937 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004938 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004939 break;
4940 default:
4941 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4942 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004943 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004944 return;
4945 }
4946 }
4947 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004948}
4949
4950static void nfs4_delegreturn_release(void *calldata)
4951{
Trond Myklebustfe650402006-01-03 09:55:18 +01004952 kfree(calldata);
4953}
4954
Andy Adamson938e1012009-04-01 09:22:28 -04004955static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4956{
4957 struct nfs4_delegreturndata *d_data;
4958
4959 d_data = (struct nfs4_delegreturndata *)data;
4960
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004961 nfs4_setup_sequence(d_data->res.server,
4962 &d_data->args.seq_args,
4963 &d_data->res.seq_res,
4964 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004965}
Andy Adamson938e1012009-04-01 09:22:28 -04004966
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004967static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004968 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01004969 .rpc_call_done = nfs4_delegreturn_done,
4970 .rpc_release = nfs4_delegreturn_release,
4971};
4972
Trond Myklebuste6f81072008-01-24 18:14:34 -05004973static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004974{
4975 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004976 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004977 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004978 struct rpc_message msg = {
4979 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4980 .rpc_cred = cred,
4981 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004982 struct rpc_task_setup task_setup_data = {
4983 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004984 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004985 .callback_ops = &nfs4_delegreturn_ops,
4986 .flags = RPC_TASK_ASYNC,
4987 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004988 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004989
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004990 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004991 if (data == NULL)
4992 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04004993 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004994 data->args.fhandle = &data->fh;
4995 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004996 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004997 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004998 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004999 data->res.fattr = &data->fattr;
5000 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005001 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005002 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005003 data->rpc_status = 0;
5004
Trond Myklebustc970aa82007-07-14 15:39:59 -04005005 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005006 msg.rpc_argp = &data->args;
5007 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005008 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005009 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005010 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005011 if (!issync)
5012 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005013 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005014 if (status != 0)
5015 goto out;
5016 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005017 if (status == 0)
5018 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5019 else
5020 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005021out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005022 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005023 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024}
5025
Trond Myklebuste6f81072008-01-24 18:14:34 -05005026int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027{
5028 struct nfs_server *server = NFS_SERVER(inode);
5029 struct nfs4_exception exception = { };
5030 int err;
5031 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005032 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Trond Myklebustca8acf82013-08-13 10:36:56 -04005033 trace_nfs4_delegreturn(inode, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034 switch (err) {
5035 case -NFS4ERR_STALE_STATEID:
5036 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 case 0:
5038 return 0;
5039 }
5040 err = nfs4_handle_exception(server, err, &exception);
5041 } while (exception.retry);
5042 return err;
5043}
5044
5045#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5046#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5047
5048/*
5049 * sleep, with exponential backoff, and retry the LOCK operation.
5050 */
5051static unsigned long
5052nfs4_set_lock_task_retry(unsigned long timeout)
5053{
Colin Cross416ad3c2013-05-06 23:50:06 +00005054 freezable_schedule_timeout_killable_unsafe(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055 timeout <<= 1;
5056 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5057 return NFS4_LOCK_MAXTIMEOUT;
5058 return timeout;
5059}
5060
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5062{
5063 struct inode *inode = state->inode;
5064 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005065 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005066 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005068 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005070 struct nfs_lockt_res res = {
5071 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072 };
5073 struct rpc_message msg = {
5074 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5075 .rpc_argp = &arg,
5076 .rpc_resp = &res,
5077 .rpc_cred = state->owner->so_cred,
5078 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079 struct nfs4_lock_state *lsp;
5080 int status;
5081
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005082 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005083 status = nfs4_set_lock_state(state, request);
5084 if (status != 0)
5085 goto out;
5086 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005087 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005088 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005089 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005090 switch (status) {
5091 case 0:
5092 request->fl_type = F_UNLCK;
5093 break;
5094 case -NFS4ERR_DENIED:
5095 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005097 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005098 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005099out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100 return status;
5101}
5102
5103static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5104{
5105 struct nfs4_exception exception = { };
5106 int err;
5107
5108 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005109 err = _nfs4_proc_getlk(state, cmd, request);
5110 trace_nfs4_get_lock(request, state, cmd, err);
5111 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112 &exception);
5113 } while (exception.retry);
5114 return err;
5115}
5116
5117static int do_vfs_lock(struct file *file, struct file_lock *fl)
5118{
5119 int res = 0;
5120 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5121 case FL_POSIX:
5122 res = posix_lock_file_wait(file, fl);
5123 break;
5124 case FL_FLOCK:
5125 res = flock_lock_file_wait(file, fl);
5126 break;
5127 default:
5128 BUG();
5129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130 return res;
5131}
5132
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005133struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005134 struct nfs_locku_args arg;
5135 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005136 struct nfs4_lock_state *lsp;
5137 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005138 struct file_lock fl;
5139 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005140 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005141};
5142
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005143static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5144 struct nfs_open_context *ctx,
5145 struct nfs4_lock_state *lsp,
5146 struct nfs_seqid *seqid)
5147{
5148 struct nfs4_unlockdata *p;
5149 struct inode *inode = lsp->ls_state->inode;
5150
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005151 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005152 if (p == NULL)
5153 return NULL;
5154 p->arg.fh = NFS_FH(inode);
5155 p->arg.fl = &p->fl;
5156 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005157 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005158 p->arg.stateid = &lsp->ls_stateid;
5159 p->lsp = lsp;
5160 atomic_inc(&lsp->ls_count);
5161 /* Ensure we don't close file until we're done freeing locks! */
5162 p->ctx = get_nfs_open_context(ctx);
5163 memcpy(&p->fl, fl, sizeof(p->fl));
5164 p->server = NFS_SERVER(inode);
5165 return p;
5166}
5167
Trond Myklebust06f814a2006-01-03 09:55:07 +01005168static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005169{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005170 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005171 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005172 nfs4_put_lock_state(calldata->lsp);
5173 put_nfs_open_context(calldata->ctx);
5174 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005175}
5176
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005177static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005178{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005179 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005180
Trond Myklebust14516c32010-07-31 14:29:06 -04005181 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5182 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005183 switch (task->tk_status) {
5184 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05005185 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
5186 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005187 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005188 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005189 case -NFS4ERR_BAD_STATEID:
5190 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005191 case -NFS4ERR_STALE_STATEID:
5192 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005193 break;
5194 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005195 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005196 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005197 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005198 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005199}
5200
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005201static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005202{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005203 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005205 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005206 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04005207 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005208 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005209 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005210 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005211 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005212 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005213 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005214 &calldata->res.seq_res,
5215 task) != 0)
5216 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005217 return;
5218out_no_action:
5219 task->tk_action = NULL;
5220out_wait:
5221 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222}
5223
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005224static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005225 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005226 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005227 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005228};
5229
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005230static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5231 struct nfs_open_context *ctx,
5232 struct nfs4_lock_state *lsp,
5233 struct nfs_seqid *seqid)
5234{
5235 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005236 struct rpc_message msg = {
5237 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5238 .rpc_cred = ctx->cred,
5239 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005240 struct rpc_task_setup task_setup_data = {
5241 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005242 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005243 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005244 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005245 .flags = RPC_TASK_ASYNC,
5246 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005247
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005248 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5249 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5250
Frank Filz137d6ac2007-07-09 15:32:29 -07005251 /* Ensure this is an unlock - when canceling a lock, the
5252 * canceled lock is passed in, and it won't be an unlock.
5253 */
5254 fl->fl_type = F_UNLCK;
5255
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005256 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5257 if (data == NULL) {
5258 nfs_free_seqid(seqid);
5259 return ERR_PTR(-ENOMEM);
5260 }
5261
Chuck Levera9c92d62013-08-09 12:48:18 -04005262 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005263 msg.rpc_argp = &data->arg;
5264 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005265 task_setup_data.callback_data = data;
5266 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005267}
5268
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5270{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005271 struct inode *inode = state->inode;
5272 struct nfs4_state_owner *sp = state->owner;
5273 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005274 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005275 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005276 struct rpc_task *task;
5277 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005278 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279
Trond Myklebust9b073572006-06-29 16:38:34 -04005280 status = nfs4_set_lock_state(state, request);
5281 /* Unlock _before_ we do the RPC call */
5282 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005283 /* Exclude nfs_delegation_claim_locks() */
5284 mutex_lock(&sp->so_delegreturn_mutex);
5285 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005286 down_read(&nfsi->rwsem);
5287 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
5288 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005289 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005290 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005291 }
5292 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005293 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005294 if (status != 0)
5295 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005296 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005297 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005298 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5299 goto out;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005300 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005301 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005302 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04005303 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005304 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005305 status = PTR_ERR(task);
5306 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005307 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005308 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005309 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005310out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005311 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005312 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005313 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314}
5315
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005316struct nfs4_lockdata {
5317 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005318 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005319 struct nfs4_lock_state *lsp;
5320 struct nfs_open_context *ctx;
5321 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005322 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005323 int rpc_status;
5324 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005325 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005326};
5327
5328static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005329 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5330 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005331{
5332 struct nfs4_lockdata *p;
5333 struct inode *inode = lsp->ls_state->inode;
5334 struct nfs_server *server = NFS_SERVER(inode);
5335
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005336 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005337 if (p == NULL)
5338 return NULL;
5339
5340 p->arg.fh = NFS_FH(inode);
5341 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005342 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005343 if (p->arg.open_seqid == NULL)
5344 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005345 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005346 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005347 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005348 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04005349 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005350 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005351 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005352 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005353 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005354 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005355 atomic_inc(&lsp->ls_count);
5356 p->ctx = get_nfs_open_context(ctx);
5357 memcpy(&p->fl, fl, sizeof(p->fl));
5358 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005359out_free_seqid:
5360 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005361out_free:
5362 kfree(p);
5363 return NULL;
5364}
5365
5366static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5367{
5368 struct nfs4_lockdata *data = calldata;
5369 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370
Harvey Harrison3110ff82008-05-02 13:42:44 -07005371 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005372 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005373 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005374 /* Do we need to do an open_to_lock_owner? */
5375 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005376 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005377 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005378 }
Trond Myklebust8e472f32013-04-20 01:30:53 -04005379 data->arg.open_stateid = &state->open_stateid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005380 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005381 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005382 } else
5383 data->arg.new_lock_owner = 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04005384 if (!nfs4_valid_open_stateid(state)) {
5385 data->rpc_status = -EBADF;
5386 task->tk_action = NULL;
5387 goto out_release_open_seqid;
5388 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005389 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005390 if (nfs4_setup_sequence(data->server,
5391 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005392 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005393 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04005394 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04005395out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005396 nfs_release_seqid(data->arg.open_seqid);
5397out_release_lock_seqid:
5398 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005399out_wait:
5400 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005401 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005402}
5403
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005404static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5405{
5406 struct nfs4_lockdata *data = calldata;
5407
Harvey Harrison3110ff82008-05-02 13:42:44 -07005408 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005409
Trond Myklebust14516c32010-07-31 14:29:06 -04005410 if (!nfs4_sequence_done(task, &data->res.seq_res))
5411 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04005412
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005413 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005414 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005415 if (data->rpc_status == 0)
5416 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
5417 else
5418 goto out;
5419 }
5420 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05005421 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005422 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04005423 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005424 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005425out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07005426 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005427}
5428
5429static void nfs4_lock_release(void *calldata)
5430{
5431 struct nfs4_lockdata *data = calldata;
5432
Harvey Harrison3110ff82008-05-02 13:42:44 -07005433 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005434 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005435 if (data->cancelled != 0) {
5436 struct rpc_task *task;
5437 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5438 data->arg.lock_seqid);
5439 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08005440 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005441 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005442 } else
5443 nfs_free_seqid(data->arg.lock_seqid);
5444 nfs4_put_lock_state(data->lsp);
5445 put_nfs_open_context(data->ctx);
5446 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005447 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005448}
5449
5450static const struct rpc_call_ops nfs4_lock_ops = {
5451 .rpc_call_prepare = nfs4_lock_prepare,
5452 .rpc_call_done = nfs4_lock_done,
5453 .rpc_release = nfs4_lock_release,
5454};
5455
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005456static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5457{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005458 switch (error) {
5459 case -NFS4ERR_ADMIN_REVOKED:
5460 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05005461 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005462 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04005463 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05005464 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005465 break;
5466 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005467 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05005468 case -NFS4ERR_EXPIRED:
5469 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005470 };
5471}
5472
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005473static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005474{
5475 struct nfs4_lockdata *data;
5476 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005477 struct rpc_message msg = {
5478 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5479 .rpc_cred = state->owner->so_cred,
5480 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005481 struct rpc_task_setup task_setup_data = {
5482 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005483 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005484 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005485 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005486 .flags = RPC_TASK_ASYNC,
5487 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005488 int ret;
5489
Harvey Harrison3110ff82008-05-02 13:42:44 -07005490 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005491 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005492 fl->fl_u.nfs4_fl.owner,
5493 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005494 if (data == NULL)
5495 return -ENOMEM;
5496 if (IS_SETLKW(cmd))
5497 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04005498 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005499 msg.rpc_argp = &data->arg;
5500 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005501 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005502 if (recovery_type > NFS_LOCK_NEW) {
5503 if (recovery_type == NFS_LOCK_RECLAIM)
5504 data->arg.reclaim = NFS_LOCK_RECLAIM;
5505 nfs4_set_sequence_privileged(&data->arg.seq_args);
5506 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04005507 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005508 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005509 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005510 ret = nfs4_wait_for_completion_rpc_task(task);
5511 if (ret == 0) {
5512 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005513 if (ret)
5514 nfs4_handle_setlk_error(data->server, data->lsp,
5515 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005516 } else
5517 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005518 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005519 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005520 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521}
5522
5523static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5524{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005525 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005526 struct nfs4_exception exception = {
5527 .inode = state->inode,
5528 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005529 int err;
5530
5531 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005532 /* Cache the lock if possible... */
5533 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5534 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005535 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005536 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
Trond Myklebust168667c2010-10-19 19:47:49 -04005537 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00005538 break;
5539 nfs4_handle_exception(server, err, &exception);
5540 } while (exception.retry);
5541 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542}
5543
5544static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5545{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005546 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005547 struct nfs4_exception exception = {
5548 .inode = state->inode,
5549 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005550 int err;
5551
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005552 err = nfs4_set_lock_state(state, request);
5553 if (err != 0)
5554 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04005555 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10005556 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
5557 return 0;
5558 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005559 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005560 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5561 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005562 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005563 trace_nfs4_lock_expired(request, state, F_SETLK, err);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005564 switch (err) {
5565 default:
5566 goto out;
5567 case -NFS4ERR_GRACE:
5568 case -NFS4ERR_DELAY:
5569 nfs4_handle_exception(server, err, &exception);
5570 err = 0;
5571 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005572 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005573out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00005574 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575}
5576
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005577#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005578/**
5579 * nfs41_check_expired_locks - possibly free a lock stateid
5580 *
5581 * @state: NFSv4 state for an inode
5582 *
5583 * Returns NFS_OK if recovery for this stateid is now finished.
5584 * Otherwise a negative NFS4ERR value is returned.
5585 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005586static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005587{
Chuck Levereb64cf92012-07-11 16:30:05 -04005588 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005589 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005590 struct nfs_server *server = NFS_SERVER(state->inode);
5591
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005592 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04005593 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005594 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
5595
5596 status = nfs41_test_stateid(server,
5597 &lsp->ls_stateid,
5598 cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04005599 trace_nfs4_test_lock_stateid(state, lsp, status);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005600 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005601 /* Free the stateid unless the server
5602 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04005603 if (status != -NFS4ERR_BAD_STATEID)
5604 nfs41_free_stateid(server,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005605 &lsp->ls_stateid,
5606 cred);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005607 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005608 ret = status;
5609 }
5610 }
5611 };
5612
5613 return ret;
5614}
5615
5616static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5617{
5618 int status = NFS_OK;
5619
5620 if (test_bit(LK_STATE_IN_USE, &state->flags))
5621 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005622 if (status != NFS_OK)
5623 status = nfs4_lock_expired(state, request);
5624 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005625}
5626#endif
5627
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5629{
Trond Myklebust9a99af492013-02-04 20:17:49 -05005630 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005631 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005632 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005633 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005634 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005636 if ((fl_flags & FL_POSIX) &&
5637 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5638 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005639 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005640 status = nfs4_set_lock_state(state, request);
5641 if (status != 0)
5642 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005643 request->fl_flags |= FL_ACCESS;
5644 status = do_vfs_lock(request->fl_file, request);
5645 if (status < 0)
5646 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005647 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005648 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005649 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005650 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005651 request->fl_flags = fl_flags & ~FL_SLEEP;
5652 status = do_vfs_lock(request->fl_file, request);
5653 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005654 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05005655 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5656 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005657 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005658 if (status != 0)
Trond Myklebust9a99af492013-02-04 20:17:49 -05005659 goto out;
5660 down_read(&nfsi->rwsem);
5661 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5662 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005663 goto out_unlock;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005664 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005665 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005666 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005667 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005668 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5669 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005670out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005671 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005672out:
5673 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674 return status;
5675}
5676
5677static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5678{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005679 struct nfs4_exception exception = {
5680 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005681 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005682 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683 int err;
5684
5685 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005686 err = _nfs4_proc_setlk(state, cmd, request);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005687 trace_nfs4_set_lock(request, state, cmd, err);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005688 if (err == -NFS4ERR_DENIED)
5689 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005691 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692 } while (exception.retry);
5693 return err;
5694}
5695
5696static int
5697nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5698{
5699 struct nfs_open_context *ctx;
5700 struct nfs4_state *state;
5701 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5702 int status;
5703
5704 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005705 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706 state = ctx->state;
5707
5708 if (request->fl_start < 0 || request->fl_end < 0)
5709 return -EINVAL;
5710
Trond Myklebustd9531262009-07-21 19:22:38 -04005711 if (IS_GETLK(cmd)) {
5712 if (state != NULL)
5713 return nfs4_proc_getlk(state, F_GETLK, request);
5714 return 0;
5715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716
5717 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5718 return -EINVAL;
5719
Trond Myklebustd9531262009-07-21 19:22:38 -04005720 if (request->fl_type == F_UNLCK) {
5721 if (state != NULL)
5722 return nfs4_proc_unlck(state, cmd, request);
5723 return 0;
5724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725
Trond Myklebustd9531262009-07-21 19:22:38 -04005726 if (state == NULL)
5727 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005728 /*
5729 * Don't rely on the VFS having checked the file open mode,
5730 * since it won't do this for flock() locks.
5731 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005732 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005733 case F_RDLCK:
5734 if (!(filp->f_mode & FMODE_READ))
5735 return -EBADF;
5736 break;
5737 case F_WRLCK:
5738 if (!(filp->f_mode & FMODE_WRITE))
5739 return -EBADF;
5740 }
5741
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742 do {
5743 status = nfs4_proc_setlk(state, cmd, request);
5744 if ((status != -EAGAIN) || IS_SETLK(cmd))
5745 break;
5746 timeout = nfs4_set_lock_task_retry(timeout);
5747 status = -ERESTARTSYS;
5748 if (signalled())
5749 break;
5750 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751 return status;
5752}
5753
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005754int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05005755{
5756 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05005757 int err;
5758
5759 err = nfs4_set_lock_state(state, fl);
5760 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005761 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04005762 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005763 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05005764}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005765
Trond Myklebustcf470c32012-03-07 13:49:12 -05005766struct nfs_release_lockowner_data {
5767 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005768 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005769 struct nfs_release_lockowner_args args;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005770 struct nfs4_sequence_args seq_args;
5771 struct nfs4_sequence_res seq_res;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005772};
5773
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005774static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
5775{
5776 struct nfs_release_lockowner_data *data = calldata;
5777 nfs40_setup_sequence(data->server,
5778 &data->seq_args, &data->seq_res, task);
5779}
5780
5781static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
5782{
5783 struct nfs_release_lockowner_data *data = calldata;
5784 nfs40_sequence_done(task, &data->seq_res);
5785}
5786
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005787static void nfs4_release_lockowner_release(void *calldata)
5788{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005789 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005790 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005791 kfree(calldata);
5792}
5793
Trond Myklebust17280172012-03-11 13:11:00 -04005794static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005795 .rpc_call_prepare = nfs4_release_lockowner_prepare,
5796 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005797 .rpc_release = nfs4_release_lockowner_release,
5798};
5799
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04005800static int nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005801{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005802 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005803 struct rpc_message msg = {
5804 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5805 };
5806
5807 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005808 return -EINVAL;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005809
Trond Myklebustcf470c32012-03-07 13:49:12 -05005810 data = kmalloc(sizeof(*data), GFP_NOFS);
5811 if (!data)
5812 return -ENOMEM;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005813 nfs4_init_sequence(&data->seq_args, &data->seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05005814 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005815 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005816 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5817 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5818 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005819
Trond Myklebustcf470c32012-03-07 13:49:12 -05005820 msg.rpc_argp = &data->args;
5821 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5822 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005823}
5824
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005825#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5826
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005827static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5828 const void *buf, size_t buflen,
5829 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005830{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005831 if (strcmp(key, "") != 0)
5832 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005833
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005834 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005835}
5836
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005837static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5838 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005839{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005840 if (strcmp(key, "") != 0)
5841 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005842
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005843 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005844}
5845
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005846static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5847 size_t list_len, const char *name,
5848 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005849{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005850 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005851
J. Bruce Fields096455a2006-03-20 23:23:42 -05005852 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5853 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005854
5855 if (list && len <= list_len)
5856 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005857 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005858}
5859
David Quigleyc9bccef2013-05-22 12:50:45 -04005860#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5861static inline int nfs4_server_supports_labels(struct nfs_server *server)
5862{
5863 return server->caps & NFS_CAP_SECURITY_LABEL;
5864}
5865
5866static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
5867 const void *buf, size_t buflen,
5868 int flags, int type)
5869{
5870 if (security_ismaclabel(key))
5871 return nfs4_set_security_label(dentry, buf, buflen);
5872
5873 return -EOPNOTSUPP;
5874}
5875
5876static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
5877 void *buf, size_t buflen, int type)
5878{
5879 if (security_ismaclabel(key))
5880 return nfs4_get_security_label(dentry->d_inode, buf, buflen);
5881 return -EOPNOTSUPP;
5882}
5883
5884static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
5885 size_t list_len, const char *name,
5886 size_t name_len, int type)
5887{
5888 size_t len = 0;
5889
5890 if (nfs_server_capable(dentry->d_inode, NFS_CAP_SECURITY_LABEL)) {
5891 len = security_inode_listsecurity(dentry->d_inode, NULL, 0);
5892 if (list && len <= list_len)
5893 security_inode_listsecurity(dentry->d_inode, list, len);
5894 }
5895 return len;
5896}
5897
5898static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
5899 .prefix = XATTR_SECURITY_PREFIX,
5900 .list = nfs4_xattr_list_nfs4_label,
5901 .get = nfs4_xattr_get_nfs4_label,
5902 .set = nfs4_xattr_set_nfs4_label,
5903};
5904#endif
5905
5906
Andy Adamson533eb462011-06-13 18:25:56 -04005907/*
5908 * nfs_fhget will use either the mounted_on_fileid or the fileid
5909 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005910static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5911{
Andy Adamson533eb462011-06-13 18:25:56 -04005912 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5913 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5914 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005915 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005916 return;
5917
5918 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005919 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005920 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5921 fattr->nlink = 2;
5922}
5923
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005924static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5925 const struct qstr *name,
5926 struct nfs4_fs_locations *fs_locations,
5927 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005928{
5929 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04005930 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005931 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005932 };
5933 struct nfs4_fs_locations_arg args = {
5934 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005935 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005936 .page = page,
5937 .bitmask = bitmask,
5938 };
Benny Halevy22958462009-04-01 09:22:02 -04005939 struct nfs4_fs_locations_res res = {
5940 .fs_locations = fs_locations,
5941 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005942 struct rpc_message msg = {
5943 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5944 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005945 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005946 };
5947 int status;
5948
Harvey Harrison3110ff82008-05-02 13:42:44 -07005949 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005950
5951 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5952 * is not supported */
5953 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5954 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5955 else
5956 bitmask[0] |= FATTR4_WORD0_FILEID;
5957
Trond Myklebustc228fd32007-01-13 02:28:11 -05005958 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005959 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005960 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005961 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005962 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005963 return status;
5964}
5965
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005966int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5967 const struct qstr *name,
5968 struct nfs4_fs_locations *fs_locations,
5969 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005970{
5971 struct nfs4_exception exception = { };
5972 int err;
5973 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04005974 err = _nfs4_proc_fs_locations(client, dir, name,
5975 fs_locations, page);
5976 trace_nfs4_get_fs_locations(dir, name, err);
5977 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005978 &exception);
5979 } while (exception.retry);
5980 return err;
5981}
5982
Andy Adamson5ec16a82013-08-08 10:57:55 -04005983/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04005984 * If 'use_integrity' is true and the state managment nfs_client
5985 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
5986 * and the machine credential as per RFC3530bis and RFC5661 Security
5987 * Considerations sections. Otherwise, just use the user cred with the
5988 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04005989 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04005990static 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 +00005991{
5992 int status;
5993 struct nfs4_secinfo_arg args = {
5994 .dir_fh = NFS_FH(dir),
5995 .name = name,
5996 };
5997 struct nfs4_secinfo_res res = {
5998 .flavors = flavors,
5999 };
6000 struct rpc_message msg = {
6001 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6002 .rpc_argp = &args,
6003 .rpc_resp = &res,
6004 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006005 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006006 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006007
6008 if (use_integrity) {
6009 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006010 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6011 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006012 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006013
6014 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006015
6016 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6017 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6018
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006019 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6020 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006021 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006022
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006023 if (cred)
6024 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006025
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006026 return status;
6027}
6028
Bryan Schumaker72de53e2012-04-27 13:27:40 -04006029int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6030 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006031{
6032 struct nfs4_exception exception = { };
6033 int err;
6034 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006035 err = -NFS4ERR_WRONGSEC;
6036
6037 /* try to use integrity protection with machine cred */
6038 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6039 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6040
6041 /*
6042 * if unable to use integrity protection, or SECINFO with
6043 * integrity protection returns NFS4ERR_WRONGSEC (which is
6044 * disallowed by spec, but exists in deployed servers) use
6045 * the current filesystem's rpc_client and the user cred.
6046 */
6047 if (err == -NFS4ERR_WRONGSEC)
6048 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6049
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006050 trace_nfs4_secinfo(dir, name, err);
6051 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006052 &exception);
6053 } while (exception.retry);
6054 return err;
6055}
6056
Andy Adamson557134a2009-04-01 09:21:53 -04006057#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04006058/*
Andy Adamson357f54d2010-12-14 10:11:57 -05006059 * Check the exchange flags returned by the server for invalid flags, having
6060 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6061 * DS flags set.
6062 */
6063static int nfs4_check_cl_exchange_flags(u32 flags)
6064{
6065 if (flags & ~EXCHGID4_FLAG_MASK_R)
6066 goto out_inval;
6067 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6068 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6069 goto out_inval;
6070 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6071 goto out_inval;
6072 return NFS_OK;
6073out_inval:
6074 return -NFS4ERR_INVAL;
6075}
6076
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006077static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006078nfs41_same_server_scope(struct nfs41_server_scope *a,
6079 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006080{
6081 if (a->server_scope_sz == b->server_scope_sz &&
6082 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6083 return true;
6084
6085 return false;
6086}
6087
Andy Adamson357f54d2010-12-14 10:11:57 -05006088/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006089 * nfs4_proc_bind_conn_to_session()
6090 *
6091 * The 4.1 client currently uses the same TCP connection for the
6092 * fore and backchannel.
6093 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006094int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006095{
6096 int status;
6097 struct nfs41_bind_conn_to_session_res res;
6098 struct rpc_message msg = {
6099 .rpc_proc =
6100 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
6101 .rpc_argp = clp,
6102 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006103 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006104 };
6105
6106 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006107
6108 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
6109 if (unlikely(res.session == NULL)) {
6110 status = -ENOMEM;
6111 goto out;
6112 }
6113
6114 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006115 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006116 if (status == 0) {
6117 if (memcmp(res.session->sess_id.data,
6118 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6119 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6120 status = -EIO;
6121 goto out_session;
6122 }
6123 if (res.dir != NFS4_CDFS4_BOTH) {
6124 dprintk("NFS: %s: Unexpected direction from server\n",
6125 __func__);
6126 status = -EIO;
6127 goto out_session;
6128 }
6129 if (res.use_conn_in_rdma_mode) {
6130 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6131 __func__);
6132 status = -EIO;
6133 goto out_session;
6134 }
6135 }
6136out_session:
6137 kfree(res.session);
6138out:
6139 dprintk("<-- %s status= %d\n", __func__, status);
6140 return status;
6141}
6142
6143/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006144 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6145 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04006146 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006147static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
6148 .how = SP4_MACH_CRED,
6149 .enforce.u.words = {
6150 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6151 1 << (OP_EXCHANGE_ID - 32) |
6152 1 << (OP_CREATE_SESSION - 32) |
6153 1 << (OP_DESTROY_SESSION - 32) |
6154 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006155 },
6156 .allow.u.words = {
6157 [0] = 1 << (OP_CLOSE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006158 1 << (OP_LOCKU) |
6159 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006160 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006161 1 << (OP_SECINFO_NO_NAME - 32) |
6162 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006163 1 << (OP_FREE_STATEID - 32) |
6164 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006165 }
6166};
6167
6168/*
6169 * Select the state protection mode for client `clp' given the server results
6170 * from exchange_id in `sp'.
6171 *
6172 * Returns 0 on success, negative errno otherwise.
6173 */
6174static int nfs4_sp4_select_mode(struct nfs_client *clp,
6175 struct nfs41_state_protection *sp)
6176{
6177 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
6178 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6179 1 << (OP_EXCHANGE_ID - 32) |
6180 1 << (OP_CREATE_SESSION - 32) |
6181 1 << (OP_DESTROY_SESSION - 32) |
6182 1 << (OP_DESTROY_CLIENTID - 32)
6183 };
6184 unsigned int i;
6185
6186 if (sp->how == SP4_MACH_CRED) {
6187 /* Print state protect result */
6188 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
6189 for (i = 0; i <= LAST_NFS4_OP; i++) {
6190 if (test_bit(i, sp->enforce.u.longs))
6191 dfprintk(MOUNT, " enforce op %d\n", i);
6192 if (test_bit(i, sp->allow.u.longs))
6193 dfprintk(MOUNT, " allow op %d\n", i);
6194 }
6195
6196 /* make sure nothing is on enforce list that isn't supported */
6197 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
6198 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
6199 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6200 return -EINVAL;
6201 }
6202 }
6203
6204 /*
6205 * Minimal mode - state operations are allowed to use machine
6206 * credential. Note this already happens by default, so the
6207 * client doesn't have to do anything more than the negotiation.
6208 *
6209 * NOTE: we don't care if EXCHANGE_ID is in the list -
6210 * we're already using the machine cred for exchange_id
6211 * and will never use a different cred.
6212 */
6213 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
6214 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
6215 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
6216 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
6217 dfprintk(MOUNT, "sp4_mach_cred:\n");
6218 dfprintk(MOUNT, " minimal mode enabled\n");
6219 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
6220 } else {
6221 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6222 return -EINVAL;
6223 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006224
6225 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
6226 test_bit(OP_LOCKU, sp->allow.u.longs)) {
6227 dfprintk(MOUNT, " cleanup mode enabled\n");
6228 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
6229 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006230
6231 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
6232 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
6233 dfprintk(MOUNT, " secinfo mode enabled\n");
6234 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
6235 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006236
6237 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
6238 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
6239 dfprintk(MOUNT, " stateid mode enabled\n");
6240 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
6241 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04006242
6243 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
6244 dfprintk(MOUNT, " write mode enabled\n");
6245 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
6246 }
6247
6248 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
6249 dfprintk(MOUNT, " commit mode enabled\n");
6250 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
6251 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006252 }
6253
6254 return 0;
6255}
6256
6257/*
6258 * _nfs4_proc_exchange_id()
6259 *
6260 * Wrapper for EXCHANGE_ID operation.
6261 */
6262static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
6263 u32 sp4_how)
Benny Halevy99fe60d2009-04-01 09:22:29 -04006264{
6265 nfs4_verifier verifier;
6266 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05006267 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04006268 .client = clp,
Trond Myklebust4f0b4292013-05-20 12:24:03 -04006269 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6270 EXCHGID4_FLAG_BIND_PRINC_STATEID,
Benny Halevy99fe60d2009-04-01 09:22:29 -04006271 };
6272 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04006273 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04006274 };
6275 int status;
6276 struct rpc_message msg = {
6277 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
6278 .rpc_argp = &args,
6279 .rpc_resp = &res,
6280 .rpc_cred = cred,
6281 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04006282
Chuck Leverf0920752012-05-21 22:45:41 -04006283 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04006284 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
6285 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006286 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
6287 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6288 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006289
Chuck Leveracdeb692012-05-21 22:46:16 -04006290 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006291 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04006292 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006293 status = -ENOMEM;
6294 goto out;
6295 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006296
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006297 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006298 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006299 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04006300 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04006301 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04006302 }
6303
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006304 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006305 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006306 status = -ENOMEM;
6307 goto out_server_scope;
6308 }
6309
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006310 switch (sp4_how) {
6311 case SP4_NONE:
6312 args.state_protect.how = SP4_NONE;
6313 break;
6314
6315 case SP4_MACH_CRED:
6316 args.state_protect = nfs4_sp4_mach_cred_request;
6317 break;
6318
6319 default:
6320 /* unsupported! */
6321 WARN_ON_ONCE(1);
6322 status = -EINVAL;
6323 goto out_server_scope;
6324 }
6325
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006326 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006327 trace_nfs4_exchange_id(clp, status);
Chuck Lever177313f2012-05-21 22:44:58 -04006328 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04006329 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006330
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006331 if (status == 0)
6332 status = nfs4_sp4_select_mode(clp, &res.state_protect);
6333
Chuck Lever177313f2012-05-21 22:44:58 -04006334 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04006335 clp->cl_clientid = res.clientid;
6336 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
6337 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
6338 clp->cl_seqid = res.seqid;
6339
Chuck Leveracdeb692012-05-21 22:46:16 -04006340 kfree(clp->cl_serverowner);
6341 clp->cl_serverowner = res.server_owner;
6342 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04006343
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006344 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04006345 kfree(clp->cl_implid);
6346 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006347
Chuck Lever177313f2012-05-21 22:44:58 -04006348 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006349 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006350 res.server_scope)) {
6351 dprintk("%s: server_scope mismatch detected\n",
6352 __func__);
6353 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006354 kfree(clp->cl_serverscope);
6355 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006356 }
6357
Chuck Lever177313f2012-05-21 22:44:58 -04006358 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006359 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006360 goto out;
6361 }
Trond Myklebust32b01312012-05-26 13:41:04 -04006362 } else
6363 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006364
Chuck Leveracdeb692012-05-21 22:46:16 -04006365out_server_owner:
6366 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006367out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006368 kfree(res.server_scope);
6369out:
Chuck Lever177313f2012-05-21 22:44:58 -04006370 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006371 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006372 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006373 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04006374 clp->cl_implid->date.seconds,
6375 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006376 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006377 return status;
6378}
6379
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006380/*
6381 * nfs4_proc_exchange_id()
6382 *
6383 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6384 *
6385 * Since the clientid has expired, all compounds using sessions
6386 * associated with the stale clientid will be returning
6387 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
6388 * be in some phase of session reset.
6389 *
6390 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
6391 */
6392int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
6393{
6394 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
6395 int status;
6396
6397 /* try SP4_MACH_CRED if krb5i/p */
6398 if (authflavor == RPC_AUTH_GSS_KRB5I ||
6399 authflavor == RPC_AUTH_GSS_KRB5P) {
6400 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
6401 if (!status)
6402 return 0;
6403 }
6404
6405 /* try SP4_NONE */
6406 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
6407}
6408
Trond Myklebust66245532012-05-25 17:18:09 -04006409static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
6410 struct rpc_cred *cred)
6411{
6412 struct rpc_message msg = {
6413 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
6414 .rpc_argp = clp,
6415 .rpc_cred = cred,
6416 };
6417 int status;
6418
6419 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006420 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04006421 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04006422 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04006423 "DESTROY_CLIENTID.", status, clp->cl_hostname);
6424 return status;
6425}
6426
6427static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
6428 struct rpc_cred *cred)
6429{
6430 unsigned int loop;
6431 int ret;
6432
6433 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
6434 ret = _nfs4_proc_destroy_clientid(clp, cred);
6435 switch (ret) {
6436 case -NFS4ERR_DELAY:
6437 case -NFS4ERR_CLIENTID_BUSY:
6438 ssleep(1);
6439 break;
6440 default:
6441 return ret;
6442 }
6443 }
6444 return 0;
6445}
6446
6447int nfs4_destroy_clientid(struct nfs_client *clp)
6448{
6449 struct rpc_cred *cred;
6450 int ret = 0;
6451
6452 if (clp->cl_mvops->minor_version < 1)
6453 goto out;
6454 if (clp->cl_exchange_flags == 0)
6455 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04006456 if (clp->cl_preserve_clid)
6457 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04006458 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04006459 ret = nfs4_proc_destroy_clientid(clp, cred);
6460 if (cred)
6461 put_rpccred(cred);
6462 switch (ret) {
6463 case 0:
6464 case -NFS4ERR_STALE_CLIENTID:
6465 clp->cl_exchange_flags = 0;
6466 }
6467out:
6468 return ret;
6469}
6470
Andy Adamson2050f0c2009-04-01 09:22:30 -04006471struct nfs4_get_lease_time_data {
6472 struct nfs4_get_lease_time_args *args;
6473 struct nfs4_get_lease_time_res *res;
6474 struct nfs_client *clp;
6475};
6476
6477static void nfs4_get_lease_time_prepare(struct rpc_task *task,
6478 void *calldata)
6479{
Andy Adamson2050f0c2009-04-01 09:22:30 -04006480 struct nfs4_get_lease_time_data *data =
6481 (struct nfs4_get_lease_time_data *)calldata;
6482
6483 dprintk("--> %s\n", __func__);
6484 /* just setup sequence, do not trigger session recovery
6485 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006486 nfs41_setup_sequence(data->clp->cl_session,
6487 &data->args->la_seq_args,
6488 &data->res->lr_seq_res,
6489 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006490 dprintk("<-- %s\n", __func__);
6491}
6492
6493/*
6494 * Called from nfs4_state_manager thread for session setup, so don't recover
6495 * from sequence operation or clientid errors.
6496 */
6497static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
6498{
6499 struct nfs4_get_lease_time_data *data =
6500 (struct nfs4_get_lease_time_data *)calldata;
6501
6502 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006503 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
6504 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04006505 switch (task->tk_status) {
6506 case -NFS4ERR_DELAY:
6507 case -NFS4ERR_GRACE:
6508 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
6509 rpc_delay(task, NFS4_POLL_RETRY_MIN);
6510 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006511 /* fall through */
6512 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006513 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006514 return;
6515 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04006516 dprintk("<-- %s\n", __func__);
6517}
6518
Trond Myklebust17280172012-03-11 13:11:00 -04006519static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04006520 .rpc_call_prepare = nfs4_get_lease_time_prepare,
6521 .rpc_call_done = nfs4_get_lease_time_done,
6522};
6523
6524int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
6525{
6526 struct rpc_task *task;
6527 struct nfs4_get_lease_time_args args;
6528 struct nfs4_get_lease_time_res res = {
6529 .lr_fsinfo = fsinfo,
6530 };
6531 struct nfs4_get_lease_time_data data = {
6532 .args = &args,
6533 .res = &res,
6534 .clp = clp,
6535 };
6536 struct rpc_message msg = {
6537 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
6538 .rpc_argp = &args,
6539 .rpc_resp = &res,
6540 };
6541 struct rpc_task_setup task_setup = {
6542 .rpc_client = clp->cl_rpcclient,
6543 .rpc_message = &msg,
6544 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006545 .callback_data = &data,
6546 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04006547 };
6548 int status;
6549
Chuck Levera9c92d62013-08-09 12:48:18 -04006550 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006551 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006552 dprintk("--> %s\n", __func__);
6553 task = rpc_run_task(&task_setup);
6554
6555 if (IS_ERR(task))
6556 status = PTR_ERR(task);
6557 else {
6558 status = task->tk_status;
6559 rpc_put_task(task);
6560 }
6561 dprintk("<-- %s return %d\n", __func__, status);
6562
6563 return status;
6564}
6565
Andy Adamsonfc931582009-04-01 09:22:31 -04006566/*
6567 * Initialize the values to be used by the client in CREATE_SESSION
6568 * If nfs4_init_session set the fore channel request and response sizes,
6569 * use them.
6570 *
6571 * Set the back channel max_resp_sz_cached to zero to force the client to
6572 * always set csa_cachethis to FALSE because the current implementation
6573 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
6574 */
6575static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
6576{
Andy Adamson18aad3d2013-06-26 12:21:49 -04006577 unsigned int max_rqst_sz, max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006578
Andy Adamson18aad3d2013-06-26 12:21:49 -04006579 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
6580 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
6581
Andy Adamsonfc931582009-04-01 09:22:31 -04006582 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04006583 args->fc_attrs.max_rqst_sz = max_rqst_sz;
6584 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006585 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05006586 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04006587
6588 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05006589 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04006590 __func__,
6591 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05006592 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04006593
6594 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04006595 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
6596 args->bc_attrs.max_resp_sz = PAGE_SIZE;
6597 args->bc_attrs.max_resp_sz_cached = 0;
6598 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
6599 args->bc_attrs.max_reqs = 1;
6600
6601 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
6602 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
6603 __func__,
6604 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
6605 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
6606 args->bc_attrs.max_reqs);
6607}
6608
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006609static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04006610{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006611 struct nfs4_channel_attrs *sent = &args->fc_attrs;
6612 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
6613
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006614 if (rcvd->max_resp_sz > sent->max_resp_sz)
6615 return -EINVAL;
6616 /*
6617 * Our requested max_ops is the minimum we need; we're not
6618 * prepared to break up compounds into smaller pieces than that.
6619 * So, no point even trying to continue if the server won't
6620 * cooperate:
6621 */
6622 if (rcvd->max_ops < sent->max_ops)
6623 return -EINVAL;
6624 if (rcvd->max_reqs == 0)
6625 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006626 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
6627 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006628 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04006629}
6630
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006631static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
6632{
6633 struct nfs4_channel_attrs *sent = &args->bc_attrs;
6634 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04006635
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006636 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
6637 return -EINVAL;
6638 if (rcvd->max_resp_sz < sent->max_resp_sz)
6639 return -EINVAL;
6640 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
6641 return -EINVAL;
6642 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006643 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006644 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006645 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006646 return -EINVAL;
6647 return 0;
6648}
Andy Adamson8d353012009-04-01 09:22:32 -04006649
Andy Adamson8d353012009-04-01 09:22:32 -04006650static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
6651 struct nfs4_session *session)
6652{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006653 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04006654
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006655 ret = nfs4_verify_fore_channel_attrs(args, session);
6656 if (ret)
6657 return ret;
6658 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04006659}
6660
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006661static int _nfs4_proc_create_session(struct nfs_client *clp,
6662 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04006663{
6664 struct nfs4_session *session = clp->cl_session;
6665 struct nfs41_create_session_args args = {
6666 .client = clp,
6667 .cb_program = NFS4_CALLBACK,
6668 };
6669 struct nfs41_create_session_res res = {
6670 .client = clp,
6671 };
6672 struct rpc_message msg = {
6673 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
6674 .rpc_argp = &args,
6675 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006676 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04006677 };
6678 int status;
6679
6680 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04006681 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04006682
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006683 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006684 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04006685
Trond Myklebust43095d32012-11-20 11:13:12 -05006686 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04006687 /* Verify the session's negotiated channel_attrs values */
6688 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04006689 /* Increment the clientid slot sequence id */
6690 clp->cl_seqid++;
6691 }
6692
6693 return status;
6694}
6695
6696/*
6697 * Issues a CREATE_SESSION operation to the server.
6698 * It is the responsibility of the caller to verify the session is
6699 * expired before calling this routine.
6700 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006701int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04006702{
6703 int status;
6704 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04006705 struct nfs4_session *session = clp->cl_session;
6706
6707 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
6708
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006709 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04006710 if (status)
6711 goto out;
6712
Andy Adamsonaacd5532011-11-09 13:58:21 -05006713 /* Init or reset the session slot tables */
6714 status = nfs4_setup_session_slot_tables(session);
6715 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04006716 if (status)
6717 goto out;
6718
6719 ptr = (unsigned *)&session->sess_id.data[0];
6720 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
6721 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04006722out:
6723 dprintk("<-- %s\n", __func__);
6724 return status;
6725}
6726
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006727/*
6728 * Issue the over-the-wire RPC DESTROY_SESSION.
6729 * The caller must serialize access to this routine.
6730 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006731int nfs4_proc_destroy_session(struct nfs4_session *session,
6732 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006733{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006734 struct rpc_message msg = {
6735 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
6736 .rpc_argp = session,
6737 .rpc_cred = cred,
6738 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006739 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006740
6741 dprintk("--> nfs4_proc_destroy_session\n");
6742
6743 /* session is still being setup */
6744 if (session->clp->cl_cons_state != NFS_CS_READY)
6745 return status;
6746
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006747 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006748 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006749
6750 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04006751 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006752 "Session has been destroyed regardless...\n", status);
6753
6754 dprintk("<-- nfs4_proc_destroy_session\n");
6755 return status;
6756}
6757
Trond Myklebust7b38c362012-05-23 13:23:31 -04006758/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006759 * Renew the cl_session lease.
6760 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006761struct nfs4_sequence_data {
6762 struct nfs_client *clp;
6763 struct nfs4_sequence_args args;
6764 struct nfs4_sequence_res res;
6765};
6766
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006767static void nfs41_sequence_release(void *data)
6768{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006769 struct nfs4_sequence_data *calldata = data;
6770 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006771
Alexandros Batsakis71358402010-02-05 03:45:05 -08006772 if (atomic_read(&clp->cl_count) > 1)
6773 nfs4_schedule_state_renewal(clp);
6774 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006775 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006776}
6777
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006778static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6779{
6780 switch(task->tk_status) {
6781 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006782 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6783 return -EAGAIN;
6784 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006785 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006786 }
6787 return 0;
6788}
6789
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006790static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006791{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006792 struct nfs4_sequence_data *calldata = data;
6793 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006794
Trond Myklebust14516c32010-07-31 14:29:06 -04006795 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6796 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006797
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006798 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006799 if (task->tk_status < 0) {
6800 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006801 if (atomic_read(&clp->cl_count) == 1)
6802 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006803
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006804 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6805 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006806 return;
6807 }
6808 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006809 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006810out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006811 dprintk("<-- %s\n", __func__);
6812}
6813
6814static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6815{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006816 struct nfs4_sequence_data *calldata = data;
6817 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006818 struct nfs4_sequence_args *args;
6819 struct nfs4_sequence_res *res;
6820
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006821 args = task->tk_msg.rpc_argp;
6822 res = task->tk_msg.rpc_resp;
6823
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006824 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006825}
6826
6827static const struct rpc_call_ops nfs41_sequence_ops = {
6828 .rpc_call_done = nfs41_sequence_call_done,
6829 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006830 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006831};
6832
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006833static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
6834 struct rpc_cred *cred,
6835 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006836{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006837 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006838 struct rpc_message msg = {
6839 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6840 .rpc_cred = cred,
6841 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006842 struct rpc_task_setup task_setup_data = {
6843 .rpc_client = clp->cl_rpcclient,
6844 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006845 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04006846 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006847 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006848
Alexandros Batsakis71358402010-02-05 03:45:05 -08006849 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006850 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006851 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006852 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006853 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006854 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006855 }
Chuck Levera9c92d62013-08-09 12:48:18 -04006856 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006857 if (is_privileged)
6858 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006859 msg.rpc_argp = &calldata->args;
6860 msg.rpc_resp = &calldata->res;
6861 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006862 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006863
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006864 return rpc_run_task(&task_setup_data);
6865}
6866
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006867static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006868{
6869 struct rpc_task *task;
6870 int ret = 0;
6871
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006872 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6873 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006874 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006875 if (IS_ERR(task))
6876 ret = PTR_ERR(task);
6877 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006878 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006879 dprintk("<-- %s status=%d\n", __func__, ret);
6880 return ret;
6881}
6882
6883static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6884{
6885 struct rpc_task *task;
6886 int ret;
6887
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006888 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006889 if (IS_ERR(task)) {
6890 ret = PTR_ERR(task);
6891 goto out;
6892 }
6893 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006894 if (!ret) {
6895 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6896
6897 if (task->tk_status == 0)
6898 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006899 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006900 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006901 rpc_put_task(task);
6902out:
6903 dprintk("<-- %s status=%d\n", __func__, ret);
6904 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006905}
6906
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006907struct nfs4_reclaim_complete_data {
6908 struct nfs_client *clp;
6909 struct nfs41_reclaim_complete_args arg;
6910 struct nfs41_reclaim_complete_res res;
6911};
6912
6913static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6914{
6915 struct nfs4_reclaim_complete_data *calldata = data;
6916
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006917 nfs41_setup_sequence(calldata->clp->cl_session,
6918 &calldata->arg.seq_args,
6919 &calldata->res.seq_res,
6920 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006921}
6922
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006923static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6924{
6925 switch(task->tk_status) {
6926 case 0:
6927 case -NFS4ERR_COMPLETE_ALREADY:
6928 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6929 break;
6930 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006931 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006932 /* fall through */
6933 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006934 return -EAGAIN;
6935 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006936 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006937 }
6938 return 0;
6939}
6940
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006941static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6942{
6943 struct nfs4_reclaim_complete_data *calldata = data;
6944 struct nfs_client *clp = calldata->clp;
6945 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6946
6947 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006948 if (!nfs41_sequence_done(task, res))
6949 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006950
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006951 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006952 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6953 rpc_restart_call_prepare(task);
6954 return;
6955 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006956 dprintk("<-- %s\n", __func__);
6957}
6958
6959static void nfs4_free_reclaim_complete_data(void *data)
6960{
6961 struct nfs4_reclaim_complete_data *calldata = data;
6962
6963 kfree(calldata);
6964}
6965
6966static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6967 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6968 .rpc_call_done = nfs4_reclaim_complete_done,
6969 .rpc_release = nfs4_free_reclaim_complete_data,
6970};
6971
6972/*
6973 * Issue a global reclaim complete.
6974 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04006975static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
6976 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006977{
6978 struct nfs4_reclaim_complete_data *calldata;
6979 struct rpc_task *task;
6980 struct rpc_message msg = {
6981 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04006982 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006983 };
6984 struct rpc_task_setup task_setup_data = {
6985 .rpc_client = clp->cl_rpcclient,
6986 .rpc_message = &msg,
6987 .callback_ops = &nfs4_reclaim_complete_call_ops,
6988 .flags = RPC_TASK_ASYNC,
6989 };
6990 int status = -ENOMEM;
6991
6992 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006993 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006994 if (calldata == NULL)
6995 goto out;
6996 calldata->clp = clp;
6997 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006998
Chuck Levera9c92d62013-08-09 12:48:18 -04006999 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007000 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007001 msg.rpc_argp = &calldata->arg;
7002 msg.rpc_resp = &calldata->res;
7003 task_setup_data.callback_data = calldata;
7004 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007005 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007006 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007007 goto out;
7008 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05007009 status = nfs4_wait_for_completion_rpc_task(task);
7010 if (status == 0)
7011 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007012 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007013 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007014out:
7015 dprintk("<-- %s status=%d\n", __func__, status);
7016 return status;
7017}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007018
7019static void
7020nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
7021{
7022 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00007023 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007024 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007025
7026 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00007027 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
7028 * right now covering the LAYOUTGET we are about to send.
7029 * However, that is not so catastrophic, and there seems
7030 * to be no way to prevent it completely.
7031 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007032 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05007033 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007034 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00007035 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
7036 NFS_I(lgp->args.inode)->layout,
7037 lgp->args.ctx->state)) {
7038 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00007039 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007040}
7041
7042static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
7043{
7044 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07007045 struct inode *inode = lgp->args.inode;
7046 struct nfs_server *server = NFS_SERVER(inode);
7047 struct pnfs_layout_hdr *lo;
7048 struct nfs4_state *state = NULL;
Weston Andros Adamson30005122013-02-28 20:30:10 -05007049 unsigned long timeo, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007050
7051 dprintk("--> %s\n", __func__);
7052
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007053 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07007054 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007055
7056 switch (task->tk_status) {
7057 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07007058 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007059 case -NFS4ERR_LAYOUTTRYLATER:
7060 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05007061 timeo = rpc_get_timeout(task->tk_client);
7062 giveup = lgp->args.timestamp + timeo;
7063 if (time_after(giveup, jiffies))
7064 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07007065 break;
7066 case -NFS4ERR_EXPIRED:
7067 case -NFS4ERR_BAD_STATEID:
7068 spin_lock(&inode->i_lock);
7069 lo = NFS_I(inode)->layout;
7070 if (!lo || list_empty(&lo->plh_segs)) {
7071 spin_unlock(&inode->i_lock);
7072 /* If the open stateid was bad, then recover it. */
7073 state = lgp->args.ctx->state;
7074 } else {
7075 LIST_HEAD(head);
7076
7077 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
7078 spin_unlock(&inode->i_lock);
7079 /* Mark the bad layout state as invalid, then
7080 * retry using the open stateid. */
7081 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007082 }
7083 }
Trond Myklebustee314c22012-10-01 17:25:48 -07007084 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
7085 rpc_restart_call_prepare(task);
7086out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007087 dprintk("<-- %s\n", __func__);
7088}
7089
Idan Kedar85541162012-08-02 11:47:10 +03007090static size_t max_response_pages(struct nfs_server *server)
7091{
7092 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
7093 return nfs_page_array_len(0, max_resp_sz);
7094}
7095
7096static void nfs4_free_pages(struct page **pages, size_t size)
7097{
7098 int i;
7099
7100 if (!pages)
7101 return;
7102
7103 for (i = 0; i < size; i++) {
7104 if (!pages[i])
7105 break;
7106 __free_page(pages[i]);
7107 }
7108 kfree(pages);
7109}
7110
7111static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
7112{
7113 struct page **pages;
7114 int i;
7115
7116 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
7117 if (!pages) {
7118 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
7119 return NULL;
7120 }
7121
7122 for (i = 0; i < size; i++) {
7123 pages[i] = alloc_page(gfp_flags);
7124 if (!pages[i]) {
7125 dprintk("%s: failed to allocate page\n", __func__);
7126 nfs4_free_pages(pages, size);
7127 return NULL;
7128 }
7129 }
7130
7131 return pages;
7132}
7133
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007134static void nfs4_layoutget_release(void *calldata)
7135{
7136 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007137 struct inode *inode = lgp->args.inode;
7138 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007139 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007140
7141 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03007142 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007143 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007144 put_nfs_open_context(lgp->args.ctx);
7145 kfree(calldata);
7146 dprintk("<-- %s\n", __func__);
7147}
7148
7149static const struct rpc_call_ops nfs4_layoutget_call_ops = {
7150 .rpc_call_prepare = nfs4_layoutget_prepare,
7151 .rpc_call_done = nfs4_layoutget_done,
7152 .rpc_release = nfs4_layoutget_release,
7153};
7154
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007155struct pnfs_layout_segment *
7156nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007157{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007158 struct inode *inode = lgp->args.inode;
7159 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007160 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007161 struct rpc_task *task;
7162 struct rpc_message msg = {
7163 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
7164 .rpc_argp = &lgp->args,
7165 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04007166 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007167 };
7168 struct rpc_task_setup task_setup_data = {
7169 .rpc_client = server->client,
7170 .rpc_message = &msg,
7171 .callback_ops = &nfs4_layoutget_call_ops,
7172 .callback_data = lgp,
7173 .flags = RPC_TASK_ASYNC,
7174 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007175 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007176 int status = 0;
7177
7178 dprintk("--> %s\n", __func__);
7179
Idan Kedar85541162012-08-02 11:47:10 +03007180 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
7181 if (!lgp->args.layout.pages) {
7182 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007183 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03007184 }
7185 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05007186 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03007187
Weston Andros Adamson35124a02011-03-24 16:48:21 -04007188 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007189 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04007190 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007191
7192 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
7193 pnfs_get_layout_hdr(NFS_I(inode)->layout);
7194
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007195 task = rpc_run_task(&task_setup_data);
7196 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007197 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007198 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00007199 if (status == 0)
7200 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007201 trace_nfs4_layoutget(lgp->args.ctx,
7202 &lgp->args.range,
7203 &lgp->res.range,
7204 status);
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05007205 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
7206 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007207 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007208 rpc_put_task(task);
7209 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007210 if (status)
7211 return ERR_PTR(status);
7212 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007213}
7214
Benny Halevycbe82602011-05-22 19:52:37 +03007215static void
7216nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
7217{
7218 struct nfs4_layoutreturn *lrp = calldata;
7219
7220 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007221 nfs41_setup_sequence(lrp->clp->cl_session,
7222 &lrp->args.seq_args,
7223 &lrp->res.seq_res,
7224 task);
Benny Halevycbe82602011-05-22 19:52:37 +03007225}
7226
7227static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
7228{
7229 struct nfs4_layoutreturn *lrp = calldata;
7230 struct nfs_server *server;
7231
7232 dprintk("--> %s\n", __func__);
7233
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007234 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03007235 return;
7236
7237 server = NFS_SERVER(lrp->args.inode);
7238 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007239 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03007240 return;
7241 }
Benny Halevycbe82602011-05-22 19:52:37 +03007242 dprintk("<-- %s\n", __func__);
7243}
7244
7245static void nfs4_layoutreturn_release(void *calldata)
7246{
7247 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04007248 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03007249
7250 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04007251 spin_lock(&lo->plh_inode->i_lock);
7252 if (lrp->res.lrs_present)
7253 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
7254 lo->plh_block_lgets--;
7255 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04007256 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03007257 kfree(calldata);
7258 dprintk("<-- %s\n", __func__);
7259}
7260
7261static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
7262 .rpc_call_prepare = nfs4_layoutreturn_prepare,
7263 .rpc_call_done = nfs4_layoutreturn_done,
7264 .rpc_release = nfs4_layoutreturn_release,
7265};
7266
7267int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
7268{
7269 struct rpc_task *task;
7270 struct rpc_message msg = {
7271 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
7272 .rpc_argp = &lrp->args,
7273 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04007274 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03007275 };
7276 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04007277 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03007278 .rpc_message = &msg,
7279 .callback_ops = &nfs4_layoutreturn_call_ops,
7280 .callback_data = lrp,
7281 };
7282 int status;
7283
7284 dprintk("--> %s\n", __func__);
Chuck Levera9c92d62013-08-09 12:48:18 -04007285 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03007286 task = rpc_run_task(&task_setup_data);
7287 if (IS_ERR(task))
7288 return PTR_ERR(task);
7289 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007290 trace_nfs4_layoutreturn(lrp->args.inode, status);
Benny Halevycbe82602011-05-22 19:52:37 +03007291 dprintk("<-- %s status=%d\n", __func__, status);
7292 rpc_put_task(task);
7293 return status;
7294}
7295
Andy Adamson7f11d8d2011-07-30 20:52:35 -04007296/*
7297 * Retrieve the list of Data Server devices from the MDS.
7298 */
7299static int _nfs4_getdevicelist(struct nfs_server *server,
7300 const struct nfs_fh *fh,
7301 struct pnfs_devicelist *devlist)
7302{
7303 struct nfs4_getdevicelist_args args = {
7304 .fh = fh,
7305 .layoutclass = server->pnfs_curr_ld->id,
7306 };
7307 struct nfs4_getdevicelist_res res = {
7308 .devlist = devlist,
7309 };
7310 struct rpc_message msg = {
7311 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
7312 .rpc_argp = &args,
7313 .rpc_resp = &res,
7314 };
7315 int status;
7316
7317 dprintk("--> %s\n", __func__);
7318 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
7319 &res.seq_res, 0);
7320 dprintk("<-- %s status=%d\n", __func__, status);
7321 return status;
7322}
7323
7324int nfs4_proc_getdevicelist(struct nfs_server *server,
7325 const struct nfs_fh *fh,
7326 struct pnfs_devicelist *devlist)
7327{
7328 struct nfs4_exception exception = { };
7329 int err;
7330
7331 do {
7332 err = nfs4_handle_exception(server,
7333 _nfs4_getdevicelist(server, fh, devlist),
7334 &exception);
7335 } while (exception.retry);
7336
7337 dprintk("%s: err=%d, num_devs=%u\n", __func__,
7338 err, devlist->num_devs);
7339
7340 return err;
7341}
7342EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
7343
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007344static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007345_nfs4_proc_getdeviceinfo(struct nfs_server *server,
7346 struct pnfs_device *pdev,
7347 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007348{
7349 struct nfs4_getdeviceinfo_args args = {
7350 .pdev = pdev,
7351 };
7352 struct nfs4_getdeviceinfo_res res = {
7353 .pdev = pdev,
7354 };
7355 struct rpc_message msg = {
7356 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
7357 .rpc_argp = &args,
7358 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007359 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007360 };
7361 int status;
7362
7363 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00007364 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007365 dprintk("<-- %s status=%d\n", __func__, status);
7366
7367 return status;
7368}
7369
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007370int nfs4_proc_getdeviceinfo(struct nfs_server *server,
7371 struct pnfs_device *pdev,
7372 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007373{
7374 struct nfs4_exception exception = { };
7375 int err;
7376
7377 do {
7378 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007379 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007380 &exception);
7381 } while (exception.retry);
7382 return err;
7383}
7384EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
7385
Andy Adamson863a3c62011-03-23 13:27:54 +00007386static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
7387{
7388 struct nfs4_layoutcommit_data *data = calldata;
7389 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007390 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00007391
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007392 nfs41_setup_sequence(session,
7393 &data->args.seq_args,
7394 &data->res.seq_res,
7395 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00007396}
7397
7398static void
7399nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
7400{
7401 struct nfs4_layoutcommit_data *data = calldata;
7402 struct nfs_server *server = NFS_SERVER(data->args.inode);
7403
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007404 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00007405 return;
7406
7407 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007408 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
7409 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
7410 case -NFS4ERR_BADLAYOUT: /* no layout */
7411 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00007412 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007413 break;
7414 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00007415 nfs_post_op_update_inode_force_wcc(data->args.inode,
7416 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007417 break;
7418 default:
7419 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
7420 rpc_restart_call_prepare(task);
7421 return;
7422 }
7423 }
Andy Adamson863a3c62011-03-23 13:27:54 +00007424}
7425
7426static void nfs4_layoutcommit_release(void *calldata)
7427{
7428 struct nfs4_layoutcommit_data *data = calldata;
7429
Andy Adamsondb29c082011-07-30 20:52:38 -04007430 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00007431 put_rpccred(data->cred);
7432 kfree(data);
7433}
7434
7435static const struct rpc_call_ops nfs4_layoutcommit_ops = {
7436 .rpc_call_prepare = nfs4_layoutcommit_prepare,
7437 .rpc_call_done = nfs4_layoutcommit_done,
7438 .rpc_release = nfs4_layoutcommit_release,
7439};
7440
7441int
Andy Adamsonef311532011-03-12 02:58:10 -05007442nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00007443{
7444 struct rpc_message msg = {
7445 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
7446 .rpc_argp = &data->args,
7447 .rpc_resp = &data->res,
7448 .rpc_cred = data->cred,
7449 };
7450 struct rpc_task_setup task_setup_data = {
7451 .task = &data->task,
7452 .rpc_client = NFS_CLIENT(data->args.inode),
7453 .rpc_message = &msg,
7454 .callback_ops = &nfs4_layoutcommit_ops,
7455 .callback_data = data,
7456 .flags = RPC_TASK_ASYNC,
7457 };
7458 struct rpc_task *task;
7459 int status = 0;
7460
7461 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
7462 "lbw: %llu inode %lu\n",
7463 data->task.tk_pid, sync,
7464 data->args.lastbytewritten,
7465 data->args.inode->i_ino);
7466
Chuck Levera9c92d62013-08-09 12:48:18 -04007467 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00007468 task = rpc_run_task(&task_setup_data);
7469 if (IS_ERR(task))
7470 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05007471 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00007472 goto out;
7473 status = nfs4_wait_for_completion_rpc_task(task);
7474 if (status != 0)
7475 goto out;
7476 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007477 trace_nfs4_layoutcommit(data->args.inode, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00007478out:
7479 dprintk("%s: status %d\n", __func__, status);
7480 rpc_put_task(task);
7481 return status;
7482}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007483
Andy Adamson97431202013-08-08 10:57:56 -04007484/**
7485 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
7486 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
7487 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007488static int
7489_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007490 struct nfs_fsinfo *info,
7491 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007492{
7493 struct nfs41_secinfo_no_name_args args = {
7494 .style = SECINFO_STYLE_CURRENT_FH,
7495 };
7496 struct nfs4_secinfo_res res = {
7497 .flavors = flavors,
7498 };
7499 struct rpc_message msg = {
7500 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
7501 .rpc_argp = &args,
7502 .rpc_resp = &res,
7503 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007504 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007505 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007506 int status;
7507
7508 if (use_integrity) {
7509 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007510 cred = nfs4_get_clid_cred(server->nfs_client);
7511 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007512 }
7513
7514 dprintk("--> %s\n", __func__);
7515 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
7516 &res.seq_res, 0);
7517 dprintk("<-- %s status=%d\n", __func__, status);
7518
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007519 if (cred)
7520 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007521
7522 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007523}
7524
7525static int
7526nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7527 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
7528{
7529 struct nfs4_exception exception = { };
7530 int err;
7531 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007532 /* first try using integrity protection */
7533 err = -NFS4ERR_WRONGSEC;
7534
7535 /* try to use integrity protection with machine cred */
7536 if (_nfs4_is_integrity_protected(server->nfs_client))
7537 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
7538 flavors, true);
7539
7540 /*
7541 * if unable to use integrity protection, or SECINFO with
7542 * integrity protection returns NFS4ERR_WRONGSEC (which is
7543 * disallowed by spec, but exists in deployed servers) use
7544 * the current filesystem's rpc_client and the user cred.
7545 */
7546 if (err == -NFS4ERR_WRONGSEC)
7547 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
7548 flavors, false);
7549
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007550 switch (err) {
7551 case 0:
7552 case -NFS4ERR_WRONGSEC:
7553 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007554 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007555 default:
7556 err = nfs4_handle_exception(server, err, &exception);
7557 }
7558 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007559out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007560 return err;
7561}
7562
7563static int
7564nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
7565 struct nfs_fsinfo *info)
7566{
7567 int err;
7568 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04007569 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007570 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04007571 struct nfs4_secinfo4 *secinfo;
7572 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007573
7574 page = alloc_page(GFP_KERNEL);
7575 if (!page) {
7576 err = -ENOMEM;
7577 goto out;
7578 }
7579
7580 flavors = page_address(page);
7581 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
7582
7583 /*
7584 * Fall back on "guess and check" method if
7585 * the server doesn't support SECINFO_NO_NAME
7586 */
7587 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
7588 err = nfs4_find_root_sec(server, fhandle, info);
7589 goto out_freepage;
7590 }
7591 if (err)
7592 goto out_freepage;
7593
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04007594 for (i = 0; i < flavors->num_flavors; i++) {
7595 secinfo = &flavors->flavors[i];
7596
7597 switch (secinfo->flavor) {
7598 case RPC_AUTH_NULL:
7599 case RPC_AUTH_UNIX:
7600 case RPC_AUTH_GSS:
7601 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
7602 &secinfo->flavor_info);
7603 break;
7604 default:
7605 flavor = RPC_AUTH_MAXFLAVOR;
7606 break;
7607 }
7608
7609 if (flavor != RPC_AUTH_MAXFLAVOR) {
7610 err = nfs4_lookup_root_sec(server, fhandle,
7611 info, flavor);
7612 if (!err)
7613 break;
7614 }
7615 }
7616
7617 if (flavor == RPC_AUTH_MAXFLAVOR)
7618 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007619
7620out_freepage:
7621 put_page(page);
7622 if (err == -EACCES)
7623 return -EPERM;
7624out:
7625 return err;
7626}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007627
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007628static int _nfs41_test_stateid(struct nfs_server *server,
7629 nfs4_stateid *stateid,
7630 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04007631{
7632 int status;
7633 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007634 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04007635 };
7636 struct nfs41_test_stateid_res res;
7637 struct rpc_message msg = {
7638 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
7639 .rpc_argp = &args,
7640 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007641 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04007642 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007643 struct rpc_clnt *rpc_client = server->client;
7644
7645 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
7646 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007647
Chuck Lever38527b12012-07-11 16:30:23 -04007648 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04007649 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007650 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007651 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007652 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04007653 if (status != NFS_OK) {
7654 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04007655 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04007656 }
7657 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04007658 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04007659}
7660
Chuck Lever38527b12012-07-11 16:30:23 -04007661/**
7662 * nfs41_test_stateid - perform a TEST_STATEID operation
7663 *
7664 * @server: server / transport on which to perform the operation
7665 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007666 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04007667 *
7668 * Returns NFS_OK if the server recognizes that "stateid" is valid.
7669 * Otherwise a negative NFS4ERR value is returned if the operation
7670 * failed or the state ID is not currently valid.
7671 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007672static int nfs41_test_stateid(struct nfs_server *server,
7673 nfs4_stateid *stateid,
7674 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04007675{
7676 struct nfs4_exception exception = { };
7677 int err;
7678 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007679 err = _nfs41_test_stateid(server, stateid, cred);
Chuck Lever377e5072012-07-11 16:29:45 -04007680 if (err != -NFS4ERR_DELAY)
7681 break;
7682 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04007683 } while (exception.retry);
7684 return err;
7685}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007686
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007687struct nfs_free_stateid_data {
7688 struct nfs_server *server;
7689 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007690 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007691};
7692
7693static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
7694{
7695 struct nfs_free_stateid_data *data = calldata;
7696 nfs41_setup_sequence(nfs4_get_session(data->server),
7697 &data->args.seq_args,
7698 &data->res.seq_res,
7699 task);
7700}
7701
7702static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
7703{
7704 struct nfs_free_stateid_data *data = calldata;
7705
7706 nfs41_sequence_done(task, &data->res.seq_res);
7707
7708 switch (task->tk_status) {
7709 case -NFS4ERR_DELAY:
7710 if (nfs4_async_handle_error(task, data->server, NULL) == -EAGAIN)
7711 rpc_restart_call_prepare(task);
7712 }
7713}
7714
7715static void nfs41_free_stateid_release(void *calldata)
7716{
7717 kfree(calldata);
7718}
7719
Trond Myklebust17f26b12013-08-21 15:48:42 -04007720static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007721 .rpc_call_prepare = nfs41_free_stateid_prepare,
7722 .rpc_call_done = nfs41_free_stateid_done,
7723 .rpc_release = nfs41_free_stateid_release,
7724};
7725
7726static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
7727 nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007728 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007729 bool privileged)
7730{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007731 struct rpc_message msg = {
7732 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007733 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007734 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007735 struct rpc_task_setup task_setup = {
7736 .rpc_client = server->client,
7737 .rpc_message = &msg,
7738 .callback_ops = &nfs41_free_stateid_ops,
7739 .flags = RPC_TASK_ASYNC,
7740 };
7741 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007742
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007743 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
7744 &task_setup.rpc_client, &msg);
7745
Chuck Lever38527b12012-07-11 16:30:23 -04007746 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007747 data = kmalloc(sizeof(*data), GFP_NOFS);
7748 if (!data)
7749 return ERR_PTR(-ENOMEM);
7750 data->server = server;
7751 nfs4_stateid_copy(&data->args.stateid, stateid);
7752
7753 task_setup.callback_data = data;
7754
7755 msg.rpc_argp = &data->args;
7756 msg.rpc_resp = &data->res;
Chuck Levera9c92d62013-08-09 12:48:18 -04007757 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007758 if (privileged)
7759 nfs4_set_sequence_privileged(&data->args.seq_args);
7760
7761 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007762}
7763
Chuck Lever38527b12012-07-11 16:30:23 -04007764/**
7765 * nfs41_free_stateid - perform a FREE_STATEID operation
7766 *
7767 * @server: server / transport on which to perform the operation
7768 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007769 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04007770 *
7771 * Returns NFS_OK if the server freed "stateid". Otherwise a
7772 * negative NFS4ERR value is returned.
7773 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007774static int nfs41_free_stateid(struct nfs_server *server,
7775 nfs4_stateid *stateid,
7776 struct rpc_cred *cred)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007777{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007778 struct rpc_task *task;
7779 int ret;
7780
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007781 task = _nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007782 if (IS_ERR(task))
7783 return PTR_ERR(task);
7784 ret = rpc_wait_for_completion_task(task);
7785 if (!ret)
7786 ret = task->tk_status;
7787 rpc_put_task(task);
7788 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007789}
Trond Myklebust36281ca2012-03-04 18:13:56 -05007790
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007791static int nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
7792{
7793 struct rpc_task *task;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007794 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007795
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007796 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007797 nfs4_free_lock_state(server, lsp);
7798 if (IS_ERR(task))
7799 return PTR_ERR(task);
7800 rpc_put_task(task);
7801 return 0;
7802}
7803
Trond Myklebust36281ca2012-03-04 18:13:56 -05007804static bool nfs41_match_stateid(const nfs4_stateid *s1,
7805 const nfs4_stateid *s2)
7806{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007807 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007808 return false;
7809
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007810 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007811 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007812 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007813 return true;
7814
7815 return false;
7816}
7817
Andy Adamson557134a2009-04-01 09:21:53 -04007818#endif /* CONFIG_NFS_V4_1 */
7819
Trond Myklebust36281ca2012-03-04 18:13:56 -05007820static bool nfs4_match_stateid(const nfs4_stateid *s1,
7821 const nfs4_stateid *s2)
7822{
Trond Myklebustf597c532012-03-04 18:13:56 -05007823 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05007824}
7825
7826
Trond Myklebust17280172012-03-11 13:11:00 -04007827static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007828 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007829 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007830 .recover_open = nfs4_open_reclaim,
7831 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04007832 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04007833 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007834};
7835
Andy Adamson591d71c2009-04-01 09:22:47 -04007836#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007837static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007838 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
7839 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
7840 .recover_open = nfs4_open_reclaim,
7841 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05007842 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007843 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04007844 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04007845};
7846#endif /* CONFIG_NFS_V4_1 */
7847
Trond Myklebust17280172012-03-11 13:11:00 -04007848static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007849 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007850 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007851 .recover_open = nfs4_open_expired,
7852 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04007853 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007854};
7855
Andy Adamson591d71c2009-04-01 09:22:47 -04007856#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007857static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007858 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
7859 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007860 .recover_open = nfs41_open_expired,
7861 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05007862 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04007863};
7864#endif /* CONFIG_NFS_V4_1 */
7865
Trond Myklebust17280172012-03-11 13:11:00 -04007866static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007867 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04007868 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007869 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04007870};
7871
7872#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007873static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007874 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04007875 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007876 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04007877};
7878#endif
7879
Trond Myklebust97dc1352010-06-16 09:52:26 -04007880static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
7881 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04007882 .init_caps = NFS_CAP_READDIRPLUS
7883 | NFS_CAP_ATOMIC_OPEN
7884 | NFS_CAP_CHANGE_ATTR
7885 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04007886 .init_client = nfs40_init_client,
7887 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007888 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007889 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007890 .free_lock_state = nfs4_release_lockowner,
Chuck Lever9915ea72013-08-09 12:48:27 -04007891 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007892 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
7893 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
7894 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007895};
7896
7897#if defined(CONFIG_NFS_V4_1)
7898static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
7899 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04007900 .init_caps = NFS_CAP_READDIRPLUS
7901 | NFS_CAP_ATOMIC_OPEN
7902 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04007903 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04007904 | NFS_CAP_STATEID_NFSV41
7905 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04007906 .init_client = nfs41_init_client,
7907 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007908 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007909 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007910 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04007911 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007912 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7913 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7914 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007915};
7916#endif
7917
Steve Dickson42c2c422013-05-22 12:50:38 -04007918#if defined(CONFIG_NFS_V4_2)
7919static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
7920 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04007921 .init_caps = NFS_CAP_READDIRPLUS
7922 | NFS_CAP_ATOMIC_OPEN
7923 | NFS_CAP_CHANGE_ATTR
7924 | NFS_CAP_POSIX_LOCK
7925 | NFS_CAP_STATEID_NFSV41
7926 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04007927 .init_client = nfs41_init_client,
7928 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04007929 .match_stateid = nfs41_match_stateid,
7930 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04007931 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04007932 .call_sync_ops = &nfs41_call_sync_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04007933 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7934 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7935 .state_renewal_ops = &nfs41_state_renewal_ops,
7936};
7937#endif
7938
Trond Myklebust97dc1352010-06-16 09:52:26 -04007939const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
7940 [0] = &nfs_v4_0_minor_ops,
7941#if defined(CONFIG_NFS_V4_1)
7942 [1] = &nfs_v4_1_minor_ops,
7943#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04007944#if defined(CONFIG_NFS_V4_2)
7945 [2] = &nfs_v4_2_minor_ops,
7946#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04007947};
7948
Trond Myklebust17f26b12013-08-21 15:48:42 -04007949static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04007950 .create = nfs_create,
7951 .lookup = nfs_lookup,
7952 .atomic_open = nfs_atomic_open,
7953 .link = nfs_link,
7954 .unlink = nfs_unlink,
7955 .symlink = nfs_symlink,
7956 .mkdir = nfs_mkdir,
7957 .rmdir = nfs_rmdir,
7958 .mknod = nfs_mknod,
7959 .rename = nfs_rename,
7960 .permission = nfs_permission,
7961 .getattr = nfs_getattr,
7962 .setattr = nfs_setattr,
7963 .getxattr = generic_getxattr,
7964 .setxattr = generic_setxattr,
7965 .listxattr = generic_listxattr,
7966 .removexattr = generic_removexattr,
7967};
7968
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08007969static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007970 .permission = nfs_permission,
7971 .getattr = nfs_getattr,
7972 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007973 .getxattr = generic_getxattr,
7974 .setxattr = generic_setxattr,
7975 .listxattr = generic_listxattr,
7976 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007977};
7978
David Howells509de812006-08-22 20:06:11 -04007979const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007980 .version = 4, /* protocol version */
7981 .dentry_ops = &nfs4_dentry_operations,
7982 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007983 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04007984 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007985 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04007986 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04007987 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007988 .getattr = nfs4_proc_getattr,
7989 .setattr = nfs4_proc_setattr,
7990 .lookup = nfs4_proc_lookup,
7991 .access = nfs4_proc_access,
7992 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007993 .create = nfs4_proc_create,
7994 .remove = nfs4_proc_remove,
7995 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04007996 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007997 .unlink_done = nfs4_proc_unlink_done,
7998 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04007999 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04008000 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04008001 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008002 .link = nfs4_proc_link,
8003 .symlink = nfs4_proc_symlink,
8004 .mkdir = nfs4_proc_mkdir,
8005 .rmdir = nfs4_proc_remove,
8006 .readdir = nfs4_proc_readdir,
8007 .mknod = nfs4_proc_mknod,
8008 .statfs = nfs4_proc_statfs,
8009 .fsinfo = nfs4_proc_fsinfo,
8010 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04008011 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008012 .decode_dirent = nfs4_decode_dirent,
8013 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04008014 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04008015 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05008016 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008017 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04008018 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04008019 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008020 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008021 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04008022 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008023 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008024 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00008025 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04008026 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04008027 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04008028 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04008029 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04008030 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00008031 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04008032 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04008033 .create_server = nfs4_create_server,
8034 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008035};
8036
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008037static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
8038 .prefix = XATTR_NAME_NFSV4_ACL,
8039 .list = nfs4_xattr_list_nfs4_acl,
8040 .get = nfs4_xattr_get_nfs4_acl,
8041 .set = nfs4_xattr_set_nfs4_acl,
8042};
8043
8044const struct xattr_handler *nfs4_xattr_handlers[] = {
8045 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04008046#ifdef CONFIG_NFS_V4_SECURITY_LABEL
8047 &nfs4_xattr_nfs4_label_handler,
8048#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008049 NULL
8050};
8051
Linus Torvalds1da177e2005-04-16 15:20:36 -07008052/*
8053 * Local variables:
8054 * c-basic-offset: 8
8055 * End:
8056 */