blob: dfef23f1a66af84c0d977fd003139423d810d2a8 [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
69#define NFSDBG_FACILITY NFSDBG_PROC
70
Trond Myklebust2066fe82006-09-15 08:30:46 -040071#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define NFS4_POLL_RETRY_MAX (15*HZ)
73
Trond Myklebustcdd4e682006-01-03 09:55:12 +010074struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010075static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080076static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050078static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Chuck Lever81934dd2012-03-01 17:01:57 -050079static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040080static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
81static 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 -040082static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
83 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -040084 struct nfs4_state *state, struct nfs4_label *ilabel,
85 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040086#ifdef CONFIG_NFS_V4_1
Bryan Schumaker1cab0652012-01-31 10:39:29 -050087static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
88static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040089#endif
David Quigleyaa9c2662013-05-22 12:50:44 -040090
91#ifdef CONFIG_NFS_V4_SECURITY_LABEL
92static inline struct nfs4_label *
93nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
94 struct iattr *sattr, struct nfs4_label *label)
95{
96 int err;
97
98 if (label == NULL)
99 return NULL;
100
101 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
102 return NULL;
103
104 if (NFS_SERVER(dir)->nfs_client->cl_minorversion < 2)
105 return NULL;
106
107 err = security_dentry_init_security(dentry, sattr->ia_mode,
108 &dentry->d_name, (void **)&label->label, &label->len);
109 if (err == 0)
110 return label;
111
112 return NULL;
113}
114static inline void
115nfs4_label_release_security(struct nfs4_label *label)
116{
117 if (label)
118 security_release_secctx(label->label, label->len);
119}
120static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
121{
122 if (label)
123 return server->attr_bitmask;
124
125 return server->attr_bitmask_nl;
126}
127#else
128static inline struct nfs4_label *
129nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
130 struct iattr *sattr, struct nfs4_label *l)
131{ return NULL; }
132static inline void
133nfs4_label_release_security(struct nfs4_label *label)
134{ return; }
135static inline u32 *
136nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
137{ return server->attr_bitmask; }
138#endif
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500141static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Trond Myklebust52567b02009-10-23 14:46:42 -0400143 if (err >= -1000)
144 return err;
145 switch (err) {
146 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500147 case -NFS4ERR_LAYOUTTRYLATER:
148 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400149 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000150 case -NFS4ERR_WRONGSEC:
151 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800152 case -NFS4ERR_BADOWNER:
153 case -NFS4ERR_BADNAME:
154 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400155 case -NFS4ERR_SHARE_DENIED:
156 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400157 case -NFS4ERR_MINOR_VERS_MISMATCH:
158 return -EPROTONOSUPPORT;
Weston Andros Adamson6168f622012-09-10 14:00:46 -0400159 case -NFS4ERR_ACCESS:
160 return -EACCES;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400161 case -NFS4ERR_FILE_OPEN:
162 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400163 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700165 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400166 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400168 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
171/*
172 * This is our standard bitmap for GETATTR requests.
173 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400174const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 FATTR4_WORD0_TYPE
176 | FATTR4_WORD0_CHANGE
177 | FATTR4_WORD0_SIZE
178 | FATTR4_WORD0_FSID
179 | FATTR4_WORD0_FILEID,
180 FATTR4_WORD1_MODE
181 | FATTR4_WORD1_NUMLINKS
182 | FATTR4_WORD1_OWNER
183 | FATTR4_WORD1_OWNER_GROUP
184 | FATTR4_WORD1_RAWDEV
185 | FATTR4_WORD1_SPACE_USED
186 | FATTR4_WORD1_TIME_ACCESS
187 | FATTR4_WORD1_TIME_METADATA
David Quigleyaa9c2662013-05-22 12:50:44 -0400188 | FATTR4_WORD1_TIME_MODIFY,
189#ifdef CONFIG_NFS_V4_SECURITY_LABEL
190 FATTR4_WORD2_SECURITY_LABEL
191#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192};
193
Trond Myklebust1549210f2012-06-05 09:16:47 -0400194static const u32 nfs4_pnfs_open_bitmap[3] = {
195 FATTR4_WORD0_TYPE
196 | FATTR4_WORD0_CHANGE
197 | FATTR4_WORD0_SIZE
198 | FATTR4_WORD0_FSID
199 | FATTR4_WORD0_FILEID,
200 FATTR4_WORD1_MODE
201 | FATTR4_WORD1_NUMLINKS
202 | FATTR4_WORD1_OWNER
203 | FATTR4_WORD1_OWNER_GROUP
204 | FATTR4_WORD1_RAWDEV
205 | FATTR4_WORD1_SPACE_USED
206 | FATTR4_WORD1_TIME_ACCESS
207 | FATTR4_WORD1_TIME_METADATA
208 | FATTR4_WORD1_TIME_MODIFY,
209 FATTR4_WORD2_MDSTHRESHOLD
210};
211
Andy Adamsone23008e2012-10-02 21:07:32 -0400212static const u32 nfs4_open_noattr_bitmap[3] = {
213 FATTR4_WORD0_TYPE
214 | FATTR4_WORD0_CHANGE
215 | FATTR4_WORD0_FILEID,
216};
217
David Quigleya09df2c2013-05-22 12:50:41 -0400218const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 FATTR4_WORD0_FILES_AVAIL
220 | FATTR4_WORD0_FILES_FREE
221 | FATTR4_WORD0_FILES_TOTAL,
222 FATTR4_WORD1_SPACE_AVAIL
223 | FATTR4_WORD1_SPACE_FREE
224 | FATTR4_WORD1_SPACE_TOTAL
225};
226
David Quigleya09df2c2013-05-22 12:50:41 -0400227const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 FATTR4_WORD0_MAXLINK
229 | FATTR4_WORD0_MAXNAME,
230 0
231};
232
Fred Isamandae100c2011-07-30 20:52:37 -0400233const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 | FATTR4_WORD0_MAXREAD
235 | FATTR4_WORD0_MAXWRITE
236 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700237 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400238 | FATTR4_WORD1_FS_LAYOUT_TYPES,
239 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240};
241
David Quigleya09df2c2013-05-22 12:50:41 -0400242const u32 nfs4_fs_locations_bitmap[3] = {
Manoj Naik830b8e32006-06-09 09:34:25 -0400243 FATTR4_WORD0_TYPE
244 | FATTR4_WORD0_CHANGE
245 | FATTR4_WORD0_SIZE
246 | FATTR4_WORD0_FSID
247 | FATTR4_WORD0_FILEID
248 | FATTR4_WORD0_FS_LOCATIONS,
249 FATTR4_WORD1_MODE
250 | FATTR4_WORD1_NUMLINKS
251 | FATTR4_WORD1_OWNER
252 | FATTR4_WORD1_OWNER_GROUP
253 | FATTR4_WORD1_RAWDEV
254 | FATTR4_WORD1_SPACE_USED
255 | FATTR4_WORD1_TIME_ACCESS
256 | FATTR4_WORD1_TIME_METADATA
257 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400258 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400259};
260
Al Virobc4785c2006-10-19 23:28:51 -0700261static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 struct nfs4_readdir_arg *readdir)
263{
Al Viro0dbb4c62006-10-19 23:28:49 -0700264 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000267 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
269 return;
270 }
271
272 readdir->cookie = 0;
273 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
274 if (cookie == 2)
275 return;
276
277 /*
278 * NFSv4 servers do not return entries for '.' and '..'
279 * Therefore, we fake these entries here. We let '.'
280 * have cookie 0 and '..' have cookie 1. Note that
281 * when talking to the server, we always send cookie 0
282 * instead of 1 or 2.
283 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800284 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 if (cookie == 0) {
287 *p++ = xdr_one; /* next */
288 *p++ = xdr_zero; /* cookie, first word */
289 *p++ = xdr_one; /* cookie, second word */
290 *p++ = xdr_one; /* entry len */
291 memcpy(p, ".\0\0\0", 4); /* entry */
292 p++;
293 *p++ = xdr_one; /* bitmap length */
294 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
295 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400296 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
298
299 *p++ = xdr_one; /* next */
300 *p++ = xdr_zero; /* cookie, first word */
301 *p++ = xdr_two; /* cookie, second word */
302 *p++ = xdr_two; /* entry len */
303 memcpy(p, "..\0\0", 4); /* entry */
304 p++;
305 *p++ = xdr_one; /* bitmap length */
306 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
307 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400308 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 readdir->pgbase = (char *)p - (char *)start;
311 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800312 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
Trond Myklebust65de8722008-12-23 15:21:44 -0500315static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
316{
317 int res = 0;
318
319 might_sleep();
320
321 if (*timeout <= 0)
322 *timeout = NFS4_POLL_RETRY_MIN;
323 if (*timeout > NFS4_POLL_RETRY_MAX)
324 *timeout = NFS4_POLL_RETRY_MAX;
Jeff Laytond3103102011-12-01 22:44:39 +0100325 freezable_schedule_timeout_killable(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500326 if (fatal_signal_pending(current))
327 res = -ERESTARTSYS;
328 *timeout <<= 1;
329 return res;
330}
331
332/* This is the error handling routine for processes that are allowed
333 * to sleep.
334 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800335static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500336{
337 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500338 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500339 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500340 int ret = errorcode;
341
342 exception->retry = 0;
343 switch(errorcode) {
344 case 0:
345 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500346 case -NFS4ERR_OPENMODE:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400347 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400348 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500349 exception->retry = 1;
350 return 0;
351 }
352 if (state == NULL)
353 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400354 ret = nfs4_schedule_stateid_recovery(server, state);
355 if (ret < 0)
356 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500357 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500358 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500359 case -NFS4ERR_ADMIN_REVOKED:
360 case -NFS4ERR_BAD_STATEID:
Trond Myklebustb570a972013-04-12 15:04:51 -0400361 if (inode != NULL && nfs4_have_delegation(inode, FMODE_READ)) {
362 nfs_remove_bad_delegation(inode);
363 exception->retry = 1;
364 break;
365 }
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500366 if (state == NULL)
367 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400368 ret = nfs4_schedule_stateid_recovery(server, state);
369 if (ret < 0)
370 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500371 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400372 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -0400373 if (state != NULL) {
374 ret = nfs4_schedule_stateid_recovery(server, state);
375 if (ret < 0)
376 break;
377 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500378 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500379 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500380 nfs4_schedule_lease_recovery(clp);
381 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500382#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400383 case -NFS4ERR_BADSESSION:
384 case -NFS4ERR_BADSLOT:
385 case -NFS4ERR_BAD_HIGH_SLOT:
386 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
387 case -NFS4ERR_DEADSESSION:
388 case -NFS4ERR_SEQ_FALSE_RETRY:
389 case -NFS4ERR_SEQ_MISORDERED:
390 dprintk("%s ERROR: %d Reset session\n", __func__,
391 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400392 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400393 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500394#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500395 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500396 if (exception->timeout > HZ) {
397 /* We have retried a decent amount, time to
398 * fail
399 */
400 ret = -EBUSY;
401 break;
402 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500403 case -NFS4ERR_GRACE:
404 case -NFS4ERR_DELAY:
405 ret = nfs4_delay(server->client, &exception->timeout);
406 if (ret != 0)
407 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400408 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500409 case -NFS4ERR_OLD_STATEID:
410 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800411 break;
412 case -NFS4ERR_BADOWNER:
413 /* The following works around a Linux server bug! */
414 case -NFS4ERR_BADNAME:
415 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
416 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
417 exception->retry = 1;
418 printk(KERN_WARNING "NFS: v4 server %s "
419 "does not accept raw "
420 "uid/gids. "
421 "Reenabling the idmapper.\n",
422 server->nfs_client->cl_hostname);
423 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500424 }
425 /* We failed to handle the error */
426 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500427wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500428 ret = nfs4_wait_clnt_recover(clp);
429 if (ret == 0)
430 exception->retry = 1;
431 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500432}
433
434
Trond Myklebust452e9352010-07-31 14:29:06 -0400435static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 spin_lock(&clp->cl_lock);
438 if (time_before(clp->cl_last_renewal,timestamp))
439 clp->cl_last_renewal = timestamp;
440 spin_unlock(&clp->cl_lock);
441}
442
Trond Myklebust452e9352010-07-31 14:29:06 -0400443static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
444{
445 do_renew_lease(server->nfs_client, timestamp);
446}
447
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400448#if defined(CONFIG_NFS_V4_1)
449
Trond Myklebustd185a332010-06-16 09:52:25 -0400450static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400451{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500452 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400453 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500454 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400455
Benny Halevydfb4f3092010-09-24 09:17:01 -0400456 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400457 /* just wake up the next guy waiting since
458 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500459 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500460 return;
Andy Adamson13615872009-04-01 09:22:17 -0400461 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500462 tbl = res->sr_slot->table;
463 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500464
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500465 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500466 /* Be nice to the server: try to ensure that the last transmitted
467 * value for highest_user_slotid <= target_highest_slotid
468 */
469 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
470 send_new_highest_used_slotid = true;
471
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500472 if (nfs41_wake_and_assign_slot(tbl, res->sr_slot)) {
473 send_new_highest_used_slotid = false;
474 goto out_unlock;
475 }
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500476 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500477
478 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
479 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500480out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500481 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400482 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500483 if (send_new_highest_used_slotid)
484 nfs41_server_notify_highest_slotid_update(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400485}
486
Trond Myklebust14516c32010-07-31 14:29:06 -0400487static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400488{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500489 struct nfs4_session *session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500490 struct nfs4_slot *slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400491 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500492 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500493 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400494
Bryan Schumaker468f8612011-04-18 15:57:32 -0400495 /* don't increment the sequence number if the task wasn't sent */
496 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400497 goto out;
498
Trond Myklebust933602e2012-11-16 12:12:38 -0500499 slot = res->sr_slot;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500500 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500501
Trond Myklebustac20d162012-12-15 15:36:07 -0500502 if (slot->interrupted) {
503 slot->interrupted = 0;
504 interrupted = true;
505 }
506
Andy Adamson691daf32009-12-04 15:55:39 -0500507 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400508 switch (res->sr_status) {
509 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400510 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500511 ++slot->seq_nr;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500512 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500513 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500514 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500515 if (res->sr_status_flags != 0)
516 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500517 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400518 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500519 case 1:
520 /*
521 * sr_status remains 1 if an RPC level error occurred.
522 * The server may or may not have processed the sequence
523 * operation..
524 * Mark the slot as having hosted an interrupted RPC call.
525 */
526 slot->interrupted = 1;
527 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400528 case -NFS4ERR_DELAY:
529 /* The server detected a resend of the RPC call and
530 * returned NFS4ERR_DELAY as per Section 2.10.6.2
531 * of RFC5661.
532 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500533 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400534 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500535 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500536 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400537 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500538 case -NFS4ERR_BADSLOT:
539 /*
540 * The slot id we used was probably retired. Try again
541 * using a different slot id.
542 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500543 goto retry_nowait;
544 case -NFS4ERR_SEQ_MISORDERED:
545 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500546 * Was the last operation on this sequence interrupted?
547 * If so, retry after bumping the sequence number.
548 */
549 if (interrupted) {
550 ++slot->seq_nr;
551 goto retry_nowait;
552 }
553 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500554 * Could this slot have been previously retired?
555 * If so, then the server may be expecting seq_nr = 1!
556 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500557 if (slot->seq_nr != 1) {
558 slot->seq_nr = 1;
559 goto retry_nowait;
560 }
561 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500562 case -NFS4ERR_SEQ_FALSE_RETRY:
563 ++slot->seq_nr;
564 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400565 default:
566 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500567 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400568 }
569out:
570 /* The session may be reset by one of the error handlers. */
571 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400572 nfs41_sequence_free_slot(res);
Trond Myklebust85563072012-12-11 10:31:12 -0500573 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500574retry_nowait:
575 if (rpc_restart_call_prepare(task)) {
576 task->tk_status = 0;
577 ret = 0;
578 }
579 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400580out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400581 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400582 goto out;
583 rpc_delay(task, NFS4_POLL_RETRY_MAX);
584 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400585}
586
Trond Myklebust14516c32010-07-31 14:29:06 -0400587static int nfs4_sequence_done(struct rpc_task *task,
588 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400589{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500590 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400591 return 1;
592 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400593}
594
Trond Myklebust9d12b212012-01-17 22:04:25 -0500595static void nfs41_init_sequence(struct nfs4_sequence_args *args,
596 struct nfs4_sequence_res *res, int cache_reply)
597{
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500598 args->sa_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500599 args->sa_cache_this = 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400600 args->sa_privileged = 0;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500601 if (cache_reply)
602 args->sa_cache_this = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500603 res->sr_slot = NULL;
604}
605
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400606static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
607{
608 args->sa_privileged = 1;
609}
610
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000611int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400612 struct nfs4_sequence_args *args,
613 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400614 struct rpc_task *task)
615{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400616 struct nfs4_slot *slot;
617 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400618
619 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400620 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400621 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400622 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400623
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400624 tbl = &session->fc_slot_table;
625
Trond Myklebust69d206b2012-11-22 13:21:02 -0500626 task->tk_timeout = 0;
627
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400628 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400629 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400630 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500631 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500632 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400633 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400634 }
635
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500636 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500637 if (IS_ERR(slot)) {
638 /* If out of memory, try again in 1/4 second */
639 if (slot == ERR_PTR(-ENOMEM))
640 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400641 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400642 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400643 }
644 spin_unlock(&tbl->slot_tbl_lock);
645
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500646 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400647
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500648 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
649 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400650
Benny Halevydfb4f3092010-09-24 09:17:01 -0400651 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500652 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400653 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400654 /*
655 * sr_status is only set in decode_sequence, and so will remain
656 * set to 1 if an rpc level failure occurs.
657 */
658 res->sr_status = 1;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400659out_success:
660 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400661 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400662out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400663 /* Privileged tasks are queued with top priority */
664 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400665 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
666 NULL, RPC_PRIORITY_PRIVILEGED);
667 else
668 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400669 spin_unlock(&tbl->slot_tbl_lock);
670 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400671}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000672EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400673
Trond Myklebust035168a2010-06-16 09:52:26 -0400674int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400675 struct nfs4_sequence_args *args,
676 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400677 struct rpc_task *task)
678{
Trond Myklebust035168a2010-06-16 09:52:26 -0400679 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400680 int ret = 0;
681
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400682 if (session == NULL) {
683 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400684 goto out;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400685 }
Trond Myklebust035168a2010-06-16 09:52:26 -0400686
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500687 dprintk("--> %s clp %p session %p sr_slot %d\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400688 __func__, session->clp, session, res->sr_slot ?
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500689 res->sr_slot->slot_nr : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400690
Trond Myklebust9d12b212012-01-17 22:04:25 -0500691 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400692out:
693 dprintk("<-- %s status=%d\n", __func__, ret);
694 return ret;
695}
696
697struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400698 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400699 struct nfs4_sequence_args *seq_args;
700 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400701};
702
703static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
704{
705 struct nfs41_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400706 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400707
Trond Myklebust035168a2010-06-16 09:52:26 -0400708 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
709
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400710 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400711}
712
Andy Adamson69ab40c2009-04-01 09:22:19 -0400713static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
714{
715 struct nfs41_call_sync_data *data = calldata;
716
Trond Myklebust14516c32010-07-31 14:29:06 -0400717 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400718}
719
Trond Myklebust17280172012-03-11 13:11:00 -0400720static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400721 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400722 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400723};
724
Bryan Schumaker7c513052011-03-24 17:12:24 +0000725static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
726 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400727 struct rpc_message *msg,
728 struct nfs4_sequence_args *args,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400729 struct nfs4_sequence_res *res)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400730{
731 int ret;
732 struct rpc_task *task;
733 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400734 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400735 .seq_args = args,
736 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400737 };
738 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000739 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400740 .rpc_message = msg,
741 .callback_ops = &nfs41_call_sync_ops,
742 .callback_data = &data
743 };
744
Andy Adamsonce5039c2009-04-01 09:22:13 -0400745 task = rpc_run_task(&task_setup);
746 if (IS_ERR(task))
747 ret = PTR_ERR(task);
748 else {
749 ret = task->tk_status;
750 rpc_put_task(task);
751 }
752 return ret;
753}
754
Trond Myklebustdf896452010-06-16 09:52:26 -0400755#else
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400756static
Trond Myklebust9d12b212012-01-17 22:04:25 -0500757void nfs41_init_sequence(struct nfs4_sequence_args *args,
758 struct nfs4_sequence_res *res, int cache_reply)
759{
760}
761
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400762static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
763{
764}
765
766
Trond Myklebust14516c32010-07-31 14:29:06 -0400767static int nfs4_sequence_done(struct rpc_task *task,
768 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400769{
Trond Myklebust14516c32010-07-31 14:29:06 -0400770 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400771}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400772#endif /* CONFIG_NFS_V4_1 */
773
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400774static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000775int _nfs4_call_sync(struct rpc_clnt *clnt,
776 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400777 struct rpc_message *msg,
778 struct nfs4_sequence_args *args,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400779 struct nfs4_sequence_res *res)
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400780{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000781 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400782}
783
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400784static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000785int nfs4_call_sync(struct rpc_clnt *clnt,
786 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000787 struct rpc_message *msg,
788 struct nfs4_sequence_args *args,
789 struct nfs4_sequence_res *res,
790 int cache_reply)
791{
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400792 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000793 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400794 args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000795}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400796
Trond Myklebust38478b22006-05-25 01:40:57 -0400797static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
Trond Myklebust38478b22006-05-25 01:40:57 -0400799 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Trond Myklebust38478b22006-05-25 01:40:57 -0400801 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400802 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700803 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400804 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700805 dir->i_version = cinfo->after;
David Howellsde242c02012-12-20 21:52:38 +0000806 nfs_fscache_invalidate(dir);
Trond Myklebust38478b22006-05-25 01:40:57 -0400807 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808}
809
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100810struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400811 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100812 struct nfs_openargs o_arg;
813 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100814 struct nfs_open_confirmargs c_arg;
815 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500816 struct nfs4_string owner_name;
817 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100818 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400819 struct nfs4_label *f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100820 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400821 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100822 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400823 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100824 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100825 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400826 unsigned int rpc_done : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -0400827 unsigned int is_recover : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100828 int rpc_status;
829 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100830};
831
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400832static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
833 int err, struct nfs4_exception *exception)
834{
835 if (err != -EINVAL)
836 return false;
837 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
838 return false;
839 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
840 exception->retry = 1;
841 return true;
842}
843
844static enum open_claim_type4
845nfs4_map_atomic_open_claim(struct nfs_server *server,
846 enum open_claim_type4 claim)
847{
848 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
849 return claim;
850 switch (claim) {
851 default:
852 return claim;
853 case NFS4_OPEN_CLAIM_FH:
854 return NFS4_OPEN_CLAIM_NULL;
855 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
856 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
857 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
858 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
859 }
860}
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400861
862static void nfs4_init_opendata_res(struct nfs4_opendata *p)
863{
864 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400865 p->o_res.f_label = p->f_label;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400866 p->o_res.seqid = p->o_arg.seqid;
867 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400868 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400869 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400870 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500871 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400872}
873
Al Viro82a2c1b2011-06-22 18:30:55 -0400874static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500875 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400876 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -0400877 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400878 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400879 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100880{
Al Viro82a2c1b2011-06-22 18:30:55 -0400881 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100882 struct inode *dir = parent->d_inode;
883 struct nfs_server *server = NFS_SERVER(dir);
884 struct nfs4_opendata *p;
885
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400886 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100887 if (p == NULL)
888 goto err;
David Quigley14c43f72013-05-22 12:50:43 -0400889
890 p->f_label = nfs4_label_alloc(server, gfp_mask);
891 if (IS_ERR(p->f_label))
892 goto err_free_p;
893
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400894 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100895 if (p->o_arg.seqid == NULL)
David Quigley14c43f72013-05-22 12:50:43 -0400896 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -0400897 nfs_sb_active(dentry->d_sb);
898 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100899 p->dir = parent;
900 p->owner = sp;
901 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500902 p->o_arg.open_flags = flags;
903 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700904 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
905 * will return permission denied for all bits until close */
906 if (!(flags & O_EXCL)) {
907 /* ask server to check for all possible rights as results
908 * are cached */
909 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
910 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700911 }
David Howells7539bba2006-08-22 20:06:09 -0400912 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400913 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
914 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400915 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100916 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -0400917 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -0400918 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
David Quigley1775fd32013-05-22 12:50:42 -0400919 p->o_arg.label = label;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400920 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
921 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400922 case NFS4_OPEN_CLAIM_NULL:
923 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
924 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
925 p->o_arg.fh = NFS_FH(dir);
926 break;
927 case NFS4_OPEN_CLAIM_PREVIOUS:
928 case NFS4_OPEN_CLAIM_FH:
929 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
930 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
931 p->o_arg.fh = NFS_FH(dentry->d_inode);
932 }
Trond Myklebust536e43d2012-01-17 22:04:26 -0500933 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500934 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400935
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100936 p->o_arg.u.attrs = &p->attrs;
937 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500938
939 verf[0] = jiffies;
940 verf[1] = current->pid;
941 memcpy(p->o_arg.u.verifier.data, verf,
942 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100943 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100944 p->c_arg.fh = &p->o_res.fh;
945 p->c_arg.stateid = &p->o_res.stateid;
946 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400947 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400948 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100949 return p;
David Quigley14c43f72013-05-22 12:50:43 -0400950
951err_free_label:
952 nfs4_label_free(p->f_label);
953err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100954 kfree(p);
955err:
956 dput(parent);
957 return NULL;
958}
959
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400960static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100961{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400962 struct nfs4_opendata *p = container_of(kref,
963 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400964 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400965
966 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400967 if (p->state != NULL)
968 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400969 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -0400970
971 nfs4_label_free(p->f_label);
972
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400973 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400974 dput(p->dentry);
975 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500976 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400977 kfree(p);
978}
979
980static void nfs4_opendata_put(struct nfs4_opendata *p)
981{
982 if (p != NULL)
983 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100984}
985
Trond Myklebust06f814a2006-01-03 09:55:07 +0100986static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
987{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100988 int ret;
989
Trond Myklebust06f814a2006-01-03 09:55:07 +0100990 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100991 return ret;
992}
993
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500994static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400995{
996 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500997
Trond Myklebust536e43d2012-01-17 22:04:26 -0500998 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500999 goto out;
1000 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001001 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001002 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1003 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001004 break;
1005 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001006 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1007 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001008 break;
1009 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001010 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1011 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001012 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001013out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001014 return ret;
1015}
1016
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001017static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001018{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001019 if (delegation == NULL)
1020 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001021 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001022 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -05001023 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -04001024 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001025 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1026 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -05001027 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001028 return 1;
1029}
1030
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001031static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001032{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001033 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001034 case FMODE_WRITE:
1035 state->n_wronly++;
1036 break;
1037 case FMODE_READ:
1038 state->n_rdonly++;
1039 break;
1040 case FMODE_READ|FMODE_WRITE:
1041 state->n_rdwr++;
1042 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001043 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001044}
1045
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001046static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001047{
1048 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001049 nfs4_stateid_copy(&state->stateid, stateid);
1050 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust92b40e92013-04-20 01:25:45 -04001051 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001052 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001053 case FMODE_READ:
1054 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1055 break;
1056 case FMODE_WRITE:
1057 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1058 break;
1059 case FMODE_READ|FMODE_WRITE:
1060 set_bit(NFS_O_RDWR_STATE, &state->flags);
1061 }
1062}
1063
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001064static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001065{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001066 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001067 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001068 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001069}
1070
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001071static 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 -07001072{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001073 /*
1074 * Protect the call to nfs4_state_set_mode_locked and
1075 * serialise the stateid update
1076 */
1077 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001078 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001079 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001080 set_bit(NFS_DELEGATED_STATE, &state->flags);
1081 }
1082 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001083 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001084 write_sequnlock(&state->seqlock);
1085 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001086 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001087 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088}
1089
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001090static 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 -05001091{
1092 struct nfs_inode *nfsi = NFS_I(state->inode);
1093 struct nfs_delegation *deleg_cur;
1094 int ret = 0;
1095
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001096 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001097
1098 rcu_read_lock();
1099 deleg_cur = rcu_dereference(nfsi->delegation);
1100 if (deleg_cur == NULL)
1101 goto no_delegation;
1102
1103 spin_lock(&deleg_cur->lock);
1104 if (nfsi->delegation != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001105 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001106 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001107 goto no_delegation_unlock;
1108
1109 if (delegation == NULL)
1110 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001111 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001112 goto no_delegation_unlock;
1113
Trond Myklebustb7391f42008-12-23 15:21:52 -05001114 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001115 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001116 ret = 1;
1117no_delegation_unlock:
1118 spin_unlock(&deleg_cur->lock);
1119no_delegation:
1120 rcu_read_unlock();
1121
1122 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001123 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001124 ret = 1;
1125 }
1126
1127 return ret;
1128}
1129
1130
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001131static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001132{
1133 struct nfs_delegation *delegation;
1134
1135 rcu_read_lock();
1136 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001137 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001138 rcu_read_unlock();
1139 return;
1140 }
1141 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001142 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001143}
1144
Trond Myklebust6ee41262007-07-08 14:11:36 -04001145static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001146{
1147 struct nfs4_state *state = opendata->state;
1148 struct nfs_inode *nfsi = NFS_I(state->inode);
1149 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001150 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001151 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001152 nfs4_stateid stateid;
1153 int ret = -EAGAIN;
1154
Trond Myklebustaac00a82007-07-05 19:02:21 -04001155 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001156 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001157 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001158 if (can_open_cached(state, fmode, open_mode)) {
1159 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001160 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001161 goto out_return_state;
1162 }
1163 spin_unlock(&state->owner->so_lock);
1164 }
Trond Myklebust34310432008-12-23 15:21:38 -05001165 rcu_read_lock();
1166 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001167 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001168 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001169 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001170 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001171 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001172 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001173 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001174 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001175 if (!opendata->is_recover) {
1176 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1177 if (ret != 0)
1178 goto out;
1179 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001180 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001181
1182 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001183 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001184 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001185 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001186out:
1187 return ERR_PTR(ret);
1188out_return_state:
1189 atomic_inc(&state->count);
1190 return state;
1191}
1192
Andy Adamsone23008e2012-10-02 21:07:32 -04001193static void
1194nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1195{
1196 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1197 struct nfs_delegation *delegation;
1198 int delegation_flags = 0;
1199
1200 rcu_read_lock();
1201 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1202 if (delegation)
1203 delegation_flags = delegation->flags;
1204 rcu_read_unlock();
1205 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1206 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1207 "returning a delegation for "
1208 "OPEN(CLAIM_DELEGATE_CUR)\n",
1209 clp->cl_hostname);
1210 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1211 nfs_inode_set_delegation(state->inode,
1212 data->owner->so_cred,
1213 &data->o_res);
1214 else
1215 nfs_inode_reclaim_delegation(state->inode,
1216 data->owner->so_cred,
1217 &data->o_res);
1218}
1219
1220/*
1221 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1222 * and update the nfs4_state.
1223 */
1224static struct nfs4_state *
1225_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1226{
1227 struct inode *inode = data->state->inode;
1228 struct nfs4_state *state = data->state;
1229 int ret;
1230
1231 if (!data->rpc_done) {
1232 ret = data->rpc_status;
1233 goto err;
1234 }
1235
1236 ret = -ESTALE;
1237 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1238 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1239 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1240 goto err;
1241
1242 ret = -ENOMEM;
1243 state = nfs4_get_open_state(inode, data->owner);
1244 if (state == NULL)
1245 goto err;
1246
1247 ret = nfs_refresh_inode(inode, &data->f_attr);
1248 if (ret)
1249 goto err;
1250
David Quigleyaa9c2662013-05-22 12:50:44 -04001251 nfs_setsecurity(inode, &data->f_attr, data->f_label);
1252
Andy Adamsone23008e2012-10-02 21:07:32 -04001253 if (data->o_res.delegation_type != 0)
1254 nfs4_opendata_check_deleg(data, state);
1255 update_open_stateid(state, &data->o_res.stateid, NULL,
1256 data->o_arg.fmode);
1257
1258 return state;
1259err:
1260 return ERR_PTR(ret);
1261
1262}
1263
1264static struct nfs4_state *
1265_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001266{
1267 struct inode *inode;
1268 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001269 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001270
Trond Myklebustaac00a82007-07-05 19:02:21 -04001271 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001272 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001273 goto out;
1274 }
1275
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001276 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001277 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001278 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001279 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001280 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001281 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001282 goto err;
1283 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001284 state = nfs4_get_open_state(inode, data->owner);
1285 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001286 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001287 if (data->o_res.delegation_type != 0)
1288 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001289 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001290 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001291 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001292out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001293 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001294 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001295err_put_inode:
1296 iput(inode);
1297err:
1298 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001299}
1300
Andy Adamsone23008e2012-10-02 21:07:32 -04001301static struct nfs4_state *
1302nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1303{
1304 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1305 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1306 return _nfs4_opendata_to_nfs4_state(data);
1307}
1308
Trond Myklebust864472e2006-01-03 09:55:15 +01001309static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1310{
1311 struct nfs_inode *nfsi = NFS_I(state->inode);
1312 struct nfs_open_context *ctx;
1313
1314 spin_lock(&state->inode->i_lock);
1315 list_for_each_entry(ctx, &nfsi->open_files, list) {
1316 if (ctx->state != state)
1317 continue;
1318 get_nfs_open_context(ctx);
1319 spin_unlock(&state->inode->i_lock);
1320 return ctx;
1321 }
1322 spin_unlock(&state->inode->i_lock);
1323 return ERR_PTR(-ENOENT);
1324}
1325
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001326static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1327 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001328{
1329 struct nfs4_opendata *opendata;
1330
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001331 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001332 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001333 if (opendata == NULL)
1334 return ERR_PTR(-ENOMEM);
1335 opendata->state = state;
1336 atomic_inc(&state->count);
1337 return opendata;
1338}
1339
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001340static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001341{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001342 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001343 int ret;
1344
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001345 opendata->o_arg.open_flags = 0;
1346 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001347 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1348 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1349 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001350 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001351 if (ret != 0)
1352 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001353 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001354 if (IS_ERR(newstate))
1355 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001356 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001357 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001358 return 0;
1359}
1360
1361static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1362{
Trond Myklebust864472e2006-01-03 09:55:15 +01001363 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001364 int ret;
1365
1366 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001367 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001368 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001369 smp_rmb();
1370 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001371 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001372 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001373 if (ret != 0)
1374 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001375 if (newstate != state)
1376 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001377 }
1378 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001379 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001380 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001381 if (ret != 0)
1382 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001383 if (newstate != state)
1384 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001385 }
1386 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001387 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001388 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001389 if (ret != 0)
1390 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001391 if (newstate != state)
1392 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001393 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001394 /*
1395 * We may have performed cached opens for all three recoveries.
1396 * Check if we need to update the current stateid.
1397 */
1398 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001399 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001400 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001401 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001402 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001403 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001404 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001405 return 0;
1406}
1407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408/*
1409 * OPEN_RECLAIM:
1410 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001412static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001414 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001415 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001416 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 int status;
1418
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001419 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1420 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001421 if (IS_ERR(opendata))
1422 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001423 rcu_read_lock();
1424 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001425 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001426 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001427 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001428 opendata->o_arg.u.delegation_type = delegation_type;
1429 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001430 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 return status;
1432}
1433
Trond Myklebust539cd032007-06-05 11:46:42 -04001434static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{
1436 struct nfs_server *server = NFS_SERVER(state->inode);
1437 struct nfs4_exception exception = { };
1438 int err;
1439 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001440 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001441 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1442 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001443 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001444 break;
1445 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 } while (exception.retry);
1447 return err;
1448}
1449
Trond Myklebust864472e2006-01-03 09:55:15 +01001450static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1451{
1452 struct nfs_open_context *ctx;
1453 int ret;
1454
1455 ctx = nfs4_state_find_open_context(state);
1456 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001457 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001458 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001459 put_nfs_open_context(ctx);
1460 return ret;
1461}
1462
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001463static 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 -07001464{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001465 switch (err) {
1466 default:
1467 printk(KERN_ERR "NFS: %s: unhandled error "
1468 "%d.\n", __func__, err);
1469 case 0:
1470 case -ENOENT:
1471 case -ESTALE:
1472 break;
1473 case -NFS4ERR_BADSESSION:
1474 case -NFS4ERR_BADSLOT:
1475 case -NFS4ERR_BAD_HIGH_SLOT:
1476 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1477 case -NFS4ERR_DEADSESSION:
1478 set_bit(NFS_DELEGATED_STATE, &state->flags);
1479 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1480 return -EAGAIN;
1481 case -NFS4ERR_STALE_CLIENTID:
1482 case -NFS4ERR_STALE_STATEID:
1483 set_bit(NFS_DELEGATED_STATE, &state->flags);
1484 case -NFS4ERR_EXPIRED:
1485 /* Don't recall a delegation if it was lost */
1486 nfs4_schedule_lease_recovery(server->nfs_client);
1487 return -EAGAIN;
1488 case -NFS4ERR_DELEG_REVOKED:
1489 case -NFS4ERR_ADMIN_REVOKED:
1490 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001491 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001492 nfs_inode_find_state_and_recover(state->inode,
1493 stateid);
1494 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001495 return 0;
1496 case -NFS4ERR_DELAY:
1497 case -NFS4ERR_GRACE:
1498 set_bit(NFS_DELEGATED_STATE, &state->flags);
1499 ssleep(1);
1500 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001501 case -ENOMEM:
1502 case -NFS4ERR_DENIED:
1503 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1504 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 return err;
1507}
1508
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001509int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1510{
1511 struct nfs_server *server = NFS_SERVER(state->inode);
1512 struct nfs4_opendata *opendata;
1513 int err;
1514
1515 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1516 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1517 if (IS_ERR(opendata))
1518 return PTR_ERR(opendata);
1519 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1520 err = nfs4_open_recover(opendata, state);
1521 nfs4_opendata_put(opendata);
1522 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1523}
1524
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001525static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1526{
1527 struct nfs4_opendata *data = calldata;
1528
1529 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001530 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001531 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001532 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001533 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001534 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001535 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001536}
1537
1538static void nfs4_open_confirm_release(void *calldata)
1539{
1540 struct nfs4_opendata *data = calldata;
1541 struct nfs4_state *state = NULL;
1542
1543 /* If this request hasn't been cancelled, do nothing */
1544 if (data->cancelled == 0)
1545 goto out_free;
1546 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001547 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001548 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001549 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001550 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001551 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001552out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001553 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001554}
1555
1556static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001557 .rpc_call_done = nfs4_open_confirm_done,
1558 .rpc_release = nfs4_open_confirm_release,
1559};
1560
1561/*
1562 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1563 */
1564static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1565{
1566 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1567 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001568 struct rpc_message msg = {
1569 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1570 .rpc_argp = &data->c_arg,
1571 .rpc_resp = &data->c_res,
1572 .rpc_cred = data->owner->so_cred,
1573 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001574 struct rpc_task_setup task_setup_data = {
1575 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001576 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001577 .callback_ops = &nfs4_open_confirm_ops,
1578 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001579 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001580 .flags = RPC_TASK_ASYNC,
1581 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 int status;
1583
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001584 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001585 data->rpc_done = 0;
1586 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001587 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001588 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001589 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001590 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001591 status = nfs4_wait_for_completion_rpc_task(task);
1592 if (status != 0) {
1593 data->cancelled = 1;
1594 smp_wmb();
1595 } else
1596 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001597 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 return status;
1599}
1600
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001601static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001603 struct nfs4_opendata *data = calldata;
1604 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001605 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001606
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001607 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001608 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001609 /*
1610 * Check if we still need to send an OPEN call, or if we can use
1611 * a delegation instead.
1612 */
1613 if (data->state != NULL) {
1614 struct nfs_delegation *delegation;
1615
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001616 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001617 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001618 rcu_read_lock();
1619 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001620 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
Trond Myklebustcd4c9be2013-04-23 14:46:25 -04001621 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
Trond Myklebust652f89f2011-12-09 19:05:58 -05001622 can_open_delegated(delegation, data->o_arg.fmode))
1623 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001624 rcu_read_unlock();
1625 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001626 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04001627 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust8188df12013-04-23 14:31:19 -04001628 switch (data->o_arg.claim) {
1629 case NFS4_OPEN_CLAIM_PREVIOUS:
1630 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1631 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001632 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04001633 case NFS4_OPEN_CLAIM_FH:
1634 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001635 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1636 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001637 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001638 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001639 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001640 &data->o_res.seq_res,
1641 task) != 0)
1642 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04001643
1644 /* Set the create mode (note dependency on the session type) */
1645 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1646 if (data->o_arg.open_flags & O_EXCL) {
1647 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1648 if (nfs4_has_persistent_session(clp))
1649 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1650 else if (clp->cl_mvops->minor_version > 0)
1651 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1652 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04001653 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001654unlock_no_action:
1655 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001656out_no_action:
1657 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001658out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001659 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001660}
1661
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001662static void nfs4_open_done(struct rpc_task *task, void *calldata)
1663{
1664 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001666 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001667
Trond Myklebust14516c32010-07-31 14:29:06 -04001668 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1669 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001670
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001671 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001672 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1673 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001674 case S_IFREG:
1675 break;
1676 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001677 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001678 break;
1679 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001680 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001681 break;
1682 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001683 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001684 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001685 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001686 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001687 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1688 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001689 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001690 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001691}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001692
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001693static void nfs4_open_release(void *calldata)
1694{
1695 struct nfs4_opendata *data = calldata;
1696 struct nfs4_state *state = NULL;
1697
1698 /* If this request hasn't been cancelled, do nothing */
1699 if (data->cancelled == 0)
1700 goto out_free;
1701 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001702 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001703 goto out_free;
1704 /* In case we need an open_confirm, no cleanup! */
1705 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1706 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001707 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001708 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001709 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001710out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001711 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001712}
1713
1714static const struct rpc_call_ops nfs4_open_ops = {
1715 .rpc_call_prepare = nfs4_open_prepare,
1716 .rpc_call_done = nfs4_open_done,
1717 .rpc_release = nfs4_open_release,
1718};
1719
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001720static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001721{
1722 struct inode *dir = data->dir->d_inode;
1723 struct nfs_server *server = NFS_SERVER(dir);
1724 struct nfs_openargs *o_arg = &data->o_arg;
1725 struct nfs_openres *o_res = &data->o_res;
1726 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001727 struct rpc_message msg = {
1728 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1729 .rpc_argp = o_arg,
1730 .rpc_resp = o_res,
1731 .rpc_cred = data->owner->so_cred,
1732 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001733 struct rpc_task_setup task_setup_data = {
1734 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001735 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001736 .callback_ops = &nfs4_open_ops,
1737 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001738 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001739 .flags = RPC_TASK_ASYNC,
1740 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001741 int status;
1742
Trond Myklebust9d12b212012-01-17 22:04:25 -05001743 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001744 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001745 data->rpc_done = 0;
1746 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001747 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001748 data->is_recover = 0;
1749 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001750 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001751 data->is_recover = 1;
1752 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04001753 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001754 if (IS_ERR(task))
1755 return PTR_ERR(task);
1756 status = nfs4_wait_for_completion_rpc_task(task);
1757 if (status != 0) {
1758 data->cancelled = 1;
1759 smp_wmb();
1760 } else
1761 status = data->rpc_status;
1762 rpc_put_task(task);
1763
1764 return status;
1765}
1766
1767static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1768{
1769 struct inode *dir = data->dir->d_inode;
1770 struct nfs_openres *o_res = &data->o_res;
1771 int status;
1772
1773 status = nfs4_run_open_task(data, 1);
1774 if (status != 0 || !data->rpc_done)
1775 return status;
1776
Trond Myklebust6926afd2012-01-07 13:22:46 -05001777 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1778
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001779 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1780 status = _nfs4_proc_open_confirm(data);
1781 if (status != 0)
1782 return status;
1783 }
1784
1785 return status;
1786}
1787
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001788static int nfs4_opendata_access(struct rpc_cred *cred,
1789 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001790 struct nfs4_state *state, fmode_t fmode,
1791 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001792{
1793 struct nfs_access_entry cache;
1794 u32 mask;
1795
1796 /* access call failed or for some reason the server doesn't
1797 * support any access modes -- defer access call until later */
1798 if (opendata->o_res.access_supported == 0)
1799 return 0;
1800
1801 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001802 /* don't check MAY_WRITE - a newly created file may not have
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001803 * write mode bits, but POSIX allows the creating process to write.
1804 * use openflags to check for exec, because fmode won't
1805 * always have FMODE_EXEC set when file open for exec. */
1806 if (openflags & __FMODE_EXEC) {
1807 /* ONLY check for exec rights */
1808 mask = MAY_EXEC;
1809 } else if (fmode & FMODE_READ)
1810 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001811
1812 cache.cred = cred;
1813 cache.jiffies = jiffies;
1814 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1815 nfs_access_add_cache(state->inode, &cache);
1816
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001817 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001818 return 0;
1819
1820 /* even though OPEN succeeded, access is denied. Close the file */
1821 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001822 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001823}
1824
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001825/*
1826 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1827 */
1828static int _nfs4_proc_open(struct nfs4_opendata *data)
1829{
1830 struct inode *dir = data->dir->d_inode;
1831 struct nfs_server *server = NFS_SERVER(dir);
1832 struct nfs_openargs *o_arg = &data->o_arg;
1833 struct nfs_openres *o_res = &data->o_res;
1834 int status;
1835
1836 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001837 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001838 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001839 if (status != 0) {
1840 if (status == -NFS4ERR_BADNAME &&
1841 !(o_arg->open_flags & O_CREAT))
1842 return -ENOENT;
1843 return status;
1844 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001845
Trond Myklebust6926afd2012-01-07 13:22:46 -05001846 nfs_fattr_map_and_free_names(server, &data->f_attr);
1847
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001848 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001849 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001850 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1851 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001853 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001855 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 }
1857 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
David Quigley1775fd32013-05-22 12:50:42 -04001858 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001859 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860}
1861
Andy Adamsond83217c2011-03-01 01:34:17 +00001862static int nfs4_recover_expired_lease(struct nfs_server *server)
1863{
1864 return nfs4_client_recover_expired_lease(server->nfs_client);
1865}
1866
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867/*
1868 * OPEN_EXPIRED:
1869 * reclaim state on the server after a network partition.
1870 * Assumes caller holds the appropriate lock
1871 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001872static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001874 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001875 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001877 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001878 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001879 if (IS_ERR(opendata))
1880 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001881 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001882 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001883 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001884 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001885 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886}
1887
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001888static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001889{
Trond Myklebust539cd032007-06-05 11:46:42 -04001890 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001891 struct nfs4_exception exception = { };
1892 int err;
1893
1894 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001895 err = _nfs4_open_expired(ctx, state);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001896 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1897 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001898 switch (err) {
1899 default:
1900 goto out;
1901 case -NFS4ERR_GRACE:
1902 case -NFS4ERR_DELAY:
1903 nfs4_handle_exception(server, err, &exception);
1904 err = 0;
1905 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001906 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001907out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001908 return err;
1909}
1910
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1912{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001914 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
Trond Myklebust864472e2006-01-03 09:55:15 +01001916 ctx = nfs4_state_find_open_context(state);
1917 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001918 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001919 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001920 put_nfs_open_context(ctx);
1921 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922}
1923
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001924#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001925static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001926{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001927 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001928 nfs4_stateid *stateid = &state->stateid;
1929 int status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001930
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001931 /* If a state reset has been done, test_stateid is unneeded */
1932 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1933 return;
1934
1935 status = nfs41_test_stateid(server, stateid);
1936 if (status != NFS_OK) {
1937 /* Free the stateid unless the server explicitly
1938 * informs us the stateid is unrecognized. */
1939 if (status != -NFS4ERR_BAD_STATEID)
1940 nfs41_free_stateid(server, stateid);
Bryan Schumaker69388672012-09-26 15:25:52 -04001941 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001942
Bryan Schumaker69388672012-09-26 15:25:52 -04001943 write_seqlock(&state->seqlock);
1944 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1945 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001946 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1947 }
1948}
1949
1950/**
1951 * nfs41_check_open_stateid - possibly free an open stateid
1952 *
1953 * @state: NFSv4 state for an inode
1954 *
1955 * Returns NFS_OK if recovery for this stateid is now finished.
1956 * Otherwise a negative NFS4ERR value is returned.
1957 */
1958static int nfs41_check_open_stateid(struct nfs4_state *state)
1959{
1960 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001961 nfs4_stateid *stateid = &state->open_stateid;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001962 int status;
1963
1964 /* If a state reset has been done, test_stateid is unneeded */
1965 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1966 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1967 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1968 return -NFS4ERR_BAD_STATEID;
1969
1970 status = nfs41_test_stateid(server, stateid);
1971 if (status != NFS_OK) {
1972 /* Free the stateid unless the server explicitly
1973 * informs us the stateid is unrecognized. */
1974 if (status != -NFS4ERR_BAD_STATEID)
1975 nfs41_free_stateid(server, stateid);
1976
1977 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1978 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1979 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001980 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001981 }
1982 return status;
1983}
1984
1985static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1986{
Chuck Levereb64cf92012-07-11 16:30:05 -04001987 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001988
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001989 nfs41_clear_delegation_stateid(state);
1990 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04001991 if (status != NFS_OK)
1992 status = nfs4_open_expired(sp, state);
1993 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001994}
1995#endif
1996
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001998 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1999 * fields corresponding to attributes that were used to store the verifier.
2000 * Make sure we clobber those fields in the later setattr call
2001 */
2002static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2003{
2004 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2005 !(sattr->ia_valid & ATTR_ATIME_SET))
2006 sattr->ia_valid |= ATTR_ATIME;
2007
2008 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2009 !(sattr->ia_valid & ATTR_MTIME_SET))
2010 sattr->ia_valid |= ATTR_MTIME;
2011}
2012
Trond Myklebustc21443c2013-02-07 14:26:21 -05002013static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2014 fmode_t fmode,
2015 int flags,
2016 struct nfs4_state **res)
2017{
2018 struct nfs4_state_owner *sp = opendata->owner;
2019 struct nfs_server *server = sp->so_server;
2020 struct nfs4_state *state;
2021 unsigned int seq;
2022 int ret;
2023
2024 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2025
2026 ret = _nfs4_proc_open(opendata);
2027 if (ret != 0)
2028 goto out;
2029
2030 state = nfs4_opendata_to_nfs4_state(opendata);
2031 ret = PTR_ERR(state);
2032 if (IS_ERR(state))
2033 goto out;
2034 if (server->caps & NFS_CAP_POSIX_LOCK)
2035 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2036
2037 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2038 if (ret != 0)
2039 goto out;
2040
Trond Myklebust91876b12013-03-28 14:01:33 -04002041 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
Trond Myklebustc21443c2013-02-07 14:26:21 -05002042 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002043 *res = state;
2044out:
2045 return ret;
2046}
2047
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002048/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002049 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 */
Andy Adamson82be4172012-05-23 05:02:35 -04002051static int _nfs4_do_open(struct inode *dir,
2052 struct dentry *dentry,
2053 fmode_t fmode,
2054 int flags,
2055 struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002056 struct nfs4_label *label,
Andy Adamson82be4172012-05-23 05:02:35 -04002057 struct rpc_cred *cred,
2058 struct nfs4_state **res,
2059 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060{
2061 struct nfs4_state_owner *sp;
2062 struct nfs4_state *state = NULL;
2063 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002064 struct nfs4_opendata *opendata;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002065 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002066 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002067 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
2069 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002071 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2072 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2074 goto out_err;
2075 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002076 status = nfs4_recover_expired_lease(server);
2077 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002078 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04002079 if (dentry->d_inode != NULL)
2080 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002081 status = -ENOMEM;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002082 if (dentry->d_inode)
2083 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002084 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002085 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002086 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002087 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
David Quigley14c43f72013-05-22 12:50:43 -04002089 if (label) {
2090 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2091 if (IS_ERR(olabel)) {
2092 status = PTR_ERR(olabel);
2093 goto err_opendata_put;
2094 }
2095 }
2096
Andy Adamson82be4172012-05-23 05:02:35 -04002097 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2098 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2099 if (!opendata->f_attr.mdsthreshold)
David Quigley14c43f72013-05-22 12:50:43 -04002100 goto err_free_label;
Trond Myklebust1549210f2012-06-05 09:16:47 -04002101 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002102 }
Al Viro82a2c1b2011-06-22 18:30:55 -04002103 if (dentry->d_inode != NULL)
2104 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002105
Trond Myklebustc21443c2013-02-07 14:26:21 -05002106 status = _nfs4_open_and_get_state(opendata, fmode, flags, &state);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002107 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002108 goto err_free_label;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002109
Trond Myklebust549b19c2013-04-16 18:42:34 -04002110 if ((opendata->o_arg.open_flags & O_EXCL) &&
2111 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002112 nfs4_exclusive_attrset(opendata, sattr);
2113
2114 nfs_fattr_init(opendata->o_res.f_attr);
2115 status = nfs4_do_setattr(state->inode, cred,
2116 opendata->o_res.f_attr, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002117 state, label, olabel);
2118 if (status == 0) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002119 nfs_setattr_update_inode(state->inode, sattr);
David Quigley1775fd32013-05-22 12:50:42 -04002120 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
David Quigleyaa9c2662013-05-22 12:50:44 -04002121 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
David Quigley1775fd32013-05-22 12:50:42 -04002122 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002123 }
Andy Adamson82be4172012-05-23 05:02:35 -04002124
2125 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2126 *ctx_th = opendata->f_attr.mdsthreshold;
2127 else
2128 kfree(opendata->f_attr.mdsthreshold);
2129 opendata->f_attr.mdsthreshold = NULL;
2130
David Quigley14c43f72013-05-22 12:50:43 -04002131 nfs4_label_free(olabel);
2132
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002133 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 *res = state;
2136 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002137err_free_label:
2138 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002139err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002140 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002141 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002142err_put_state_owner:
2143 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 *res = NULL;
2146 return status;
2147}
2148
2149
Andy Adamson82be4172012-05-23 05:02:35 -04002150static struct nfs4_state *nfs4_do_open(struct inode *dir,
2151 struct dentry *dentry,
2152 fmode_t fmode,
2153 int flags,
2154 struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002155 struct nfs4_label *label,
Andy Adamson82be4172012-05-23 05:02:35 -04002156 struct rpc_cred *cred,
2157 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002159 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 struct nfs4_exception exception = { };
2161 struct nfs4_state *res;
2162 int status;
2163
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002164 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 do {
David Quigley1775fd32013-05-22 12:50:42 -04002166 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, label, cred,
Andy Adamson82be4172012-05-23 05:02:35 -04002167 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 if (status == 0)
2169 break;
2170 /* NOTE: BAD_SEQID means the server and client disagree about the
2171 * book-keeping w.r.t. state-changing operations
2172 * (OPEN/CLOSE/LOCK/LOCKU...)
2173 * It is actually a sign of a bug on the client or on the server.
2174 *
2175 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002176 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 * have unhashed the old state_owner for us, and that we can
2178 * therefore safely retry using a new one. We should still warn
2179 * the user though...
2180 */
2181 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002182 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002183 " returned a bad sequence-id error!\n",
2184 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 exception.retry = 1;
2186 continue;
2187 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002188 /*
2189 * BAD_STATEID on OPEN means that the server cancelled our
2190 * state before it received the OPEN_CONFIRM.
2191 * Recover by retrying the request as per the discussion
2192 * on Page 181 of RFC3530.
2193 */
2194 if (status == -NFS4ERR_BAD_STATEID) {
2195 exception.retry = 1;
2196 continue;
2197 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002198 if (status == -EAGAIN) {
2199 /* We must have found a delegation */
2200 exception.retry = 1;
2201 continue;
2202 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002203 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2204 continue;
2205 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 status, &exception));
2207 } while (exception.retry);
2208 return res;
2209}
2210
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002211static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2212 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002213 struct nfs4_state *state, struct nfs4_label *ilabel,
2214 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002216 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002218 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 .iap = sattr,
2220 .server = server,
2221 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002222 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 };
2224 struct nfs_setattrres res = {
2225 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002226 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 .server = server,
2228 };
2229 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002230 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2231 .rpc_argp = &arg,
2232 .rpc_resp = &res,
2233 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002235 unsigned long timestamp = jiffies;
Trond Myklebustee3ae842013-04-29 10:35:36 -04002236 fmode_t fmode;
2237 bool truncate;
Trond Myklebust65e43082005-08-16 11:49:44 -04002238 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
David Quigleyaa9c2662013-05-22 12:50:44 -04002240 arg.bitmask = nfs4_bitmask(server, ilabel);
2241 if (ilabel)
2242 arg.bitmask = nfs4_bitmask(server, olabel);
2243
Trond Myklebust0e574af2005-10-27 22:12:38 -04002244 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245
Trond Myklebustee3ae842013-04-29 10:35:36 -04002246 /* Servers should only apply open mode checks for file size changes */
2247 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2248 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2249
2250 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2251 /* Use that stateid */
2252 } else if (truncate && state != NULL && nfs4_valid_open_stateid(state)) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002253 struct nfs_lockowner lockowner = {
2254 .l_owner = current->files,
2255 .l_pid = current->tgid,
2256 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002257 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002258 &lockowner);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002259 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002260 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
Bryan Schumaker7c513052011-03-24 17:12:24 +00002262 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002263 if (status == 0 && state != NULL)
2264 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002265 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266}
2267
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002268static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2269 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002270 struct nfs4_state *state, struct nfs4_label *ilabel,
2271 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002273 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002274 struct nfs4_exception exception = {
2275 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002276 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002277 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 int err;
2279 do {
David Quigley1775fd32013-05-22 12:50:42 -04002280 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
Trond Myklebust451146b2012-04-18 16:29:11 -04002281 switch (err) {
2282 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002283 if (!(sattr->ia_valid & ATTR_SIZE)) {
2284 pr_warn_once("NFSv4: server %s is incorrectly "
2285 "applying open mode checks to "
2286 "a SETATTR that is not "
2287 "changing file size.\n",
2288 server->nfs_client->cl_hostname);
2289 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002290 if (state && !(state->state & FMODE_WRITE)) {
2291 err = -EBADF;
2292 if (sattr->ia_valid & ATTR_OPEN)
2293 err = -EACCES;
2294 goto out;
2295 }
2296 }
2297 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002299out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 return err;
2301}
2302
2303struct nfs4_closedata {
2304 struct inode *inode;
2305 struct nfs4_state *state;
2306 struct nfs_closeargs arg;
2307 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002308 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002309 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002310 bool roc;
2311 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312};
2313
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002314static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002315{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002316 struct nfs4_closedata *calldata = data;
2317 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002318 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002319
Fred Isamanf7e89172011-01-06 11:36:32 +00002320 if (calldata->roc)
2321 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002322 nfs4_put_open_state(calldata->state);
2323 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002324 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002325 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002326 kfree(calldata);
2327}
2328
Trond Myklebust88069f72009-12-08 08:33:16 -05002329static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2330 fmode_t fmode)
2331{
2332 spin_lock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002333 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002334 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
2335 case FMODE_WRITE:
2336 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2337 break;
2338 case FMODE_READ:
2339 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2340 break;
2341 case 0:
2342 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2343 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2344 clear_bit(NFS_OPEN_STATE, &state->flags);
2345 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002346 spin_unlock(&state->owner->so_lock);
2347}
2348
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002349static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002351 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 struct nfs_server *server = NFS_SERVER(calldata->inode);
2354
Chuck Levera3ca5652012-03-01 17:00:40 -05002355 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002356 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2357 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 /* hmm. we are done with the inode, and in the process of freeing
2359 * the state_owner. we keep this around to process errors
2360 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 switch (task->tk_status) {
2362 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002363 if (calldata->roc)
2364 pnfs_roc_set_barrier(state->inode,
2365 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002366 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002367 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002368 nfs4_close_clear_stateid_flags(state,
2369 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 break;
2371 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002372 case -NFS4ERR_OLD_STATEID:
2373 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002375 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002376 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002378 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2379 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002381 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002382 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002383 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384}
2385
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002386static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002388 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002389 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002390 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002391 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002392
Chuck Levera3ca5652012-03-01 17:00:40 -05002393 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002394 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002395 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002396
Trond Myklebust88069f72009-12-08 08:33:16 -05002397 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2398 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002399 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002400 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002401 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002402 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002403 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2404 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2405 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002406 }
2407 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002408 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2409 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2410 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002411 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002412 }
Trond Myklebust5d422302013-03-14 16:57:48 -04002413 if (!nfs4_valid_open_stateid(state))
2414 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002415 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002416
2417 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002418 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002419 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002420 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002421
Fred Isamanf7e89172011-01-06 11:36:32 +00002422 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002423 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002424 if (calldata->roc &&
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002425 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2426 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002427 goto out_wait;
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002428 }
Fred Isamanf7e89172011-01-06 11:36:32 +00002429 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002430
Trond Myklebust516a6af2005-10-27 22:12:41 -04002431 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002432 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002433 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002434 &calldata->arg.seq_args,
2435 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002436 task) != 0)
2437 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002438 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002439 return;
2440out_no_action:
2441 task->tk_action = NULL;
2442out_wait:
2443 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444}
2445
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002446static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002447 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002448 .rpc_call_done = nfs4_close_done,
2449 .rpc_release = nfs4_free_closedata,
2450};
2451
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452/*
2453 * It is possible for data to be read/written from a mem-mapped file
2454 * after the sys_close call (which hits the vfs layer as a flush).
2455 * This means that we can't safely call nfsv4 close on a file until
2456 * the inode is cleared. This in turn means that we are not good
2457 * NFSv4 citizens - we do not indicate to the server to update the file's
2458 * share state even when we are done with one of the three share
2459 * stateid's in the inode.
2460 *
2461 * NOTE: Caller must be holding the sp->so_owner semaphore!
2462 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002463int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002465 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002467 struct nfs4_state_owner *sp = state->owner;
2468 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002469 struct rpc_message msg = {
2470 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2471 .rpc_cred = state->owner->so_cred,
2472 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002473 struct rpc_task_setup task_setup_data = {
2474 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002475 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002476 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002477 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002478 .flags = RPC_TASK_ASYNC,
2479 };
Trond Myklebust95121352005-10-18 14:20:12 -07002480 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002482 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002484 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002485 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002486 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002488 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002489 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002491 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002492 if (calldata->arg.seqid == NULL)
2493 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002494 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002495 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002496 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002497 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002498 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002499 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002500 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002501
Trond Myklebust1174dd12010-12-21 10:52:24 -05002502 msg.rpc_argp = &calldata->arg;
2503 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002504 task_setup_data.callback_data = calldata;
2505 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002506 if (IS_ERR(task))
2507 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002508 status = 0;
2509 if (wait)
2510 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002511 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002512 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002513out_free_calldata:
2514 kfree(calldata);
2515out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002516 nfs4_put_open_state(state);
2517 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002518 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519}
2520
Trond Myklebust2b484292010-09-17 10:56:51 -04002521static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002522nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04002525 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2526
2527 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528
Trond Myklebust565277f2007-10-15 18:17:53 -04002529 /* Protect against concurrent sillydeletes */
David Quigley1775fd32013-05-22 12:50:42 -04002530 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, label,
Andy Adamson82be4172012-05-23 05:02:35 -04002531 ctx->cred, &ctx->mdsthreshold);
David Quigleyaa9c2662013-05-22 12:50:44 -04002532
2533 nfs4_label_release_security(label);
2534
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002535 if (IS_ERR(state))
2536 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002537 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002538 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539}
2540
Trond Myklebust1185a552009-12-03 15:54:02 -05002541static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002542{
2543 if (ctx->state == NULL)
2544 return;
2545 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002546 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002547 else
Al Viro643168c2011-06-22 18:20:23 -04002548 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002549}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
2551static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2552{
Benny Halevy43652ad2009-04-01 09:21:54 -04002553 struct nfs4_server_caps_arg args = {
2554 .fhandle = fhandle,
2555 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 struct nfs4_server_caps_res res = {};
2557 struct rpc_message msg = {
2558 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002559 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 .rpc_resp = &res,
2561 };
2562 int status;
2563
Bryan Schumaker7c513052011-03-24 17:12:24 +00002564 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 if (status == 0) {
2566 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002567 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2568 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2569 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2570 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2571 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2573 server->caps |= NFS_CAP_ACLS;
2574 if (res.has_links != 0)
2575 server->caps |= NFS_CAP_HARDLINKS;
2576 if (res.has_symlinks != 0)
2577 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002578 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2579 server->caps |= NFS_CAP_FILEID;
2580 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2581 server->caps |= NFS_CAP_MODE;
2582 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2583 server->caps |= NFS_CAP_NLINK;
2584 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2585 server->caps |= NFS_CAP_OWNER;
2586 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2587 server->caps |= NFS_CAP_OWNER_GROUP;
2588 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2589 server->caps |= NFS_CAP_ATIME;
2590 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2591 server->caps |= NFS_CAP_CTIME;
2592 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2593 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04002594#ifdef CONFIG_NFS_V4_SECURITY_LABEL
2595 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
2596 server->caps |= NFS_CAP_SECURITY_LABEL;
2597#endif
2598 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
2599 sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002600
David Quigleyaa9c2662013-05-22 12:50:44 -04002601 if (server->caps & NFS_CAP_SECURITY_LABEL) {
2602 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2603 res.attr_bitmask[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2604 }
Trond Myklebusta65318b2009-03-11 14:10:28 -04002605 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2606 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2607 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002609 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002611
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 return status;
2613}
2614
Trond Myklebust55a97592006-06-09 09:34:19 -04002615int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616{
2617 struct nfs4_exception exception = { };
2618 int err;
2619 do {
2620 err = nfs4_handle_exception(server,
2621 _nfs4_server_capabilities(server, fhandle),
2622 &exception);
2623 } while (exception.retry);
2624 return err;
2625}
2626
2627static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2628 struct nfs_fsinfo *info)
2629{
David Quigleyaa9c2662013-05-22 12:50:44 -04002630 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04002632 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 };
2634 struct nfs4_lookup_res res = {
2635 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002636 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 .fh = fhandle,
2638 };
2639 struct rpc_message msg = {
2640 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2641 .rpc_argp = &args,
2642 .rpc_resp = &res,
2643 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002644
David Quigleyaa9c2662013-05-22 12:50:44 -04002645 bitmask[0] = nfs4_fattr_bitmap[0];
2646 bitmask[1] = nfs4_fattr_bitmap[1];
2647 /*
2648 * Process the label in the upcoming getfattr
2649 */
2650 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
2651
Trond Myklebust0e574af2005-10-27 22:12:38 -04002652 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002653 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654}
2655
2656static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2657 struct nfs_fsinfo *info)
2658{
2659 struct nfs4_exception exception = { };
2660 int err;
2661 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002662 err = _nfs4_lookup_root(server, fhandle, info);
2663 switch (err) {
2664 case 0:
2665 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002666 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002667 default:
2668 err = nfs4_handle_exception(server, err, &exception);
2669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002671out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 return err;
2673}
2674
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002675static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2676 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2677{
2678 struct rpc_auth *auth;
2679 int ret;
2680
2681 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002682 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04002683 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002684 goto out;
2685 }
2686 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002687out:
2688 return ret;
2689}
2690
Chuck Lever9a744ba2013-03-16 15:56:02 -04002691/*
2692 * Retry pseudoroot lookup with various security flavors. We do this when:
2693 *
2694 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2695 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2696 *
2697 * Returns zero on success, or a negative NFS4ERR value, or a
2698 * negative errno value.
2699 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002700static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002701 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702{
Chuck Lever9a744ba2013-03-16 15:56:02 -04002703 /* Per 3530bis 15.33.5 */
2704 static const rpc_authflavor_t flav_array[] = {
2705 RPC_AUTH_GSS_KRB5P,
2706 RPC_AUTH_GSS_KRB5I,
2707 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04002708 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04002709 RPC_AUTH_NULL,
2710 };
2711 int status = -EPERM;
2712 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713
Chuck Lever9a744ba2013-03-16 15:56:02 -04002714 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002715 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002716 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002717 continue;
2718 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002719 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04002720
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002721 /*
2722 * -EACCESS could mean that the user doesn't have correct permissions
2723 * to access the mount. It could also mean that we tried to mount
2724 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2725 * existing mount programs don't handle -EACCES very well so it should
2726 * be mapped to -EPERM instead.
2727 */
2728 if (status == -EACCES)
2729 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002730 return status;
2731}
2732
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002733static int nfs4_do_find_root_sec(struct nfs_server *server,
2734 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
2735{
2736 int mv = server->nfs_client->cl_minorversion;
2737 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
2738}
2739
2740/**
2741 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
2742 * @server: initialized nfs_server handle
2743 * @fhandle: we fill in the pseudo-fs root file handle
2744 * @info: we fill in an FSINFO struct
2745 *
2746 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002747 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002748int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2749 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002750{
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002751 int status;
2752
2753 status = nfs4_lookup_root(server, fhandle, info);
2754 if ((status == -NFS4ERR_WRONGSEC) &&
2755 !(server->flags & NFS_MOUNT_SECFLAVOUR))
2756 status = nfs4_do_find_root_sec(server, fhandle, info);
2757
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 if (status == 0)
2759 status = nfs4_server_capabilities(server, fhandle);
2760 if (status == 0)
2761 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002762
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002763 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764}
2765
Bryan Schumakerbae36242012-05-10 15:07:31 -04002766static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2767 struct nfs_fsinfo *info)
2768{
2769 int error;
2770 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04002771 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04002772
2773 error = nfs4_server_capabilities(server, mntfh);
2774 if (error < 0) {
2775 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2776 return error;
2777 }
2778
David Quigley14c43f72013-05-22 12:50:43 -04002779 label = nfs4_label_alloc(server, GFP_KERNEL);
2780 if (IS_ERR(label))
2781 return PTR_ERR(label);
2782
David Quigley1775fd32013-05-22 12:50:42 -04002783 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04002784 if (error < 0) {
2785 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04002786 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04002787 }
2788
2789 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2790 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2791 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2792
David Quigley14c43f72013-05-22 12:50:43 -04002793err_free_label:
2794 nfs4_label_free(label);
2795
Bryan Schumakerbae36242012-05-10 15:07:31 -04002796 return error;
2797}
2798
Manoj Naik6b97fd32006-06-09 09:34:29 -04002799/*
2800 * Get locations and (maybe) other attributes of a referral.
2801 * Note that we'll actually follow the referral later when
2802 * we detect fsid mismatch in inode revalidation
2803 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002804static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2805 const struct qstr *name, struct nfs_fattr *fattr,
2806 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002807{
2808 int status = -ENOMEM;
2809 struct page *page = NULL;
2810 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002811
2812 page = alloc_page(GFP_KERNEL);
2813 if (page == NULL)
2814 goto out;
2815 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2816 if (locations == NULL)
2817 goto out;
2818
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002819 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002820 if (status != 0)
2821 goto out;
2822 /* Make sure server returned a different fsid for the referral */
2823 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002824 dprintk("%s: server did not return a different fsid for"
2825 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002826 status = -EIO;
2827 goto out;
2828 }
Andy Adamson533eb462011-06-13 18:25:56 -04002829 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2830 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002831
Andy Adamson533eb462011-06-13 18:25:56 -04002832 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002833 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002834 memset(fhandle, 0, sizeof(struct nfs_fh));
2835out:
2836 if (page)
2837 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002838 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002839 return status;
2840}
2841
David Quigley1775fd32013-05-22 12:50:42 -04002842static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
2843 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844{
2845 struct nfs4_getattr_arg args = {
2846 .fh = fhandle,
2847 .bitmask = server->attr_bitmask,
2848 };
2849 struct nfs4_getattr_res res = {
2850 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002851 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 .server = server,
2853 };
2854 struct rpc_message msg = {
2855 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2856 .rpc_argp = &args,
2857 .rpc_resp = &res,
2858 };
David Quigleyaa9c2662013-05-22 12:50:44 -04002859
2860 args.bitmask = nfs4_bitmask(server, label);
2861
Trond Myklebust0e574af2005-10-27 22:12:38 -04002862 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002863 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864}
2865
David Quigley1775fd32013-05-22 12:50:42 -04002866static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
2867 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868{
2869 struct nfs4_exception exception = { };
2870 int err;
2871 do {
2872 err = nfs4_handle_exception(server,
David Quigley1775fd32013-05-22 12:50:42 -04002873 _nfs4_proc_getattr(server, fhandle, fattr, label),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 &exception);
2875 } while (exception.retry);
2876 return err;
2877}
2878
2879/*
2880 * The file is not closed if it is opened due to the a request to change
2881 * the size of the file. The open call will not be needed once the
2882 * VFS layer lookup-intents are implemented.
2883 *
2884 * Close is called when the inode is destroyed.
2885 * If we haven't opened the file for O_WRONLY, we
2886 * need to in the size_change case to obtain a stateid.
2887 *
2888 * Got race?
2889 * Because OPEN is always done by name in nfsv4, it is
2890 * possible that we opened a different file by the same
2891 * name. We can recognize this race condition, but we
2892 * can't do anything about it besides returning an error.
2893 *
2894 * This will be fixed with VFS changes (lookup-intent).
2895 */
2896static int
2897nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2898 struct iattr *sattr)
2899{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002900 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002901 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002902 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04002903 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 int status;
2905
Benny Halevy8a1636c2010-07-14 15:43:57 -04002906 if (pnfs_ld_layoutret_on_setattr(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04002907 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04002908
Trond Myklebust0e574af2005-10-27 22:12:38 -04002909 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
Andy Adamson26699402012-05-30 16:12:24 -04002911 /* Deal with open(O_TRUNC) */
2912 if (sattr->ia_valid & ATTR_OPEN)
2913 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2914
2915 /* Optimization: if the end result is no change, don't RPC */
2916 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2917 return 0;
2918
Trond Myklebustd5308382005-11-04 15:33:38 -05002919 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002920 if (sattr->ia_valid & ATTR_FILE) {
2921 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002922
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002923 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002924 if (ctx) {
2925 cred = ctx->cred;
2926 state = ctx->state;
2927 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002928 }
2929
David Quigley14c43f72013-05-22 12:50:43 -04002930 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
2931 if (IS_ERR(label))
2932 return PTR_ERR(label);
2933
2934 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04002935 if (status == 0) {
Trond Myklebust65e43082005-08-16 11:49:44 -04002936 nfs_setattr_update_inode(inode, sattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04002937 nfs_setsecurity(inode, fattr, label);
2938 }
David Quigley14c43f72013-05-22 12:50:43 -04002939 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 return status;
2941}
2942
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002943static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2944 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04002945 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04002946{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002947 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002948 int status;
2949 struct nfs4_lookup_arg args = {
2950 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002951 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002952 .name = name,
2953 };
2954 struct nfs4_lookup_res res = {
2955 .server = server,
2956 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04002957 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04002958 .fh = fhandle,
2959 };
2960 struct rpc_message msg = {
2961 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2962 .rpc_argp = &args,
2963 .rpc_resp = &res,
2964 };
2965
David Quigleyaa9c2662013-05-22 12:50:44 -04002966 args.bitmask = nfs4_bitmask(server, label);
2967
David Howells2b3de442006-08-22 20:06:09 -04002968 nfs_fattr_init(fattr);
2969
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002971 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 dprintk("NFS reply lookup: %d\n", status);
2973 return status;
2974}
2975
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002976static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002977{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002978 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002979 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002980 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2981 fattr->nlink = 2;
2982}
2983
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002984static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2985 struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04002986 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002987{
2988 struct nfs4_exception exception = { };
2989 struct rpc_clnt *client = *clnt;
2990 int err;
2991 do {
David Quigley1775fd32013-05-22 12:50:42 -04002992 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002993 switch (err) {
2994 case -NFS4ERR_BADNAME:
2995 err = -ENOENT;
2996 goto out;
2997 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002998 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002999 goto out;
3000 case -NFS4ERR_WRONGSEC:
3001 err = -EPERM;
3002 if (client != *clnt)
3003 goto out;
3004
3005 client = nfs4_create_sec_client(client, dir, name);
3006 if (IS_ERR(client))
3007 return PTR_ERR(client);
3008
3009 exception.retry = 1;
3010 break;
3011 default:
3012 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3013 }
3014 } while (exception.retry);
3015
3016out:
3017 if (err == 0)
3018 *clnt = client;
3019 else if (client != *clnt)
3020 rpc_shutdown_client(client);
3021
3022 return err;
3023}
3024
Bryan Schumaker80a16b22012-04-27 13:27:46 -04003025static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003026 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3027 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003029 int status;
3030 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003031
David Quigley1775fd32013-05-22 12:50:42 -04003032 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003033 if (client != NFS_CLIENT(dir)) {
3034 rpc_shutdown_client(client);
3035 nfs_fixup_secinfo_attributes(fattr);
3036 }
3037 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038}
3039
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003040struct rpc_clnt *
3041nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3042 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3043{
3044 int status;
3045 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
3046
David Quigley1775fd32013-05-22 12:50:42 -04003047 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003048 if (status < 0) {
3049 rpc_shutdown_client(client);
3050 return ERR_PTR(status);
3051 }
3052 return client;
3053}
3054
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3056{
Trond Myklebust76b32992007-08-10 17:45:11 -04003057 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 struct nfs4_accessargs args = {
3059 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003060 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003062 struct nfs4_accessres res = {
3063 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003064 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 struct rpc_message msg = {
3066 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3067 .rpc_argp = &args,
3068 .rpc_resp = &res,
3069 .rpc_cred = entry->cred,
3070 };
3071 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003072 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073
3074 /*
3075 * Determine which access bits we want to ask for...
3076 */
3077 if (mode & MAY_READ)
3078 args.access |= NFS4_ACCESS_READ;
3079 if (S_ISDIR(inode->i_mode)) {
3080 if (mode & MAY_WRITE)
3081 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3082 if (mode & MAY_EXEC)
3083 args.access |= NFS4_ACCESS_LOOKUP;
3084 } else {
3085 if (mode & MAY_WRITE)
3086 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3087 if (mode & MAY_EXEC)
3088 args.access |= NFS4_ACCESS_EXECUTE;
3089 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003090
3091 res.fattr = nfs_alloc_fattr();
3092 if (res.fattr == NULL)
3093 return -ENOMEM;
3094
Bryan Schumaker7c513052011-03-24 17:12:24 +00003095 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003097 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003098 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003100 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 return status;
3102}
3103
3104static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3105{
3106 struct nfs4_exception exception = { };
3107 int err;
3108 do {
3109 err = nfs4_handle_exception(NFS_SERVER(inode),
3110 _nfs4_proc_access(inode, entry),
3111 &exception);
3112 } while (exception.retry);
3113 return err;
3114}
3115
3116/*
3117 * TODO: For the time being, we don't try to get any attributes
3118 * along with any of the zero-copy operations READ, READDIR,
3119 * READLINK, WRITE.
3120 *
3121 * In the case of the first three, we want to put the GETATTR
3122 * after the read-type operation -- this is because it is hard
3123 * to predict the length of a GETATTR response in v4, and thus
3124 * align the READ data correctly. This means that the GETATTR
3125 * may end up partially falling into the page cache, and we should
3126 * shift it into the 'tail' of the xdr_buf before processing.
3127 * To do this efficiently, we need to know the total length
3128 * of data received, which doesn't seem to be available outside
3129 * of the RPC layer.
3130 *
3131 * In the case of WRITE, we also want to put the GETATTR after
3132 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003133 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 *
3135 * Both of these changes to the XDR layer would in fact be quite
3136 * minor, but I decided to leave them for a subsequent patch.
3137 */
3138static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3139 unsigned int pgbase, unsigned int pglen)
3140{
3141 struct nfs4_readlink args = {
3142 .fh = NFS_FH(inode),
3143 .pgbase = pgbase,
3144 .pglen = pglen,
3145 .pages = &page,
3146 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003147 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 struct rpc_message msg = {
3149 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3150 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003151 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 };
3153
Bryan Schumaker7c513052011-03-24 17:12:24 +00003154 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 -07003155}
3156
3157static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3158 unsigned int pgbase, unsigned int pglen)
3159{
3160 struct nfs4_exception exception = { };
3161 int err;
3162 do {
3163 err = nfs4_handle_exception(NFS_SERVER(inode),
3164 _nfs4_proc_readlink(inode, page, pgbase, pglen),
3165 &exception);
3166 } while (exception.retry);
3167 return err;
3168}
3169
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003171 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173static int
3174nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003175 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176{
David Quigleyaa9c2662013-05-22 12:50:44 -04003177 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003178 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 int status = 0;
3181
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003182 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3183 if (IS_ERR(ctx))
3184 return PTR_ERR(ctx);
3185
David Quigleyaa9c2662013-05-22 12:50:44 -04003186 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3187
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003188 sattr->ia_mode &= ~current_umask();
Trond Myklebust8626e4a2012-07-16 12:01:42 -04003189 state = nfs4_do_open(dir, dentry, ctx->mode,
David Quigley1775fd32013-05-22 12:50:42 -04003190 flags, sattr, ilabel, ctx->cred,
Trond Myklebust8626e4a2012-07-16 12:01:42 -04003191 &ctx->mdsthreshold);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04003192 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 if (IS_ERR(state)) {
3194 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003195 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04003197 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04003198 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003199 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003201 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003202 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 return status;
3204}
3205
3206static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3207{
Trond Myklebust16e42952005-10-27 22:12:44 -04003208 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003209 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003211 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003213 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003214 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003215 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003217 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3218 .rpc_argp = &args,
3219 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003221 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003222
Bryan Schumaker7c513052011-03-24 17:12:24 +00003223 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003224 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003225 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 return status;
3227}
3228
3229static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3230{
3231 struct nfs4_exception exception = { };
3232 int err;
3233 do {
3234 err = nfs4_handle_exception(NFS_SERVER(dir),
3235 _nfs4_proc_remove(dir, name),
3236 &exception);
3237 } while (exception.retry);
3238 return err;
3239}
3240
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003241static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003243 struct nfs_server *server = NFS_SERVER(dir);
3244 struct nfs_removeargs *args = msg->rpc_argp;
3245 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003247 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003249 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003250
3251 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252}
3253
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003254static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3255{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003256 nfs4_setup_sequence(NFS_SERVER(data->dir),
3257 &data->args.seq_args,
3258 &data->res.seq_res,
3259 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260}
3261
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003262static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003264 struct nfs_removeres *res = task->tk_msg.rpc_resp;
3265
Trond Myklebust14516c32010-07-31 14:29:06 -04003266 if (!nfs4_sequence_done(task, &res->seq_res))
3267 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003268 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003269 return 0;
3270 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003271 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272}
3273
Jeff Laytond3d41522010-09-17 17:31:57 -04003274static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3275{
3276 struct nfs_server *server = NFS_SERVER(dir);
3277 struct nfs_renameargs *arg = msg->rpc_argp;
3278 struct nfs_renameres *res = msg->rpc_resp;
3279
3280 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003281 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003282 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003283}
3284
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003285static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3286{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003287 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3288 &data->args.seq_args,
3289 &data->res.seq_res,
3290 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003291}
3292
3293static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3294 struct inode *new_dir)
3295{
3296 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3297
3298 if (!nfs4_sequence_done(task, &res->seq_res))
3299 return 0;
3300 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3301 return 0;
3302
3303 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003304 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003305 return 1;
3306}
3307
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3309 struct inode *new_dir, struct qstr *new_name)
3310{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003311 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003312 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 .old_dir = NFS_FH(old_dir),
3314 .new_dir = NFS_FH(new_dir),
3315 .old_name = old_name,
3316 .new_name = new_name,
3317 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003318 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003319 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003320 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 struct rpc_message msg = {
3322 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3323 .rpc_argp = &arg,
3324 .rpc_resp = &res,
3325 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003326 int status = -ENOMEM;
David Quigley14c43f72013-05-22 12:50:43 -04003327
Bryan Schumaker7c513052011-03-24 17:12:24 +00003328 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 if (!status) {
3330 update_changeattr(old_dir, &res.old_cinfo);
3331 update_changeattr(new_dir, &res.new_cinfo);
3332 }
3333 return status;
3334}
3335
3336static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3337 struct inode *new_dir, struct qstr *new_name)
3338{
3339 struct nfs4_exception exception = { };
3340 int err;
3341 do {
3342 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3343 _nfs4_proc_rename(old_dir, old_name,
3344 new_dir, new_name),
3345 &exception);
3346 } while (exception.retry);
3347 return err;
3348}
3349
3350static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3351{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003352 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 struct nfs4_link_arg arg = {
3354 .fh = NFS_FH(inode),
3355 .dir_fh = NFS_FH(dir),
3356 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003357 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003359 struct nfs4_link_res res = {
3360 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003361 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003362 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 struct rpc_message msg = {
3364 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3365 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003366 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003368 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369
Trond Myklebust136f2622010-04-16 16:22:49 -04003370 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003371 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003372 goto out;
3373
David Quigley14c43f72013-05-22 12:50:43 -04003374 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3375 if (IS_ERR(res.label)) {
3376 status = PTR_ERR(res.label);
3377 goto out;
3378 }
David Quigleyaa9c2662013-05-22 12:50:44 -04003379 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04003380
Bryan Schumaker7c513052011-03-24 17:12:24 +00003381 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003382 if (!status) {
3383 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04003384 status = nfs_post_op_update_inode(inode, res.fattr);
3385 if (!status)
3386 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003387 }
David Quigley14c43f72013-05-22 12:50:43 -04003388
3389
3390 nfs4_label_free(res.label);
3391
Trond Myklebust136f2622010-04-16 16:22:49 -04003392out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003393 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 return status;
3395}
3396
3397static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3398{
3399 struct nfs4_exception exception = { };
3400 int err;
3401 do {
3402 err = nfs4_handle_exception(NFS_SERVER(inode),
3403 _nfs4_proc_link(inode, dir, name),
3404 &exception);
3405 } while (exception.retry);
3406 return err;
3407}
3408
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003409struct nfs4_createdata {
3410 struct rpc_message msg;
3411 struct nfs4_create_arg arg;
3412 struct nfs4_create_res res;
3413 struct nfs_fh fh;
3414 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003415 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003416};
3417
3418static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3419 struct qstr *name, struct iattr *sattr, u32 ftype)
3420{
3421 struct nfs4_createdata *data;
3422
3423 data = kzalloc(sizeof(*data), GFP_KERNEL);
3424 if (data != NULL) {
3425 struct nfs_server *server = NFS_SERVER(dir);
3426
David Quigley14c43f72013-05-22 12:50:43 -04003427 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3428 if (IS_ERR(data->label))
3429 goto out_free;
3430
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003431 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3432 data->msg.rpc_argp = &data->arg;
3433 data->msg.rpc_resp = &data->res;
3434 data->arg.dir_fh = NFS_FH(dir);
3435 data->arg.server = server;
3436 data->arg.name = name;
3437 data->arg.attrs = sattr;
3438 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04003439 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003440 data->res.server = server;
3441 data->res.fh = &data->fh;
3442 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003443 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003444 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003445 }
3446 return data;
David Quigley14c43f72013-05-22 12:50:43 -04003447out_free:
3448 kfree(data);
3449 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003450}
3451
3452static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3453{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003454 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003455 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003456 if (status == 0) {
3457 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04003458 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003459 }
3460 return status;
3461}
3462
3463static void nfs4_free_createdata(struct nfs4_createdata *data)
3464{
David Quigley14c43f72013-05-22 12:50:43 -04003465 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003466 kfree(data);
3467}
3468
Chuck Lever4f390c12006-08-22 20:06:22 -04003469static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003470 struct page *page, unsigned int len, struct iattr *sattr,
3471 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003473 struct nfs4_createdata *data;
3474 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475
Chuck Lever94a6d752006-08-22 20:06:23 -04003476 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003477 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003478
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003479 status = -ENOMEM;
3480 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3481 if (data == NULL)
3482 goto out;
3483
3484 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3485 data->arg.u.symlink.pages = &page;
3486 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04003487 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003489 status = nfs4_do_create(dir, dentry, data);
3490
3491 nfs4_free_createdata(data);
3492out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 return status;
3494}
3495
Chuck Lever4f390c12006-08-22 20:06:22 -04003496static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003497 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498{
3499 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003500 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04003502
3503 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3504
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 do {
3506 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003507 _nfs4_proc_symlink(dir, dentry, page,
David Quigley1775fd32013-05-22 12:50:42 -04003508 len, sattr, label),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 &exception);
3510 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003511
3512 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 return err;
3514}
3515
3516static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003517 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003519 struct nfs4_createdata *data;
3520 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003522 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3523 if (data == NULL)
3524 goto out;
3525
David Quigley1775fd32013-05-22 12:50:42 -04003526 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003527 status = nfs4_do_create(dir, dentry, data);
3528
3529 nfs4_free_createdata(data);
3530out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 return status;
3532}
3533
3534static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3535 struct iattr *sattr)
3536{
3537 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003538 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003540
David Quigleyaa9c2662013-05-22 12:50:44 -04003541 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3542
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003543 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 do {
3545 err = nfs4_handle_exception(NFS_SERVER(dir),
David Quigley1775fd32013-05-22 12:50:42 -04003546 _nfs4_proc_mkdir(dir, dentry, sattr, label),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 &exception);
3548 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003549 nfs4_label_release_security(label);
3550
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 return err;
3552}
3553
3554static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003555 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556{
3557 struct inode *dir = dentry->d_inode;
3558 struct nfs4_readdir_arg args = {
3559 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003560 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 .pgbase = 0,
3562 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003563 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003564 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 };
3566 struct nfs4_readdir_res res;
3567 struct rpc_message msg = {
3568 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3569 .rpc_argp = &args,
3570 .rpc_resp = &res,
3571 .rpc_cred = cred,
3572 };
3573 int status;
3574
Harvey Harrison3110ff82008-05-02 13:42:44 -07003575 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003576 dentry->d_parent->d_name.name,
3577 dentry->d_name.name,
3578 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003579 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003581 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 -05003582 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003583 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003584 status += args.pgbase;
3585 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003586
3587 nfs_invalidate_atime(dir);
3588
Harvey Harrison3110ff82008-05-02 13:42:44 -07003589 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 return status;
3591}
3592
3593static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003594 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595{
3596 struct nfs4_exception exception = { };
3597 int err;
3598 do {
3599 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3600 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003601 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 &exception);
3603 } while (exception.retry);
3604 return err;
3605}
3606
3607static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04003608 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003610 struct nfs4_createdata *data;
3611 int mode = sattr->ia_mode;
3612 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003614 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3615 if (data == NULL)
3616 goto out;
3617
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003619 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003621 data->arg.ftype = NF4BLK;
3622 data->arg.u.device.specdata1 = MAJOR(rdev);
3623 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 }
3625 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003626 data->arg.ftype = NF4CHR;
3627 data->arg.u.device.specdata1 = MAJOR(rdev);
3628 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003629 } else if (!S_ISSOCK(mode)) {
3630 status = -EINVAL;
3631 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 }
David Quigley1775fd32013-05-22 12:50:42 -04003633
David Quigleyaa9c2662013-05-22 12:50:44 -04003634 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003635 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003636out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003637 nfs4_free_createdata(data);
3638out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 return status;
3640}
3641
3642static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3643 struct iattr *sattr, dev_t rdev)
3644{
3645 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003646 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003648
David Quigleyaa9c2662013-05-22 12:50:44 -04003649 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3650
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003651 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 do {
3653 err = nfs4_handle_exception(NFS_SERVER(dir),
David Quigleyaa9c2662013-05-22 12:50:44 -04003654 _nfs4_proc_mknod(dir, dentry, sattr, label, rdev),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 &exception);
3656 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003657
3658 nfs4_label_release_security(label);
3659
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 return err;
3661}
3662
3663static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3664 struct nfs_fsstat *fsstat)
3665{
3666 struct nfs4_statfs_arg args = {
3667 .fh = fhandle,
3668 .bitmask = server->attr_bitmask,
3669 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003670 struct nfs4_statfs_res res = {
3671 .fsstat = fsstat,
3672 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 struct rpc_message msg = {
3674 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3675 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003676 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 };
3678
Trond Myklebust0e574af2005-10-27 22:12:38 -04003679 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003680 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681}
3682
3683static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3684{
3685 struct nfs4_exception exception = { };
3686 int err;
3687 do {
3688 err = nfs4_handle_exception(server,
3689 _nfs4_proc_statfs(server, fhandle, fsstat),
3690 &exception);
3691 } while (exception.retry);
3692 return err;
3693}
3694
3695static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3696 struct nfs_fsinfo *fsinfo)
3697{
3698 struct nfs4_fsinfo_arg args = {
3699 .fh = fhandle,
3700 .bitmask = server->attr_bitmask,
3701 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003702 struct nfs4_fsinfo_res res = {
3703 .fsinfo = fsinfo,
3704 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 struct rpc_message msg = {
3706 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3707 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003708 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 };
3710
Bryan Schumaker7c513052011-03-24 17:12:24 +00003711 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712}
3713
3714static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3715{
3716 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04003717 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 int err;
3719
3720 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04003721 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
3722 if (err == 0) {
3723 struct nfs_client *clp = server->nfs_client;
3724
3725 spin_lock(&clp->cl_lock);
3726 clp->cl_lease_time = fsinfo->lease_time * HZ;
3727 clp->cl_last_renewal = now;
3728 spin_unlock(&clp->cl_lock);
3729 break;
3730 }
3731 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 } while (exception.retry);
3733 return err;
3734}
3735
3736static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3737{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003738 int error;
3739
Trond Myklebust0e574af2005-10-27 22:12:38 -04003740 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003741 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003742 if (error == 0) {
3743 /* block layout checks this! */
3744 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003745 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003746 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003747
3748 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749}
3750
3751static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3752 struct nfs_pathconf *pathconf)
3753{
3754 struct nfs4_pathconf_arg args = {
3755 .fh = fhandle,
3756 .bitmask = server->attr_bitmask,
3757 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003758 struct nfs4_pathconf_res res = {
3759 .pathconf = pathconf,
3760 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 struct rpc_message msg = {
3762 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3763 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003764 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 };
3766
3767 /* None of the pathconf attributes are mandatory to implement */
3768 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3769 memset(pathconf, 0, sizeof(*pathconf));
3770 return 0;
3771 }
3772
Trond Myklebust0e574af2005-10-27 22:12:38 -04003773 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003774 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775}
3776
3777static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3778 struct nfs_pathconf *pathconf)
3779{
3780 struct nfs4_exception exception = { };
3781 int err;
3782
3783 do {
3784 err = nfs4_handle_exception(server,
3785 _nfs4_proc_pathconf(server, fhandle, pathconf),
3786 &exception);
3787 } while (exception.retry);
3788 return err;
3789}
3790
Trond Myklebust5521abf2013-03-16 20:54:34 -04003791int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04003792 const struct nfs_open_context *ctx,
3793 const struct nfs_lock_context *l_ctx,
3794 fmode_t fmode)
3795{
3796 const struct nfs_lockowner *lockowner = NULL;
3797
3798 if (l_ctx != NULL)
3799 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003800 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04003801}
3802EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
3803
Trond Myklebust5521abf2013-03-16 20:54:34 -04003804static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
3805 const struct nfs_open_context *ctx,
3806 const struct nfs_lock_context *l_ctx,
3807 fmode_t fmode)
3808{
3809 nfs4_stateid current_stateid;
3810
3811 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode))
3812 return false;
3813 return nfs4_stateid_match(stateid, &current_stateid);
3814}
3815
3816static bool nfs4_error_stateid_expired(int err)
3817{
3818 switch (err) {
3819 case -NFS4ERR_DELEG_REVOKED:
3820 case -NFS4ERR_ADMIN_REVOKED:
3821 case -NFS4ERR_BAD_STATEID:
3822 case -NFS4ERR_STALE_STATEID:
3823 case -NFS4ERR_OLD_STATEID:
3824 case -NFS4ERR_OPENMODE:
3825 case -NFS4ERR_EXPIRED:
3826 return true;
3827 }
3828 return false;
3829}
3830
Benny Halevyd20581a2011-05-22 19:52:03 +03003831void __nfs4_read_done_cb(struct nfs_read_data *data)
3832{
Fred Isamancd841602012-04-20 14:47:44 -04003833 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003834}
3835
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003836static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837{
Fred Isamancd841602012-04-20 14:47:44 -04003838 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003840 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003841 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003842 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003844
Benny Halevyd20581a2011-05-22 19:52:03 +03003845 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003847 renew_lease(server, data->timestamp);
3848 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849}
3850
Trond Myklebust5521abf2013-03-16 20:54:34 -04003851static bool nfs4_read_stateid_changed(struct rpc_task *task,
3852 struct nfs_readargs *args)
3853{
3854
3855 if (!nfs4_error_stateid_expired(task->tk_status) ||
3856 nfs4_stateid_is_current(&args->stateid,
3857 args->context,
3858 args->lock_context,
3859 FMODE_READ))
3860 return false;
3861 rpc_restart_call_prepare(task);
3862 return true;
3863}
3864
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003865static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3866{
3867
3868 dprintk("--> %s\n", __func__);
3869
3870 if (!nfs4_sequence_done(task, &data->res.seq_res))
3871 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003872 if (nfs4_read_stateid_changed(task, &data->args))
3873 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003874 return data->read_done_cb ? data->read_done_cb(task, data) :
3875 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003876}
3877
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003878static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 data->timestamp = jiffies;
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003881 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003882 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003883 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884}
3885
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003886static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3887{
Trond Myklebust9b206142013-03-17 15:52:00 -04003888 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003889 &data->args.seq_args,
3890 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04003891 task))
3892 return;
3893 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3894 data->args.lock_context, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895}
3896
Fred Isamanb029bc92011-03-03 15:13:42 +00003897static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898{
Fred Isamancd841602012-04-20 14:47:44 -04003899 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003901 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003902 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003903 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003905 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003907 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003908 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003909 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910}
3911
Trond Myklebust5521abf2013-03-16 20:54:34 -04003912static bool nfs4_write_stateid_changed(struct rpc_task *task,
3913 struct nfs_writeargs *args)
3914{
3915
3916 if (!nfs4_error_stateid_expired(task->tk_status) ||
3917 nfs4_stateid_is_current(&args->stateid,
3918 args->context,
3919 args->lock_context,
3920 FMODE_WRITE))
3921 return false;
3922 rpc_restart_call_prepare(task);
3923 return true;
3924}
3925
Fred Isamanb029bc92011-03-03 15:13:42 +00003926static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3927{
3928 if (!nfs4_sequence_done(task, &data->res.seq_res))
3929 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003930 if (nfs4_write_stateid_changed(task, &data->args))
3931 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003932 return data->write_done_cb ? data->write_done_cb(task, data) :
3933 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003934}
3935
Trond Myklebust5a37f852012-04-28 14:55:16 -04003936static
3937bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003938{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003939 const struct nfs_pgio_header *hdr = data->header;
3940
3941 /* Don't request attributes for pNFS or O_DIRECT writes */
3942 if (data->ds_clp != NULL || hdr->dreq != NULL)
3943 return false;
3944 /* Otherwise, request attributes if and only if we don't hold
3945 * a delegation
3946 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003947 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003948}
Fred Isamana69aef12011-03-03 15:13:47 +00003949
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003950static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951{
Fred Isamancd841602012-04-20 14:47:44 -04003952 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003953
Trond Myklebust5a37f852012-04-28 14:55:16 -04003954 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003955 data->args.bitmask = NULL;
3956 data->res.fattr = NULL;
3957 } else
3958 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003959
Fred Isamanb029bc92011-03-03 15:13:42 +00003960 if (!data->write_done_cb)
3961 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003962 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 data->timestamp = jiffies;
3964
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003965 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003966 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967}
3968
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003969static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3970{
Trond Myklebust9b206142013-03-17 15:52:00 -04003971 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003972 &data->args.seq_args,
3973 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04003974 task))
3975 return;
3976 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3977 data->args.lock_context, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978}
3979
Fred Isaman0b7c0152012-04-20 14:47:39 -04003980static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3981{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003982 nfs4_setup_sequence(NFS_SERVER(data->inode),
3983 &data->args.seq_args,
3984 &data->res.seq_res,
3985 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986}
3987
Fred Isaman0b7c0152012-04-20 14:47:39 -04003988static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003991
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003992 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003993 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003994 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003996 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997}
3998
Fred Isaman0b7c0152012-04-20 14:47:39 -04003999static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004000{
4001 if (!nfs4_sequence_done(task, &data->res.seq_res))
4002 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004003 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004004}
4005
Fred Isaman0b7c0152012-04-20 14:47:39 -04004006static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004008 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004009
Fred Isaman0b7c0152012-04-20 14:47:39 -04004010 if (data->commit_done_cb == NULL)
4011 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004012 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004013 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05004014 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015}
4016
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004017struct nfs4_renewdata {
4018 struct nfs_client *client;
4019 unsigned long timestamp;
4020};
4021
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022/*
4023 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4024 * standalone procedure for queueing an asynchronous RENEW.
4025 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004026static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004027{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004028 struct nfs4_renewdata *data = calldata;
4029 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004030
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004031 if (atomic_read(&clp->cl_count) > 1)
4032 nfs4_schedule_state_renewal(clp);
4033 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004034 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004035}
4036
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004037static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004039 struct nfs4_renewdata *data = calldata;
4040 struct nfs_client *clp = data->client;
4041 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042
4043 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04004044 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004045 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4046 return;
4047 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004048 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004049 return;
4050 }
4051 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004053 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054}
4055
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004056static const struct rpc_call_ops nfs4_renew_ops = {
4057 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004058 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004059};
4060
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004061static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062{
4063 struct rpc_message msg = {
4064 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4065 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004066 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004068 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004070 if (renew_flags == 0)
4071 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004072 if (!atomic_inc_not_zero(&clp->cl_count))
4073 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004074 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004075 if (data == NULL)
4076 return -ENOMEM;
4077 data->client = clp;
4078 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004079 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004080 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081}
4082
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004083static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084{
4085 struct rpc_message msg = {
4086 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4087 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004088 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 };
4090 unsigned long now = jiffies;
4091 int status;
4092
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004093 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 if (status < 0)
4095 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004096 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 return 0;
4098}
4099
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004100static inline int nfs4_server_supports_acls(struct nfs_server *server)
4101{
4102 return (server->caps & NFS_CAP_ACLS)
4103 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
4104 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
4105}
4106
Trond Myklebust21f498c2012-08-24 10:59:25 -04004107/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4108 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004109 * the stack.
4110 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004111#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004112
Neil Hormane9e3d722011-03-04 19:26:03 -05004113static int buf_to_pages_noslab(const void *buf, size_t buflen,
4114 struct page **pages, unsigned int *pgbase)
4115{
4116 struct page *newpage, **spages;
4117 int rc = 0;
4118 size_t len;
4119 spages = pages;
4120
4121 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004122 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004123 newpage = alloc_page(GFP_KERNEL);
4124
4125 if (newpage == NULL)
4126 goto unwind;
4127 memcpy(page_address(newpage), buf, len);
4128 buf += len;
4129 buflen -= len;
4130 *pages++ = newpage;
4131 rc++;
4132 } while (buflen != 0);
4133
4134 return rc;
4135
4136unwind:
4137 for(; rc > 0; rc--)
4138 __free_page(spages[rc-1]);
4139 return -ENOMEM;
4140}
4141
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004142struct nfs4_cached_acl {
4143 int cached;
4144 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004145 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004146};
4147
4148static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004149{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004150 struct nfs_inode *nfsi = NFS_I(inode);
4151
4152 spin_lock(&inode->i_lock);
4153 kfree(nfsi->nfs4_acl);
4154 nfsi->nfs4_acl = acl;
4155 spin_unlock(&inode->i_lock);
4156}
4157
4158static void nfs4_zap_acl_attr(struct inode *inode)
4159{
4160 nfs4_set_cached_acl(inode, NULL);
4161}
4162
4163static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4164{
4165 struct nfs_inode *nfsi = NFS_I(inode);
4166 struct nfs4_cached_acl *acl;
4167 int ret = -ENOENT;
4168
4169 spin_lock(&inode->i_lock);
4170 acl = nfsi->nfs4_acl;
4171 if (acl == NULL)
4172 goto out;
4173 if (buf == NULL) /* user is just asking for length */
4174 goto out_len;
4175 if (acl->cached == 0)
4176 goto out;
4177 ret = -ERANGE; /* see getxattr(2) man page */
4178 if (acl->len > buflen)
4179 goto out;
4180 memcpy(buf, acl->data, acl->len);
4181out_len:
4182 ret = acl->len;
4183out:
4184 spin_unlock(&inode->i_lock);
4185 return ret;
4186}
4187
Sachin Prabhu5794d212012-04-17 14:36:40 +01004188static 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 +00004189{
4190 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004191 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004192
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004193 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004194 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004195 if (acl == NULL)
4196 goto out;
4197 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004198 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004199 } else {
4200 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4201 if (acl == NULL)
4202 goto out;
4203 acl->cached = 0;
4204 }
4205 acl->len = acl_len;
4206out:
4207 nfs4_set_cached_acl(inode, acl);
4208}
4209
Andy Adamsonbf118a32011-12-07 11:55:27 -05004210/*
4211 * The getxattr API returns the required buffer length when called with a
4212 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4213 * the required buf. On a NULL buf, we send a page of data to the server
4214 * guessing that the ACL request can be serviced by a page. If so, we cache
4215 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4216 * the cache. If not so, we throw away the page, and cache the required
4217 * length. The next getxattr call will then produce another round trip to
4218 * the server, this time with the input buf of the required size.
4219 */
Trond Myklebust16b42892006-08-24 12:27:15 -04004220static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004221{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004222 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004223 struct nfs_getaclargs args = {
4224 .fh = NFS_FH(inode),
4225 .acl_pages = pages,
4226 .acl_len = buflen,
4227 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004228 struct nfs_getaclres res = {
4229 .acl_len = buflen,
4230 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004231 struct rpc_message msg = {
4232 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4233 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004234 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004235 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004236 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4237 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004238
Andy Adamsonbf118a32011-12-07 11:55:27 -05004239 /* As long as we're doing a round trip to the server anyway,
4240 * let's be prepared for a page of acl data. */
4241 if (npages == 0)
4242 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004243 if (npages > ARRAY_SIZE(pages))
4244 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004245
Andy Adamsonbf118a32011-12-07 11:55:27 -05004246 for (i = 0; i < npages; i++) {
4247 pages[i] = alloc_page(GFP_KERNEL);
4248 if (!pages[i])
4249 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004250 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004251
4252 /* for decoding across pages */
4253 res.acl_scratch = alloc_page(GFP_KERNEL);
4254 if (!res.acl_scratch)
4255 goto out_free;
4256
Andy Adamsonbf118a32011-12-07 11:55:27 -05004257 args.acl_len = npages * PAGE_SIZE;
4258 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004259
Peng Taode040be2012-01-10 22:42:47 +08004260 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004261 __func__, buf, buflen, npages, args.acl_len);
4262 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4263 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004264 if (ret)
4265 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004266
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004267 /* Handle the case where the passed-in buffer is too short */
4268 if (res.acl_flags & NFS4_ACL_TRUNC) {
4269 /* Did the user only issue a request for the acl length? */
4270 if (buf == NULL)
4271 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004272 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004273 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004274 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004275 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004276 if (buf) {
4277 if (res.acl_len > buflen) {
4278 ret = -ERANGE;
4279 goto out_free;
4280 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004281 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004282 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004283out_ok:
4284 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004285out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004286 for (i = 0; i < npages; i++)
4287 if (pages[i])
4288 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004289 if (res.acl_scratch)
4290 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004291 return ret;
4292}
4293
Trond Myklebust16b42892006-08-24 12:27:15 -04004294static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4295{
4296 struct nfs4_exception exception = { };
4297 ssize_t ret;
4298 do {
4299 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4300 if (ret >= 0)
4301 break;
4302 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4303 } while (exception.retry);
4304 return ret;
4305}
4306
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004307static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4308{
4309 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004310 int ret;
4311
4312 if (!nfs4_server_supports_acls(server))
4313 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004314 ret = nfs_revalidate_inode(server, inode);
4315 if (ret < 0)
4316 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004317 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4318 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004319 ret = nfs4_read_cached_acl(inode, buf, buflen);
4320 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004321 /* -ENOENT is returned if there is no ACL or if there is an ACL
4322 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004323 return ret;
4324 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004325}
4326
Trond Myklebust16b42892006-08-24 12:27:15 -04004327static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004328{
4329 struct nfs_server *server = NFS_SERVER(inode);
4330 struct page *pages[NFS4ACL_MAXPAGES];
4331 struct nfs_setaclargs arg = {
4332 .fh = NFS_FH(inode),
4333 .acl_pages = pages,
4334 .acl_len = buflen,
4335 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004336 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004337 struct rpc_message msg = {
4338 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4339 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004340 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004341 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004342 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004343 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004344
4345 if (!nfs4_server_supports_acls(server))
4346 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004347 if (npages > ARRAY_SIZE(pages))
4348 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004349 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4350 if (i < 0)
4351 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004352 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004353 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004354
4355 /*
4356 * Free each page after tx, so the only ref left is
4357 * held by the network stack
4358 */
4359 for (; i > 0; i--)
4360 put_page(pages[i-1]);
4361
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004362 /*
4363 * Acl update can result in inode attribute update.
4364 * so mark the attribute cache invalid.
4365 */
4366 spin_lock(&inode->i_lock);
4367 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4368 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004369 nfs_access_zap_cache(inode);
4370 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004371 return ret;
4372}
4373
Trond Myklebust16b42892006-08-24 12:27:15 -04004374static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4375{
4376 struct nfs4_exception exception = { };
4377 int err;
4378 do {
4379 err = nfs4_handle_exception(NFS_SERVER(inode),
4380 __nfs4_proc_set_acl(inode, buf, buflen),
4381 &exception);
4382 } while (exception.retry);
4383 return err;
4384}
4385
David Quigleyaa9c2662013-05-22 12:50:44 -04004386#ifdef CONFIG_NFS_V4_SECURITY_LABEL
4387static int _nfs4_get_security_label(struct inode *inode, void *buf,
4388 size_t buflen)
4389{
4390 struct nfs_server *server = NFS_SERVER(inode);
4391 struct nfs_fattr fattr;
4392 struct nfs4_label label = {0, 0, buflen, buf};
4393
4394 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4395 struct nfs4_getattr_arg args = {
4396 .fh = NFS_FH(inode),
4397 .bitmask = bitmask,
4398 };
4399 struct nfs4_getattr_res res = {
4400 .fattr = &fattr,
4401 .label = &label,
4402 .server = server,
4403 };
4404 struct rpc_message msg = {
4405 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4406 .rpc_argp = &args,
4407 .rpc_resp = &res,
4408 };
4409 int ret;
4410
4411 nfs_fattr_init(&fattr);
4412
4413 ret = rpc_call_sync(server->client, &msg, 0);
4414 if (ret)
4415 return ret;
4416 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4417 return -ENOENT;
4418 if (buflen < label.len)
4419 return -ERANGE;
4420 return 0;
4421}
4422
4423static int nfs4_get_security_label(struct inode *inode, void *buf,
4424 size_t buflen)
4425{
4426 struct nfs4_exception exception = { };
4427 int err;
4428
4429 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4430 return -EOPNOTSUPP;
4431
4432 do {
4433 err = nfs4_handle_exception(NFS_SERVER(inode),
4434 _nfs4_get_security_label(inode, buf, buflen),
4435 &exception);
4436 } while (exception.retry);
4437 return err;
4438}
4439
4440static int _nfs4_do_set_security_label(struct inode *inode,
4441 struct nfs4_label *ilabel,
4442 struct nfs_fattr *fattr,
4443 struct nfs4_label *olabel)
4444{
4445
4446 struct iattr sattr = {0};
4447 struct nfs_server *server = NFS_SERVER(inode);
4448 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4449 struct nfs_setattrargs args = {
4450 .fh = NFS_FH(inode),
4451 .iap = &sattr,
4452 .server = server,
4453 .bitmask = bitmask,
4454 .label = ilabel,
4455 };
4456 struct nfs_setattrres res = {
4457 .fattr = fattr,
4458 .label = olabel,
4459 .server = server,
4460 };
4461 struct rpc_message msg = {
4462 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
4463 .rpc_argp = &args,
4464 .rpc_resp = &res,
4465 };
4466 int status;
4467
4468 nfs4_stateid_copy(&args.stateid, &zero_stateid);
4469
4470 status = rpc_call_sync(server->client, &msg, 0);
4471 if (status)
4472 dprintk("%s failed: %d\n", __func__, status);
4473
4474 return status;
4475}
4476
4477static int nfs4_do_set_security_label(struct inode *inode,
4478 struct nfs4_label *ilabel,
4479 struct nfs_fattr *fattr,
4480 struct nfs4_label *olabel)
4481{
4482 struct nfs4_exception exception = { };
4483 int err;
4484
4485 do {
4486 err = nfs4_handle_exception(NFS_SERVER(inode),
4487 _nfs4_do_set_security_label(inode, ilabel,
4488 fattr, olabel),
4489 &exception);
4490 } while (exception.retry);
4491 return err;
4492}
4493
4494static int
4495nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4496{
4497 struct nfs4_label ilabel, *olabel = NULL;
4498 struct nfs_fattr fattr;
4499 struct rpc_cred *cred;
4500 struct inode *inode = dentry->d_inode;
4501 int status;
4502
4503 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4504 return -EOPNOTSUPP;
4505
4506 nfs_fattr_init(&fattr);
4507
4508 ilabel.pi = 0;
4509 ilabel.lfs = 0;
4510 ilabel.label = (char *)buf;
4511 ilabel.len = buflen;
4512
4513 cred = rpc_lookup_cred();
4514 if (IS_ERR(cred))
4515 return PTR_ERR(cred);
4516
4517 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4518 if (IS_ERR(olabel)) {
4519 status = -PTR_ERR(olabel);
4520 goto out;
4521 }
4522
4523 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4524 if (status == 0)
4525 nfs_setsecurity(inode, &fattr, olabel);
4526
4527 nfs4_label_free(olabel);
4528out:
4529 put_rpccred(cred);
4530 return status;
4531}
4532#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4533
4534
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004536nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004538 struct nfs_client *clp = server->nfs_client;
4539
4540 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 return 0;
4542 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004543 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004544 case -NFS4ERR_ADMIN_REVOKED:
4545 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004546 if (state == NULL)
4547 break;
4548 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004549 case -NFS4ERR_OPENMODE:
4550 if (state == NULL)
4551 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004552 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4553 goto stateid_invalid;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004554 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004555 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004556 if (state != NULL) {
4557 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4558 goto stateid_invalid;
4559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004561 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004562 nfs4_schedule_lease_recovery(clp);
4563 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004564#if defined(CONFIG_NFS_V4_1)
4565 case -NFS4ERR_BADSESSION:
4566 case -NFS4ERR_BADSLOT:
4567 case -NFS4ERR_BAD_HIGH_SLOT:
4568 case -NFS4ERR_DEADSESSION:
4569 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4570 case -NFS4ERR_SEQ_FALSE_RETRY:
4571 case -NFS4ERR_SEQ_MISORDERED:
4572 dprintk("%s ERROR %d, Reset session\n", __func__,
4573 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004574 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004575 task->tk_status = 0;
4576 return -EAGAIN;
4577#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004579 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004580 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4582 task->tk_status = 0;
4583 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004584 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 case -NFS4ERR_OLD_STATEID:
4586 task->tk_status = 0;
4587 return -EAGAIN;
4588 }
4589 task->tk_status = nfs4_map_errors(task->tk_status);
4590 return 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004591stateid_invalid:
4592 task->tk_status = -EIO;
4593 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004594wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004595 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004596 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4597 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4598 task->tk_status = 0;
4599 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600}
4601
Chuck Leverf0920752012-05-21 22:45:41 -04004602static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4603 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004604{
4605 __be32 verf[2];
4606
Chuck Lever2c820d92012-05-21 22:45:33 -04004607 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4608 /* An impossible timestamp guarantees this value
4609 * will never match a generated boot time. */
4610 verf[0] = 0;
4611 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4612 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004613 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4614 verf[0] = (__be32)nn->boot_time.tv_sec;
4615 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004616 }
Chuck Levercd937102012-03-02 17:14:31 -05004617 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4618}
4619
Chuck Levere984a552012-09-14 17:24:21 -04004620static unsigned int
4621nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4622 char *buf, size_t len)
4623{
4624 unsigned int result;
4625
4626 rcu_read_lock();
4627 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4628 clp->cl_ipaddr,
4629 rpc_peeraddr2str(clp->cl_rpcclient,
4630 RPC_DISPLAY_ADDR),
4631 rpc_peeraddr2str(clp->cl_rpcclient,
4632 RPC_DISPLAY_PROTO));
4633 rcu_read_unlock();
4634 return result;
4635}
4636
4637static unsigned int
4638nfs4_init_uniform_client_string(const struct nfs_client *clp,
4639 char *buf, size_t len)
4640{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004641 char *nodename = clp->cl_rpcclient->cl_nodename;
4642
4643 if (nfs4_client_id_uniquifier[0] != '\0')
4644 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004645 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4646 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004647 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004648}
4649
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004650/**
4651 * nfs4_proc_setclientid - Negotiate client ID
4652 * @clp: state data structure
4653 * @program: RPC program for NFSv4 callback service
4654 * @port: IP port number for NFS4 callback service
4655 * @cred: RPC credential to use for this call
4656 * @res: where to place the result
4657 *
4658 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4659 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004660int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4661 unsigned short port, struct rpc_cred *cred,
4662 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663{
4664 nfs4_verifier sc_verifier;
4665 struct nfs4_setclientid setclientid = {
4666 .sc_verifier = &sc_verifier,
4667 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004668 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 };
4670 struct rpc_message msg = {
4671 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4672 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004673 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004674 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004676 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677
Chuck Leverde734832012-07-11 16:30:50 -04004678 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004679 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004680 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4681 setclientid.sc_name_len =
4682 nfs4_init_uniform_client_string(clp,
4683 setclientid.sc_name,
4684 sizeof(setclientid.sc_name));
4685 else
4686 setclientid.sc_name_len =
4687 nfs4_init_nonuniform_client_string(clp,
4688 setclientid.sc_name,
4689 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004690 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004691 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004692 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004693 sizeof(setclientid.sc_netid),
4694 rpc_peeraddr2str(clp->cl_rpcclient,
4695 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004696 rcu_read_unlock();
4697 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004698 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 clp->cl_ipaddr, port >> 8, port & 255);
4700
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004701 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4702 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4703 setclientid.sc_name_len, setclientid.sc_name);
4704 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4705 dprintk("NFS reply setclientid: %d\n", status);
4706 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707}
4708
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004709/**
4710 * nfs4_proc_setclientid_confirm - Confirm client ID
4711 * @clp: state data structure
4712 * @res: result of a previous SETCLIENTID
4713 * @cred: RPC credential to use for this call
4714 *
4715 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4716 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004717int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004718 struct nfs4_setclientid_res *arg,
4719 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 struct rpc_message msg = {
4722 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004723 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004724 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726 int status;
4727
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004728 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4729 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4730 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004731 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004732 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 return status;
4734}
4735
Trond Myklebustfe650402006-01-03 09:55:18 +01004736struct nfs4_delegreturndata {
4737 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004738 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004739 struct nfs_fh fh;
4740 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004741 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004742 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004743 int rpc_status;
4744};
4745
Trond Myklebustfe650402006-01-03 09:55:18 +01004746static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4747{
4748 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004749
Trond Myklebust14516c32010-07-31 14:29:06 -04004750 if (!nfs4_sequence_done(task, &data->res.seq_res))
4751 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004752
Ricardo Labiaga79708862009-12-07 09:23:21 -05004753 switch (task->tk_status) {
4754 case -NFS4ERR_STALE_STATEID:
4755 case -NFS4ERR_EXPIRED:
4756 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004757 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004758 break;
4759 default:
4760 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4761 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004762 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004763 return;
4764 }
4765 }
4766 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004767}
4768
4769static void nfs4_delegreturn_release(void *calldata)
4770{
Trond Myklebustfe650402006-01-03 09:55:18 +01004771 kfree(calldata);
4772}
4773
Andy Adamson938e1012009-04-01 09:22:28 -04004774#if defined(CONFIG_NFS_V4_1)
4775static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4776{
4777 struct nfs4_delegreturndata *d_data;
4778
4779 d_data = (struct nfs4_delegreturndata *)data;
4780
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004781 nfs4_setup_sequence(d_data->res.server,
4782 &d_data->args.seq_args,
4783 &d_data->res.seq_res,
4784 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004785}
4786#endif /* CONFIG_NFS_V4_1 */
4787
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004788static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004789#if defined(CONFIG_NFS_V4_1)
4790 .rpc_call_prepare = nfs4_delegreturn_prepare,
4791#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004792 .rpc_call_done = nfs4_delegreturn_done,
4793 .rpc_release = nfs4_delegreturn_release,
4794};
4795
Trond Myklebuste6f81072008-01-24 18:14:34 -05004796static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004797{
4798 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004799 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004800 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004801 struct rpc_message msg = {
4802 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4803 .rpc_cred = cred,
4804 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004805 struct rpc_task_setup task_setup_data = {
4806 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004807 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004808 .callback_ops = &nfs4_delegreturn_ops,
4809 .flags = RPC_TASK_ASYNC,
4810 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004811 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004812
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004813 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004814 if (data == NULL)
4815 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004816 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004817 data->args.fhandle = &data->fh;
4818 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004819 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004820 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004821 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004822 data->res.fattr = &data->fattr;
4823 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004824 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004825 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004826 data->rpc_status = 0;
4827
Trond Myklebustc970aa82007-07-14 15:39:59 -04004828 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004829 msg.rpc_argp = &data->args;
4830 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004831 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004832 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004833 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004834 if (!issync)
4835 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004836 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004837 if (status != 0)
4838 goto out;
4839 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004840 if (status == 0)
4841 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4842 else
4843 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004844out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004845 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004846 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847}
4848
Trond Myklebuste6f81072008-01-24 18:14:34 -05004849int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850{
4851 struct nfs_server *server = NFS_SERVER(inode);
4852 struct nfs4_exception exception = { };
4853 int err;
4854 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004855 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 switch (err) {
4857 case -NFS4ERR_STALE_STATEID:
4858 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859 case 0:
4860 return 0;
4861 }
4862 err = nfs4_handle_exception(server, err, &exception);
4863 } while (exception.retry);
4864 return err;
4865}
4866
4867#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4868#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4869
4870/*
4871 * sleep, with exponential backoff, and retry the LOCK operation.
4872 */
4873static unsigned long
4874nfs4_set_lock_task_retry(unsigned long timeout)
4875{
Jeff Laytond3103102011-12-01 22:44:39 +01004876 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877 timeout <<= 1;
4878 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4879 return NFS4_LOCK_MAXTIMEOUT;
4880 return timeout;
4881}
4882
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4884{
4885 struct inode *inode = state->inode;
4886 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004887 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004888 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004890 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004892 struct nfs_lockt_res res = {
4893 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894 };
4895 struct rpc_message msg = {
4896 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4897 .rpc_argp = &arg,
4898 .rpc_resp = &res,
4899 .rpc_cred = state->owner->so_cred,
4900 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901 struct nfs4_lock_state *lsp;
4902 int status;
4903
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004904 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004905 status = nfs4_set_lock_state(state, request);
4906 if (status != 0)
4907 goto out;
4908 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004909 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004910 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004911 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004912 switch (status) {
4913 case 0:
4914 request->fl_type = F_UNLCK;
4915 break;
4916 case -NFS4ERR_DENIED:
4917 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004919 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004920out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 return status;
4922}
4923
4924static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4925{
4926 struct nfs4_exception exception = { };
4927 int err;
4928
4929 do {
4930 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4931 _nfs4_proc_getlk(state, cmd, request),
4932 &exception);
4933 } while (exception.retry);
4934 return err;
4935}
4936
4937static int do_vfs_lock(struct file *file, struct file_lock *fl)
4938{
4939 int res = 0;
4940 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4941 case FL_POSIX:
4942 res = posix_lock_file_wait(file, fl);
4943 break;
4944 case FL_FLOCK:
4945 res = flock_lock_file_wait(file, fl);
4946 break;
4947 default:
4948 BUG();
4949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950 return res;
4951}
4952
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004953struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004954 struct nfs_locku_args arg;
4955 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004956 struct nfs4_lock_state *lsp;
4957 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004958 struct file_lock fl;
4959 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004960 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004961};
4962
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004963static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4964 struct nfs_open_context *ctx,
4965 struct nfs4_lock_state *lsp,
4966 struct nfs_seqid *seqid)
4967{
4968 struct nfs4_unlockdata *p;
4969 struct inode *inode = lsp->ls_state->inode;
4970
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004971 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004972 if (p == NULL)
4973 return NULL;
4974 p->arg.fh = NFS_FH(inode);
4975 p->arg.fl = &p->fl;
4976 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004977 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004978 p->arg.stateid = &lsp->ls_stateid;
4979 p->lsp = lsp;
4980 atomic_inc(&lsp->ls_count);
4981 /* Ensure we don't close file until we're done freeing locks! */
4982 p->ctx = get_nfs_open_context(ctx);
4983 memcpy(&p->fl, fl, sizeof(p->fl));
4984 p->server = NFS_SERVER(inode);
4985 return p;
4986}
4987
Trond Myklebust06f814a2006-01-03 09:55:07 +01004988static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004989{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004990 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004991 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004992 nfs4_put_lock_state(calldata->lsp);
4993 put_nfs_open_context(calldata->ctx);
4994 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004995}
4996
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004997static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004998{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004999 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005000
Trond Myklebust14516c32010-07-31 14:29:06 -04005001 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5002 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005003 switch (task->tk_status) {
5004 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05005005 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
5006 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005007 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005008 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005009 case -NFS4ERR_BAD_STATEID:
5010 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005011 case -NFS4ERR_STALE_STATEID:
5012 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005013 break;
5014 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005015 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005016 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005017 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005018 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005019}
5020
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005021static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005022{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005023 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005025 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005026 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04005027 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005028 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005029 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005030 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005031 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005032 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005033 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005034 &calldata->res.seq_res,
5035 task) != 0)
5036 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005037 return;
5038out_no_action:
5039 task->tk_action = NULL;
5040out_wait:
5041 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042}
5043
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005044static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005045 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005046 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005047 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005048};
5049
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005050static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5051 struct nfs_open_context *ctx,
5052 struct nfs4_lock_state *lsp,
5053 struct nfs_seqid *seqid)
5054{
5055 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005056 struct rpc_message msg = {
5057 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5058 .rpc_cred = ctx->cred,
5059 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005060 struct rpc_task_setup task_setup_data = {
5061 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005062 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005063 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005064 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005065 .flags = RPC_TASK_ASYNC,
5066 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005067
Frank Filz137d6ac2007-07-09 15:32:29 -07005068 /* Ensure this is an unlock - when canceling a lock, the
5069 * canceled lock is passed in, and it won't be an unlock.
5070 */
5071 fl->fl_type = F_UNLCK;
5072
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005073 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5074 if (data == NULL) {
5075 nfs_free_seqid(seqid);
5076 return ERR_PTR(-ENOMEM);
5077 }
5078
Trond Myklebust9d12b212012-01-17 22:04:25 -05005079 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005080 msg.rpc_argp = &data->arg;
5081 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005082 task_setup_data.callback_data = data;
5083 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005084}
5085
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5087{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005088 struct inode *inode = state->inode;
5089 struct nfs4_state_owner *sp = state->owner;
5090 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005091 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005092 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005093 struct rpc_task *task;
5094 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005095 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096
Trond Myklebust9b073572006-06-29 16:38:34 -04005097 status = nfs4_set_lock_state(state, request);
5098 /* Unlock _before_ we do the RPC call */
5099 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005100 /* Exclude nfs_delegation_claim_locks() */
5101 mutex_lock(&sp->so_delegreturn_mutex);
5102 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005103 down_read(&nfsi->rwsem);
5104 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
5105 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005106 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005107 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005108 }
5109 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005110 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005111 if (status != 0)
5112 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005113 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005114 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005115 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5116 goto out;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005117 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005118 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005119 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04005120 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005121 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005122 status = PTR_ERR(task);
5123 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005124 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005125 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005126 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005127out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005128 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005129 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130}
5131
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005132struct nfs4_lockdata {
5133 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005134 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005135 struct nfs4_lock_state *lsp;
5136 struct nfs_open_context *ctx;
5137 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005138 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005139 int rpc_status;
5140 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005141 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005142};
5143
5144static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005145 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5146 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005147{
5148 struct nfs4_lockdata *p;
5149 struct inode *inode = lsp->ls_state->inode;
5150 struct nfs_server *server = NFS_SERVER(inode);
5151
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005152 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005153 if (p == NULL)
5154 return NULL;
5155
5156 p->arg.fh = NFS_FH(inode);
5157 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005158 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005159 if (p->arg.open_seqid == NULL)
5160 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005161 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005162 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005163 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005164 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04005165 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005166 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005167 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005168 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005169 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005170 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005171 atomic_inc(&lsp->ls_count);
5172 p->ctx = get_nfs_open_context(ctx);
5173 memcpy(&p->fl, fl, sizeof(p->fl));
5174 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005175out_free_seqid:
5176 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005177out_free:
5178 kfree(p);
5179 return NULL;
5180}
5181
5182static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5183{
5184 struct nfs4_lockdata *data = calldata;
5185 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186
Harvey Harrison3110ff82008-05-02 13:42:44 -07005187 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005188 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005189 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005190 /* Do we need to do an open_to_lock_owner? */
5191 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005192 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005193 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005194 }
Trond Myklebust8e472f32013-04-20 01:30:53 -04005195 data->arg.open_stateid = &state->open_stateid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005196 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005197 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005198 } else
5199 data->arg.new_lock_owner = 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04005200 if (!nfs4_valid_open_stateid(state)) {
5201 data->rpc_status = -EBADF;
5202 task->tk_action = NULL;
5203 goto out_release_open_seqid;
5204 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005205 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005206 if (nfs4_setup_sequence(data->server,
5207 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005208 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005209 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04005210 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04005211out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005212 nfs_release_seqid(data->arg.open_seqid);
5213out_release_lock_seqid:
5214 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005215out_wait:
5216 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005217 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005218}
5219
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005220static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5221{
5222 struct nfs4_lockdata *data = calldata;
5223
Harvey Harrison3110ff82008-05-02 13:42:44 -07005224 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005225
Trond Myklebust14516c32010-07-31 14:29:06 -04005226 if (!nfs4_sequence_done(task, &data->res.seq_res))
5227 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04005228
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005229 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005230 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005231 if (data->rpc_status == 0)
5232 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
5233 else
5234 goto out;
5235 }
5236 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05005237 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005238 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04005239 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005240 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005241out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07005242 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005243}
5244
5245static void nfs4_lock_release(void *calldata)
5246{
5247 struct nfs4_lockdata *data = calldata;
5248
Harvey Harrison3110ff82008-05-02 13:42:44 -07005249 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005250 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005251 if (data->cancelled != 0) {
5252 struct rpc_task *task;
5253 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5254 data->arg.lock_seqid);
5255 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08005256 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005257 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005258 } else
5259 nfs_free_seqid(data->arg.lock_seqid);
5260 nfs4_put_lock_state(data->lsp);
5261 put_nfs_open_context(data->ctx);
5262 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005263 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005264}
5265
5266static const struct rpc_call_ops nfs4_lock_ops = {
5267 .rpc_call_prepare = nfs4_lock_prepare,
5268 .rpc_call_done = nfs4_lock_done,
5269 .rpc_release = nfs4_lock_release,
5270};
5271
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005272static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5273{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005274 switch (error) {
5275 case -NFS4ERR_ADMIN_REVOKED:
5276 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05005277 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005278 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04005279 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05005280 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005281 break;
5282 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005283 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05005284 case -NFS4ERR_EXPIRED:
5285 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005286 };
5287}
5288
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005289static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005290{
5291 struct nfs4_lockdata *data;
5292 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005293 struct rpc_message msg = {
5294 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5295 .rpc_cred = state->owner->so_cred,
5296 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005297 struct rpc_task_setup task_setup_data = {
5298 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005299 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005300 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005301 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005302 .flags = RPC_TASK_ASYNC,
5303 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005304 int ret;
5305
Harvey Harrison3110ff82008-05-02 13:42:44 -07005306 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005307 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005308 fl->fl_u.nfs4_fl.owner,
5309 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005310 if (data == NULL)
5311 return -ENOMEM;
5312 if (IS_SETLKW(cmd))
5313 data->arg.block = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -05005314 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005315 msg.rpc_argp = &data->arg;
5316 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005317 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005318 if (recovery_type > NFS_LOCK_NEW) {
5319 if (recovery_type == NFS_LOCK_RECLAIM)
5320 data->arg.reclaim = NFS_LOCK_RECLAIM;
5321 nfs4_set_sequence_privileged(&data->arg.seq_args);
5322 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04005323 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005324 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005325 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005326 ret = nfs4_wait_for_completion_rpc_task(task);
5327 if (ret == 0) {
5328 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005329 if (ret)
5330 nfs4_handle_setlk_error(data->server, data->lsp,
5331 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005332 } else
5333 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005334 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005335 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005336 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337}
5338
5339static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5340{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005341 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005342 struct nfs4_exception exception = {
5343 .inode = state->inode,
5344 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005345 int err;
5346
5347 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005348 /* Cache the lock if possible... */
5349 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5350 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005351 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04005352 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00005353 break;
5354 nfs4_handle_exception(server, err, &exception);
5355 } while (exception.retry);
5356 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357}
5358
5359static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5360{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005361 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005362 struct nfs4_exception exception = {
5363 .inode = state->inode,
5364 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005365 int err;
5366
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005367 err = nfs4_set_lock_state(state, request);
5368 if (err != 0)
5369 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00005370 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005371 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5372 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005373 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005374 switch (err) {
5375 default:
5376 goto out;
5377 case -NFS4ERR_GRACE:
5378 case -NFS4ERR_DELAY:
5379 nfs4_handle_exception(server, err, &exception);
5380 err = 0;
5381 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005382 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005383out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00005384 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385}
5386
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005387#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005388/**
5389 * nfs41_check_expired_locks - possibly free a lock stateid
5390 *
5391 * @state: NFSv4 state for an inode
5392 *
5393 * Returns NFS_OK if recovery for this stateid is now finished.
5394 * Otherwise a negative NFS4ERR value is returned.
5395 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005396static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005397{
Chuck Levereb64cf92012-07-11 16:30:05 -04005398 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005399 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005400 struct nfs_server *server = NFS_SERVER(state->inode);
5401
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005402 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04005403 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005404 status = nfs41_test_stateid(server, &lsp->ls_stateid);
5405 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005406 /* Free the stateid unless the server
5407 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04005408 if (status != -NFS4ERR_BAD_STATEID)
5409 nfs41_free_stateid(server,
5410 &lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005411 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005412 ret = status;
5413 }
5414 }
5415 };
5416
5417 return ret;
5418}
5419
5420static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5421{
5422 int status = NFS_OK;
5423
5424 if (test_bit(LK_STATE_IN_USE, &state->flags))
5425 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005426 if (status != NFS_OK)
5427 status = nfs4_lock_expired(state, request);
5428 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005429}
5430#endif
5431
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5433{
Trond Myklebust9a99af42013-02-04 20:17:49 -05005434 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005435 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005436 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af42013-02-04 20:17:49 -05005437 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005438 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005440 if ((fl_flags & FL_POSIX) &&
5441 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5442 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005443 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005444 status = nfs4_set_lock_state(state, request);
5445 if (status != 0)
5446 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005447 request->fl_flags |= FL_ACCESS;
5448 status = do_vfs_lock(request->fl_file, request);
5449 if (status < 0)
5450 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005451 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005452 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005453 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005454 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005455 request->fl_flags = fl_flags & ~FL_SLEEP;
5456 status = do_vfs_lock(request->fl_file, request);
5457 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005458 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05005459 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5460 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005461 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005462 if (status != 0)
Trond Myklebust9a99af42013-02-04 20:17:49 -05005463 goto out;
5464 down_read(&nfsi->rwsem);
5465 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5466 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005467 goto out_unlock;
Trond Myklebust9a99af42013-02-04 20:17:49 -05005468 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005469 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005470 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005471 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005472 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5473 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005474out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005475 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005476out:
5477 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 return status;
5479}
5480
5481static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5482{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005483 struct nfs4_exception exception = {
5484 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005485 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005486 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 int err;
5488
5489 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005490 err = _nfs4_proc_setlk(state, cmd, request);
5491 if (err == -NFS4ERR_DENIED)
5492 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005494 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 } while (exception.retry);
5496 return err;
5497}
5498
5499static int
5500nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5501{
5502 struct nfs_open_context *ctx;
5503 struct nfs4_state *state;
5504 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5505 int status;
5506
5507 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005508 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509 state = ctx->state;
5510
5511 if (request->fl_start < 0 || request->fl_end < 0)
5512 return -EINVAL;
5513
Trond Myklebustd9531262009-07-21 19:22:38 -04005514 if (IS_GETLK(cmd)) {
5515 if (state != NULL)
5516 return nfs4_proc_getlk(state, F_GETLK, request);
5517 return 0;
5518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519
5520 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5521 return -EINVAL;
5522
Trond Myklebustd9531262009-07-21 19:22:38 -04005523 if (request->fl_type == F_UNLCK) {
5524 if (state != NULL)
5525 return nfs4_proc_unlck(state, cmd, request);
5526 return 0;
5527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528
Trond Myklebustd9531262009-07-21 19:22:38 -04005529 if (state == NULL)
5530 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005531 /*
5532 * Don't rely on the VFS having checked the file open mode,
5533 * since it won't do this for flock() locks.
5534 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005535 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005536 case F_RDLCK:
5537 if (!(filp->f_mode & FMODE_READ))
5538 return -EBADF;
5539 break;
5540 case F_WRLCK:
5541 if (!(filp->f_mode & FMODE_WRITE))
5542 return -EBADF;
5543 }
5544
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545 do {
5546 status = nfs4_proc_setlk(state, cmd, request);
5547 if ((status != -EAGAIN) || IS_SETLK(cmd))
5548 break;
5549 timeout = nfs4_set_lock_task_retry(timeout);
5550 status = -ERESTARTSYS;
5551 if (signalled())
5552 break;
5553 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554 return status;
5555}
5556
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04005557int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05005558{
5559 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05005560 int err;
5561
5562 err = nfs4_set_lock_state(state, fl);
5563 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04005564 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04005565 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04005566 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05005567}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005568
Trond Myklebustcf470c32012-03-07 13:49:12 -05005569struct nfs_release_lockowner_data {
5570 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005571 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005572 struct nfs_release_lockowner_args args;
5573};
5574
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005575static void nfs4_release_lockowner_release(void *calldata)
5576{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005577 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005578 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005579 kfree(calldata);
5580}
5581
Trond Myklebust17280172012-03-11 13:11:00 -04005582static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005583 .rpc_release = nfs4_release_lockowner_release,
5584};
5585
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04005586static int nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005587{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005588 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005589 struct rpc_message msg = {
5590 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5591 };
5592
5593 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005594 return -EINVAL;
5595 data = kmalloc(sizeof(*data), GFP_NOFS);
5596 if (!data)
5597 return -ENOMEM;
5598 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005599 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005600 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5601 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5602 data->args.lock_owner.s_dev = server->s_dev;
5603 msg.rpc_argp = &data->args;
5604 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5605 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005606}
5607
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005608#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5609
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005610static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5611 const void *buf, size_t buflen,
5612 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005613{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005614 if (strcmp(key, "") != 0)
5615 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005616
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005617 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005618}
5619
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005620static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5621 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005622{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005623 if (strcmp(key, "") != 0)
5624 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005625
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005626 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005627}
5628
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005629static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5630 size_t list_len, const char *name,
5631 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005632{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005633 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005634
J. Bruce Fields096455a2006-03-20 23:23:42 -05005635 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5636 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005637
5638 if (list && len <= list_len)
5639 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005640 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005641}
5642
David Quigleyc9bccef2013-05-22 12:50:45 -04005643#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5644static inline int nfs4_server_supports_labels(struct nfs_server *server)
5645{
5646 return server->caps & NFS_CAP_SECURITY_LABEL;
5647}
5648
5649static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
5650 const void *buf, size_t buflen,
5651 int flags, int type)
5652{
5653 if (security_ismaclabel(key))
5654 return nfs4_set_security_label(dentry, buf, buflen);
5655
5656 return -EOPNOTSUPP;
5657}
5658
5659static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
5660 void *buf, size_t buflen, int type)
5661{
5662 if (security_ismaclabel(key))
5663 return nfs4_get_security_label(dentry->d_inode, buf, buflen);
5664 return -EOPNOTSUPP;
5665}
5666
5667static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
5668 size_t list_len, const char *name,
5669 size_t name_len, int type)
5670{
5671 size_t len = 0;
5672
5673 if (nfs_server_capable(dentry->d_inode, NFS_CAP_SECURITY_LABEL)) {
5674 len = security_inode_listsecurity(dentry->d_inode, NULL, 0);
5675 if (list && len <= list_len)
5676 security_inode_listsecurity(dentry->d_inode, list, len);
5677 }
5678 return len;
5679}
5680
5681static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
5682 .prefix = XATTR_SECURITY_PREFIX,
5683 .list = nfs4_xattr_list_nfs4_label,
5684 .get = nfs4_xattr_get_nfs4_label,
5685 .set = nfs4_xattr_set_nfs4_label,
5686};
5687#endif
5688
5689
Andy Adamson533eb462011-06-13 18:25:56 -04005690/*
5691 * nfs_fhget will use either the mounted_on_fileid or the fileid
5692 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005693static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5694{
Andy Adamson533eb462011-06-13 18:25:56 -04005695 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5696 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5697 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005698 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005699 return;
5700
5701 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005702 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005703 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5704 fattr->nlink = 2;
5705}
5706
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005707static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5708 const struct qstr *name,
5709 struct nfs4_fs_locations *fs_locations,
5710 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005711{
5712 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04005713 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005714 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005715 };
5716 struct nfs4_fs_locations_arg args = {
5717 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005718 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005719 .page = page,
5720 .bitmask = bitmask,
5721 };
Benny Halevy22958462009-04-01 09:22:02 -04005722 struct nfs4_fs_locations_res res = {
5723 .fs_locations = fs_locations,
5724 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005725 struct rpc_message msg = {
5726 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5727 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005728 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005729 };
5730 int status;
5731
Harvey Harrison3110ff82008-05-02 13:42:44 -07005732 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005733
5734 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5735 * is not supported */
5736 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5737 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5738 else
5739 bitmask[0] |= FATTR4_WORD0_FILEID;
5740
Trond Myklebustc228fd32007-01-13 02:28:11 -05005741 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005742 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005743 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005744 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005745 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005746 return status;
5747}
5748
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005749int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5750 const struct qstr *name,
5751 struct nfs4_fs_locations *fs_locations,
5752 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005753{
5754 struct nfs4_exception exception = { };
5755 int err;
5756 do {
5757 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005758 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005759 &exception);
5760 } while (exception.retry);
5761 return err;
5762}
5763
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005764static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5765{
5766 int status;
5767 struct nfs4_secinfo_arg args = {
5768 .dir_fh = NFS_FH(dir),
5769 .name = name,
5770 };
5771 struct nfs4_secinfo_res res = {
5772 .flavors = flavors,
5773 };
5774 struct rpc_message msg = {
5775 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5776 .rpc_argp = &args,
5777 .rpc_resp = &res,
5778 };
5779
5780 dprintk("NFS call secinfo %s\n", name->name);
5781 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5782 dprintk("NFS reply secinfo: %d\n", status);
5783 return status;
5784}
5785
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005786int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5787 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005788{
5789 struct nfs4_exception exception = { };
5790 int err;
5791 do {
5792 err = nfs4_handle_exception(NFS_SERVER(dir),
5793 _nfs4_proc_secinfo(dir, name, flavors),
5794 &exception);
5795 } while (exception.retry);
5796 return err;
5797}
5798
Andy Adamson557134a2009-04-01 09:21:53 -04005799#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005800/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005801 * Check the exchange flags returned by the server for invalid flags, having
5802 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5803 * DS flags set.
5804 */
5805static int nfs4_check_cl_exchange_flags(u32 flags)
5806{
5807 if (flags & ~EXCHGID4_FLAG_MASK_R)
5808 goto out_inval;
5809 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5810 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5811 goto out_inval;
5812 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5813 goto out_inval;
5814 return NFS_OK;
5815out_inval:
5816 return -NFS4ERR_INVAL;
5817}
5818
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005819static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005820nfs41_same_server_scope(struct nfs41_server_scope *a,
5821 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005822{
5823 if (a->server_scope_sz == b->server_scope_sz &&
5824 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5825 return true;
5826
5827 return false;
5828}
5829
Andy Adamson357f54d2010-12-14 10:11:57 -05005830/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005831 * nfs4_proc_bind_conn_to_session()
5832 *
5833 * The 4.1 client currently uses the same TCP connection for the
5834 * fore and backchannel.
5835 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005836int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005837{
5838 int status;
5839 struct nfs41_bind_conn_to_session_res res;
5840 struct rpc_message msg = {
5841 .rpc_proc =
5842 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5843 .rpc_argp = clp,
5844 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005845 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005846 };
5847
5848 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005849
5850 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5851 if (unlikely(res.session == NULL)) {
5852 status = -ENOMEM;
5853 goto out;
5854 }
5855
5856 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5857 if (status == 0) {
5858 if (memcmp(res.session->sess_id.data,
5859 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5860 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5861 status = -EIO;
5862 goto out_session;
5863 }
5864 if (res.dir != NFS4_CDFS4_BOTH) {
5865 dprintk("NFS: %s: Unexpected direction from server\n",
5866 __func__);
5867 status = -EIO;
5868 goto out_session;
5869 }
5870 if (res.use_conn_in_rdma_mode) {
5871 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5872 __func__);
5873 status = -EIO;
5874 goto out_session;
5875 }
5876 }
5877out_session:
5878 kfree(res.session);
5879out:
5880 dprintk("<-- %s status= %d\n", __func__, status);
5881 return status;
5882}
5883
5884/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005885 * nfs4_proc_exchange_id()
5886 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005887 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5888 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005889 * Since the clientid has expired, all compounds using sessions
5890 * associated with the stale clientid will be returning
5891 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5892 * be in some phase of session reset.
5893 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005894int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005895{
5896 nfs4_verifier verifier;
5897 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005898 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005899 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005900 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005901 };
5902 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005903 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005904 };
5905 int status;
5906 struct rpc_message msg = {
5907 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5908 .rpc_argp = &args,
5909 .rpc_resp = &res,
5910 .rpc_cred = cred,
5911 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005912
Chuck Leverf0920752012-05-21 22:45:41 -04005913 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005914 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5915 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005916 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5917 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5918 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005919
Chuck Leveracdeb692012-05-21 22:46:16 -04005920 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005921 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005922 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005923 status = -ENOMEM;
5924 goto out;
5925 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005926
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005927 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005928 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005929 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005930 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005931 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005932 }
5933
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005934 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005935 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005936 status = -ENOMEM;
5937 goto out_server_scope;
5938 }
5939
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005940 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005941 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005942 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005943
Chuck Lever177313f2012-05-21 22:44:58 -04005944 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005945 clp->cl_clientid = res.clientid;
5946 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5947 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5948 clp->cl_seqid = res.seqid;
5949
Chuck Leveracdeb692012-05-21 22:46:16 -04005950 kfree(clp->cl_serverowner);
5951 clp->cl_serverowner = res.server_owner;
5952 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005953
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005954 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005955 kfree(clp->cl_implid);
5956 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005957
Chuck Lever177313f2012-05-21 22:44:58 -04005958 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005959 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005960 res.server_scope)) {
5961 dprintk("%s: server_scope mismatch detected\n",
5962 __func__);
5963 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005964 kfree(clp->cl_serverscope);
5965 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005966 }
5967
Chuck Lever177313f2012-05-21 22:44:58 -04005968 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005969 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005970 goto out;
5971 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005972 } else
5973 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005974
Chuck Leveracdeb692012-05-21 22:46:16 -04005975out_server_owner:
5976 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005977out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005978 kfree(res.server_scope);
5979out:
Chuck Lever177313f2012-05-21 22:44:58 -04005980 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005981 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005982 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005983 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04005984 clp->cl_implid->date.seconds,
5985 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005986 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005987 return status;
5988}
5989
Trond Myklebust66245532012-05-25 17:18:09 -04005990static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5991 struct rpc_cred *cred)
5992{
5993 struct rpc_message msg = {
5994 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5995 .rpc_argp = clp,
5996 .rpc_cred = cred,
5997 };
5998 int status;
5999
6000 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6001 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04006002 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04006003 "DESTROY_CLIENTID.", status, clp->cl_hostname);
6004 return status;
6005}
6006
6007static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
6008 struct rpc_cred *cred)
6009{
6010 unsigned int loop;
6011 int ret;
6012
6013 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
6014 ret = _nfs4_proc_destroy_clientid(clp, cred);
6015 switch (ret) {
6016 case -NFS4ERR_DELAY:
6017 case -NFS4ERR_CLIENTID_BUSY:
6018 ssleep(1);
6019 break;
6020 default:
6021 return ret;
6022 }
6023 }
6024 return 0;
6025}
6026
6027int nfs4_destroy_clientid(struct nfs_client *clp)
6028{
6029 struct rpc_cred *cred;
6030 int ret = 0;
6031
6032 if (clp->cl_mvops->minor_version < 1)
6033 goto out;
6034 if (clp->cl_exchange_flags == 0)
6035 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04006036 if (clp->cl_preserve_clid)
6037 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04006038 cred = nfs4_get_exchange_id_cred(clp);
6039 ret = nfs4_proc_destroy_clientid(clp, cred);
6040 if (cred)
6041 put_rpccred(cred);
6042 switch (ret) {
6043 case 0:
6044 case -NFS4ERR_STALE_CLIENTID:
6045 clp->cl_exchange_flags = 0;
6046 }
6047out:
6048 return ret;
6049}
6050
Andy Adamson2050f0c2009-04-01 09:22:30 -04006051struct nfs4_get_lease_time_data {
6052 struct nfs4_get_lease_time_args *args;
6053 struct nfs4_get_lease_time_res *res;
6054 struct nfs_client *clp;
6055};
6056
6057static void nfs4_get_lease_time_prepare(struct rpc_task *task,
6058 void *calldata)
6059{
Andy Adamson2050f0c2009-04-01 09:22:30 -04006060 struct nfs4_get_lease_time_data *data =
6061 (struct nfs4_get_lease_time_data *)calldata;
6062
6063 dprintk("--> %s\n", __func__);
6064 /* just setup sequence, do not trigger session recovery
6065 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006066 nfs41_setup_sequence(data->clp->cl_session,
6067 &data->args->la_seq_args,
6068 &data->res->lr_seq_res,
6069 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006070 dprintk("<-- %s\n", __func__);
6071}
6072
6073/*
6074 * Called from nfs4_state_manager thread for session setup, so don't recover
6075 * from sequence operation or clientid errors.
6076 */
6077static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
6078{
6079 struct nfs4_get_lease_time_data *data =
6080 (struct nfs4_get_lease_time_data *)calldata;
6081
6082 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006083 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
6084 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04006085 switch (task->tk_status) {
6086 case -NFS4ERR_DELAY:
6087 case -NFS4ERR_GRACE:
6088 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
6089 rpc_delay(task, NFS4_POLL_RETRY_MIN);
6090 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006091 /* fall through */
6092 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006093 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006094 return;
6095 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04006096 dprintk("<-- %s\n", __func__);
6097}
6098
Trond Myklebust17280172012-03-11 13:11:00 -04006099static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04006100 .rpc_call_prepare = nfs4_get_lease_time_prepare,
6101 .rpc_call_done = nfs4_get_lease_time_done,
6102};
6103
6104int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
6105{
6106 struct rpc_task *task;
6107 struct nfs4_get_lease_time_args args;
6108 struct nfs4_get_lease_time_res res = {
6109 .lr_fsinfo = fsinfo,
6110 };
6111 struct nfs4_get_lease_time_data data = {
6112 .args = &args,
6113 .res = &res,
6114 .clp = clp,
6115 };
6116 struct rpc_message msg = {
6117 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
6118 .rpc_argp = &args,
6119 .rpc_resp = &res,
6120 };
6121 struct rpc_task_setup task_setup = {
6122 .rpc_client = clp->cl_rpcclient,
6123 .rpc_message = &msg,
6124 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006125 .callback_data = &data,
6126 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04006127 };
6128 int status;
6129
Trond Myklebust9d12b212012-01-17 22:04:25 -05006130 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006131 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006132 dprintk("--> %s\n", __func__);
6133 task = rpc_run_task(&task_setup);
6134
6135 if (IS_ERR(task))
6136 status = PTR_ERR(task);
6137 else {
6138 status = task->tk_status;
6139 rpc_put_task(task);
6140 }
6141 dprintk("<-- %s return %d\n", __func__, status);
6142
6143 return status;
6144}
6145
Andy Adamsonfc931582009-04-01 09:22:31 -04006146/*
6147 * Initialize the values to be used by the client in CREATE_SESSION
6148 * If nfs4_init_session set the fore channel request and response sizes,
6149 * use them.
6150 *
6151 * Set the back channel max_resp_sz_cached to zero to force the client to
6152 * always set csa_cachethis to FALSE because the current implementation
6153 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
6154 */
6155static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
6156{
6157 struct nfs4_session *session = args->client->cl_session;
Trond Myklebustae72ae672012-11-20 11:02:55 -05006158 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
6159 mxresp_sz = session->fc_target_max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006160
6161 if (mxrqst_sz == 0)
6162 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
6163 if (mxresp_sz == 0)
6164 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
6165 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04006166 args->fc_attrs.max_rqst_sz = mxrqst_sz;
6167 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006168 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05006169 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04006170
6171 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05006172 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04006173 __func__,
6174 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05006175 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04006176
6177 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04006178 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
6179 args->bc_attrs.max_resp_sz = PAGE_SIZE;
6180 args->bc_attrs.max_resp_sz_cached = 0;
6181 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
6182 args->bc_attrs.max_reqs = 1;
6183
6184 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
6185 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
6186 __func__,
6187 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
6188 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
6189 args->bc_attrs.max_reqs);
6190}
6191
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006192static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04006193{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006194 struct nfs4_channel_attrs *sent = &args->fc_attrs;
6195 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
6196
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006197 if (rcvd->max_resp_sz > sent->max_resp_sz)
6198 return -EINVAL;
6199 /*
6200 * Our requested max_ops is the minimum we need; we're not
6201 * prepared to break up compounds into smaller pieces than that.
6202 * So, no point even trying to continue if the server won't
6203 * cooperate:
6204 */
6205 if (rcvd->max_ops < sent->max_ops)
6206 return -EINVAL;
6207 if (rcvd->max_reqs == 0)
6208 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006209 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
6210 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006211 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04006212}
6213
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006214static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
6215{
6216 struct nfs4_channel_attrs *sent = &args->bc_attrs;
6217 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04006218
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006219 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
6220 return -EINVAL;
6221 if (rcvd->max_resp_sz < sent->max_resp_sz)
6222 return -EINVAL;
6223 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
6224 return -EINVAL;
6225 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006226 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006227 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006228 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006229 return -EINVAL;
6230 return 0;
6231}
Andy Adamson8d353012009-04-01 09:22:32 -04006232
Andy Adamson8d353012009-04-01 09:22:32 -04006233static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
6234 struct nfs4_session *session)
6235{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006236 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04006237
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006238 ret = nfs4_verify_fore_channel_attrs(args, session);
6239 if (ret)
6240 return ret;
6241 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04006242}
6243
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006244static int _nfs4_proc_create_session(struct nfs_client *clp,
6245 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04006246{
6247 struct nfs4_session *session = clp->cl_session;
6248 struct nfs41_create_session_args args = {
6249 .client = clp,
6250 .cb_program = NFS4_CALLBACK,
6251 };
6252 struct nfs41_create_session_res res = {
6253 .client = clp,
6254 };
6255 struct rpc_message msg = {
6256 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
6257 .rpc_argp = &args,
6258 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006259 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04006260 };
6261 int status;
6262
6263 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04006264 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04006265
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006266 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04006267
Trond Myklebust43095d32012-11-20 11:13:12 -05006268 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04006269 /* Verify the session's negotiated channel_attrs values */
6270 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04006271 /* Increment the clientid slot sequence id */
6272 clp->cl_seqid++;
6273 }
6274
6275 return status;
6276}
6277
6278/*
6279 * Issues a CREATE_SESSION operation to the server.
6280 * It is the responsibility of the caller to verify the session is
6281 * expired before calling this routine.
6282 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006283int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04006284{
6285 int status;
6286 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04006287 struct nfs4_session *session = clp->cl_session;
6288
6289 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
6290
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006291 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04006292 if (status)
6293 goto out;
6294
Andy Adamsonaacd5532011-11-09 13:58:21 -05006295 /* Init or reset the session slot tables */
6296 status = nfs4_setup_session_slot_tables(session);
6297 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04006298 if (status)
6299 goto out;
6300
6301 ptr = (unsigned *)&session->sess_id.data[0];
6302 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
6303 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04006304out:
6305 dprintk("<-- %s\n", __func__);
6306 return status;
6307}
6308
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006309/*
6310 * Issue the over-the-wire RPC DESTROY_SESSION.
6311 * The caller must serialize access to this routine.
6312 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006313int nfs4_proc_destroy_session(struct nfs4_session *session,
6314 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006315{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006316 struct rpc_message msg = {
6317 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
6318 .rpc_argp = session,
6319 .rpc_cred = cred,
6320 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006321 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006322
6323 dprintk("--> nfs4_proc_destroy_session\n");
6324
6325 /* session is still being setup */
6326 if (session->clp->cl_cons_state != NFS_CS_READY)
6327 return status;
6328
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006329 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006330
6331 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04006332 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006333 "Session has been destroyed regardless...\n", status);
6334
6335 dprintk("<-- nfs4_proc_destroy_session\n");
6336 return status;
6337}
6338
Trond Myklebust7b38c362012-05-23 13:23:31 -04006339/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006340 * Renew the cl_session lease.
6341 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006342struct nfs4_sequence_data {
6343 struct nfs_client *clp;
6344 struct nfs4_sequence_args args;
6345 struct nfs4_sequence_res res;
6346};
6347
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006348static void nfs41_sequence_release(void *data)
6349{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006350 struct nfs4_sequence_data *calldata = data;
6351 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006352
Alexandros Batsakis71358402010-02-05 03:45:05 -08006353 if (atomic_read(&clp->cl_count) > 1)
6354 nfs4_schedule_state_renewal(clp);
6355 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006356 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006357}
6358
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006359static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6360{
6361 switch(task->tk_status) {
6362 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006363 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6364 return -EAGAIN;
6365 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006366 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006367 }
6368 return 0;
6369}
6370
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006371static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006372{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006373 struct nfs4_sequence_data *calldata = data;
6374 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006375
Trond Myklebust14516c32010-07-31 14:29:06 -04006376 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6377 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006378
6379 if (task->tk_status < 0) {
6380 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006381 if (atomic_read(&clp->cl_count) == 1)
6382 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006383
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006384 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6385 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006386 return;
6387 }
6388 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006389 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006390out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006391 dprintk("<-- %s\n", __func__);
6392}
6393
6394static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6395{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006396 struct nfs4_sequence_data *calldata = data;
6397 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006398 struct nfs4_sequence_args *args;
6399 struct nfs4_sequence_res *res;
6400
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006401 args = task->tk_msg.rpc_argp;
6402 res = task->tk_msg.rpc_resp;
6403
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006404 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006405}
6406
6407static const struct rpc_call_ops nfs41_sequence_ops = {
6408 .rpc_call_done = nfs41_sequence_call_done,
6409 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006410 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006411};
6412
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006413static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
6414 struct rpc_cred *cred,
6415 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006416{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006417 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006418 struct rpc_message msg = {
6419 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6420 .rpc_cred = cred,
6421 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006422 struct rpc_task_setup task_setup_data = {
6423 .rpc_client = clp->cl_rpcclient,
6424 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006425 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04006426 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006427 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006428
Alexandros Batsakis71358402010-02-05 03:45:05 -08006429 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006430 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006431 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006432 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006433 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006434 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006435 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05006436 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006437 if (is_privileged)
6438 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006439 msg.rpc_argp = &calldata->args;
6440 msg.rpc_resp = &calldata->res;
6441 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006442 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006443
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006444 return rpc_run_task(&task_setup_data);
6445}
6446
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006447static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006448{
6449 struct rpc_task *task;
6450 int ret = 0;
6451
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006452 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6453 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006454 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006455 if (IS_ERR(task))
6456 ret = PTR_ERR(task);
6457 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006458 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006459 dprintk("<-- %s status=%d\n", __func__, ret);
6460 return ret;
6461}
6462
6463static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6464{
6465 struct rpc_task *task;
6466 int ret;
6467
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006468 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006469 if (IS_ERR(task)) {
6470 ret = PTR_ERR(task);
6471 goto out;
6472 }
6473 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006474 if (!ret) {
6475 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6476
6477 if (task->tk_status == 0)
6478 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006479 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006480 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006481 rpc_put_task(task);
6482out:
6483 dprintk("<-- %s status=%d\n", __func__, ret);
6484 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006485}
6486
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006487struct nfs4_reclaim_complete_data {
6488 struct nfs_client *clp;
6489 struct nfs41_reclaim_complete_args arg;
6490 struct nfs41_reclaim_complete_res res;
6491};
6492
6493static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6494{
6495 struct nfs4_reclaim_complete_data *calldata = data;
6496
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006497 nfs41_setup_sequence(calldata->clp->cl_session,
6498 &calldata->arg.seq_args,
6499 &calldata->res.seq_res,
6500 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006501}
6502
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006503static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6504{
6505 switch(task->tk_status) {
6506 case 0:
6507 case -NFS4ERR_COMPLETE_ALREADY:
6508 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6509 break;
6510 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006511 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006512 /* fall through */
6513 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006514 return -EAGAIN;
6515 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006516 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006517 }
6518 return 0;
6519}
6520
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006521static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6522{
6523 struct nfs4_reclaim_complete_data *calldata = data;
6524 struct nfs_client *clp = calldata->clp;
6525 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6526
6527 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006528 if (!nfs41_sequence_done(task, res))
6529 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006530
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006531 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6532 rpc_restart_call_prepare(task);
6533 return;
6534 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006535 dprintk("<-- %s\n", __func__);
6536}
6537
6538static void nfs4_free_reclaim_complete_data(void *data)
6539{
6540 struct nfs4_reclaim_complete_data *calldata = data;
6541
6542 kfree(calldata);
6543}
6544
6545static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6546 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6547 .rpc_call_done = nfs4_reclaim_complete_done,
6548 .rpc_release = nfs4_free_reclaim_complete_data,
6549};
6550
6551/*
6552 * Issue a global reclaim complete.
6553 */
6554static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6555{
6556 struct nfs4_reclaim_complete_data *calldata;
6557 struct rpc_task *task;
6558 struct rpc_message msg = {
6559 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6560 };
6561 struct rpc_task_setup task_setup_data = {
6562 .rpc_client = clp->cl_rpcclient,
6563 .rpc_message = &msg,
6564 .callback_ops = &nfs4_reclaim_complete_call_ops,
6565 .flags = RPC_TASK_ASYNC,
6566 };
6567 int status = -ENOMEM;
6568
6569 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006570 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006571 if (calldata == NULL)
6572 goto out;
6573 calldata->clp = clp;
6574 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006575
Trond Myklebust9d12b212012-01-17 22:04:25 -05006576 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006577 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006578 msg.rpc_argp = &calldata->arg;
6579 msg.rpc_resp = &calldata->res;
6580 task_setup_data.callback_data = calldata;
6581 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006582 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006583 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006584 goto out;
6585 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006586 status = nfs4_wait_for_completion_rpc_task(task);
6587 if (status == 0)
6588 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006589 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006590 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006591out:
6592 dprintk("<-- %s status=%d\n", __func__, status);
6593 return status;
6594}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006595
6596static void
6597nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6598{
6599 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006600 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006601 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006602
6603 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006604 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6605 * right now covering the LAYOUTGET we are about to send.
6606 * However, that is not so catastrophic, and there seems
6607 * to be no way to prevent it completely.
6608 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006609 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006610 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006611 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006612 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6613 NFS_I(lgp->args.inode)->layout,
6614 lgp->args.ctx->state)) {
6615 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00006616 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006617}
6618
6619static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6620{
6621 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006622 struct inode *inode = lgp->args.inode;
6623 struct nfs_server *server = NFS_SERVER(inode);
6624 struct pnfs_layout_hdr *lo;
6625 struct nfs4_state *state = NULL;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006626 unsigned long timeo, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006627
6628 dprintk("--> %s\n", __func__);
6629
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006630 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006631 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006632
6633 switch (task->tk_status) {
6634 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006635 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006636 case -NFS4ERR_LAYOUTTRYLATER:
6637 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05006638 timeo = rpc_get_timeout(task->tk_client);
6639 giveup = lgp->args.timestamp + timeo;
6640 if (time_after(giveup, jiffies))
6641 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006642 break;
6643 case -NFS4ERR_EXPIRED:
6644 case -NFS4ERR_BAD_STATEID:
6645 spin_lock(&inode->i_lock);
6646 lo = NFS_I(inode)->layout;
6647 if (!lo || list_empty(&lo->plh_segs)) {
6648 spin_unlock(&inode->i_lock);
6649 /* If the open stateid was bad, then recover it. */
6650 state = lgp->args.ctx->state;
6651 } else {
6652 LIST_HEAD(head);
6653
6654 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6655 spin_unlock(&inode->i_lock);
6656 /* Mark the bad layout state as invalid, then
6657 * retry using the open stateid. */
6658 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006659 }
6660 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006661 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6662 rpc_restart_call_prepare(task);
6663out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006664 dprintk("<-- %s\n", __func__);
6665}
6666
Idan Kedar85541162012-08-02 11:47:10 +03006667static size_t max_response_pages(struct nfs_server *server)
6668{
6669 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6670 return nfs_page_array_len(0, max_resp_sz);
6671}
6672
6673static void nfs4_free_pages(struct page **pages, size_t size)
6674{
6675 int i;
6676
6677 if (!pages)
6678 return;
6679
6680 for (i = 0; i < size; i++) {
6681 if (!pages[i])
6682 break;
6683 __free_page(pages[i]);
6684 }
6685 kfree(pages);
6686}
6687
6688static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6689{
6690 struct page **pages;
6691 int i;
6692
6693 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6694 if (!pages) {
6695 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6696 return NULL;
6697 }
6698
6699 for (i = 0; i < size; i++) {
6700 pages[i] = alloc_page(gfp_flags);
6701 if (!pages[i]) {
6702 dprintk("%s: failed to allocate page\n", __func__);
6703 nfs4_free_pages(pages, size);
6704 return NULL;
6705 }
6706 }
6707
6708 return pages;
6709}
6710
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006711static void nfs4_layoutget_release(void *calldata)
6712{
6713 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006714 struct inode *inode = lgp->args.inode;
6715 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006716 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006717
6718 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006719 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006720 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006721 put_nfs_open_context(lgp->args.ctx);
6722 kfree(calldata);
6723 dprintk("<-- %s\n", __func__);
6724}
6725
6726static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6727 .rpc_call_prepare = nfs4_layoutget_prepare,
6728 .rpc_call_done = nfs4_layoutget_done,
6729 .rpc_release = nfs4_layoutget_release,
6730};
6731
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006732struct pnfs_layout_segment *
6733nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006734{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006735 struct inode *inode = lgp->args.inode;
6736 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006737 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006738 struct rpc_task *task;
6739 struct rpc_message msg = {
6740 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6741 .rpc_argp = &lgp->args,
6742 .rpc_resp = &lgp->res,
6743 };
6744 struct rpc_task_setup task_setup_data = {
6745 .rpc_client = server->client,
6746 .rpc_message = &msg,
6747 .callback_ops = &nfs4_layoutget_call_ops,
6748 .callback_data = lgp,
6749 .flags = RPC_TASK_ASYNC,
6750 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006751 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006752 int status = 0;
6753
6754 dprintk("--> %s\n", __func__);
6755
Idan Kedar85541162012-08-02 11:47:10 +03006756 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6757 if (!lgp->args.layout.pages) {
6758 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006759 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006760 }
6761 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006762 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03006763
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006764 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006765 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006766 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006767
6768 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
6769 pnfs_get_layout_hdr(NFS_I(inode)->layout);
6770
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006771 task = rpc_run_task(&task_setup_data);
6772 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006773 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006774 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006775 if (status == 0)
6776 status = task->tk_status;
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05006777 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
6778 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006779 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006780 rpc_put_task(task);
6781 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006782 if (status)
6783 return ERR_PTR(status);
6784 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006785}
6786
Benny Halevycbe82602011-05-22 19:52:37 +03006787static void
6788nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6789{
6790 struct nfs4_layoutreturn *lrp = calldata;
6791
6792 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006793 nfs41_setup_sequence(lrp->clp->cl_session,
6794 &lrp->args.seq_args,
6795 &lrp->res.seq_res,
6796 task);
Benny Halevycbe82602011-05-22 19:52:37 +03006797}
6798
6799static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6800{
6801 struct nfs4_layoutreturn *lrp = calldata;
6802 struct nfs_server *server;
6803
6804 dprintk("--> %s\n", __func__);
6805
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006806 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03006807 return;
6808
6809 server = NFS_SERVER(lrp->args.inode);
6810 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006811 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006812 return;
6813 }
Benny Halevycbe82602011-05-22 19:52:37 +03006814 dprintk("<-- %s\n", __func__);
6815}
6816
6817static void nfs4_layoutreturn_release(void *calldata)
6818{
6819 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006820 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006821
6822 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006823 spin_lock(&lo->plh_inode->i_lock);
6824 if (lrp->res.lrs_present)
6825 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6826 lo->plh_block_lgets--;
6827 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006828 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006829 kfree(calldata);
6830 dprintk("<-- %s\n", __func__);
6831}
6832
6833static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6834 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6835 .rpc_call_done = nfs4_layoutreturn_done,
6836 .rpc_release = nfs4_layoutreturn_release,
6837};
6838
6839int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6840{
6841 struct rpc_task *task;
6842 struct rpc_message msg = {
6843 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6844 .rpc_argp = &lrp->args,
6845 .rpc_resp = &lrp->res,
6846 };
6847 struct rpc_task_setup task_setup_data = {
6848 .rpc_client = lrp->clp->cl_rpcclient,
6849 .rpc_message = &msg,
6850 .callback_ops = &nfs4_layoutreturn_call_ops,
6851 .callback_data = lrp,
6852 };
6853 int status;
6854
6855 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006856 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006857 task = rpc_run_task(&task_setup_data);
6858 if (IS_ERR(task))
6859 return PTR_ERR(task);
6860 status = task->tk_status;
6861 dprintk("<-- %s status=%d\n", __func__, status);
6862 rpc_put_task(task);
6863 return status;
6864}
6865
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006866/*
6867 * Retrieve the list of Data Server devices from the MDS.
6868 */
6869static int _nfs4_getdevicelist(struct nfs_server *server,
6870 const struct nfs_fh *fh,
6871 struct pnfs_devicelist *devlist)
6872{
6873 struct nfs4_getdevicelist_args args = {
6874 .fh = fh,
6875 .layoutclass = server->pnfs_curr_ld->id,
6876 };
6877 struct nfs4_getdevicelist_res res = {
6878 .devlist = devlist,
6879 };
6880 struct rpc_message msg = {
6881 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6882 .rpc_argp = &args,
6883 .rpc_resp = &res,
6884 };
6885 int status;
6886
6887 dprintk("--> %s\n", __func__);
6888 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6889 &res.seq_res, 0);
6890 dprintk("<-- %s status=%d\n", __func__, status);
6891 return status;
6892}
6893
6894int nfs4_proc_getdevicelist(struct nfs_server *server,
6895 const struct nfs_fh *fh,
6896 struct pnfs_devicelist *devlist)
6897{
6898 struct nfs4_exception exception = { };
6899 int err;
6900
6901 do {
6902 err = nfs4_handle_exception(server,
6903 _nfs4_getdevicelist(server, fh, devlist),
6904 &exception);
6905 } while (exception.retry);
6906
6907 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6908 err, devlist->num_devs);
6909
6910 return err;
6911}
6912EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6913
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006914static int
6915_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6916{
6917 struct nfs4_getdeviceinfo_args args = {
6918 .pdev = pdev,
6919 };
6920 struct nfs4_getdeviceinfo_res res = {
6921 .pdev = pdev,
6922 };
6923 struct rpc_message msg = {
6924 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6925 .rpc_argp = &args,
6926 .rpc_resp = &res,
6927 };
6928 int status;
6929
6930 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006931 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006932 dprintk("<-- %s status=%d\n", __func__, status);
6933
6934 return status;
6935}
6936
6937int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6938{
6939 struct nfs4_exception exception = { };
6940 int err;
6941
6942 do {
6943 err = nfs4_handle_exception(server,
6944 _nfs4_proc_getdeviceinfo(server, pdev),
6945 &exception);
6946 } while (exception.retry);
6947 return err;
6948}
6949EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6950
Andy Adamson863a3c62011-03-23 13:27:54 +00006951static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6952{
6953 struct nfs4_layoutcommit_data *data = calldata;
6954 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006955 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00006956
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006957 nfs41_setup_sequence(session,
6958 &data->args.seq_args,
6959 &data->res.seq_res,
6960 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00006961}
6962
6963static void
6964nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6965{
6966 struct nfs4_layoutcommit_data *data = calldata;
6967 struct nfs_server *server = NFS_SERVER(data->args.inode);
6968
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006969 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00006970 return;
6971
6972 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006973 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6974 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6975 case -NFS4ERR_BADLAYOUT: /* no layout */
6976 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006977 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006978 break;
6979 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006980 nfs_post_op_update_inode_force_wcc(data->args.inode,
6981 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006982 break;
6983 default:
6984 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6985 rpc_restart_call_prepare(task);
6986 return;
6987 }
6988 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006989}
6990
6991static void nfs4_layoutcommit_release(void *calldata)
6992{
6993 struct nfs4_layoutcommit_data *data = calldata;
6994
Andy Adamsondb29c082011-07-30 20:52:38 -04006995 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006996 put_rpccred(data->cred);
6997 kfree(data);
6998}
6999
7000static const struct rpc_call_ops nfs4_layoutcommit_ops = {
7001 .rpc_call_prepare = nfs4_layoutcommit_prepare,
7002 .rpc_call_done = nfs4_layoutcommit_done,
7003 .rpc_release = nfs4_layoutcommit_release,
7004};
7005
7006int
Andy Adamsonef311532011-03-12 02:58:10 -05007007nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00007008{
7009 struct rpc_message msg = {
7010 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
7011 .rpc_argp = &data->args,
7012 .rpc_resp = &data->res,
7013 .rpc_cred = data->cred,
7014 };
7015 struct rpc_task_setup task_setup_data = {
7016 .task = &data->task,
7017 .rpc_client = NFS_CLIENT(data->args.inode),
7018 .rpc_message = &msg,
7019 .callback_ops = &nfs4_layoutcommit_ops,
7020 .callback_data = data,
7021 .flags = RPC_TASK_ASYNC,
7022 };
7023 struct rpc_task *task;
7024 int status = 0;
7025
7026 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
7027 "lbw: %llu inode %lu\n",
7028 data->task.tk_pid, sync,
7029 data->args.lastbytewritten,
7030 data->args.inode->i_ino);
7031
Trond Myklebust9d12b212012-01-17 22:04:25 -05007032 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00007033 task = rpc_run_task(&task_setup_data);
7034 if (IS_ERR(task))
7035 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05007036 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00007037 goto out;
7038 status = nfs4_wait_for_completion_rpc_task(task);
7039 if (status != 0)
7040 goto out;
7041 status = task->tk_status;
7042out:
7043 dprintk("%s: status %d\n", __func__, status);
7044 rpc_put_task(task);
7045 return status;
7046}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007047
7048static int
7049_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7050 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
7051{
7052 struct nfs41_secinfo_no_name_args args = {
7053 .style = SECINFO_STYLE_CURRENT_FH,
7054 };
7055 struct nfs4_secinfo_res res = {
7056 .flavors = flavors,
7057 };
7058 struct rpc_message msg = {
7059 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
7060 .rpc_argp = &args,
7061 .rpc_resp = &res,
7062 };
7063 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
7064}
7065
7066static int
7067nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7068 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
7069{
7070 struct nfs4_exception exception = { };
7071 int err;
7072 do {
7073 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
7074 switch (err) {
7075 case 0:
7076 case -NFS4ERR_WRONGSEC:
7077 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007078 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007079 default:
7080 err = nfs4_handle_exception(server, err, &exception);
7081 }
7082 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007083out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007084 return err;
7085}
7086
7087static int
7088nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
7089 struct nfs_fsinfo *info)
7090{
7091 int err;
7092 struct page *page;
7093 rpc_authflavor_t flavor;
7094 struct nfs4_secinfo_flavors *flavors;
7095
7096 page = alloc_page(GFP_KERNEL);
7097 if (!page) {
7098 err = -ENOMEM;
7099 goto out;
7100 }
7101
7102 flavors = page_address(page);
7103 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
7104
7105 /*
7106 * Fall back on "guess and check" method if
7107 * the server doesn't support SECINFO_NO_NAME
7108 */
7109 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
7110 err = nfs4_find_root_sec(server, fhandle, info);
7111 goto out_freepage;
7112 }
7113 if (err)
7114 goto out_freepage;
7115
7116 flavor = nfs_find_best_sec(flavors);
7117 if (err == 0)
7118 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
7119
7120out_freepage:
7121 put_page(page);
7122 if (err == -EACCES)
7123 return -EPERM;
7124out:
7125 return err;
7126}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007127
7128static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04007129{
7130 int status;
7131 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007132 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04007133 };
7134 struct nfs41_test_stateid_res res;
7135 struct rpc_message msg = {
7136 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
7137 .rpc_argp = &args,
7138 .rpc_resp = &res,
7139 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007140
Chuck Lever38527b12012-07-11 16:30:23 -04007141 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05007142 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007143 nfs4_set_sequence_privileged(&args.seq_args);
7144 status = nfs4_call_sync_sequence(server->client, server, &msg,
7145 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04007146 if (status != NFS_OK) {
7147 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04007148 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04007149 }
7150 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04007151 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04007152}
7153
Chuck Lever38527b12012-07-11 16:30:23 -04007154/**
7155 * nfs41_test_stateid - perform a TEST_STATEID operation
7156 *
7157 * @server: server / transport on which to perform the operation
7158 * @stateid: state ID to test
7159 *
7160 * Returns NFS_OK if the server recognizes that "stateid" is valid.
7161 * Otherwise a negative NFS4ERR value is returned if the operation
7162 * failed or the state ID is not currently valid.
7163 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007164static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04007165{
7166 struct nfs4_exception exception = { };
7167 int err;
7168 do {
Chuck Lever377e5072012-07-11 16:29:45 -04007169 err = _nfs41_test_stateid(server, stateid);
7170 if (err != -NFS4ERR_DELAY)
7171 break;
7172 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04007173 } while (exception.retry);
7174 return err;
7175}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007176
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007177struct nfs_free_stateid_data {
7178 struct nfs_server *server;
7179 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007180 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007181};
7182
7183static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
7184{
7185 struct nfs_free_stateid_data *data = calldata;
7186 nfs41_setup_sequence(nfs4_get_session(data->server),
7187 &data->args.seq_args,
7188 &data->res.seq_res,
7189 task);
7190}
7191
7192static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
7193{
7194 struct nfs_free_stateid_data *data = calldata;
7195
7196 nfs41_sequence_done(task, &data->res.seq_res);
7197
7198 switch (task->tk_status) {
7199 case -NFS4ERR_DELAY:
7200 if (nfs4_async_handle_error(task, data->server, NULL) == -EAGAIN)
7201 rpc_restart_call_prepare(task);
7202 }
7203}
7204
7205static void nfs41_free_stateid_release(void *calldata)
7206{
7207 kfree(calldata);
7208}
7209
7210const struct rpc_call_ops nfs41_free_stateid_ops = {
7211 .rpc_call_prepare = nfs41_free_stateid_prepare,
7212 .rpc_call_done = nfs41_free_stateid_done,
7213 .rpc_release = nfs41_free_stateid_release,
7214};
7215
7216static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
7217 nfs4_stateid *stateid,
7218 bool privileged)
7219{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007220 struct rpc_message msg = {
7221 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007222 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007223 struct rpc_task_setup task_setup = {
7224 .rpc_client = server->client,
7225 .rpc_message = &msg,
7226 .callback_ops = &nfs41_free_stateid_ops,
7227 .flags = RPC_TASK_ASYNC,
7228 };
7229 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007230
Chuck Lever38527b12012-07-11 16:30:23 -04007231 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007232 data = kmalloc(sizeof(*data), GFP_NOFS);
7233 if (!data)
7234 return ERR_PTR(-ENOMEM);
7235 data->server = server;
7236 nfs4_stateid_copy(&data->args.stateid, stateid);
7237
7238 task_setup.callback_data = data;
7239
7240 msg.rpc_argp = &data->args;
7241 msg.rpc_resp = &data->res;
7242 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
7243 if (privileged)
7244 nfs4_set_sequence_privileged(&data->args.seq_args);
7245
7246 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007247}
7248
Chuck Lever38527b12012-07-11 16:30:23 -04007249/**
7250 * nfs41_free_stateid - perform a FREE_STATEID operation
7251 *
7252 * @server: server / transport on which to perform the operation
7253 * @stateid: state ID to release
7254 *
7255 * Returns NFS_OK if the server freed "stateid". Otherwise a
7256 * negative NFS4ERR value is returned.
7257 */
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007258static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007259{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007260 struct rpc_task *task;
7261 int ret;
7262
7263 task = _nfs41_free_stateid(server, stateid, true);
7264 if (IS_ERR(task))
7265 return PTR_ERR(task);
7266 ret = rpc_wait_for_completion_task(task);
7267 if (!ret)
7268 ret = task->tk_status;
7269 rpc_put_task(task);
7270 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007271}
Trond Myklebust36281ca2012-03-04 18:13:56 -05007272
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007273static int nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
7274{
7275 struct rpc_task *task;
7276
7277 task = _nfs41_free_stateid(server, &lsp->ls_stateid, false);
7278 nfs4_free_lock_state(server, lsp);
7279 if (IS_ERR(task))
7280 return PTR_ERR(task);
7281 rpc_put_task(task);
7282 return 0;
7283}
7284
Trond Myklebust36281ca2012-03-04 18:13:56 -05007285static bool nfs41_match_stateid(const nfs4_stateid *s1,
7286 const nfs4_stateid *s2)
7287{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007288 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007289 return false;
7290
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007291 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007292 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007293 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007294 return true;
7295
7296 return false;
7297}
7298
Andy Adamson557134a2009-04-01 09:21:53 -04007299#endif /* CONFIG_NFS_V4_1 */
7300
Trond Myklebust36281ca2012-03-04 18:13:56 -05007301static bool nfs4_match_stateid(const nfs4_stateid *s1,
7302 const nfs4_stateid *s2)
7303{
Trond Myklebustf597c532012-03-04 18:13:56 -05007304 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05007305}
7306
7307
Trond Myklebust17280172012-03-11 13:11:00 -04007308static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007309 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007310 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007311 .recover_open = nfs4_open_reclaim,
7312 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04007313 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04007314 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04007315 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007316};
7317
Andy Adamson591d71c2009-04-01 09:22:47 -04007318#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007319static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007320 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
7321 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
7322 .recover_open = nfs4_open_reclaim,
7323 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05007324 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04007325 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007326 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04007327 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04007328};
7329#endif /* CONFIG_NFS_V4_1 */
7330
Trond Myklebust17280172012-03-11 13:11:00 -04007331static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007332 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007333 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007334 .recover_open = nfs4_open_expired,
7335 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04007336 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04007337 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007338};
7339
Andy Adamson591d71c2009-04-01 09:22:47 -04007340#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007341static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007342 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
7343 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007344 .recover_open = nfs41_open_expired,
7345 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05007346 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04007347 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04007348};
7349#endif /* CONFIG_NFS_V4_1 */
7350
Trond Myklebust17280172012-03-11 13:11:00 -04007351static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007352 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04007353 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007354 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04007355};
7356
7357#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007358static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007359 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04007360 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007361 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04007362};
7363#endif
7364
Trond Myklebust97dc1352010-06-16 09:52:26 -04007365static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
7366 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04007367 .init_caps = NFS_CAP_READDIRPLUS
7368 | NFS_CAP_ATOMIC_OPEN
7369 | NFS_CAP_CHANGE_ATTR
7370 | NFS_CAP_POSIX_LOCK,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007371 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007372 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007373 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007374 .free_lock_state = nfs4_release_lockowner,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007375 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
7376 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
7377 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007378};
7379
7380#if defined(CONFIG_NFS_V4_1)
7381static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
7382 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04007383 .init_caps = NFS_CAP_READDIRPLUS
7384 | NFS_CAP_ATOMIC_OPEN
7385 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04007386 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04007387 | NFS_CAP_STATEID_NFSV41
7388 | NFS_CAP_ATOMIC_OPEN_V1,
Bryan Schumaker104287c2012-11-12 14:13:13 -05007389 .call_sync = nfs4_call_sync_sequence,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007390 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007391 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007392 .free_lock_state = nfs41_free_lock_state,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007393 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7394 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7395 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007396};
7397#endif
7398
Steve Dickson42c2c422013-05-22 12:50:38 -04007399#if defined(CONFIG_NFS_V4_2)
7400static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
7401 .minor_version = 2,
7402 .call_sync = nfs4_call_sync_sequence,
7403 .match_stateid = nfs41_match_stateid,
7404 .find_root_sec = nfs41_find_root_sec,
7405 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7406 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7407 .state_renewal_ops = &nfs41_state_renewal_ops,
7408};
7409#endif
7410
Trond Myklebust97dc1352010-06-16 09:52:26 -04007411const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
7412 [0] = &nfs_v4_0_minor_ops,
7413#if defined(CONFIG_NFS_V4_1)
7414 [1] = &nfs_v4_1_minor_ops,
7415#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04007416#if defined(CONFIG_NFS_V4_2)
7417 [2] = &nfs_v4_2_minor_ops,
7418#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04007419};
7420
Bryan Schumaker73a79702012-07-16 16:39:12 -04007421const struct inode_operations nfs4_dir_inode_operations = {
7422 .create = nfs_create,
7423 .lookup = nfs_lookup,
7424 .atomic_open = nfs_atomic_open,
7425 .link = nfs_link,
7426 .unlink = nfs_unlink,
7427 .symlink = nfs_symlink,
7428 .mkdir = nfs_mkdir,
7429 .rmdir = nfs_rmdir,
7430 .mknod = nfs_mknod,
7431 .rename = nfs_rename,
7432 .permission = nfs_permission,
7433 .getattr = nfs_getattr,
7434 .setattr = nfs_setattr,
7435 .getxattr = generic_getxattr,
7436 .setxattr = generic_setxattr,
7437 .listxattr = generic_listxattr,
7438 .removexattr = generic_removexattr,
7439};
7440
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08007441static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007442 .permission = nfs_permission,
7443 .getattr = nfs_getattr,
7444 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007445 .getxattr = generic_getxattr,
7446 .setxattr = generic_setxattr,
7447 .listxattr = generic_listxattr,
7448 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007449};
7450
David Howells509de812006-08-22 20:06:11 -04007451const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007452 .version = 4, /* protocol version */
7453 .dentry_ops = &nfs4_dentry_operations,
7454 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007455 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04007456 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007457 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04007458 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04007459 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007460 .getattr = nfs4_proc_getattr,
7461 .setattr = nfs4_proc_setattr,
7462 .lookup = nfs4_proc_lookup,
7463 .access = nfs4_proc_access,
7464 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007465 .create = nfs4_proc_create,
7466 .remove = nfs4_proc_remove,
7467 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04007468 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007469 .unlink_done = nfs4_proc_unlink_done,
7470 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04007471 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04007472 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04007473 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007474 .link = nfs4_proc_link,
7475 .symlink = nfs4_proc_symlink,
7476 .mkdir = nfs4_proc_mkdir,
7477 .rmdir = nfs4_proc_remove,
7478 .readdir = nfs4_proc_readdir,
7479 .mknod = nfs4_proc_mknod,
7480 .statfs = nfs4_proc_statfs,
7481 .fsinfo = nfs4_proc_fsinfo,
7482 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04007483 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007484 .decode_dirent = nfs4_decode_dirent,
7485 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04007486 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04007487 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05007488 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007489 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04007490 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04007491 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007492 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007493 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04007494 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007495 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007496 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00007497 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04007498 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04007499 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04007500 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04007501 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04007502 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00007503 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04007504 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04007505 .create_server = nfs4_create_server,
7506 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007507};
7508
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007509static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7510 .prefix = XATTR_NAME_NFSV4_ACL,
7511 .list = nfs4_xattr_list_nfs4_acl,
7512 .get = nfs4_xattr_get_nfs4_acl,
7513 .set = nfs4_xattr_set_nfs4_acl,
7514};
7515
7516const struct xattr_handler *nfs4_xattr_handlers[] = {
7517 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04007518#ifdef CONFIG_NFS_V4_SECURITY_LABEL
7519 &nfs4_xattr_nfs4_label_handler,
7520#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007521 NULL
7522};
7523
Linus Torvalds1da177e2005-04-16 15:20:36 -07007524/*
7525 * Local variables:
7526 * c-basic-offset: 8
7527 * End:
7528 */