blob: 3b5166c20962cb0e91d71d3d97231900bd5ef786 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Trond Myklebust6926afd2012-01-07 13:22:46 -050054#include <linux/nfs_idmap.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000055#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050056#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010057#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050066#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000067#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Trond Myklebustc6d01c62013-08-09 11:51:26 -040069#include "nfs4trace.h"
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define NFSDBG_FACILITY NFSDBG_PROC
72
Trond Myklebust2066fe82006-09-15 08:30:46 -040073#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define NFS4_POLL_RETRY_MAX (15*HZ)
75
Trond Myklebustcdd4e682006-01-03 09:55:12 +010076struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010077static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080078static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050080static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Chuck Lever81934dd2012-03-01 17:01:57 -050081static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040082static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
83static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040084static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
85 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -040086 struct nfs4_state *state, struct nfs4_label *ilabel,
87 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040088#ifdef CONFIG_NFS_V4_1
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040089static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
90 struct rpc_cred *);
91static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *,
92 struct rpc_cred *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040093#endif
David Quigleyaa9c2662013-05-22 12:50:44 -040094
95#ifdef CONFIG_NFS_V4_SECURITY_LABEL
96static inline struct nfs4_label *
97nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
98 struct iattr *sattr, struct nfs4_label *label)
99{
100 int err;
101
102 if (label == NULL)
103 return NULL;
104
105 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
106 return NULL;
107
108 if (NFS_SERVER(dir)->nfs_client->cl_minorversion < 2)
109 return NULL;
110
111 err = security_dentry_init_security(dentry, sattr->ia_mode,
112 &dentry->d_name, (void **)&label->label, &label->len);
113 if (err == 0)
114 return label;
115
116 return NULL;
117}
118static inline void
119nfs4_label_release_security(struct nfs4_label *label)
120{
121 if (label)
122 security_release_secctx(label->label, label->len);
123}
124static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
125{
126 if (label)
127 return server->attr_bitmask;
128
129 return server->attr_bitmask_nl;
130}
131#else
132static inline struct nfs4_label *
133nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
134 struct iattr *sattr, struct nfs4_label *l)
135{ return NULL; }
136static inline void
137nfs4_label_release_security(struct nfs4_label *label)
138{ return; }
139static inline u32 *
140nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
141{ return server->attr_bitmask; }
142#endif
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500145static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Trond Myklebust52567b02009-10-23 14:46:42 -0400147 if (err >= -1000)
148 return err;
149 switch (err) {
150 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500151 case -NFS4ERR_LAYOUTTRYLATER:
152 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400153 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000154 case -NFS4ERR_WRONGSEC:
155 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800156 case -NFS4ERR_BADOWNER:
157 case -NFS4ERR_BADNAME:
158 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400159 case -NFS4ERR_SHARE_DENIED:
160 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400161 case -NFS4ERR_MINOR_VERS_MISMATCH:
162 return -EPROTONOSUPPORT;
Weston Andros Adamson6168f622012-09-10 14:00:46 -0400163 case -NFS4ERR_ACCESS:
164 return -EACCES;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400165 case -NFS4ERR_FILE_OPEN:
166 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400167 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700169 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400170 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400172 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
175/*
176 * This is our standard bitmap for GETATTR requests.
177 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400178const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 FATTR4_WORD0_TYPE
180 | FATTR4_WORD0_CHANGE
181 | FATTR4_WORD0_SIZE
182 | FATTR4_WORD0_FSID
183 | FATTR4_WORD0_FILEID,
184 FATTR4_WORD1_MODE
185 | FATTR4_WORD1_NUMLINKS
186 | FATTR4_WORD1_OWNER
187 | FATTR4_WORD1_OWNER_GROUP
188 | FATTR4_WORD1_RAWDEV
189 | FATTR4_WORD1_SPACE_USED
190 | FATTR4_WORD1_TIME_ACCESS
191 | FATTR4_WORD1_TIME_METADATA
David Quigleyaa9c2662013-05-22 12:50:44 -0400192 | FATTR4_WORD1_TIME_MODIFY,
193#ifdef CONFIG_NFS_V4_SECURITY_LABEL
194 FATTR4_WORD2_SECURITY_LABEL
195#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196};
197
Trond Myklebust1549210f2012-06-05 09:16:47 -0400198static const u32 nfs4_pnfs_open_bitmap[3] = {
199 FATTR4_WORD0_TYPE
200 | FATTR4_WORD0_CHANGE
201 | FATTR4_WORD0_SIZE
202 | FATTR4_WORD0_FSID
203 | FATTR4_WORD0_FILEID,
204 FATTR4_WORD1_MODE
205 | FATTR4_WORD1_NUMLINKS
206 | FATTR4_WORD1_OWNER
207 | FATTR4_WORD1_OWNER_GROUP
208 | FATTR4_WORD1_RAWDEV
209 | FATTR4_WORD1_SPACE_USED
210 | FATTR4_WORD1_TIME_ACCESS
211 | FATTR4_WORD1_TIME_METADATA
212 | FATTR4_WORD1_TIME_MODIFY,
213 FATTR4_WORD2_MDSTHRESHOLD
214};
215
Andy Adamsone23008e2012-10-02 21:07:32 -0400216static const u32 nfs4_open_noattr_bitmap[3] = {
217 FATTR4_WORD0_TYPE
218 | FATTR4_WORD0_CHANGE
219 | FATTR4_WORD0_FILEID,
220};
221
David Quigleya09df2c2013-05-22 12:50:41 -0400222const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 FATTR4_WORD0_FILES_AVAIL
224 | FATTR4_WORD0_FILES_FREE
225 | FATTR4_WORD0_FILES_TOTAL,
226 FATTR4_WORD1_SPACE_AVAIL
227 | FATTR4_WORD1_SPACE_FREE
228 | FATTR4_WORD1_SPACE_TOTAL
229};
230
David Quigleya09df2c2013-05-22 12:50:41 -0400231const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 FATTR4_WORD0_MAXLINK
233 | FATTR4_WORD0_MAXNAME,
234 0
235};
236
Fred Isamandae100c2011-07-30 20:52:37 -0400237const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 | FATTR4_WORD0_MAXREAD
239 | FATTR4_WORD0_MAXWRITE
240 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700241 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400242 | FATTR4_WORD1_FS_LAYOUT_TYPES,
243 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244};
245
David Quigleya09df2c2013-05-22 12:50:41 -0400246const u32 nfs4_fs_locations_bitmap[3] = {
Manoj Naik830b8e32006-06-09 09:34:25 -0400247 FATTR4_WORD0_TYPE
248 | FATTR4_WORD0_CHANGE
249 | FATTR4_WORD0_SIZE
250 | FATTR4_WORD0_FSID
251 | FATTR4_WORD0_FILEID
252 | FATTR4_WORD0_FS_LOCATIONS,
253 FATTR4_WORD1_MODE
254 | FATTR4_WORD1_NUMLINKS
255 | FATTR4_WORD1_OWNER
256 | FATTR4_WORD1_OWNER_GROUP
257 | FATTR4_WORD1_RAWDEV
258 | FATTR4_WORD1_SPACE_USED
259 | FATTR4_WORD1_TIME_ACCESS
260 | FATTR4_WORD1_TIME_METADATA
261 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400262 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400263};
264
Al Virobc4785c2006-10-19 23:28:51 -0700265static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 struct nfs4_readdir_arg *readdir)
267{
Al Viro0dbb4c62006-10-19 23:28:49 -0700268 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000271 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
273 return;
274 }
275
276 readdir->cookie = 0;
277 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
278 if (cookie == 2)
279 return;
280
281 /*
282 * NFSv4 servers do not return entries for '.' and '..'
283 * Therefore, we fake these entries here. We let '.'
284 * have cookie 0 and '..' have cookie 1. Note that
285 * when talking to the server, we always send cookie 0
286 * instead of 1 or 2.
287 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800288 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 if (cookie == 0) {
291 *p++ = xdr_one; /* next */
292 *p++ = xdr_zero; /* cookie, first word */
293 *p++ = xdr_one; /* cookie, second word */
294 *p++ = xdr_one; /* entry len */
295 memcpy(p, ".\0\0\0", 4); /* entry */
296 p++;
297 *p++ = xdr_one; /* bitmap length */
298 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
299 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400300 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302
303 *p++ = xdr_one; /* next */
304 *p++ = xdr_zero; /* cookie, first word */
305 *p++ = xdr_two; /* cookie, second word */
306 *p++ = xdr_two; /* entry len */
307 memcpy(p, "..\0\0", 4); /* entry */
308 p++;
309 *p++ = xdr_one; /* bitmap length */
310 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
311 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400312 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 readdir->pgbase = (char *)p - (char *)start;
315 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800316 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
Trond Myklebust65de8722008-12-23 15:21:44 -0500319static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
320{
321 int res = 0;
322
323 might_sleep();
324
325 if (*timeout <= 0)
326 *timeout = NFS4_POLL_RETRY_MIN;
327 if (*timeout > NFS4_POLL_RETRY_MAX)
328 *timeout = NFS4_POLL_RETRY_MAX;
Colin Cross416ad3c2013-05-06 23:50:06 +0000329 freezable_schedule_timeout_killable_unsafe(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500330 if (fatal_signal_pending(current))
331 res = -ERESTARTSYS;
332 *timeout <<= 1;
333 return res;
334}
335
336/* This is the error handling routine for processes that are allowed
337 * to sleep.
338 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800339static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500340{
341 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500342 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500343 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500344 int ret = errorcode;
345
346 exception->retry = 0;
347 switch(errorcode) {
348 case 0:
349 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500350 case -NFS4ERR_OPENMODE:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400351 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400352 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500353 exception->retry = 1;
354 return 0;
355 }
356 if (state == NULL)
357 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400358 ret = nfs4_schedule_stateid_recovery(server, state);
359 if (ret < 0)
360 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500361 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500362 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500363 case -NFS4ERR_ADMIN_REVOKED:
364 case -NFS4ERR_BAD_STATEID:
Trond Myklebustb570a972013-04-12 15:04:51 -0400365 if (inode != NULL && nfs4_have_delegation(inode, FMODE_READ)) {
366 nfs_remove_bad_delegation(inode);
367 exception->retry = 1;
368 break;
369 }
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500370 if (state == NULL)
371 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400372 ret = nfs4_schedule_stateid_recovery(server, state);
373 if (ret < 0)
374 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500375 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400376 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -0400377 if (state != NULL) {
378 ret = nfs4_schedule_stateid_recovery(server, state);
379 if (ret < 0)
380 break;
381 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500382 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500383 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500384 nfs4_schedule_lease_recovery(clp);
385 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500386#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400387 case -NFS4ERR_BADSESSION:
388 case -NFS4ERR_BADSLOT:
389 case -NFS4ERR_BAD_HIGH_SLOT:
390 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
391 case -NFS4ERR_DEADSESSION:
392 case -NFS4ERR_SEQ_FALSE_RETRY:
393 case -NFS4ERR_SEQ_MISORDERED:
394 dprintk("%s ERROR: %d Reset session\n", __func__,
395 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400396 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400397 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500398#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500399 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500400 if (exception->timeout > HZ) {
401 /* We have retried a decent amount, time to
402 * fail
403 */
404 ret = -EBUSY;
405 break;
406 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500407 case -NFS4ERR_GRACE:
408 case -NFS4ERR_DELAY:
409 ret = nfs4_delay(server->client, &exception->timeout);
410 if (ret != 0)
411 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400412 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500413 case -NFS4ERR_OLD_STATEID:
414 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800415 break;
416 case -NFS4ERR_BADOWNER:
417 /* The following works around a Linux server bug! */
418 case -NFS4ERR_BADNAME:
419 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
420 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
421 exception->retry = 1;
422 printk(KERN_WARNING "NFS: v4 server %s "
423 "does not accept raw "
424 "uid/gids. "
425 "Reenabling the idmapper.\n",
426 server->nfs_client->cl_hostname);
427 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500428 }
429 /* We failed to handle the error */
430 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500431wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500432 ret = nfs4_wait_clnt_recover(clp);
433 if (ret == 0)
434 exception->retry = 1;
435 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500436}
437
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400438/*
439 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
440 * or 'false' otherwise.
441 */
442static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
443{
444 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
445
446 if (flavor == RPC_AUTH_GSS_KRB5I ||
447 flavor == RPC_AUTH_GSS_KRB5P)
448 return true;
449
450 return false;
451}
Trond Myklebust65de8722008-12-23 15:21:44 -0500452
Trond Myklebust452e9352010-07-31 14:29:06 -0400453static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 spin_lock(&clp->cl_lock);
456 if (time_before(clp->cl_last_renewal,timestamp))
457 clp->cl_last_renewal = timestamp;
458 spin_unlock(&clp->cl_lock);
459}
460
Trond Myklebust452e9352010-07-31 14:29:06 -0400461static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
462{
463 do_renew_lease(server->nfs_client, timestamp);
464}
465
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400466struct nfs4_call_sync_data {
467 const struct nfs_server *seq_server;
468 struct nfs4_sequence_args *seq_args;
469 struct nfs4_sequence_res *seq_res;
470};
471
Chuck Levera9c92d62013-08-09 12:48:18 -0400472static void nfs4_init_sequence(struct nfs4_sequence_args *args,
473 struct nfs4_sequence_res *res, int cache_reply)
474{
475 args->sa_slot = NULL;
476 args->sa_cache_this = cache_reply;
477 args->sa_privileged = 0;
478
479 res->sr_slot = NULL;
480}
481
482static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
483{
484 args->sa_privileged = 1;
485}
486
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400487#if defined(CONFIG_NFS_V4_1)
488
Trond Myklebustd185a332010-06-16 09:52:25 -0400489static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400490{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500491 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400492 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500493 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400494
Benny Halevydfb4f3092010-09-24 09:17:01 -0400495 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400496 /* just wake up the next guy waiting since
497 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500498 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500499 return;
Andy Adamson13615872009-04-01 09:22:17 -0400500 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500501 tbl = res->sr_slot->table;
502 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500503
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500504 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500505 /* Be nice to the server: try to ensure that the last transmitted
506 * value for highest_user_slotid <= target_highest_slotid
507 */
508 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
509 send_new_highest_used_slotid = true;
510
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500511 if (nfs41_wake_and_assign_slot(tbl, res->sr_slot)) {
512 send_new_highest_used_slotid = false;
513 goto out_unlock;
514 }
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500515 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500516
517 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
518 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500519out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500520 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400521 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500522 if (send_new_highest_used_slotid)
523 nfs41_server_notify_highest_slotid_update(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400524}
525
Trond Myklebust14516c32010-07-31 14:29:06 -0400526static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400527{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500528 struct nfs4_session *session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500529 struct nfs4_slot *slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400530 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500531 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500532 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400533
Bryan Schumaker468f8612011-04-18 15:57:32 -0400534 /* don't increment the sequence number if the task wasn't sent */
535 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400536 goto out;
537
Trond Myklebust933602e2012-11-16 12:12:38 -0500538 slot = res->sr_slot;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500539 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500540
Trond Myklebustac20d162012-12-15 15:36:07 -0500541 if (slot->interrupted) {
542 slot->interrupted = 0;
543 interrupted = true;
544 }
545
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400546 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500547 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400548 switch (res->sr_status) {
549 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400550 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500551 ++slot->seq_nr;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500552 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500553 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500554 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500555 if (res->sr_status_flags != 0)
556 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500557 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400558 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500559 case 1:
560 /*
561 * sr_status remains 1 if an RPC level error occurred.
562 * The server may or may not have processed the sequence
563 * operation..
564 * Mark the slot as having hosted an interrupted RPC call.
565 */
566 slot->interrupted = 1;
567 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400568 case -NFS4ERR_DELAY:
569 /* The server detected a resend of the RPC call and
570 * returned NFS4ERR_DELAY as per Section 2.10.6.2
571 * of RFC5661.
572 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500573 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400574 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500575 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500576 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400577 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500578 case -NFS4ERR_BADSLOT:
579 /*
580 * The slot id we used was probably retired. Try again
581 * using a different slot id.
582 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500583 goto retry_nowait;
584 case -NFS4ERR_SEQ_MISORDERED:
585 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500586 * Was the last operation on this sequence interrupted?
587 * If so, retry after bumping the sequence number.
588 */
589 if (interrupted) {
590 ++slot->seq_nr;
591 goto retry_nowait;
592 }
593 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500594 * Could this slot have been previously retired?
595 * If so, then the server may be expecting seq_nr = 1!
596 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500597 if (slot->seq_nr != 1) {
598 slot->seq_nr = 1;
599 goto retry_nowait;
600 }
601 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500602 case -NFS4ERR_SEQ_FALSE_RETRY:
603 ++slot->seq_nr;
604 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400605 default:
606 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500607 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400608 }
609out:
610 /* The session may be reset by one of the error handlers. */
611 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400612 nfs41_sequence_free_slot(res);
Trond Myklebust85563072012-12-11 10:31:12 -0500613 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500614retry_nowait:
615 if (rpc_restart_call_prepare(task)) {
616 task->tk_status = 0;
617 ret = 0;
618 }
619 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400620out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400621 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400622 goto out;
623 rpc_delay(task, NFS4_POLL_RETRY_MAX);
624 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400625}
626
Trond Myklebust14516c32010-07-31 14:29:06 -0400627static int nfs4_sequence_done(struct rpc_task *task,
628 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400629{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500630 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400631 return 1;
632 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400633}
634
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000635int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400636 struct nfs4_sequence_args *args,
637 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400638 struct rpc_task *task)
639{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400640 struct nfs4_slot *slot;
641 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400642
643 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400644 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400645 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400646 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400647
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400648 tbl = &session->fc_slot_table;
649
Trond Myklebust69d206b2012-11-22 13:21:02 -0500650 task->tk_timeout = 0;
651
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400652 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400653 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400654 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500655 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500656 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400657 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400658 }
659
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500660 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500661 if (IS_ERR(slot)) {
662 /* If out of memory, try again in 1/4 second */
663 if (slot == ERR_PTR(-ENOMEM))
664 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400665 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400666 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400667 }
668 spin_unlock(&tbl->slot_tbl_lock);
669
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500670 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400671
Chuck Levere8d92382013-08-09 12:47:51 -0400672 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500673 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400674
Benny Halevydfb4f3092010-09-24 09:17:01 -0400675 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500676 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400677 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400678 /*
679 * sr_status is only set in decode_sequence, and so will remain
680 * set to 1 if an rpc level failure occurs.
681 */
682 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400683 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400684out_success:
685 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400686 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400687out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400688 /* Privileged tasks are queued with top priority */
689 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400690 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
691 NULL, RPC_PRIORITY_PRIVILEGED);
692 else
693 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400694 spin_unlock(&tbl->slot_tbl_lock);
695 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400696}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000697EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400698
Chuck Lever5a580e02013-08-09 12:48:09 -0400699static int nfs4_setup_sequence(const struct nfs_server *server,
700 struct nfs4_sequence_args *args,
701 struct nfs4_sequence_res *res,
702 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400703{
Trond Myklebust035168a2010-06-16 09:52:26 -0400704 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400705 int ret = 0;
706
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400707 if (session == NULL) {
708 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400709 goto out;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400710 }
Trond Myklebust035168a2010-06-16 09:52:26 -0400711
Chuck Levere8d92382013-08-09 12:47:51 -0400712 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400713 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400714 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168a2010-06-16 09:52:26 -0400715
Trond Myklebust9d12b212012-01-17 22:04:25 -0500716 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400717out:
718 dprintk("<-- %s status=%d\n", __func__, ret);
719 return ret;
720}
721
Andy Adamsonce5039c2009-04-01 09:22:13 -0400722static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
723{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400724 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400725 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400726
Trond Myklebust035168a2010-06-16 09:52:26 -0400727 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
728
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400729 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400730}
731
Andy Adamson69ab40c2009-04-01 09:22:19 -0400732static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
733{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400734 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400735
Trond Myklebust14516c32010-07-31 14:29:06 -0400736 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400737}
738
Trond Myklebust17280172012-03-11 13:11:00 -0400739static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400740 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400741 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400742};
743
Trond Myklebustdf896452010-06-16 09:52:26 -0400744#else
Chuck Lever5a580e02013-08-09 12:48:09 -0400745static int nfs4_setup_sequence(const struct nfs_server *server,
746 struct nfs4_sequence_args *args,
747 struct nfs4_sequence_res *res,
748 struct rpc_task *task)
749{
750 rpc_call_start(task);
751 return 0;
752}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400753
Trond Myklebust14516c32010-07-31 14:29:06 -0400754static int nfs4_sequence_done(struct rpc_task *task,
755 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400756{
Trond Myklebust14516c32010-07-31 14:29:06 -0400757 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400758}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400759#endif /* CONFIG_NFS_V4_1 */
760
Chuck Lever9915ea72013-08-09 12:48:27 -0400761static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
762{
763 struct nfs4_call_sync_data *data = calldata;
764 nfs4_setup_sequence(data->seq_server,
765 data->seq_args, data->seq_res, task);
766}
767
768static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
769{
770 struct nfs4_call_sync_data *data = calldata;
771 nfs4_sequence_done(task, data->seq_res);
772}
773
774static const struct rpc_call_ops nfs40_call_sync_ops = {
775 .rpc_call_prepare = nfs40_call_sync_prepare,
776 .rpc_call_done = nfs40_call_sync_done,
777};
778
779static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
780 struct nfs_server *server,
781 struct rpc_message *msg,
782 struct nfs4_sequence_args *args,
783 struct nfs4_sequence_res *res)
784{
785 int ret;
786 struct rpc_task *task;
787 struct nfs_client *clp = server->nfs_client;
788 struct nfs4_call_sync_data data = {
789 .seq_server = server,
790 .seq_args = args,
791 .seq_res = res,
792 };
793 struct rpc_task_setup task_setup = {
794 .rpc_client = clnt,
795 .rpc_message = msg,
796 .callback_ops = clp->cl_mvops->call_sync_ops,
797 .callback_data = &data
798 };
799
800 task = rpc_run_task(&task_setup);
801 if (IS_ERR(task))
802 ret = PTR_ERR(task);
803 else {
804 ret = task->tk_status;
805 rpc_put_task(task);
806 }
807 return ret;
808}
809
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400810static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000811int nfs4_call_sync(struct rpc_clnt *clnt,
812 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000813 struct rpc_message *msg,
814 struct nfs4_sequence_args *args,
815 struct nfs4_sequence_res *res,
816 int cache_reply)
817{
Chuck Levera9c92d62013-08-09 12:48:18 -0400818 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -0400819 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000820}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400821
Trond Myklebust38478b22006-05-25 01:40:57 -0400822static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
Trond Myklebust38478b22006-05-25 01:40:57 -0400824 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Trond Myklebust38478b22006-05-25 01:40:57 -0400826 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400827 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700828 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400829 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700830 dir->i_version = cinfo->after;
David Howellsde242c02012-12-20 21:52:38 +0000831 nfs_fscache_invalidate(dir);
Trond Myklebust38478b22006-05-25 01:40:57 -0400832 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833}
834
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100835struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400836 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100837 struct nfs_openargs o_arg;
838 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100839 struct nfs_open_confirmargs c_arg;
840 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500841 struct nfs4_string owner_name;
842 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100843 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400844 struct nfs4_label *f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100845 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400846 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100847 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400848 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100849 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100850 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400851 unsigned int rpc_done : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -0400852 unsigned int is_recover : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100853 int rpc_status;
854 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100855};
856
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400857static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
858 int err, struct nfs4_exception *exception)
859{
860 if (err != -EINVAL)
861 return false;
862 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
863 return false;
864 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
865 exception->retry = 1;
866 return true;
867}
868
869static enum open_claim_type4
870nfs4_map_atomic_open_claim(struct nfs_server *server,
871 enum open_claim_type4 claim)
872{
873 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
874 return claim;
875 switch (claim) {
876 default:
877 return claim;
878 case NFS4_OPEN_CLAIM_FH:
879 return NFS4_OPEN_CLAIM_NULL;
880 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
881 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
882 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
883 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
884 }
885}
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400886
887static void nfs4_init_opendata_res(struct nfs4_opendata *p)
888{
889 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400890 p->o_res.f_label = p->f_label;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400891 p->o_res.seqid = p->o_arg.seqid;
892 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400893 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400894 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400895 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500896 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400897}
898
Al Viro82a2c1b2011-06-22 18:30:55 -0400899static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500900 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400901 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -0400902 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400903 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400904 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100905{
Al Viro82a2c1b2011-06-22 18:30:55 -0400906 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100907 struct inode *dir = parent->d_inode;
908 struct nfs_server *server = NFS_SERVER(dir);
909 struct nfs4_opendata *p;
910
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400911 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100912 if (p == NULL)
913 goto err;
David Quigley14c43f72013-05-22 12:50:43 -0400914
915 p->f_label = nfs4_label_alloc(server, gfp_mask);
916 if (IS_ERR(p->f_label))
917 goto err_free_p;
918
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400919 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100920 if (p->o_arg.seqid == NULL)
David Quigley14c43f72013-05-22 12:50:43 -0400921 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -0400922 nfs_sb_active(dentry->d_sb);
923 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100924 p->dir = parent;
925 p->owner = sp;
926 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500927 p->o_arg.open_flags = flags;
928 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700929 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
930 * will return permission denied for all bits until close */
931 if (!(flags & O_EXCL)) {
932 /* ask server to check for all possible rights as results
933 * are cached */
934 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
935 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700936 }
David Howells7539bba2006-08-22 20:06:09 -0400937 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400938 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
939 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400940 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100941 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -0400942 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -0400943 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
David Quigley1775fd32013-05-22 12:50:42 -0400944 p->o_arg.label = label;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400945 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
946 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400947 case NFS4_OPEN_CLAIM_NULL:
948 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
949 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
950 p->o_arg.fh = NFS_FH(dir);
951 break;
952 case NFS4_OPEN_CLAIM_PREVIOUS:
953 case NFS4_OPEN_CLAIM_FH:
954 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
955 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
956 p->o_arg.fh = NFS_FH(dentry->d_inode);
957 }
Trond Myklebust536e43d2012-01-17 22:04:26 -0500958 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -0400959 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400960
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100961 p->o_arg.u.attrs = &p->attrs;
962 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500963
964 verf[0] = jiffies;
965 verf[1] = current->pid;
966 memcpy(p->o_arg.u.verifier.data, verf,
967 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100968 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100969 p->c_arg.fh = &p->o_res.fh;
970 p->c_arg.stateid = &p->o_res.stateid;
971 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400972 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400973 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100974 return p;
David Quigley14c43f72013-05-22 12:50:43 -0400975
976err_free_label:
977 nfs4_label_free(p->f_label);
978err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100979 kfree(p);
980err:
981 dput(parent);
982 return NULL;
983}
984
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400985static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100986{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400987 struct nfs4_opendata *p = container_of(kref,
988 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400989 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400990
991 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400992 if (p->state != NULL)
993 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400994 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -0400995
996 nfs4_label_free(p->f_label);
997
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400998 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400999 dput(p->dentry);
1000 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001001 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001002 kfree(p);
1003}
1004
1005static void nfs4_opendata_put(struct nfs4_opendata *p)
1006{
1007 if (p != NULL)
1008 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001009}
1010
Trond Myklebust06f814a2006-01-03 09:55:07 +01001011static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1012{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001013 int ret;
1014
Trond Myklebust06f814a2006-01-03 09:55:07 +01001015 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001016 return ret;
1017}
1018
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001019static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001020{
1021 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001022
Trond Myklebust536e43d2012-01-17 22:04:26 -05001023 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001024 goto out;
1025 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001026 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001027 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1028 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001029 break;
1030 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001031 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1032 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001033 break;
1034 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001035 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1036 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001037 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001038out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001039 return ret;
1040}
1041
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001042static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001043{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001044 if (delegation == NULL)
1045 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001046 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001047 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -05001048 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -04001049 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001050 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1051 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -05001052 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001053 return 1;
1054}
1055
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001056static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001057{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001058 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001059 case FMODE_WRITE:
1060 state->n_wronly++;
1061 break;
1062 case FMODE_READ:
1063 state->n_rdonly++;
1064 break;
1065 case FMODE_READ|FMODE_WRITE:
1066 state->n_rdwr++;
1067 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001068 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001069}
1070
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001071static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001072{
1073 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001074 nfs4_stateid_copy(&state->stateid, stateid);
1075 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust92b40e92013-04-20 01:25:45 -04001076 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001077 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001078 case FMODE_READ:
1079 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1080 break;
1081 case FMODE_WRITE:
1082 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1083 break;
1084 case FMODE_READ|FMODE_WRITE:
1085 set_bit(NFS_O_RDWR_STATE, &state->flags);
1086 }
1087}
1088
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001089static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001090{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001091 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001092 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001093 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001094}
1095
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001096static 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 -07001097{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001098 /*
1099 * Protect the call to nfs4_state_set_mode_locked and
1100 * serialise the stateid update
1101 */
1102 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001103 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001104 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001105 set_bit(NFS_DELEGATED_STATE, &state->flags);
1106 }
1107 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001108 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001109 write_sequnlock(&state->seqlock);
1110 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001111 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001112 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113}
1114
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001115static 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 -05001116{
1117 struct nfs_inode *nfsi = NFS_I(state->inode);
1118 struct nfs_delegation *deleg_cur;
1119 int ret = 0;
1120
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001121 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001122
1123 rcu_read_lock();
1124 deleg_cur = rcu_dereference(nfsi->delegation);
1125 if (deleg_cur == NULL)
1126 goto no_delegation;
1127
1128 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001129 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001130 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001131 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001132 goto no_delegation_unlock;
1133
1134 if (delegation == NULL)
1135 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001136 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001137 goto no_delegation_unlock;
1138
Trond Myklebustb7391f42008-12-23 15:21:52 -05001139 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001140 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001141 ret = 1;
1142no_delegation_unlock:
1143 spin_unlock(&deleg_cur->lock);
1144no_delegation:
1145 rcu_read_unlock();
1146
1147 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001148 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001149 ret = 1;
1150 }
1151
1152 return ret;
1153}
1154
1155
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001156static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001157{
1158 struct nfs_delegation *delegation;
1159
1160 rcu_read_lock();
1161 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001162 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001163 rcu_read_unlock();
1164 return;
1165 }
1166 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001167 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001168}
1169
Trond Myklebust6ee41262007-07-08 14:11:36 -04001170static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001171{
1172 struct nfs4_state *state = opendata->state;
1173 struct nfs_inode *nfsi = NFS_I(state->inode);
1174 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001175 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001176 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001177 nfs4_stateid stateid;
1178 int ret = -EAGAIN;
1179
Trond Myklebustaac00a82007-07-05 19:02:21 -04001180 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001181 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001182 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001183 if (can_open_cached(state, fmode, open_mode)) {
1184 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001185 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001186 goto out_return_state;
1187 }
1188 spin_unlock(&state->owner->so_lock);
1189 }
Trond Myklebust34310432008-12-23 15:21:38 -05001190 rcu_read_lock();
1191 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001192 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001193 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001194 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001195 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001196 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001197 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001198 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001199 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001200 if (!opendata->is_recover) {
1201 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1202 if (ret != 0)
1203 goto out;
1204 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001205 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001206
1207 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001208 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001209 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001210 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001211out:
1212 return ERR_PTR(ret);
1213out_return_state:
1214 atomic_inc(&state->count);
1215 return state;
1216}
1217
Andy Adamsone23008e2012-10-02 21:07:32 -04001218static void
1219nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1220{
1221 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1222 struct nfs_delegation *delegation;
1223 int delegation_flags = 0;
1224
1225 rcu_read_lock();
1226 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1227 if (delegation)
1228 delegation_flags = delegation->flags;
1229 rcu_read_unlock();
1230 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1231 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1232 "returning a delegation for "
1233 "OPEN(CLAIM_DELEGATE_CUR)\n",
1234 clp->cl_hostname);
1235 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1236 nfs_inode_set_delegation(state->inode,
1237 data->owner->so_cred,
1238 &data->o_res);
1239 else
1240 nfs_inode_reclaim_delegation(state->inode,
1241 data->owner->so_cred,
1242 &data->o_res);
1243}
1244
1245/*
1246 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1247 * and update the nfs4_state.
1248 */
1249static struct nfs4_state *
1250_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1251{
1252 struct inode *inode = data->state->inode;
1253 struct nfs4_state *state = data->state;
1254 int ret;
1255
1256 if (!data->rpc_done) {
1257 ret = data->rpc_status;
1258 goto err;
1259 }
1260
1261 ret = -ESTALE;
1262 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1263 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1264 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1265 goto err;
1266
1267 ret = -ENOMEM;
1268 state = nfs4_get_open_state(inode, data->owner);
1269 if (state == NULL)
1270 goto err;
1271
1272 ret = nfs_refresh_inode(inode, &data->f_attr);
1273 if (ret)
1274 goto err;
1275
David Quigleyaa9c2662013-05-22 12:50:44 -04001276 nfs_setsecurity(inode, &data->f_attr, data->f_label);
1277
Andy Adamsone23008e2012-10-02 21:07:32 -04001278 if (data->o_res.delegation_type != 0)
1279 nfs4_opendata_check_deleg(data, state);
1280 update_open_stateid(state, &data->o_res.stateid, NULL,
1281 data->o_arg.fmode);
1282
1283 return state;
1284err:
1285 return ERR_PTR(ret);
1286
1287}
1288
1289static struct nfs4_state *
1290_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001291{
1292 struct inode *inode;
1293 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001294 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001295
Trond Myklebustaac00a82007-07-05 19:02:21 -04001296 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001297 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001298 goto out;
1299 }
1300
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001301 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001302 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001303 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001304 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001305 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001306 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001307 goto err;
1308 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001309 state = nfs4_get_open_state(inode, data->owner);
1310 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001311 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001312 if (data->o_res.delegation_type != 0)
1313 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001314 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001315 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001316 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001317out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001318 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001319 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001320err_put_inode:
1321 iput(inode);
1322err:
1323 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001324}
1325
Andy Adamsone23008e2012-10-02 21:07:32 -04001326static struct nfs4_state *
1327nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1328{
1329 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1330 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1331 return _nfs4_opendata_to_nfs4_state(data);
1332}
1333
Trond Myklebust864472e2006-01-03 09:55:15 +01001334static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1335{
1336 struct nfs_inode *nfsi = NFS_I(state->inode);
1337 struct nfs_open_context *ctx;
1338
1339 spin_lock(&state->inode->i_lock);
1340 list_for_each_entry(ctx, &nfsi->open_files, list) {
1341 if (ctx->state != state)
1342 continue;
1343 get_nfs_open_context(ctx);
1344 spin_unlock(&state->inode->i_lock);
1345 return ctx;
1346 }
1347 spin_unlock(&state->inode->i_lock);
1348 return ERR_PTR(-ENOENT);
1349}
1350
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001351static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1352 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001353{
1354 struct nfs4_opendata *opendata;
1355
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001356 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001357 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001358 if (opendata == NULL)
1359 return ERR_PTR(-ENOMEM);
1360 opendata->state = state;
1361 atomic_inc(&state->count);
1362 return opendata;
1363}
1364
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001365static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001366{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001367 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001368 int ret;
1369
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001370 opendata->o_arg.open_flags = 0;
1371 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001372 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1373 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1374 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001375 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001376 if (ret != 0)
1377 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001378 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001379 if (IS_ERR(newstate))
1380 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001381 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001382 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001383 return 0;
1384}
1385
1386static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1387{
Trond Myklebust864472e2006-01-03 09:55:15 +01001388 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001389 int ret;
1390
1391 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001392 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001393 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001394 smp_rmb();
1395 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001396 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001397 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001398 if (ret != 0)
1399 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001400 if (newstate != state)
1401 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001402 }
1403 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001404 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001405 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001406 if (ret != 0)
1407 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001408 if (newstate != state)
1409 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001410 }
1411 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001412 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001413 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001414 if (ret != 0)
1415 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001416 if (newstate != state)
1417 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001418 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001419 /*
1420 * We may have performed cached opens for all three recoveries.
1421 * Check if we need to update the current stateid.
1422 */
1423 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001424 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001425 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001426 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001427 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001428 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001429 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001430 return 0;
1431}
1432
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433/*
1434 * OPEN_RECLAIM:
1435 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001437static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001439 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001440 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001441 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 int status;
1443
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001444 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1445 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001446 if (IS_ERR(opendata))
1447 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001448 rcu_read_lock();
1449 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001450 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001451 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001452 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001453 opendata->o_arg.u.delegation_type = delegation_type;
1454 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001455 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 return status;
1457}
1458
Trond Myklebust539cd032007-06-05 11:46:42 -04001459static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
1461 struct nfs_server *server = NFS_SERVER(state->inode);
1462 struct nfs4_exception exception = { };
1463 int err;
1464 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001465 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001466 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001467 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1468 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001469 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001470 break;
1471 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 } while (exception.retry);
1473 return err;
1474}
1475
Trond Myklebust864472e2006-01-03 09:55:15 +01001476static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1477{
1478 struct nfs_open_context *ctx;
1479 int ret;
1480
1481 ctx = nfs4_state_find_open_context(state);
1482 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001483 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001484 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001485 put_nfs_open_context(ctx);
1486 return ret;
1487}
1488
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001489static 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 -07001490{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001491 switch (err) {
1492 default:
1493 printk(KERN_ERR "NFS: %s: unhandled error "
1494 "%d.\n", __func__, err);
1495 case 0:
1496 case -ENOENT:
1497 case -ESTALE:
1498 break;
1499 case -NFS4ERR_BADSESSION:
1500 case -NFS4ERR_BADSLOT:
1501 case -NFS4ERR_BAD_HIGH_SLOT:
1502 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1503 case -NFS4ERR_DEADSESSION:
1504 set_bit(NFS_DELEGATED_STATE, &state->flags);
1505 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1506 return -EAGAIN;
1507 case -NFS4ERR_STALE_CLIENTID:
1508 case -NFS4ERR_STALE_STATEID:
1509 set_bit(NFS_DELEGATED_STATE, &state->flags);
1510 case -NFS4ERR_EXPIRED:
1511 /* Don't recall a delegation if it was lost */
1512 nfs4_schedule_lease_recovery(server->nfs_client);
1513 return -EAGAIN;
1514 case -NFS4ERR_DELEG_REVOKED:
1515 case -NFS4ERR_ADMIN_REVOKED:
1516 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001517 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001518 nfs_inode_find_state_and_recover(state->inode,
1519 stateid);
1520 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001521 return 0;
1522 case -NFS4ERR_DELAY:
1523 case -NFS4ERR_GRACE:
1524 set_bit(NFS_DELEGATED_STATE, &state->flags);
1525 ssleep(1);
1526 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001527 case -ENOMEM:
1528 case -NFS4ERR_DENIED:
1529 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1530 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 return err;
1533}
1534
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001535int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1536{
1537 struct nfs_server *server = NFS_SERVER(state->inode);
1538 struct nfs4_opendata *opendata;
1539 int err;
1540
1541 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1542 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1543 if (IS_ERR(opendata))
1544 return PTR_ERR(opendata);
1545 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1546 err = nfs4_open_recover(opendata, state);
1547 nfs4_opendata_put(opendata);
1548 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1549}
1550
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001551static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1552{
1553 struct nfs4_opendata *data = calldata;
1554
1555 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001556 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001557 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001558 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001559 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001560 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001561 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001562}
1563
1564static void nfs4_open_confirm_release(void *calldata)
1565{
1566 struct nfs4_opendata *data = calldata;
1567 struct nfs4_state *state = NULL;
1568
1569 /* If this request hasn't been cancelled, do nothing */
1570 if (data->cancelled == 0)
1571 goto out_free;
1572 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001573 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001574 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001575 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001576 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001577 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001578out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001579 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001580}
1581
1582static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001583 .rpc_call_done = nfs4_open_confirm_done,
1584 .rpc_release = nfs4_open_confirm_release,
1585};
1586
1587/*
1588 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1589 */
1590static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1591{
1592 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1593 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001594 struct rpc_message msg = {
1595 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1596 .rpc_argp = &data->c_arg,
1597 .rpc_resp = &data->c_res,
1598 .rpc_cred = data->owner->so_cred,
1599 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001600 struct rpc_task_setup task_setup_data = {
1601 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001602 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001603 .callback_ops = &nfs4_open_confirm_ops,
1604 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001605 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001606 .flags = RPC_TASK_ASYNC,
1607 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 int status;
1609
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001610 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001611 data->rpc_done = 0;
1612 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001613 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001614 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001615 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001616 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001617 status = nfs4_wait_for_completion_rpc_task(task);
1618 if (status != 0) {
1619 data->cancelled = 1;
1620 smp_wmb();
1621 } else
1622 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001623 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 return status;
1625}
1626
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001627static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001629 struct nfs4_opendata *data = calldata;
1630 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001631 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001632
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001633 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001634 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001635 /*
1636 * Check if we still need to send an OPEN call, or if we can use
1637 * a delegation instead.
1638 */
1639 if (data->state != NULL) {
1640 struct nfs_delegation *delegation;
1641
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001642 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001643 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001644 rcu_read_lock();
1645 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001646 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
Trond Myklebustcd4c9be2013-04-23 14:46:25 -04001647 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
Trond Myklebust652f89f2011-12-09 19:05:58 -05001648 can_open_delegated(delegation, data->o_arg.fmode))
1649 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001650 rcu_read_unlock();
1651 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001652 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04001653 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust8188df12013-04-23 14:31:19 -04001654 switch (data->o_arg.claim) {
1655 case NFS4_OPEN_CLAIM_PREVIOUS:
1656 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1657 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001658 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04001659 case NFS4_OPEN_CLAIM_FH:
1660 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001661 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1662 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001663 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001664 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001665 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001666 &data->o_res.seq_res,
1667 task) != 0)
1668 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04001669
1670 /* Set the create mode (note dependency on the session type) */
1671 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1672 if (data->o_arg.open_flags & O_EXCL) {
1673 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1674 if (nfs4_has_persistent_session(clp))
1675 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1676 else if (clp->cl_mvops->minor_version > 0)
1677 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1678 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04001679 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001680unlock_no_action:
1681 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001682out_no_action:
1683 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001684out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001685 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001686}
1687
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001688static void nfs4_open_done(struct rpc_task *task, void *calldata)
1689{
1690 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001692 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001693
Trond Myklebust14516c32010-07-31 14:29:06 -04001694 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1695 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001696
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001697 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001698 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1699 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001700 case S_IFREG:
1701 break;
1702 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001703 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001704 break;
1705 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001706 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001707 break;
1708 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001709 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001710 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001711 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001712 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001713 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1714 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001715 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001716 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001717}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001718
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001719static void nfs4_open_release(void *calldata)
1720{
1721 struct nfs4_opendata *data = calldata;
1722 struct nfs4_state *state = NULL;
1723
1724 /* If this request hasn't been cancelled, do nothing */
1725 if (data->cancelled == 0)
1726 goto out_free;
1727 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001728 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001729 goto out_free;
1730 /* In case we need an open_confirm, no cleanup! */
1731 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1732 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001733 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001734 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001735 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001736out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001737 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001738}
1739
1740static const struct rpc_call_ops nfs4_open_ops = {
1741 .rpc_call_prepare = nfs4_open_prepare,
1742 .rpc_call_done = nfs4_open_done,
1743 .rpc_release = nfs4_open_release,
1744};
1745
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001746static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001747{
1748 struct inode *dir = data->dir->d_inode;
1749 struct nfs_server *server = NFS_SERVER(dir);
1750 struct nfs_openargs *o_arg = &data->o_arg;
1751 struct nfs_openres *o_res = &data->o_res;
1752 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001753 struct rpc_message msg = {
1754 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1755 .rpc_argp = o_arg,
1756 .rpc_resp = o_res,
1757 .rpc_cred = data->owner->so_cred,
1758 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001759 struct rpc_task_setup task_setup_data = {
1760 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001761 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001762 .callback_ops = &nfs4_open_ops,
1763 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001764 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001765 .flags = RPC_TASK_ASYNC,
1766 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001767 int status;
1768
Chuck Levera9c92d62013-08-09 12:48:18 -04001769 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001770 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001771 data->rpc_done = 0;
1772 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001773 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001774 data->is_recover = 0;
1775 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001776 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001777 data->is_recover = 1;
1778 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04001779 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001780 if (IS_ERR(task))
1781 return PTR_ERR(task);
1782 status = nfs4_wait_for_completion_rpc_task(task);
1783 if (status != 0) {
1784 data->cancelled = 1;
1785 smp_wmb();
1786 } else
1787 status = data->rpc_status;
1788 rpc_put_task(task);
1789
1790 return status;
1791}
1792
1793static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1794{
1795 struct inode *dir = data->dir->d_inode;
1796 struct nfs_openres *o_res = &data->o_res;
1797 int status;
1798
1799 status = nfs4_run_open_task(data, 1);
1800 if (status != 0 || !data->rpc_done)
1801 return status;
1802
Trond Myklebust6926afd2012-01-07 13:22:46 -05001803 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1804
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001805 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1806 status = _nfs4_proc_open_confirm(data);
1807 if (status != 0)
1808 return status;
1809 }
1810
1811 return status;
1812}
1813
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001814static int nfs4_opendata_access(struct rpc_cred *cred,
1815 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001816 struct nfs4_state *state, fmode_t fmode,
1817 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001818{
1819 struct nfs_access_entry cache;
1820 u32 mask;
1821
1822 /* access call failed or for some reason the server doesn't
1823 * support any access modes -- defer access call until later */
1824 if (opendata->o_res.access_supported == 0)
1825 return 0;
1826
1827 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001828 /* don't check MAY_WRITE - a newly created file may not have
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001829 * write mode bits, but POSIX allows the creating process to write.
1830 * use openflags to check for exec, because fmode won't
1831 * always have FMODE_EXEC set when file open for exec. */
1832 if (openflags & __FMODE_EXEC) {
1833 /* ONLY check for exec rights */
1834 mask = MAY_EXEC;
1835 } else if (fmode & FMODE_READ)
1836 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001837
1838 cache.cred = cred;
1839 cache.jiffies = jiffies;
1840 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1841 nfs_access_add_cache(state->inode, &cache);
1842
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001843 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001844 return 0;
1845
1846 /* even though OPEN succeeded, access is denied. Close the file */
1847 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001848 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001849}
1850
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001851/*
1852 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1853 */
1854static int _nfs4_proc_open(struct nfs4_opendata *data)
1855{
1856 struct inode *dir = data->dir->d_inode;
1857 struct nfs_server *server = NFS_SERVER(dir);
1858 struct nfs_openargs *o_arg = &data->o_arg;
1859 struct nfs_openres *o_res = &data->o_res;
1860 int status;
1861
1862 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001863 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001864 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001865 if (status != 0) {
1866 if (status == -NFS4ERR_BADNAME &&
1867 !(o_arg->open_flags & O_CREAT))
1868 return -ENOENT;
1869 return status;
1870 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001871
Trond Myklebust6926afd2012-01-07 13:22:46 -05001872 nfs_fattr_map_and_free_names(server, &data->f_attr);
1873
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001874 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001875 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001876 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1877 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001879 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001881 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 }
1883 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
David Quigley1775fd32013-05-22 12:50:42 -04001884 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001885 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886}
1887
Andy Adamsond83217c2011-03-01 01:34:17 +00001888static int nfs4_recover_expired_lease(struct nfs_server *server)
1889{
1890 return nfs4_client_recover_expired_lease(server->nfs_client);
1891}
1892
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893/*
1894 * OPEN_EXPIRED:
1895 * reclaim state on the server after a network partition.
1896 * Assumes caller holds the appropriate lock
1897 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001898static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001900 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001901 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001903 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001904 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001905 if (IS_ERR(opendata))
1906 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001907 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001908 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001909 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001910 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001911 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912}
1913
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001914static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001915{
Trond Myklebust539cd032007-06-05 11:46:42 -04001916 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001917 struct nfs4_exception exception = { };
1918 int err;
1919
1920 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001921 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001922 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001923 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1924 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001925 switch (err) {
1926 default:
1927 goto out;
1928 case -NFS4ERR_GRACE:
1929 case -NFS4ERR_DELAY:
1930 nfs4_handle_exception(server, err, &exception);
1931 err = 0;
1932 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001933 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001934out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001935 return err;
1936}
1937
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1939{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001941 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
Trond Myklebust864472e2006-01-03 09:55:15 +01001943 ctx = nfs4_state_find_open_context(state);
1944 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001945 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001946 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001947 put_nfs_open_context(ctx);
1948 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949}
1950
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001951#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001952static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001953{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001954 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001955 nfs4_stateid *stateid = &state->stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001956 struct nfs_delegation *delegation;
1957 struct rpc_cred *cred = NULL;
1958 int status = -NFS4ERR_BAD_STATEID;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001959
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001960 /* If a state reset has been done, test_stateid is unneeded */
1961 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1962 return;
1963
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001964 /* Get the delegation credential for use by test/free_stateid */
1965 rcu_read_lock();
1966 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1967 if (delegation != NULL &&
1968 nfs4_stateid_match(&delegation->stateid, stateid)) {
1969 cred = get_rpccred(delegation->cred);
1970 rcu_read_unlock();
1971 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04001972 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001973 } else
1974 rcu_read_unlock();
1975
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001976 if (status != NFS_OK) {
1977 /* Free the stateid unless the server explicitly
1978 * informs us the stateid is unrecognized. */
1979 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001980 nfs41_free_stateid(server, stateid, cred);
Bryan Schumaker69388672012-09-26 15:25:52 -04001981 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001982
Bryan Schumaker69388672012-09-26 15:25:52 -04001983 write_seqlock(&state->seqlock);
1984 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1985 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001986 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1987 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001988
1989 if (cred != NULL)
1990 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001991}
1992
1993/**
1994 * nfs41_check_open_stateid - possibly free an open stateid
1995 *
1996 * @state: NFSv4 state for an inode
1997 *
1998 * Returns NFS_OK if recovery for this stateid is now finished.
1999 * Otherwise a negative NFS4ERR value is returned.
2000 */
2001static int nfs41_check_open_stateid(struct nfs4_state *state)
2002{
2003 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002004 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002005 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002006 int status;
2007
2008 /* If a state reset has been done, test_stateid is unneeded */
2009 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2010 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2011 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2012 return -NFS4ERR_BAD_STATEID;
2013
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002014 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002015 trace_nfs4_test_open_stateid(state, NULL, status);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002016 if (status != NFS_OK) {
2017 /* Free the stateid unless the server explicitly
2018 * informs us the stateid is unrecognized. */
2019 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002020 nfs41_free_stateid(server, stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002021
2022 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2023 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2024 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002025 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002026 }
2027 return status;
2028}
2029
2030static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2031{
Chuck Levereb64cf92012-07-11 16:30:05 -04002032 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002033
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002034 nfs41_clear_delegation_stateid(state);
2035 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002036 if (status != NFS_OK)
2037 status = nfs4_open_expired(sp, state);
2038 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002039}
2040#endif
2041
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002043 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2044 * fields corresponding to attributes that were used to store the verifier.
2045 * Make sure we clobber those fields in the later setattr call
2046 */
2047static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2048{
2049 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2050 !(sattr->ia_valid & ATTR_ATIME_SET))
2051 sattr->ia_valid |= ATTR_ATIME;
2052
2053 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2054 !(sattr->ia_valid & ATTR_MTIME_SET))
2055 sattr->ia_valid |= ATTR_MTIME;
2056}
2057
Trond Myklebustc21443c2013-02-07 14:26:21 -05002058static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2059 fmode_t fmode,
2060 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002061 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002062{
2063 struct nfs4_state_owner *sp = opendata->owner;
2064 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002065 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002066 struct nfs4_state *state;
2067 unsigned int seq;
2068 int ret;
2069
2070 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2071
2072 ret = _nfs4_proc_open(opendata);
2073 if (ret != 0)
2074 goto out;
2075
2076 state = nfs4_opendata_to_nfs4_state(opendata);
2077 ret = PTR_ERR(state);
2078 if (IS_ERR(state))
2079 goto out;
2080 if (server->caps & NFS_CAP_POSIX_LOCK)
2081 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2082
Trond Myklebust275bb302013-05-29 13:11:28 -04002083 dentry = opendata->dentry;
2084 if (dentry->d_inode == NULL) {
2085 /* FIXME: Is this d_drop() ever needed? */
2086 d_drop(dentry);
2087 dentry = d_add_unique(dentry, igrab(state->inode));
2088 if (dentry == NULL) {
2089 dentry = opendata->dentry;
2090 } else if (dentry != ctx->dentry) {
2091 dput(ctx->dentry);
2092 ctx->dentry = dget(dentry);
2093 }
2094 nfs_set_verifier(dentry,
2095 nfs_save_change_attribute(opendata->dir->d_inode));
2096 }
2097
Trond Myklebustc21443c2013-02-07 14:26:21 -05002098 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2099 if (ret != 0)
2100 goto out;
2101
Trond Myklebust3efb9722013-05-29 13:17:04 -04002102 ctx->state = state;
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002103 if (dentry->d_inode == state->inode) {
2104 nfs_inode_attach_open_context(ctx);
2105 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2106 nfs4_schedule_stateid_recovery(server, state);
2107 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002108out:
2109 return ret;
2110}
2111
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002112/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002113 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 */
Andy Adamson82be4172012-05-23 05:02:35 -04002115static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002116 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002117 int flags,
2118 struct iattr *sattr,
Trond Myklebust959d9212013-06-28 16:29:51 -04002119 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120{
2121 struct nfs4_state_owner *sp;
2122 struct nfs4_state *state = NULL;
2123 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002124 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002125 struct dentry *dentry = ctx->dentry;
2126 struct rpc_cred *cred = ctx->cred;
2127 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2128 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002129 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002130 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002131 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
2133 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002135 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2136 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2138 goto out_err;
2139 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002140 status = nfs4_recover_expired_lease(server);
2141 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002142 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04002143 if (dentry->d_inode != NULL)
2144 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002145 status = -ENOMEM;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002146 if (dentry->d_inode)
2147 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002148 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002149 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002150 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002151 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
David Quigley14c43f72013-05-22 12:50:43 -04002153 if (label) {
2154 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2155 if (IS_ERR(olabel)) {
2156 status = PTR_ERR(olabel);
2157 goto err_opendata_put;
2158 }
2159 }
2160
Andy Adamson82be4172012-05-23 05:02:35 -04002161 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2162 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2163 if (!opendata->f_attr.mdsthreshold)
David Quigley14c43f72013-05-22 12:50:43 -04002164 goto err_free_label;
Trond Myklebust1549210f2012-06-05 09:16:47 -04002165 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002166 }
Al Viro82a2c1b2011-06-22 18:30:55 -04002167 if (dentry->d_inode != NULL)
2168 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002169
Trond Myklebust3efb9722013-05-29 13:17:04 -04002170 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002171 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002172 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002173 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002174
Trond Myklebust549b19c2013-04-16 18:42:34 -04002175 if ((opendata->o_arg.open_flags & O_EXCL) &&
2176 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002177 nfs4_exclusive_attrset(opendata, sattr);
2178
2179 nfs_fattr_init(opendata->o_res.f_attr);
2180 status = nfs4_do_setattr(state->inode, cred,
2181 opendata->o_res.f_attr, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002182 state, label, olabel);
2183 if (status == 0) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002184 nfs_setattr_update_inode(state->inode, sattr);
David Quigley1775fd32013-05-22 12:50:42 -04002185 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
David Quigleyaa9c2662013-05-22 12:50:44 -04002186 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
David Quigley1775fd32013-05-22 12:50:42 -04002187 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002188 }
Andy Adamson82be4172012-05-23 05:02:35 -04002189
2190 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2191 *ctx_th = opendata->f_attr.mdsthreshold;
2192 else
2193 kfree(opendata->f_attr.mdsthreshold);
2194 opendata->f_attr.mdsthreshold = NULL;
2195
David Quigley14c43f72013-05-22 12:50:43 -04002196 nfs4_label_free(olabel);
2197
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002198 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002201err_free_label:
2202 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002203err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002204 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002205 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002206err_put_state_owner:
2207 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 return status;
2210}
2211
2212
Andy Adamson82be4172012-05-23 05:02:35 -04002213static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002214 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002215 int flags,
2216 struct iattr *sattr,
Trond Myklebust959d9212013-06-28 16:29:51 -04002217 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002219 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 struct nfs4_exception exception = { };
2221 struct nfs4_state *res;
2222 int status;
2223
2224 do {
Trond Myklebust959d9212013-06-28 16:29:51 -04002225 status = _nfs4_do_open(dir, ctx, flags, sattr, label);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002226 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002227 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 if (status == 0)
2229 break;
2230 /* NOTE: BAD_SEQID means the server and client disagree about the
2231 * book-keeping w.r.t. state-changing operations
2232 * (OPEN/CLOSE/LOCK/LOCKU...)
2233 * It is actually a sign of a bug on the client or on the server.
2234 *
2235 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002236 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 * have unhashed the old state_owner for us, and that we can
2238 * therefore safely retry using a new one. We should still warn
2239 * the user though...
2240 */
2241 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002242 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002243 " returned a bad sequence-id error!\n",
2244 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 exception.retry = 1;
2246 continue;
2247 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002248 /*
2249 * BAD_STATEID on OPEN means that the server cancelled our
2250 * state before it received the OPEN_CONFIRM.
2251 * Recover by retrying the request as per the discussion
2252 * on Page 181 of RFC3530.
2253 */
2254 if (status == -NFS4ERR_BAD_STATEID) {
2255 exception.retry = 1;
2256 continue;
2257 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002258 if (status == -EAGAIN) {
2259 /* We must have found a delegation */
2260 exception.retry = 1;
2261 continue;
2262 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002263 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2264 continue;
2265 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 status, &exception));
2267 } while (exception.retry);
2268 return res;
2269}
2270
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002271static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2272 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002273 struct nfs4_state *state, struct nfs4_label *ilabel,
2274 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002276 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002278 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 .iap = sattr,
2280 .server = server,
2281 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002282 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 };
2284 struct nfs_setattrres res = {
2285 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002286 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 .server = server,
2288 };
2289 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002290 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2291 .rpc_argp = &arg,
2292 .rpc_resp = &res,
2293 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002295 unsigned long timestamp = jiffies;
Trond Myklebustee3ae842013-04-29 10:35:36 -04002296 fmode_t fmode;
2297 bool truncate;
Trond Myklebust65e43082005-08-16 11:49:44 -04002298 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
David Quigleyaa9c2662013-05-22 12:50:44 -04002300 arg.bitmask = nfs4_bitmask(server, ilabel);
2301 if (ilabel)
2302 arg.bitmask = nfs4_bitmask(server, olabel);
2303
Trond Myklebust0e574af2005-10-27 22:12:38 -04002304 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
Trond Myklebustee3ae842013-04-29 10:35:36 -04002306 /* Servers should only apply open mode checks for file size changes */
2307 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2308 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2309
2310 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2311 /* Use that stateid */
2312 } else if (truncate && state != NULL && nfs4_valid_open_stateid(state)) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002313 struct nfs_lockowner lockowner = {
2314 .l_owner = current->files,
2315 .l_pid = current->tgid,
2316 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002317 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002318 &lockowner);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002319 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002320 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
Bryan Schumaker7c513052011-03-24 17:12:24 +00002322 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002323 if (status == 0 && state != NULL)
2324 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002325 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326}
2327
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002328static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2329 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002330 struct nfs4_state *state, struct nfs4_label *ilabel,
2331 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002333 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002334 struct nfs4_exception exception = {
2335 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002336 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002337 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 int err;
2339 do {
David Quigley1775fd32013-05-22 12:50:42 -04002340 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
Trond Myklebustc1578b72013-08-12 16:58:42 -04002341 trace_nfs4_setattr(inode, err);
Trond Myklebust451146b2012-04-18 16:29:11 -04002342 switch (err) {
2343 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002344 if (!(sattr->ia_valid & ATTR_SIZE)) {
2345 pr_warn_once("NFSv4: server %s is incorrectly "
2346 "applying open mode checks to "
2347 "a SETATTR that is not "
2348 "changing file size.\n",
2349 server->nfs_client->cl_hostname);
2350 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002351 if (state && !(state->state & FMODE_WRITE)) {
2352 err = -EBADF;
2353 if (sattr->ia_valid & ATTR_OPEN)
2354 err = -EACCES;
2355 goto out;
2356 }
2357 }
2358 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002360out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 return err;
2362}
2363
2364struct nfs4_closedata {
2365 struct inode *inode;
2366 struct nfs4_state *state;
2367 struct nfs_closeargs arg;
2368 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002369 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002370 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002371 bool roc;
2372 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373};
2374
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002375static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002376{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002377 struct nfs4_closedata *calldata = data;
2378 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002379 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002380
Fred Isamanf7e89172011-01-06 11:36:32 +00002381 if (calldata->roc)
2382 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002383 nfs4_put_open_state(calldata->state);
2384 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002385 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002386 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002387 kfree(calldata);
2388}
2389
Trond Myklebust88069f72009-12-08 08:33:16 -05002390static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2391 fmode_t fmode)
2392{
2393 spin_lock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002394 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002395 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
2396 case FMODE_WRITE:
2397 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2398 break;
2399 case FMODE_READ:
2400 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2401 break;
2402 case 0:
2403 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2404 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2405 clear_bit(NFS_OPEN_STATE, &state->flags);
2406 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002407 spin_unlock(&state->owner->so_lock);
2408}
2409
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002410static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002412 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 struct nfs_server *server = NFS_SERVER(calldata->inode);
2415
Chuck Levera3ca5652012-03-01 17:00:40 -05002416 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002417 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2418 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002419 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 /* hmm. we are done with the inode, and in the process of freeing
2421 * the state_owner. we keep this around to process errors
2422 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 switch (task->tk_status) {
2424 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002425 if (calldata->roc)
2426 pnfs_roc_set_barrier(state->inode,
2427 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002428 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002429 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002430 nfs4_close_clear_stateid_flags(state,
2431 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 break;
2433 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002434 case -NFS4ERR_OLD_STATEID:
2435 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002437 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002438 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002440 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2441 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002443 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002444 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002445 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446}
2447
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002448static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002450 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002451 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002452 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002453 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002454
Chuck Levera3ca5652012-03-01 17:00:40 -05002455 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002456 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002457 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002458
Trond Myklebust88069f72009-12-08 08:33:16 -05002459 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2460 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002461 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002462 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002463 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002464 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002465 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2466 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2467 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002468 }
2469 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002470 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2471 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2472 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002473 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002474 }
Trond Myklebust5d422302013-03-14 16:57:48 -04002475 if (!nfs4_valid_open_stateid(state))
2476 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002477 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002478
2479 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002480 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002481 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002482 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002483
Fred Isamanf7e89172011-01-06 11:36:32 +00002484 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002485 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002486 if (calldata->roc &&
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002487 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2488 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002489 goto out_wait;
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002490 }
Fred Isamanf7e89172011-01-06 11:36:32 +00002491 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002492
Trond Myklebust516a6af2005-10-27 22:12:41 -04002493 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002494 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002495 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002496 &calldata->arg.seq_args,
2497 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002498 task) != 0)
2499 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002500 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002501 return;
2502out_no_action:
2503 task->tk_action = NULL;
2504out_wait:
2505 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506}
2507
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002508static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002509 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002510 .rpc_call_done = nfs4_close_done,
2511 .rpc_release = nfs4_free_closedata,
2512};
2513
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514/*
2515 * It is possible for data to be read/written from a mem-mapped file
2516 * after the sys_close call (which hits the vfs layer as a flush).
2517 * This means that we can't safely call nfsv4 close on a file until
2518 * the inode is cleared. This in turn means that we are not good
2519 * NFSv4 citizens - we do not indicate to the server to update the file's
2520 * share state even when we are done with one of the three share
2521 * stateid's in the inode.
2522 *
2523 * NOTE: Caller must be holding the sp->so_owner semaphore!
2524 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002525int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002527 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002529 struct nfs4_state_owner *sp = state->owner;
2530 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002531 struct rpc_message msg = {
2532 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2533 .rpc_cred = state->owner->so_cred,
2534 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002535 struct rpc_task_setup task_setup_data = {
2536 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002537 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002538 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002539 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002540 .flags = RPC_TASK_ASYNC,
2541 };
Trond Myklebust95121352005-10-18 14:20:12 -07002542 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002544 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002546 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04002547 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002548 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002550 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002551 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002553 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002554 if (calldata->arg.seqid == NULL)
2555 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002556 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002557 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002558 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002559 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002560 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002561 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002562 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002563
Trond Myklebust1174dd12010-12-21 10:52:24 -05002564 msg.rpc_argp = &calldata->arg;
2565 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002566 task_setup_data.callback_data = calldata;
2567 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002568 if (IS_ERR(task))
2569 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002570 status = 0;
2571 if (wait)
2572 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002573 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002574 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002575out_free_calldata:
2576 kfree(calldata);
2577out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002578 nfs4_put_open_state(state);
2579 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002580 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581}
2582
Trond Myklebust2b484292010-09-17 10:56:51 -04002583static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002584nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04002587 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2588
2589 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
Trond Myklebust565277f2007-10-15 18:17:53 -04002591 /* Protect against concurrent sillydeletes */
Trond Myklebust959d9212013-06-28 16:29:51 -04002592 state = nfs4_do_open(dir, ctx, open_flags, attr, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04002593
2594 nfs4_label_release_security(label);
2595
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002596 if (IS_ERR(state))
2597 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04002598 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599}
2600
Trond Myklebust1185a552009-12-03 15:54:02 -05002601static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002602{
2603 if (ctx->state == NULL)
2604 return;
2605 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002606 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002607 else
Al Viro643168c2011-06-22 18:20:23 -04002608 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002609}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610
2611static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2612{
Benny Halevy43652ad2009-04-01 09:21:54 -04002613 struct nfs4_server_caps_arg args = {
2614 .fhandle = fhandle,
2615 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 struct nfs4_server_caps_res res = {};
2617 struct rpc_message msg = {
2618 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002619 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 .rpc_resp = &res,
2621 };
2622 int status;
2623
Bryan Schumaker7c513052011-03-24 17:12:24 +00002624 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 if (status == 0) {
2626 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002627 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2628 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2629 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2630 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2631 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2633 server->caps |= NFS_CAP_ACLS;
2634 if (res.has_links != 0)
2635 server->caps |= NFS_CAP_HARDLINKS;
2636 if (res.has_symlinks != 0)
2637 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002638 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2639 server->caps |= NFS_CAP_FILEID;
2640 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2641 server->caps |= NFS_CAP_MODE;
2642 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2643 server->caps |= NFS_CAP_NLINK;
2644 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2645 server->caps |= NFS_CAP_OWNER;
2646 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2647 server->caps |= NFS_CAP_OWNER_GROUP;
2648 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2649 server->caps |= NFS_CAP_ATIME;
2650 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2651 server->caps |= NFS_CAP_CTIME;
2652 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2653 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04002654#ifdef CONFIG_NFS_V4_SECURITY_LABEL
2655 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
2656 server->caps |= NFS_CAP_SECURITY_LABEL;
2657#endif
2658 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
2659 sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002660
David Quigleyaa9c2662013-05-22 12:50:44 -04002661 if (server->caps & NFS_CAP_SECURITY_LABEL) {
2662 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2663 res.attr_bitmask[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2664 }
Trond Myklebusta65318b2009-03-11 14:10:28 -04002665 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2666 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2667 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002669 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002671
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 return status;
2673}
2674
Trond Myklebust55a97592006-06-09 09:34:19 -04002675int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676{
2677 struct nfs4_exception exception = { };
2678 int err;
2679 do {
2680 err = nfs4_handle_exception(server,
2681 _nfs4_server_capabilities(server, fhandle),
2682 &exception);
2683 } while (exception.retry);
2684 return err;
2685}
2686
2687static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2688 struct nfs_fsinfo *info)
2689{
David Quigleyaa9c2662013-05-22 12:50:44 -04002690 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04002692 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 };
2694 struct nfs4_lookup_res res = {
2695 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002696 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 .fh = fhandle,
2698 };
2699 struct rpc_message msg = {
2700 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2701 .rpc_argp = &args,
2702 .rpc_resp = &res,
2703 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002704
David Quigleyaa9c2662013-05-22 12:50:44 -04002705 bitmask[0] = nfs4_fattr_bitmap[0];
2706 bitmask[1] = nfs4_fattr_bitmap[1];
2707 /*
2708 * Process the label in the upcoming getfattr
2709 */
2710 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
2711
Trond Myklebust0e574af2005-10-27 22:12:38 -04002712 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002713 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714}
2715
2716static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2717 struct nfs_fsinfo *info)
2718{
2719 struct nfs4_exception exception = { };
2720 int err;
2721 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002722 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04002723 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002724 switch (err) {
2725 case 0:
2726 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002727 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002728 default:
2729 err = nfs4_handle_exception(server, err, &exception);
2730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002732out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 return err;
2734}
2735
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002736static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2737 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2738{
Trond Myklebustc2190662013-08-26 19:23:04 -04002739 struct rpc_auth_create_args auth_args = {
2740 .pseudoflavor = flavor,
2741 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002742 struct rpc_auth *auth;
2743 int ret;
2744
Trond Myklebustc2190662013-08-26 19:23:04 -04002745 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002746 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04002747 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002748 goto out;
2749 }
2750 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002751out:
2752 return ret;
2753}
2754
Chuck Lever9a744ba2013-03-16 15:56:02 -04002755/*
2756 * Retry pseudoroot lookup with various security flavors. We do this when:
2757 *
2758 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2759 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2760 *
2761 * Returns zero on success, or a negative NFS4ERR value, or a
2762 * negative errno value.
2763 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002764static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002765 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766{
Chuck Lever9a744ba2013-03-16 15:56:02 -04002767 /* Per 3530bis 15.33.5 */
2768 static const rpc_authflavor_t flav_array[] = {
2769 RPC_AUTH_GSS_KRB5P,
2770 RPC_AUTH_GSS_KRB5I,
2771 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04002772 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04002773 RPC_AUTH_NULL,
2774 };
2775 int status = -EPERM;
2776 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777
Chuck Lever9a744ba2013-03-16 15:56:02 -04002778 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002779 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002780 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002781 continue;
2782 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002783 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04002784
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002785 /*
2786 * -EACCESS could mean that the user doesn't have correct permissions
2787 * to access the mount. It could also mean that we tried to mount
2788 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2789 * existing mount programs don't handle -EACCES very well so it should
2790 * be mapped to -EPERM instead.
2791 */
2792 if (status == -EACCES)
2793 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002794 return status;
2795}
2796
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002797static int nfs4_do_find_root_sec(struct nfs_server *server,
2798 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
2799{
2800 int mv = server->nfs_client->cl_minorversion;
2801 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
2802}
2803
2804/**
2805 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
2806 * @server: initialized nfs_server handle
2807 * @fhandle: we fill in the pseudo-fs root file handle
2808 * @info: we fill in an FSINFO struct
2809 *
2810 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002811 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002812int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2813 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002814{
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002815 int status;
2816
2817 status = nfs4_lookup_root(server, fhandle, info);
2818 if ((status == -NFS4ERR_WRONGSEC) &&
2819 !(server->flags & NFS_MOUNT_SECFLAVOUR))
2820 status = nfs4_do_find_root_sec(server, fhandle, info);
2821
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 if (status == 0)
2823 status = nfs4_server_capabilities(server, fhandle);
2824 if (status == 0)
2825 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002826
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002827 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828}
2829
Bryan Schumakerbae36242012-05-10 15:07:31 -04002830static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2831 struct nfs_fsinfo *info)
2832{
2833 int error;
2834 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04002835 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04002836
2837 error = nfs4_server_capabilities(server, mntfh);
2838 if (error < 0) {
2839 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2840 return error;
2841 }
2842
David Quigley14c43f72013-05-22 12:50:43 -04002843 label = nfs4_label_alloc(server, GFP_KERNEL);
2844 if (IS_ERR(label))
2845 return PTR_ERR(label);
2846
David Quigley1775fd32013-05-22 12:50:42 -04002847 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04002848 if (error < 0) {
2849 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04002850 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04002851 }
2852
2853 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2854 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2855 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2856
David Quigley14c43f72013-05-22 12:50:43 -04002857err_free_label:
2858 nfs4_label_free(label);
2859
Bryan Schumakerbae36242012-05-10 15:07:31 -04002860 return error;
2861}
2862
Manoj Naik6b97fd32006-06-09 09:34:29 -04002863/*
2864 * Get locations and (maybe) other attributes of a referral.
2865 * Note that we'll actually follow the referral later when
2866 * we detect fsid mismatch in inode revalidation
2867 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002868static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2869 const struct qstr *name, struct nfs_fattr *fattr,
2870 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002871{
2872 int status = -ENOMEM;
2873 struct page *page = NULL;
2874 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002875
2876 page = alloc_page(GFP_KERNEL);
2877 if (page == NULL)
2878 goto out;
2879 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2880 if (locations == NULL)
2881 goto out;
2882
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002883 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002884 if (status != 0)
2885 goto out;
2886 /* Make sure server returned a different fsid for the referral */
2887 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002888 dprintk("%s: server did not return a different fsid for"
2889 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002890 status = -EIO;
2891 goto out;
2892 }
Andy Adamson533eb462011-06-13 18:25:56 -04002893 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2894 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002895
Andy Adamson533eb462011-06-13 18:25:56 -04002896 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002897 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002898 memset(fhandle, 0, sizeof(struct nfs_fh));
2899out:
2900 if (page)
2901 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002902 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002903 return status;
2904}
2905
David Quigley1775fd32013-05-22 12:50:42 -04002906static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
2907 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908{
2909 struct nfs4_getattr_arg args = {
2910 .fh = fhandle,
2911 .bitmask = server->attr_bitmask,
2912 };
2913 struct nfs4_getattr_res res = {
2914 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002915 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 .server = server,
2917 };
2918 struct rpc_message msg = {
2919 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2920 .rpc_argp = &args,
2921 .rpc_resp = &res,
2922 };
David Quigleyaa9c2662013-05-22 12:50:44 -04002923
2924 args.bitmask = nfs4_bitmask(server, label);
2925
Trond Myklebust0e574af2005-10-27 22:12:38 -04002926 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002927 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928}
2929
David Quigley1775fd32013-05-22 12:50:42 -04002930static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
2931 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932{
2933 struct nfs4_exception exception = { };
2934 int err;
2935 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04002936 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
2937 trace_nfs4_getattr(server, fhandle, fattr, err);
2938 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 &exception);
2940 } while (exception.retry);
2941 return err;
2942}
2943
2944/*
2945 * The file is not closed if it is opened due to the a request to change
2946 * the size of the file. The open call will not be needed once the
2947 * VFS layer lookup-intents are implemented.
2948 *
2949 * Close is called when the inode is destroyed.
2950 * If we haven't opened the file for O_WRONLY, we
2951 * need to in the size_change case to obtain a stateid.
2952 *
2953 * Got race?
2954 * Because OPEN is always done by name in nfsv4, it is
2955 * possible that we opened a different file by the same
2956 * name. We can recognize this race condition, but we
2957 * can't do anything about it besides returning an error.
2958 *
2959 * This will be fixed with VFS changes (lookup-intent).
2960 */
2961static int
2962nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2963 struct iattr *sattr)
2964{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002965 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002966 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002967 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04002968 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 int status;
2970
Benny Halevy8a1636c2010-07-14 15:43:57 -04002971 if (pnfs_ld_layoutret_on_setattr(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04002972 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04002973
Trond Myklebust0e574af2005-10-27 22:12:38 -04002974 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975
Andy Adamson26699402012-05-30 16:12:24 -04002976 /* Deal with open(O_TRUNC) */
2977 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03002978 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04002979
2980 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03002981 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04002982 return 0;
2983
Trond Myklebustd5308382005-11-04 15:33:38 -05002984 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002985 if (sattr->ia_valid & ATTR_FILE) {
2986 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002987
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002988 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002989 if (ctx) {
2990 cred = ctx->cred;
2991 state = ctx->state;
2992 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002993 }
2994
David Quigley14c43f72013-05-22 12:50:43 -04002995 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
2996 if (IS_ERR(label))
2997 return PTR_ERR(label);
2998
2999 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003000 if (status == 0) {
Trond Myklebust65e43082005-08-16 11:49:44 -04003001 nfs_setattr_update_inode(inode, sattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003002 nfs_setsecurity(inode, fattr, label);
3003 }
David Quigley14c43f72013-05-22 12:50:43 -04003004 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 return status;
3006}
3007
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003008static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3009 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003010 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003011{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003012 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003013 int status;
3014 struct nfs4_lookup_arg args = {
3015 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003016 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003017 .name = name,
3018 };
3019 struct nfs4_lookup_res res = {
3020 .server = server,
3021 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003022 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003023 .fh = fhandle,
3024 };
3025 struct rpc_message msg = {
3026 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3027 .rpc_argp = &args,
3028 .rpc_resp = &res,
3029 };
3030
David Quigleyaa9c2662013-05-22 12:50:44 -04003031 args.bitmask = nfs4_bitmask(server, label);
3032
David Howells2b3de442006-08-22 20:06:09 -04003033 nfs_fattr_init(fattr);
3034
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003036 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 dprintk("NFS reply lookup: %d\n", status);
3038 return status;
3039}
3040
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003041static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003042{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003043 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003044 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003045 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3046 fattr->nlink = 2;
3047}
3048
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003049static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3050 struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003051 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003052{
3053 struct nfs4_exception exception = { };
3054 struct rpc_clnt *client = *clnt;
3055 int err;
3056 do {
David Quigley1775fd32013-05-22 12:50:42 -04003057 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003058 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003059 switch (err) {
3060 case -NFS4ERR_BADNAME:
3061 err = -ENOENT;
3062 goto out;
3063 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003064 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003065 goto out;
3066 case -NFS4ERR_WRONGSEC:
3067 err = -EPERM;
3068 if (client != *clnt)
3069 goto out;
3070
3071 client = nfs4_create_sec_client(client, dir, name);
3072 if (IS_ERR(client))
3073 return PTR_ERR(client);
3074
3075 exception.retry = 1;
3076 break;
3077 default:
3078 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3079 }
3080 } while (exception.retry);
3081
3082out:
3083 if (err == 0)
3084 *clnt = client;
3085 else if (client != *clnt)
3086 rpc_shutdown_client(client);
3087
3088 return err;
3089}
3090
Bryan Schumaker80a16b22012-04-27 13:27:46 -04003091static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003092 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3093 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003095 int status;
3096 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003097
David Quigley1775fd32013-05-22 12:50:42 -04003098 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003099 if (client != NFS_CLIENT(dir)) {
3100 rpc_shutdown_client(client);
3101 nfs_fixup_secinfo_attributes(fattr);
3102 }
3103 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104}
3105
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003106struct rpc_clnt *
3107nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3108 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3109{
3110 int status;
3111 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
3112
David Quigley1775fd32013-05-22 12:50:42 -04003113 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003114 if (status < 0) {
3115 rpc_shutdown_client(client);
3116 return ERR_PTR(status);
3117 }
3118 return client;
3119}
3120
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3122{
Trond Myklebust76b32992007-08-10 17:45:11 -04003123 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 struct nfs4_accessargs args = {
3125 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003126 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003128 struct nfs4_accessres res = {
3129 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003130 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 struct rpc_message msg = {
3132 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3133 .rpc_argp = &args,
3134 .rpc_resp = &res,
3135 .rpc_cred = entry->cred,
3136 };
3137 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003138 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139
3140 /*
3141 * Determine which access bits we want to ask for...
3142 */
3143 if (mode & MAY_READ)
3144 args.access |= NFS4_ACCESS_READ;
3145 if (S_ISDIR(inode->i_mode)) {
3146 if (mode & MAY_WRITE)
3147 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3148 if (mode & MAY_EXEC)
3149 args.access |= NFS4_ACCESS_LOOKUP;
3150 } else {
3151 if (mode & MAY_WRITE)
3152 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3153 if (mode & MAY_EXEC)
3154 args.access |= NFS4_ACCESS_EXECUTE;
3155 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003156
3157 res.fattr = nfs_alloc_fattr();
3158 if (res.fattr == NULL)
3159 return -ENOMEM;
3160
Bryan Schumaker7c513052011-03-24 17:12:24 +00003161 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003163 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003164 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003166 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 return status;
3168}
3169
3170static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3171{
3172 struct nfs4_exception exception = { };
3173 int err;
3174 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003175 err = _nfs4_proc_access(inode, entry);
3176 trace_nfs4_access(inode, err);
3177 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 &exception);
3179 } while (exception.retry);
3180 return err;
3181}
3182
3183/*
3184 * TODO: For the time being, we don't try to get any attributes
3185 * along with any of the zero-copy operations READ, READDIR,
3186 * READLINK, WRITE.
3187 *
3188 * In the case of the first three, we want to put the GETATTR
3189 * after the read-type operation -- this is because it is hard
3190 * to predict the length of a GETATTR response in v4, and thus
3191 * align the READ data correctly. This means that the GETATTR
3192 * may end up partially falling into the page cache, and we should
3193 * shift it into the 'tail' of the xdr_buf before processing.
3194 * To do this efficiently, we need to know the total length
3195 * of data received, which doesn't seem to be available outside
3196 * of the RPC layer.
3197 *
3198 * In the case of WRITE, we also want to put the GETATTR after
3199 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003200 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 *
3202 * Both of these changes to the XDR layer would in fact be quite
3203 * minor, but I decided to leave them for a subsequent patch.
3204 */
3205static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3206 unsigned int pgbase, unsigned int pglen)
3207{
3208 struct nfs4_readlink args = {
3209 .fh = NFS_FH(inode),
3210 .pgbase = pgbase,
3211 .pglen = pglen,
3212 .pages = &page,
3213 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003214 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 struct rpc_message msg = {
3216 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3217 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003218 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 };
3220
Bryan Schumaker7c513052011-03-24 17:12:24 +00003221 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 -07003222}
3223
3224static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3225 unsigned int pgbase, unsigned int pglen)
3226{
3227 struct nfs4_exception exception = { };
3228 int err;
3229 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003230 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3231 trace_nfs4_readlink(inode, err);
3232 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 &exception);
3234 } while (exception.retry);
3235 return err;
3236}
3237
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003239 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241static int
3242nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003243 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244{
David Quigleyaa9c2662013-05-22 12:50:44 -04003245 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003246 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 int status = 0;
3249
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003250 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3251 if (IS_ERR(ctx))
3252 return PTR_ERR(ctx);
3253
David Quigleyaa9c2662013-05-22 12:50:44 -04003254 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3255
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003256 sattr->ia_mode &= ~current_umask();
Trond Myklebust959d9212013-06-28 16:29:51 -04003257 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 if (IS_ERR(state)) {
3259 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003260 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003263 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003264 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 return status;
3266}
3267
3268static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3269{
Trond Myklebust16e42952005-10-27 22:12:44 -04003270 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003271 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003273 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003275 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003276 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003277 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003279 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3280 .rpc_argp = &args,
3281 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003283 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003284
Bryan Schumaker7c513052011-03-24 17:12:24 +00003285 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003286 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003287 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 return status;
3289}
3290
3291static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3292{
3293 struct nfs4_exception exception = { };
3294 int err;
3295 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003296 err = _nfs4_proc_remove(dir, name);
3297 trace_nfs4_remove(dir, name, err);
3298 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 &exception);
3300 } while (exception.retry);
3301 return err;
3302}
3303
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003304static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003306 struct nfs_server *server = NFS_SERVER(dir);
3307 struct nfs_removeargs *args = msg->rpc_argp;
3308 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003310 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04003312 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003313
3314 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315}
3316
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003317static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3318{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003319 nfs4_setup_sequence(NFS_SERVER(data->dir),
3320 &data->args.seq_args,
3321 &data->res.seq_res,
3322 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323}
3324
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003325static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003327 struct nfs_unlinkdata *data = task->tk_calldata;
3328 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003329
Trond Myklebust14516c32010-07-31 14:29:06 -04003330 if (!nfs4_sequence_done(task, &res->seq_res))
3331 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003332 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003333 return 0;
3334 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003335 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336}
3337
Jeff Laytond3d41522010-09-17 17:31:57 -04003338static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3339{
3340 struct nfs_server *server = NFS_SERVER(dir);
3341 struct nfs_renameargs *arg = msg->rpc_argp;
3342 struct nfs_renameres *res = msg->rpc_resp;
3343
3344 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003345 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04003346 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003347}
3348
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003349static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3350{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003351 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3352 &data->args.seq_args,
3353 &data->res.seq_res,
3354 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003355}
3356
3357static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3358 struct inode *new_dir)
3359{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003360 struct nfs_renamedata *data = task->tk_calldata;
3361 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04003362
3363 if (!nfs4_sequence_done(task, &res->seq_res))
3364 return 0;
3365 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3366 return 0;
3367
3368 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003369 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003370 return 1;
3371}
3372
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3374 struct inode *new_dir, struct qstr *new_name)
3375{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003376 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003377 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 .old_dir = NFS_FH(old_dir),
3379 .new_dir = NFS_FH(new_dir),
3380 .old_name = old_name,
3381 .new_name = new_name,
3382 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003383 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003384 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003385 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 struct rpc_message msg = {
3387 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3388 .rpc_argp = &arg,
3389 .rpc_resp = &res,
3390 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003391 int status = -ENOMEM;
David Quigley14c43f72013-05-22 12:50:43 -04003392
Bryan Schumaker7c513052011-03-24 17:12:24 +00003393 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 if (!status) {
3395 update_changeattr(old_dir, &res.old_cinfo);
3396 update_changeattr(new_dir, &res.new_cinfo);
3397 }
3398 return status;
3399}
3400
3401static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3402 struct inode *new_dir, struct qstr *new_name)
3403{
3404 struct nfs4_exception exception = { };
3405 int err;
3406 do {
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003407 err = _nfs4_proc_rename(old_dir, old_name,
3408 new_dir, new_name);
3409 trace_nfs4_rename(old_dir, old_name, new_dir, new_name, err);
3410 err = nfs4_handle_exception(NFS_SERVER(old_dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 &exception);
3412 } while (exception.retry);
3413 return err;
3414}
3415
3416static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3417{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003418 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 struct nfs4_link_arg arg = {
3420 .fh = NFS_FH(inode),
3421 .dir_fh = NFS_FH(dir),
3422 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003423 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003425 struct nfs4_link_res res = {
3426 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003427 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003428 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 struct rpc_message msg = {
3430 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3431 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003432 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003434 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435
Trond Myklebust136f2622010-04-16 16:22:49 -04003436 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003437 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003438 goto out;
3439
David Quigley14c43f72013-05-22 12:50:43 -04003440 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3441 if (IS_ERR(res.label)) {
3442 status = PTR_ERR(res.label);
3443 goto out;
3444 }
David Quigleyaa9c2662013-05-22 12:50:44 -04003445 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04003446
Bryan Schumaker7c513052011-03-24 17:12:24 +00003447 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003448 if (!status) {
3449 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04003450 status = nfs_post_op_update_inode(inode, res.fattr);
3451 if (!status)
3452 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003453 }
David Quigley14c43f72013-05-22 12:50:43 -04003454
3455
3456 nfs4_label_free(res.label);
3457
Trond Myklebust136f2622010-04-16 16:22:49 -04003458out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003459 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 return status;
3461}
3462
3463static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3464{
3465 struct nfs4_exception exception = { };
3466 int err;
3467 do {
3468 err = nfs4_handle_exception(NFS_SERVER(inode),
3469 _nfs4_proc_link(inode, dir, name),
3470 &exception);
3471 } while (exception.retry);
3472 return err;
3473}
3474
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003475struct nfs4_createdata {
3476 struct rpc_message msg;
3477 struct nfs4_create_arg arg;
3478 struct nfs4_create_res res;
3479 struct nfs_fh fh;
3480 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003481 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003482};
3483
3484static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3485 struct qstr *name, struct iattr *sattr, u32 ftype)
3486{
3487 struct nfs4_createdata *data;
3488
3489 data = kzalloc(sizeof(*data), GFP_KERNEL);
3490 if (data != NULL) {
3491 struct nfs_server *server = NFS_SERVER(dir);
3492
David Quigley14c43f72013-05-22 12:50:43 -04003493 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3494 if (IS_ERR(data->label))
3495 goto out_free;
3496
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003497 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3498 data->msg.rpc_argp = &data->arg;
3499 data->msg.rpc_resp = &data->res;
3500 data->arg.dir_fh = NFS_FH(dir);
3501 data->arg.server = server;
3502 data->arg.name = name;
3503 data->arg.attrs = sattr;
3504 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04003505 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003506 data->res.server = server;
3507 data->res.fh = &data->fh;
3508 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003509 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003510 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003511 }
3512 return data;
David Quigley14c43f72013-05-22 12:50:43 -04003513out_free:
3514 kfree(data);
3515 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003516}
3517
3518static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3519{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003520 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003521 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003522 if (status == 0) {
3523 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04003524 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003525 }
3526 return status;
3527}
3528
3529static void nfs4_free_createdata(struct nfs4_createdata *data)
3530{
David Quigley14c43f72013-05-22 12:50:43 -04003531 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003532 kfree(data);
3533}
3534
Chuck Lever4f390c12006-08-22 20:06:22 -04003535static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003536 struct page *page, unsigned int len, struct iattr *sattr,
3537 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003539 struct nfs4_createdata *data;
3540 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541
Chuck Lever94a6d752006-08-22 20:06:23 -04003542 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003543 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003544
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003545 status = -ENOMEM;
3546 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3547 if (data == NULL)
3548 goto out;
3549
3550 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3551 data->arg.u.symlink.pages = &page;
3552 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04003553 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003555 status = nfs4_do_create(dir, dentry, data);
3556
3557 nfs4_free_createdata(data);
3558out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 return status;
3560}
3561
Chuck Lever4f390c12006-08-22 20:06:22 -04003562static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003563 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564{
3565 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003566 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04003568
3569 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3570
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003572 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3573 trace_nfs4_symlink(dir, &dentry->d_name, err);
3574 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 &exception);
3576 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003577
3578 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 return err;
3580}
3581
3582static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003583 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003585 struct nfs4_createdata *data;
3586 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003588 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3589 if (data == NULL)
3590 goto out;
3591
David Quigley1775fd32013-05-22 12:50:42 -04003592 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003593 status = nfs4_do_create(dir, dentry, data);
3594
3595 nfs4_free_createdata(data);
3596out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 return status;
3598}
3599
3600static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3601 struct iattr *sattr)
3602{
3603 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003604 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003606
David Quigleyaa9c2662013-05-22 12:50:44 -04003607 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3608
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003609 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003611 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3612 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3613 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 &exception);
3615 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003616 nfs4_label_release_security(label);
3617
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 return err;
3619}
3620
3621static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003622 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623{
3624 struct inode *dir = dentry->d_inode;
3625 struct nfs4_readdir_arg args = {
3626 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003627 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 .pgbase = 0,
3629 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003630 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003631 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 };
3633 struct nfs4_readdir_res res;
3634 struct rpc_message msg = {
3635 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3636 .rpc_argp = &args,
3637 .rpc_resp = &res,
3638 .rpc_cred = cred,
3639 };
3640 int status;
3641
Harvey Harrison3110ff82008-05-02 13:42:44 -07003642 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003643 dentry->d_parent->d_name.name,
3644 dentry->d_name.name,
3645 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003646 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003648 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 -05003649 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003650 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003651 status += args.pgbase;
3652 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003653
3654 nfs_invalidate_atime(dir);
3655
Harvey Harrison3110ff82008-05-02 13:42:44 -07003656 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 return status;
3658}
3659
3660static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003661 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662{
3663 struct nfs4_exception exception = { };
3664 int err;
3665 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003666 err = _nfs4_proc_readdir(dentry, cred, cookie,
3667 pages, count, plus);
3668 trace_nfs4_readdir(dentry->d_inode, err);
3669 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 &exception);
3671 } while (exception.retry);
3672 return err;
3673}
3674
3675static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04003676 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003678 struct nfs4_createdata *data;
3679 int mode = sattr->ia_mode;
3680 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003682 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3683 if (data == NULL)
3684 goto out;
3685
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003687 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003689 data->arg.ftype = NF4BLK;
3690 data->arg.u.device.specdata1 = MAJOR(rdev);
3691 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 }
3693 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003694 data->arg.ftype = NF4CHR;
3695 data->arg.u.device.specdata1 = MAJOR(rdev);
3696 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003697 } else if (!S_ISSOCK(mode)) {
3698 status = -EINVAL;
3699 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 }
David Quigley1775fd32013-05-22 12:50:42 -04003701
David Quigleyaa9c2662013-05-22 12:50:44 -04003702 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003703 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003704out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003705 nfs4_free_createdata(data);
3706out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 return status;
3708}
3709
3710static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3711 struct iattr *sattr, dev_t rdev)
3712{
3713 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003714 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003716
David Quigleyaa9c2662013-05-22 12:50:44 -04003717 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3718
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003719 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003721 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
3722 trace_nfs4_mknod(dir, &dentry->d_name, err);
3723 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 &exception);
3725 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003726
3727 nfs4_label_release_security(label);
3728
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 return err;
3730}
3731
3732static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3733 struct nfs_fsstat *fsstat)
3734{
3735 struct nfs4_statfs_arg args = {
3736 .fh = fhandle,
3737 .bitmask = server->attr_bitmask,
3738 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003739 struct nfs4_statfs_res res = {
3740 .fsstat = fsstat,
3741 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 struct rpc_message msg = {
3743 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3744 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003745 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 };
3747
Trond Myklebust0e574af2005-10-27 22:12:38 -04003748 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003749 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750}
3751
3752static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3753{
3754 struct nfs4_exception exception = { };
3755 int err;
3756 do {
3757 err = nfs4_handle_exception(server,
3758 _nfs4_proc_statfs(server, fhandle, fsstat),
3759 &exception);
3760 } while (exception.retry);
3761 return err;
3762}
3763
3764static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3765 struct nfs_fsinfo *fsinfo)
3766{
3767 struct nfs4_fsinfo_arg args = {
3768 .fh = fhandle,
3769 .bitmask = server->attr_bitmask,
3770 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003771 struct nfs4_fsinfo_res res = {
3772 .fsinfo = fsinfo,
3773 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 struct rpc_message msg = {
3775 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3776 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003777 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 };
3779
Bryan Schumaker7c513052011-03-24 17:12:24 +00003780 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781}
3782
3783static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3784{
3785 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04003786 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 int err;
3788
3789 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04003790 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003791 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04003792 if (err == 0) {
3793 struct nfs_client *clp = server->nfs_client;
3794
3795 spin_lock(&clp->cl_lock);
3796 clp->cl_lease_time = fsinfo->lease_time * HZ;
3797 clp->cl_last_renewal = now;
3798 spin_unlock(&clp->cl_lock);
3799 break;
3800 }
3801 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 } while (exception.retry);
3803 return err;
3804}
3805
3806static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3807{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003808 int error;
3809
Trond Myklebust0e574af2005-10-27 22:12:38 -04003810 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003811 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003812 if (error == 0) {
3813 /* block layout checks this! */
3814 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003815 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003816 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003817
3818 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819}
3820
3821static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3822 struct nfs_pathconf *pathconf)
3823{
3824 struct nfs4_pathconf_arg args = {
3825 .fh = fhandle,
3826 .bitmask = server->attr_bitmask,
3827 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003828 struct nfs4_pathconf_res res = {
3829 .pathconf = pathconf,
3830 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 struct rpc_message msg = {
3832 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3833 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003834 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 };
3836
3837 /* None of the pathconf attributes are mandatory to implement */
3838 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3839 memset(pathconf, 0, sizeof(*pathconf));
3840 return 0;
3841 }
3842
Trond Myklebust0e574af2005-10-27 22:12:38 -04003843 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003844 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845}
3846
3847static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3848 struct nfs_pathconf *pathconf)
3849{
3850 struct nfs4_exception exception = { };
3851 int err;
3852
3853 do {
3854 err = nfs4_handle_exception(server,
3855 _nfs4_proc_pathconf(server, fhandle, pathconf),
3856 &exception);
3857 } while (exception.retry);
3858 return err;
3859}
3860
Trond Myklebust5521abf2013-03-16 20:54:34 -04003861int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04003862 const struct nfs_open_context *ctx,
3863 const struct nfs_lock_context *l_ctx,
3864 fmode_t fmode)
3865{
3866 const struct nfs_lockowner *lockowner = NULL;
3867
3868 if (l_ctx != NULL)
3869 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003870 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04003871}
3872EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
3873
Trond Myklebust5521abf2013-03-16 20:54:34 -04003874static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
3875 const struct nfs_open_context *ctx,
3876 const struct nfs_lock_context *l_ctx,
3877 fmode_t fmode)
3878{
3879 nfs4_stateid current_stateid;
3880
3881 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode))
3882 return false;
3883 return nfs4_stateid_match(stateid, &current_stateid);
3884}
3885
3886static bool nfs4_error_stateid_expired(int err)
3887{
3888 switch (err) {
3889 case -NFS4ERR_DELEG_REVOKED:
3890 case -NFS4ERR_ADMIN_REVOKED:
3891 case -NFS4ERR_BAD_STATEID:
3892 case -NFS4ERR_STALE_STATEID:
3893 case -NFS4ERR_OLD_STATEID:
3894 case -NFS4ERR_OPENMODE:
3895 case -NFS4ERR_EXPIRED:
3896 return true;
3897 }
3898 return false;
3899}
3900
Benny Halevyd20581a2011-05-22 19:52:03 +03003901void __nfs4_read_done_cb(struct nfs_read_data *data)
3902{
Fred Isamancd841602012-04-20 14:47:44 -04003903 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003904}
3905
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003906static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907{
Fred Isamancd841602012-04-20 14:47:44 -04003908 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909
Trond Myklebustcc668ab2013-08-14 15:31:28 -04003910 trace_nfs4_read(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003911 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003912 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003913 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003915
Benny Halevyd20581a2011-05-22 19:52:03 +03003916 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003918 renew_lease(server, data->timestamp);
3919 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920}
3921
Trond Myklebust5521abf2013-03-16 20:54:34 -04003922static bool nfs4_read_stateid_changed(struct rpc_task *task,
3923 struct nfs_readargs *args)
3924{
3925
3926 if (!nfs4_error_stateid_expired(task->tk_status) ||
3927 nfs4_stateid_is_current(&args->stateid,
3928 args->context,
3929 args->lock_context,
3930 FMODE_READ))
3931 return false;
3932 rpc_restart_call_prepare(task);
3933 return true;
3934}
3935
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003936static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3937{
3938
3939 dprintk("--> %s\n", __func__);
3940
3941 if (!nfs4_sequence_done(task, &data->res.seq_res))
3942 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003943 if (nfs4_read_stateid_changed(task, &data->args))
3944 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003945 return data->read_done_cb ? data->read_done_cb(task, data) :
3946 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003947}
3948
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003949static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003952 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003953 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Chuck Levera9c92d62013-08-09 12:48:18 -04003954 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955}
3956
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003957static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3958{
Trond Myklebust9b206142013-03-17 15:52:00 -04003959 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003960 &data->args.seq_args,
3961 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04003962 task))
3963 return;
3964 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3965 data->args.lock_context, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966}
3967
Fred Isamanb029bc92011-03-03 15:13:42 +00003968static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969{
Fred Isamancd841602012-04-20 14:47:44 -04003970 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971
Trond Myklebustcc668ab2013-08-14 15:31:28 -04003972 trace_nfs4_write(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003973 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003974 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003975 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003977 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003979 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003980 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003981 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982}
3983
Trond Myklebust5521abf2013-03-16 20:54:34 -04003984static bool nfs4_write_stateid_changed(struct rpc_task *task,
3985 struct nfs_writeargs *args)
3986{
3987
3988 if (!nfs4_error_stateid_expired(task->tk_status) ||
3989 nfs4_stateid_is_current(&args->stateid,
3990 args->context,
3991 args->lock_context,
3992 FMODE_WRITE))
3993 return false;
3994 rpc_restart_call_prepare(task);
3995 return true;
3996}
3997
Fred Isamanb029bc92011-03-03 15:13:42 +00003998static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3999{
4000 if (!nfs4_sequence_done(task, &data->res.seq_res))
4001 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004002 if (nfs4_write_stateid_changed(task, &data->args))
4003 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03004004 return data->write_done_cb ? data->write_done_cb(task, data) :
4005 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00004006}
4007
Trond Myklebust5a37f852012-04-28 14:55:16 -04004008static
4009bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00004010{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004011 const struct nfs_pgio_header *hdr = data->header;
4012
4013 /* Don't request attributes for pNFS or O_DIRECT writes */
4014 if (data->ds_clp != NULL || hdr->dreq != NULL)
4015 return false;
4016 /* Otherwise, request attributes if and only if we don't hold
4017 * a delegation
4018 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004019 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004020}
Fred Isamana69aef12011-03-03 15:13:47 +00004021
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004022static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023{
Fred Isamancd841602012-04-20 14:47:44 -04004024 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004025
Trond Myklebust5a37f852012-04-28 14:55:16 -04004026 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00004027 data->args.bitmask = NULL;
4028 data->res.fattr = NULL;
4029 } else
4030 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004031
Fred Isamanb029bc92011-03-03 15:13:42 +00004032 if (!data->write_done_cb)
4033 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004034 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 data->timestamp = jiffies;
4036
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004037 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004038 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039}
4040
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04004041static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
4042{
Trond Myklebust9b206142013-03-17 15:52:00 -04004043 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004044 &data->args.seq_args,
4045 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004046 task))
4047 return;
4048 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4049 data->args.lock_context, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050}
4051
Fred Isaman0b7c0152012-04-20 14:47:39 -04004052static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4053{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004054 nfs4_setup_sequence(NFS_SERVER(data->inode),
4055 &data->args.seq_args,
4056 &data->res.seq_res,
4057 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058}
4059
Fred Isaman0b7c0152012-04-20 14:47:39 -04004060static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004063
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004064 trace_nfs4_commit(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004065 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004066 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004067 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004069 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070}
4071
Fred Isaman0b7c0152012-04-20 14:47:39 -04004072static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004073{
4074 if (!nfs4_sequence_done(task, &data->res.seq_res))
4075 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004076 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004077}
4078
Fred Isaman0b7c0152012-04-20 14:47:39 -04004079static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004081 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004082
Fred Isaman0b7c0152012-04-20 14:47:39 -04004083 if (data->commit_done_cb == NULL)
4084 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004085 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004086 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004087 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088}
4089
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004090struct nfs4_renewdata {
4091 struct nfs_client *client;
4092 unsigned long timestamp;
4093};
4094
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095/*
4096 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4097 * standalone procedure for queueing an asynchronous RENEW.
4098 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004099static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004100{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004101 struct nfs4_renewdata *data = calldata;
4102 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004103
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004104 if (atomic_read(&clp->cl_count) > 1)
4105 nfs4_schedule_state_renewal(clp);
4106 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004107 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004108}
4109
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004110static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004112 struct nfs4_renewdata *data = calldata;
4113 struct nfs_client *clp = data->client;
4114 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004116 trace_nfs4_renew_async(clp, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04004118 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004119 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4120 return;
4121 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004122 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004123 return;
4124 }
4125 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004127 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128}
4129
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004130static const struct rpc_call_ops nfs4_renew_ops = {
4131 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004132 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004133};
4134
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004135static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136{
4137 struct rpc_message msg = {
4138 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4139 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004140 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004142 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004144 if (renew_flags == 0)
4145 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004146 if (!atomic_inc_not_zero(&clp->cl_count))
4147 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004148 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004149 if (data == NULL)
4150 return -ENOMEM;
4151 data->client = clp;
4152 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004153 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004154 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155}
4156
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004157static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158{
4159 struct rpc_message msg = {
4160 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4161 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004162 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 };
4164 unsigned long now = jiffies;
4165 int status;
4166
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004167 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 if (status < 0)
4169 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004170 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 return 0;
4172}
4173
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004174static inline int nfs4_server_supports_acls(struct nfs_server *server)
4175{
4176 return (server->caps & NFS_CAP_ACLS)
4177 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
4178 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
4179}
4180
Trond Myklebust21f498c2012-08-24 10:59:25 -04004181/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4182 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004183 * the stack.
4184 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004185#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004186
Neil Hormane9e3d722011-03-04 19:26:03 -05004187static int buf_to_pages_noslab(const void *buf, size_t buflen,
4188 struct page **pages, unsigned int *pgbase)
4189{
4190 struct page *newpage, **spages;
4191 int rc = 0;
4192 size_t len;
4193 spages = pages;
4194
4195 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004196 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004197 newpage = alloc_page(GFP_KERNEL);
4198
4199 if (newpage == NULL)
4200 goto unwind;
4201 memcpy(page_address(newpage), buf, len);
4202 buf += len;
4203 buflen -= len;
4204 *pages++ = newpage;
4205 rc++;
4206 } while (buflen != 0);
4207
4208 return rc;
4209
4210unwind:
4211 for(; rc > 0; rc--)
4212 __free_page(spages[rc-1]);
4213 return -ENOMEM;
4214}
4215
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004216struct nfs4_cached_acl {
4217 int cached;
4218 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004219 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004220};
4221
4222static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004223{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004224 struct nfs_inode *nfsi = NFS_I(inode);
4225
4226 spin_lock(&inode->i_lock);
4227 kfree(nfsi->nfs4_acl);
4228 nfsi->nfs4_acl = acl;
4229 spin_unlock(&inode->i_lock);
4230}
4231
4232static void nfs4_zap_acl_attr(struct inode *inode)
4233{
4234 nfs4_set_cached_acl(inode, NULL);
4235}
4236
4237static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4238{
4239 struct nfs_inode *nfsi = NFS_I(inode);
4240 struct nfs4_cached_acl *acl;
4241 int ret = -ENOENT;
4242
4243 spin_lock(&inode->i_lock);
4244 acl = nfsi->nfs4_acl;
4245 if (acl == NULL)
4246 goto out;
4247 if (buf == NULL) /* user is just asking for length */
4248 goto out_len;
4249 if (acl->cached == 0)
4250 goto out;
4251 ret = -ERANGE; /* see getxattr(2) man page */
4252 if (acl->len > buflen)
4253 goto out;
4254 memcpy(buf, acl->data, acl->len);
4255out_len:
4256 ret = acl->len;
4257out:
4258 spin_unlock(&inode->i_lock);
4259 return ret;
4260}
4261
Sachin Prabhu5794d212012-04-17 14:36:40 +01004262static 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 +00004263{
4264 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004265 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004266
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004267 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004268 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004269 if (acl == NULL)
4270 goto out;
4271 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004272 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004273 } else {
4274 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4275 if (acl == NULL)
4276 goto out;
4277 acl->cached = 0;
4278 }
4279 acl->len = acl_len;
4280out:
4281 nfs4_set_cached_acl(inode, acl);
4282}
4283
Andy Adamsonbf118a32011-12-07 11:55:27 -05004284/*
4285 * The getxattr API returns the required buffer length when called with a
4286 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4287 * the required buf. On a NULL buf, we send a page of data to the server
4288 * guessing that the ACL request can be serviced by a page. If so, we cache
4289 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4290 * the cache. If not so, we throw away the page, and cache the required
4291 * length. The next getxattr call will then produce another round trip to
4292 * the server, this time with the input buf of the required size.
4293 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004294static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004295{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004296 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004297 struct nfs_getaclargs args = {
4298 .fh = NFS_FH(inode),
4299 .acl_pages = pages,
4300 .acl_len = buflen,
4301 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004302 struct nfs_getaclres res = {
4303 .acl_len = buflen,
4304 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004305 struct rpc_message msg = {
4306 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4307 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004308 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004309 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004310 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4311 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004312
Andy Adamsonbf118a32011-12-07 11:55:27 -05004313 /* As long as we're doing a round trip to the server anyway,
4314 * let's be prepared for a page of acl data. */
4315 if (npages == 0)
4316 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004317 if (npages > ARRAY_SIZE(pages))
4318 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004319
Andy Adamsonbf118a32011-12-07 11:55:27 -05004320 for (i = 0; i < npages; i++) {
4321 pages[i] = alloc_page(GFP_KERNEL);
4322 if (!pages[i])
4323 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004324 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004325
4326 /* for decoding across pages */
4327 res.acl_scratch = alloc_page(GFP_KERNEL);
4328 if (!res.acl_scratch)
4329 goto out_free;
4330
Andy Adamsonbf118a32011-12-07 11:55:27 -05004331 args.acl_len = npages * PAGE_SIZE;
4332 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004333
Peng Taode040be2012-01-10 22:42:47 +08004334 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004335 __func__, buf, buflen, npages, args.acl_len);
4336 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4337 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004338 if (ret)
4339 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004340
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004341 /* Handle the case where the passed-in buffer is too short */
4342 if (res.acl_flags & NFS4_ACL_TRUNC) {
4343 /* Did the user only issue a request for the acl length? */
4344 if (buf == NULL)
4345 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004346 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004347 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004348 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004349 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004350 if (buf) {
4351 if (res.acl_len > buflen) {
4352 ret = -ERANGE;
4353 goto out_free;
4354 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004355 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004356 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004357out_ok:
4358 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004359out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004360 for (i = 0; i < npages; i++)
4361 if (pages[i])
4362 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004363 if (res.acl_scratch)
4364 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004365 return ret;
4366}
4367
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004368static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4369{
4370 struct nfs4_exception exception = { };
4371 ssize_t ret;
4372 do {
4373 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04004374 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004375 if (ret >= 0)
4376 break;
4377 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4378 } while (exception.retry);
4379 return ret;
4380}
4381
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004382static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4383{
4384 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004385 int ret;
4386
4387 if (!nfs4_server_supports_acls(server))
4388 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004389 ret = nfs_revalidate_inode(server, inode);
4390 if (ret < 0)
4391 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004392 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4393 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004394 ret = nfs4_read_cached_acl(inode, buf, buflen);
4395 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004396 /* -ENOENT is returned if there is no ACL or if there is an ACL
4397 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004398 return ret;
4399 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004400}
4401
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004402static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004403{
4404 struct nfs_server *server = NFS_SERVER(inode);
4405 struct page *pages[NFS4ACL_MAXPAGES];
4406 struct nfs_setaclargs arg = {
4407 .fh = NFS_FH(inode),
4408 .acl_pages = pages,
4409 .acl_len = buflen,
4410 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004411 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004412 struct rpc_message msg = {
4413 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4414 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004415 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004416 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004417 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004418 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004419
4420 if (!nfs4_server_supports_acls(server))
4421 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004422 if (npages > ARRAY_SIZE(pages))
4423 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004424 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4425 if (i < 0)
4426 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004427 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004428 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004429
4430 /*
4431 * Free each page after tx, so the only ref left is
4432 * held by the network stack
4433 */
4434 for (; i > 0; i--)
4435 put_page(pages[i-1]);
4436
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004437 /*
4438 * Acl update can result in inode attribute update.
4439 * so mark the attribute cache invalid.
4440 */
4441 spin_lock(&inode->i_lock);
4442 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4443 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004444 nfs_access_zap_cache(inode);
4445 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004446 return ret;
4447}
4448
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004449static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4450{
4451 struct nfs4_exception exception = { };
4452 int err;
4453 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004454 err = __nfs4_proc_set_acl(inode, buf, buflen);
4455 trace_nfs4_set_acl(inode, err);
4456 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004457 &exception);
4458 } while (exception.retry);
4459 return err;
4460}
4461
David Quigleyaa9c2662013-05-22 12:50:44 -04004462#ifdef CONFIG_NFS_V4_SECURITY_LABEL
4463static int _nfs4_get_security_label(struct inode *inode, void *buf,
4464 size_t buflen)
4465{
4466 struct nfs_server *server = NFS_SERVER(inode);
4467 struct nfs_fattr fattr;
4468 struct nfs4_label label = {0, 0, buflen, buf};
4469
4470 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4471 struct nfs4_getattr_arg args = {
4472 .fh = NFS_FH(inode),
4473 .bitmask = bitmask,
4474 };
4475 struct nfs4_getattr_res res = {
4476 .fattr = &fattr,
4477 .label = &label,
4478 .server = server,
4479 };
4480 struct rpc_message msg = {
4481 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4482 .rpc_argp = &args,
4483 .rpc_resp = &res,
4484 };
4485 int ret;
4486
4487 nfs_fattr_init(&fattr);
4488
4489 ret = rpc_call_sync(server->client, &msg, 0);
4490 if (ret)
4491 return ret;
4492 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4493 return -ENOENT;
4494 if (buflen < label.len)
4495 return -ERANGE;
4496 return 0;
4497}
4498
4499static int nfs4_get_security_label(struct inode *inode, void *buf,
4500 size_t buflen)
4501{
4502 struct nfs4_exception exception = { };
4503 int err;
4504
4505 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4506 return -EOPNOTSUPP;
4507
4508 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004509 err = _nfs4_get_security_label(inode, buf, buflen);
4510 trace_nfs4_get_security_label(inode, err);
4511 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004512 &exception);
4513 } while (exception.retry);
4514 return err;
4515}
4516
4517static int _nfs4_do_set_security_label(struct inode *inode,
4518 struct nfs4_label *ilabel,
4519 struct nfs_fattr *fattr,
4520 struct nfs4_label *olabel)
4521{
4522
4523 struct iattr sattr = {0};
4524 struct nfs_server *server = NFS_SERVER(inode);
4525 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4526 struct nfs_setattrargs args = {
4527 .fh = NFS_FH(inode),
4528 .iap = &sattr,
4529 .server = server,
4530 .bitmask = bitmask,
4531 .label = ilabel,
4532 };
4533 struct nfs_setattrres res = {
4534 .fattr = fattr,
4535 .label = olabel,
4536 .server = server,
4537 };
4538 struct rpc_message msg = {
4539 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
4540 .rpc_argp = &args,
4541 .rpc_resp = &res,
4542 };
4543 int status;
4544
4545 nfs4_stateid_copy(&args.stateid, &zero_stateid);
4546
4547 status = rpc_call_sync(server->client, &msg, 0);
4548 if (status)
4549 dprintk("%s failed: %d\n", __func__, status);
4550
4551 return status;
4552}
4553
4554static int nfs4_do_set_security_label(struct inode *inode,
4555 struct nfs4_label *ilabel,
4556 struct nfs_fattr *fattr,
4557 struct nfs4_label *olabel)
4558{
4559 struct nfs4_exception exception = { };
4560 int err;
4561
4562 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004563 err = _nfs4_do_set_security_label(inode, ilabel,
4564 fattr, olabel);
4565 trace_nfs4_set_security_label(inode, err);
4566 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004567 &exception);
4568 } while (exception.retry);
4569 return err;
4570}
4571
4572static int
4573nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4574{
4575 struct nfs4_label ilabel, *olabel = NULL;
4576 struct nfs_fattr fattr;
4577 struct rpc_cred *cred;
4578 struct inode *inode = dentry->d_inode;
4579 int status;
4580
4581 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4582 return -EOPNOTSUPP;
4583
4584 nfs_fattr_init(&fattr);
4585
4586 ilabel.pi = 0;
4587 ilabel.lfs = 0;
4588 ilabel.label = (char *)buf;
4589 ilabel.len = buflen;
4590
4591 cred = rpc_lookup_cred();
4592 if (IS_ERR(cred))
4593 return PTR_ERR(cred);
4594
4595 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4596 if (IS_ERR(olabel)) {
4597 status = -PTR_ERR(olabel);
4598 goto out;
4599 }
4600
4601 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4602 if (status == 0)
4603 nfs_setsecurity(inode, &fattr, olabel);
4604
4605 nfs4_label_free(olabel);
4606out:
4607 put_rpccred(cred);
4608 return status;
4609}
4610#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4611
4612
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004614nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004616 struct nfs_client *clp = server->nfs_client;
4617
4618 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 return 0;
4620 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004621 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004622 case -NFS4ERR_ADMIN_REVOKED:
4623 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004624 if (state == NULL)
4625 break;
4626 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004627 case -NFS4ERR_OPENMODE:
4628 if (state == NULL)
4629 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004630 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4631 goto stateid_invalid;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004632 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004633 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004634 if (state != NULL) {
4635 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4636 goto stateid_invalid;
4637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004639 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004640 nfs4_schedule_lease_recovery(clp);
4641 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004642#if defined(CONFIG_NFS_V4_1)
4643 case -NFS4ERR_BADSESSION:
4644 case -NFS4ERR_BADSLOT:
4645 case -NFS4ERR_BAD_HIGH_SLOT:
4646 case -NFS4ERR_DEADSESSION:
4647 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4648 case -NFS4ERR_SEQ_FALSE_RETRY:
4649 case -NFS4ERR_SEQ_MISORDERED:
4650 dprintk("%s ERROR %d, Reset session\n", __func__,
4651 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004652 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004653 task->tk_status = 0;
4654 return -EAGAIN;
4655#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004657 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004658 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4660 task->tk_status = 0;
4661 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004662 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 case -NFS4ERR_OLD_STATEID:
4664 task->tk_status = 0;
4665 return -EAGAIN;
4666 }
4667 task->tk_status = nfs4_map_errors(task->tk_status);
4668 return 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004669stateid_invalid:
4670 task->tk_status = -EIO;
4671 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004672wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004673 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004674 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4675 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4676 task->tk_status = 0;
4677 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678}
4679
Chuck Leverf0920752012-05-21 22:45:41 -04004680static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4681 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004682{
4683 __be32 verf[2];
4684
Chuck Lever2c820d92012-05-21 22:45:33 -04004685 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4686 /* An impossible timestamp guarantees this value
4687 * will never match a generated boot time. */
4688 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04004689 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04004690 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004691 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04004692 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
4693 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04004694 }
Chuck Levercd937102012-03-02 17:14:31 -05004695 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4696}
4697
Chuck Levere984a552012-09-14 17:24:21 -04004698static unsigned int
4699nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4700 char *buf, size_t len)
4701{
4702 unsigned int result;
4703
4704 rcu_read_lock();
4705 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4706 clp->cl_ipaddr,
4707 rpc_peeraddr2str(clp->cl_rpcclient,
4708 RPC_DISPLAY_ADDR),
4709 rpc_peeraddr2str(clp->cl_rpcclient,
4710 RPC_DISPLAY_PROTO));
4711 rcu_read_unlock();
4712 return result;
4713}
4714
4715static unsigned int
4716nfs4_init_uniform_client_string(const struct nfs_client *clp,
4717 char *buf, size_t len)
4718{
Trond Myklebust55b59292013-07-24 16:36:35 -04004719 const char *nodename = clp->cl_rpcclient->cl_nodename;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004720
4721 if (nfs4_client_id_uniquifier[0] != '\0')
Trond Myklebust55b59292013-07-24 16:36:35 -04004722 return scnprintf(buf, len, "Linux NFSv%u.%u %s/%s",
4723 clp->rpc_ops->version,
4724 clp->cl_minorversion,
4725 nfs4_client_id_uniquifier,
4726 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004727 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4728 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004729 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004730}
4731
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004732/**
4733 * nfs4_proc_setclientid - Negotiate client ID
4734 * @clp: state data structure
4735 * @program: RPC program for NFSv4 callback service
4736 * @port: IP port number for NFS4 callback service
4737 * @cred: RPC credential to use for this call
4738 * @res: where to place the result
4739 *
4740 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4741 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004742int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4743 unsigned short port, struct rpc_cred *cred,
4744 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745{
4746 nfs4_verifier sc_verifier;
4747 struct nfs4_setclientid setclientid = {
4748 .sc_verifier = &sc_verifier,
4749 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004750 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 };
4752 struct rpc_message msg = {
4753 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4754 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004755 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004756 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004758 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759
Chuck Leverde734832012-07-11 16:30:50 -04004760 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004761 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004762 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4763 setclientid.sc_name_len =
4764 nfs4_init_uniform_client_string(clp,
4765 setclientid.sc_name,
4766 sizeof(setclientid.sc_name));
4767 else
4768 setclientid.sc_name_len =
4769 nfs4_init_nonuniform_client_string(clp,
4770 setclientid.sc_name,
4771 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004772 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004773 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004774 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Djalal Harounife2d5392013-06-18 17:58:12 +01004775 sizeof(setclientid.sc_netid), "%s",
Chuck Leverd4d3c502007-12-10 14:57:09 -05004776 rpc_peeraddr2str(clp->cl_rpcclient,
4777 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004778 rcu_read_unlock();
4779 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004780 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 clp->cl_ipaddr, port >> 8, port & 255);
4782
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004783 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4784 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4785 setclientid.sc_name_len, setclientid.sc_name);
4786 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004787 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004788 dprintk("NFS reply setclientid: %d\n", status);
4789 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790}
4791
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004792/**
4793 * nfs4_proc_setclientid_confirm - Confirm client ID
4794 * @clp: state data structure
4795 * @res: result of a previous SETCLIENTID
4796 * @cred: RPC credential to use for this call
4797 *
4798 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4799 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004800int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004801 struct nfs4_setclientid_res *arg,
4802 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 struct rpc_message msg = {
4805 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004806 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004807 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809 int status;
4810
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004811 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4812 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4813 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004814 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004815 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004816 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817 return status;
4818}
4819
Trond Myklebustfe650402006-01-03 09:55:18 +01004820struct nfs4_delegreturndata {
4821 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004822 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004823 struct nfs_fh fh;
4824 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004825 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004826 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004827 int rpc_status;
4828};
4829
Trond Myklebustfe650402006-01-03 09:55:18 +01004830static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4831{
4832 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004833
Trond Myklebust14516c32010-07-31 14:29:06 -04004834 if (!nfs4_sequence_done(task, &data->res.seq_res))
4835 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004836
Trond Myklebustca8acf82013-08-13 10:36:56 -04004837 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004838 switch (task->tk_status) {
4839 case -NFS4ERR_STALE_STATEID:
4840 case -NFS4ERR_EXPIRED:
4841 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004842 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004843 break;
4844 default:
4845 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4846 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004847 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004848 return;
4849 }
4850 }
4851 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004852}
4853
4854static void nfs4_delegreturn_release(void *calldata)
4855{
Trond Myklebustfe650402006-01-03 09:55:18 +01004856 kfree(calldata);
4857}
4858
Andy Adamson938e1012009-04-01 09:22:28 -04004859#if defined(CONFIG_NFS_V4_1)
4860static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4861{
4862 struct nfs4_delegreturndata *d_data;
4863
4864 d_data = (struct nfs4_delegreturndata *)data;
4865
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004866 nfs4_setup_sequence(d_data->res.server,
4867 &d_data->args.seq_args,
4868 &d_data->res.seq_res,
4869 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004870}
4871#endif /* CONFIG_NFS_V4_1 */
4872
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004873static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004874#if defined(CONFIG_NFS_V4_1)
4875 .rpc_call_prepare = nfs4_delegreturn_prepare,
4876#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004877 .rpc_call_done = nfs4_delegreturn_done,
4878 .rpc_release = nfs4_delegreturn_release,
4879};
4880
Trond Myklebuste6f81072008-01-24 18:14:34 -05004881static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004882{
4883 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004884 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004885 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004886 struct rpc_message msg = {
4887 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4888 .rpc_cred = cred,
4889 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004890 struct rpc_task_setup task_setup_data = {
4891 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004892 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004893 .callback_ops = &nfs4_delegreturn_ops,
4894 .flags = RPC_TASK_ASYNC,
4895 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004896 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004897
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004898 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004899 if (data == NULL)
4900 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04004901 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004902 data->args.fhandle = &data->fh;
4903 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004904 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004905 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004906 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004907 data->res.fattr = &data->fattr;
4908 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004909 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004910 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004911 data->rpc_status = 0;
4912
Trond Myklebustc970aa82007-07-14 15:39:59 -04004913 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004914 msg.rpc_argp = &data->args;
4915 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004916 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004917 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004918 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004919 if (!issync)
4920 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004921 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004922 if (status != 0)
4923 goto out;
4924 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004925 if (status == 0)
4926 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4927 else
4928 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004929out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004930 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004931 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932}
4933
Trond Myklebuste6f81072008-01-24 18:14:34 -05004934int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935{
4936 struct nfs_server *server = NFS_SERVER(inode);
4937 struct nfs4_exception exception = { };
4938 int err;
4939 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004940 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Trond Myklebustca8acf82013-08-13 10:36:56 -04004941 trace_nfs4_delegreturn(inode, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 switch (err) {
4943 case -NFS4ERR_STALE_STATEID:
4944 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 case 0:
4946 return 0;
4947 }
4948 err = nfs4_handle_exception(server, err, &exception);
4949 } while (exception.retry);
4950 return err;
4951}
4952
4953#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4954#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4955
4956/*
4957 * sleep, with exponential backoff, and retry the LOCK operation.
4958 */
4959static unsigned long
4960nfs4_set_lock_task_retry(unsigned long timeout)
4961{
Colin Cross416ad3c2013-05-06 23:50:06 +00004962 freezable_schedule_timeout_killable_unsafe(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963 timeout <<= 1;
4964 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4965 return NFS4_LOCK_MAXTIMEOUT;
4966 return timeout;
4967}
4968
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4970{
4971 struct inode *inode = state->inode;
4972 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004973 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004974 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004976 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004978 struct nfs_lockt_res res = {
4979 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 };
4981 struct rpc_message msg = {
4982 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4983 .rpc_argp = &arg,
4984 .rpc_resp = &res,
4985 .rpc_cred = state->owner->so_cred,
4986 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987 struct nfs4_lock_state *lsp;
4988 int status;
4989
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004990 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004991 status = nfs4_set_lock_state(state, request);
4992 if (status != 0)
4993 goto out;
4994 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004995 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004996 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004997 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004998 switch (status) {
4999 case 0:
5000 request->fl_type = F_UNLCK;
5001 break;
5002 case -NFS4ERR_DENIED:
5003 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005005 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005006out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007 return status;
5008}
5009
5010static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5011{
5012 struct nfs4_exception exception = { };
5013 int err;
5014
5015 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005016 err = _nfs4_proc_getlk(state, cmd, request);
5017 trace_nfs4_get_lock(request, state, cmd, err);
5018 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 &exception);
5020 } while (exception.retry);
5021 return err;
5022}
5023
5024static int do_vfs_lock(struct file *file, struct file_lock *fl)
5025{
5026 int res = 0;
5027 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5028 case FL_POSIX:
5029 res = posix_lock_file_wait(file, fl);
5030 break;
5031 case FL_FLOCK:
5032 res = flock_lock_file_wait(file, fl);
5033 break;
5034 default:
5035 BUG();
5036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 return res;
5038}
5039
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005040struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005041 struct nfs_locku_args arg;
5042 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005043 struct nfs4_lock_state *lsp;
5044 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005045 struct file_lock fl;
5046 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005047 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005048};
5049
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005050static struct nfs4_unlockdata *nfs4_alloc_unlockdata(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 *p;
5056 struct inode *inode = lsp->ls_state->inode;
5057
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005058 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005059 if (p == NULL)
5060 return NULL;
5061 p->arg.fh = NFS_FH(inode);
5062 p->arg.fl = &p->fl;
5063 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005064 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005065 p->arg.stateid = &lsp->ls_stateid;
5066 p->lsp = lsp;
5067 atomic_inc(&lsp->ls_count);
5068 /* Ensure we don't close file until we're done freeing locks! */
5069 p->ctx = get_nfs_open_context(ctx);
5070 memcpy(&p->fl, fl, sizeof(p->fl));
5071 p->server = NFS_SERVER(inode);
5072 return p;
5073}
5074
Trond Myklebust06f814a2006-01-03 09:55:07 +01005075static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005076{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005077 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005078 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005079 nfs4_put_lock_state(calldata->lsp);
5080 put_nfs_open_context(calldata->ctx);
5081 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005082}
5083
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005084static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005085{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005086 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005087
Trond Myklebust14516c32010-07-31 14:29:06 -04005088 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5089 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005090 switch (task->tk_status) {
5091 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05005092 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
5093 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005094 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005095 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005096 case -NFS4ERR_BAD_STATEID:
5097 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005098 case -NFS4ERR_STALE_STATEID:
5099 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005100 break;
5101 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005102 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005103 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005104 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005105 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005106}
5107
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005108static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005109{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005110 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005112 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005113 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04005114 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005115 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005116 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005117 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005118 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005119 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005120 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005121 &calldata->res.seq_res,
5122 task) != 0)
5123 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005124 return;
5125out_no_action:
5126 task->tk_action = NULL;
5127out_wait:
5128 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129}
5130
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005131static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005132 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005133 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005134 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005135};
5136
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005137static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5138 struct nfs_open_context *ctx,
5139 struct nfs4_lock_state *lsp,
5140 struct nfs_seqid *seqid)
5141{
5142 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005143 struct rpc_message msg = {
5144 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5145 .rpc_cred = ctx->cred,
5146 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005147 struct rpc_task_setup task_setup_data = {
5148 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005149 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005150 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005151 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005152 .flags = RPC_TASK_ASYNC,
5153 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005154
Frank Filz137d6ac2007-07-09 15:32:29 -07005155 /* Ensure this is an unlock - when canceling a lock, the
5156 * canceled lock is passed in, and it won't be an unlock.
5157 */
5158 fl->fl_type = F_UNLCK;
5159
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005160 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5161 if (data == NULL) {
5162 nfs_free_seqid(seqid);
5163 return ERR_PTR(-ENOMEM);
5164 }
5165
Chuck Levera9c92d62013-08-09 12:48:18 -04005166 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005167 msg.rpc_argp = &data->arg;
5168 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005169 task_setup_data.callback_data = data;
5170 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005171}
5172
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5174{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005175 struct inode *inode = state->inode;
5176 struct nfs4_state_owner *sp = state->owner;
5177 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005178 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005179 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005180 struct rpc_task *task;
5181 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005182 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183
Trond Myklebust9b073572006-06-29 16:38:34 -04005184 status = nfs4_set_lock_state(state, request);
5185 /* Unlock _before_ we do the RPC call */
5186 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005187 /* Exclude nfs_delegation_claim_locks() */
5188 mutex_lock(&sp->so_delegreturn_mutex);
5189 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005190 down_read(&nfsi->rwsem);
5191 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
5192 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005193 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005194 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005195 }
5196 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005197 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005198 if (status != 0)
5199 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005200 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005201 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005202 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5203 goto out;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005204 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005205 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005206 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04005207 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005208 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005209 status = PTR_ERR(task);
5210 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005211 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005212 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005213 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005214out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005215 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005216 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005217 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218}
5219
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005220struct nfs4_lockdata {
5221 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005222 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005223 struct nfs4_lock_state *lsp;
5224 struct nfs_open_context *ctx;
5225 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005226 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005227 int rpc_status;
5228 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005229 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005230};
5231
5232static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005233 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5234 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005235{
5236 struct nfs4_lockdata *p;
5237 struct inode *inode = lsp->ls_state->inode;
5238 struct nfs_server *server = NFS_SERVER(inode);
5239
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005240 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005241 if (p == NULL)
5242 return NULL;
5243
5244 p->arg.fh = NFS_FH(inode);
5245 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005246 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005247 if (p->arg.open_seqid == NULL)
5248 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005249 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005250 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005251 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005252 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04005253 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005254 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005255 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005256 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005257 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005258 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005259 atomic_inc(&lsp->ls_count);
5260 p->ctx = get_nfs_open_context(ctx);
5261 memcpy(&p->fl, fl, sizeof(p->fl));
5262 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005263out_free_seqid:
5264 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005265out_free:
5266 kfree(p);
5267 return NULL;
5268}
5269
5270static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5271{
5272 struct nfs4_lockdata *data = calldata;
5273 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274
Harvey Harrison3110ff82008-05-02 13:42:44 -07005275 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005276 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005277 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005278 /* Do we need to do an open_to_lock_owner? */
5279 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005280 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005281 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005282 }
Trond Myklebust8e472f32013-04-20 01:30:53 -04005283 data->arg.open_stateid = &state->open_stateid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005284 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005285 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005286 } else
5287 data->arg.new_lock_owner = 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04005288 if (!nfs4_valid_open_stateid(state)) {
5289 data->rpc_status = -EBADF;
5290 task->tk_action = NULL;
5291 goto out_release_open_seqid;
5292 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005293 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005294 if (nfs4_setup_sequence(data->server,
5295 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005296 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005297 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04005298 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04005299out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005300 nfs_release_seqid(data->arg.open_seqid);
5301out_release_lock_seqid:
5302 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005303out_wait:
5304 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005305 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005306}
5307
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005308static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5309{
5310 struct nfs4_lockdata *data = calldata;
5311
Harvey Harrison3110ff82008-05-02 13:42:44 -07005312 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005313
Trond Myklebust14516c32010-07-31 14:29:06 -04005314 if (!nfs4_sequence_done(task, &data->res.seq_res))
5315 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04005316
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005317 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005318 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005319 if (data->rpc_status == 0)
5320 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
5321 else
5322 goto out;
5323 }
5324 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05005325 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005326 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04005327 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005328 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005329out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07005330 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005331}
5332
5333static void nfs4_lock_release(void *calldata)
5334{
5335 struct nfs4_lockdata *data = calldata;
5336
Harvey Harrison3110ff82008-05-02 13:42:44 -07005337 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005338 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005339 if (data->cancelled != 0) {
5340 struct rpc_task *task;
5341 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5342 data->arg.lock_seqid);
5343 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08005344 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005345 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005346 } else
5347 nfs_free_seqid(data->arg.lock_seqid);
5348 nfs4_put_lock_state(data->lsp);
5349 put_nfs_open_context(data->ctx);
5350 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005351 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005352}
5353
5354static const struct rpc_call_ops nfs4_lock_ops = {
5355 .rpc_call_prepare = nfs4_lock_prepare,
5356 .rpc_call_done = nfs4_lock_done,
5357 .rpc_release = nfs4_lock_release,
5358};
5359
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005360static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5361{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005362 switch (error) {
5363 case -NFS4ERR_ADMIN_REVOKED:
5364 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05005365 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005366 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04005367 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05005368 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005369 break;
5370 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005371 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05005372 case -NFS4ERR_EXPIRED:
5373 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005374 };
5375}
5376
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005377static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005378{
5379 struct nfs4_lockdata *data;
5380 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005381 struct rpc_message msg = {
5382 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5383 .rpc_cred = state->owner->so_cred,
5384 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005385 struct rpc_task_setup task_setup_data = {
5386 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005387 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005388 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005389 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005390 .flags = RPC_TASK_ASYNC,
5391 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005392 int ret;
5393
Harvey Harrison3110ff82008-05-02 13:42:44 -07005394 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005395 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005396 fl->fl_u.nfs4_fl.owner,
5397 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005398 if (data == NULL)
5399 return -ENOMEM;
5400 if (IS_SETLKW(cmd))
5401 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04005402 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005403 msg.rpc_argp = &data->arg;
5404 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005405 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005406 if (recovery_type > NFS_LOCK_NEW) {
5407 if (recovery_type == NFS_LOCK_RECLAIM)
5408 data->arg.reclaim = NFS_LOCK_RECLAIM;
5409 nfs4_set_sequence_privileged(&data->arg.seq_args);
5410 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04005411 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005412 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005413 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005414 ret = nfs4_wait_for_completion_rpc_task(task);
5415 if (ret == 0) {
5416 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005417 if (ret)
5418 nfs4_handle_setlk_error(data->server, data->lsp,
5419 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005420 } else
5421 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005422 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005423 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005424 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425}
5426
5427static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5428{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005429 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005430 struct nfs4_exception exception = {
5431 .inode = state->inode,
5432 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005433 int err;
5434
5435 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005436 /* Cache the lock if possible... */
5437 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5438 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005439 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005440 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
Trond Myklebust168667c2010-10-19 19:47:49 -04005441 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00005442 break;
5443 nfs4_handle_exception(server, err, &exception);
5444 } while (exception.retry);
5445 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446}
5447
5448static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5449{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005450 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005451 struct nfs4_exception exception = {
5452 .inode = state->inode,
5453 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005454 int err;
5455
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005456 err = nfs4_set_lock_state(state, request);
5457 if (err != 0)
5458 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00005459 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005460 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5461 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005462 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005463 trace_nfs4_lock_expired(request, state, F_SETLK, err);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005464 switch (err) {
5465 default:
5466 goto out;
5467 case -NFS4ERR_GRACE:
5468 case -NFS4ERR_DELAY:
5469 nfs4_handle_exception(server, err, &exception);
5470 err = 0;
5471 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005472 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005473out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00005474 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475}
5476
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005477#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005478/**
5479 * nfs41_check_expired_locks - possibly free a lock stateid
5480 *
5481 * @state: NFSv4 state for an inode
5482 *
5483 * Returns NFS_OK if recovery for this stateid is now finished.
5484 * Otherwise a negative NFS4ERR value is returned.
5485 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005486static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005487{
Chuck Levereb64cf92012-07-11 16:30:05 -04005488 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005489 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005490 struct nfs_server *server = NFS_SERVER(state->inode);
5491
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005492 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04005493 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005494 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
5495
5496 status = nfs41_test_stateid(server,
5497 &lsp->ls_stateid,
5498 cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04005499 trace_nfs4_test_lock_stateid(state, lsp, status);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005500 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005501 /* Free the stateid unless the server
5502 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04005503 if (status != -NFS4ERR_BAD_STATEID)
5504 nfs41_free_stateid(server,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005505 &lsp->ls_stateid,
5506 cred);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005507 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005508 ret = status;
5509 }
5510 }
5511 };
5512
5513 return ret;
5514}
5515
5516static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5517{
5518 int status = NFS_OK;
5519
5520 if (test_bit(LK_STATE_IN_USE, &state->flags))
5521 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005522 if (status != NFS_OK)
5523 status = nfs4_lock_expired(state, request);
5524 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005525}
5526#endif
5527
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5529{
Trond Myklebust9a99af492013-02-04 20:17:49 -05005530 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005531 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005532 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005533 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005534 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005536 if ((fl_flags & FL_POSIX) &&
5537 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5538 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005539 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005540 status = nfs4_set_lock_state(state, request);
5541 if (status != 0)
5542 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005543 request->fl_flags |= FL_ACCESS;
5544 status = do_vfs_lock(request->fl_file, request);
5545 if (status < 0)
5546 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005547 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005548 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005549 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005550 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005551 request->fl_flags = fl_flags & ~FL_SLEEP;
5552 status = do_vfs_lock(request->fl_file, request);
5553 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005554 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05005555 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5556 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005557 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005558 if (status != 0)
Trond Myklebust9a99af492013-02-04 20:17:49 -05005559 goto out;
5560 down_read(&nfsi->rwsem);
5561 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5562 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005563 goto out_unlock;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005564 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005565 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005566 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005567 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005568 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5569 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005570out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005571 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005572out:
5573 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 return status;
5575}
5576
5577static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5578{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005579 struct nfs4_exception exception = {
5580 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005581 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005582 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583 int err;
5584
5585 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005586 err = _nfs4_proc_setlk(state, cmd, request);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005587 trace_nfs4_set_lock(request, state, cmd, err);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005588 if (err == -NFS4ERR_DENIED)
5589 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005591 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592 } while (exception.retry);
5593 return err;
5594}
5595
5596static int
5597nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5598{
5599 struct nfs_open_context *ctx;
5600 struct nfs4_state *state;
5601 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5602 int status;
5603
5604 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005605 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606 state = ctx->state;
5607
5608 if (request->fl_start < 0 || request->fl_end < 0)
5609 return -EINVAL;
5610
Trond Myklebustd9531262009-07-21 19:22:38 -04005611 if (IS_GETLK(cmd)) {
5612 if (state != NULL)
5613 return nfs4_proc_getlk(state, F_GETLK, request);
5614 return 0;
5615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616
5617 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5618 return -EINVAL;
5619
Trond Myklebustd9531262009-07-21 19:22:38 -04005620 if (request->fl_type == F_UNLCK) {
5621 if (state != NULL)
5622 return nfs4_proc_unlck(state, cmd, request);
5623 return 0;
5624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625
Trond Myklebustd9531262009-07-21 19:22:38 -04005626 if (state == NULL)
5627 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005628 /*
5629 * Don't rely on the VFS having checked the file open mode,
5630 * since it won't do this for flock() locks.
5631 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005632 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005633 case F_RDLCK:
5634 if (!(filp->f_mode & FMODE_READ))
5635 return -EBADF;
5636 break;
5637 case F_WRLCK:
5638 if (!(filp->f_mode & FMODE_WRITE))
5639 return -EBADF;
5640 }
5641
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642 do {
5643 status = nfs4_proc_setlk(state, cmd, request);
5644 if ((status != -EAGAIN) || IS_SETLK(cmd))
5645 break;
5646 timeout = nfs4_set_lock_task_retry(timeout);
5647 status = -ERESTARTSYS;
5648 if (signalled())
5649 break;
5650 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651 return status;
5652}
5653
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005654int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05005655{
5656 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05005657 int err;
5658
5659 err = nfs4_set_lock_state(state, fl);
5660 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005661 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04005662 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005663 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05005664}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005665
Trond Myklebustcf470c32012-03-07 13:49:12 -05005666struct nfs_release_lockowner_data {
5667 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005668 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005669 struct nfs_release_lockowner_args args;
5670};
5671
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005672static void nfs4_release_lockowner_release(void *calldata)
5673{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005674 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005675 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005676 kfree(calldata);
5677}
5678
Trond Myklebust17280172012-03-11 13:11:00 -04005679static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005680 .rpc_release = nfs4_release_lockowner_release,
5681};
5682
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04005683static int nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005684{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005685 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005686 struct rpc_message msg = {
5687 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5688 };
5689
5690 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005691 return -EINVAL;
5692 data = kmalloc(sizeof(*data), GFP_NOFS);
5693 if (!data)
5694 return -ENOMEM;
5695 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005696 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005697 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5698 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5699 data->args.lock_owner.s_dev = server->s_dev;
5700 msg.rpc_argp = &data->args;
5701 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5702 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005703}
5704
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005705#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5706
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005707static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5708 const void *buf, size_t buflen,
5709 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005710{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005711 if (strcmp(key, "") != 0)
5712 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005713
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005714 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005715}
5716
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005717static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5718 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005719{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005720 if (strcmp(key, "") != 0)
5721 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005722
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005723 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005724}
5725
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005726static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5727 size_t list_len, const char *name,
5728 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005729{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005730 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005731
J. Bruce Fields096455a2006-03-20 23:23:42 -05005732 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5733 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005734
5735 if (list && len <= list_len)
5736 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005737 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005738}
5739
David Quigleyc9bccef2013-05-22 12:50:45 -04005740#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5741static inline int nfs4_server_supports_labels(struct nfs_server *server)
5742{
5743 return server->caps & NFS_CAP_SECURITY_LABEL;
5744}
5745
5746static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
5747 const void *buf, size_t buflen,
5748 int flags, int type)
5749{
5750 if (security_ismaclabel(key))
5751 return nfs4_set_security_label(dentry, buf, buflen);
5752
5753 return -EOPNOTSUPP;
5754}
5755
5756static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
5757 void *buf, size_t buflen, int type)
5758{
5759 if (security_ismaclabel(key))
5760 return nfs4_get_security_label(dentry->d_inode, buf, buflen);
5761 return -EOPNOTSUPP;
5762}
5763
5764static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
5765 size_t list_len, const char *name,
5766 size_t name_len, int type)
5767{
5768 size_t len = 0;
5769
5770 if (nfs_server_capable(dentry->d_inode, NFS_CAP_SECURITY_LABEL)) {
5771 len = security_inode_listsecurity(dentry->d_inode, NULL, 0);
5772 if (list && len <= list_len)
5773 security_inode_listsecurity(dentry->d_inode, list, len);
5774 }
5775 return len;
5776}
5777
5778static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
5779 .prefix = XATTR_SECURITY_PREFIX,
5780 .list = nfs4_xattr_list_nfs4_label,
5781 .get = nfs4_xattr_get_nfs4_label,
5782 .set = nfs4_xattr_set_nfs4_label,
5783};
5784#endif
5785
5786
Andy Adamson533eb462011-06-13 18:25:56 -04005787/*
5788 * nfs_fhget will use either the mounted_on_fileid or the fileid
5789 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005790static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5791{
Andy Adamson533eb462011-06-13 18:25:56 -04005792 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5793 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5794 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005795 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005796 return;
5797
5798 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005799 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005800 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5801 fattr->nlink = 2;
5802}
5803
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005804static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5805 const struct qstr *name,
5806 struct nfs4_fs_locations *fs_locations,
5807 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005808{
5809 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04005810 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005811 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005812 };
5813 struct nfs4_fs_locations_arg args = {
5814 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005815 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005816 .page = page,
5817 .bitmask = bitmask,
5818 };
Benny Halevy22958462009-04-01 09:22:02 -04005819 struct nfs4_fs_locations_res res = {
5820 .fs_locations = fs_locations,
5821 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005822 struct rpc_message msg = {
5823 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5824 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005825 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005826 };
5827 int status;
5828
Harvey Harrison3110ff82008-05-02 13:42:44 -07005829 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005830
5831 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5832 * is not supported */
5833 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5834 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5835 else
5836 bitmask[0] |= FATTR4_WORD0_FILEID;
5837
Trond Myklebustc228fd32007-01-13 02:28:11 -05005838 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005839 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005840 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005841 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005842 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005843 return status;
5844}
5845
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005846int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5847 const struct qstr *name,
5848 struct nfs4_fs_locations *fs_locations,
5849 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005850{
5851 struct nfs4_exception exception = { };
5852 int err;
5853 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04005854 err = _nfs4_proc_fs_locations(client, dir, name,
5855 fs_locations, page);
5856 trace_nfs4_get_fs_locations(dir, name, err);
5857 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005858 &exception);
5859 } while (exception.retry);
5860 return err;
5861}
5862
Andy Adamson5ec16a82013-08-08 10:57:55 -04005863/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04005864 * If 'use_integrity' is true and the state managment nfs_client
5865 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
5866 * and the machine credential as per RFC3530bis and RFC5661 Security
5867 * Considerations sections. Otherwise, just use the user cred with the
5868 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04005869 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04005870static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005871{
5872 int status;
5873 struct nfs4_secinfo_arg args = {
5874 .dir_fh = NFS_FH(dir),
5875 .name = name,
5876 };
5877 struct nfs4_secinfo_res res = {
5878 .flavors = flavors,
5879 };
5880 struct rpc_message msg = {
5881 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5882 .rpc_argp = &args,
5883 .rpc_resp = &res,
5884 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04005885 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
5886
5887 if (use_integrity) {
5888 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
5889 msg.rpc_cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
5890 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005891
5892 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04005893 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
5894 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005895 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04005896
5897 if (msg.rpc_cred)
5898 put_rpccred(msg.rpc_cred);
5899
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005900 return status;
5901}
5902
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005903int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5904 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005905{
5906 struct nfs4_exception exception = { };
5907 int err;
5908 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04005909 err = -NFS4ERR_WRONGSEC;
5910
5911 /* try to use integrity protection with machine cred */
5912 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
5913 err = _nfs4_proc_secinfo(dir, name, flavors, true);
5914
5915 /*
5916 * if unable to use integrity protection, or SECINFO with
5917 * integrity protection returns NFS4ERR_WRONGSEC (which is
5918 * disallowed by spec, but exists in deployed servers) use
5919 * the current filesystem's rpc_client and the user cred.
5920 */
5921 if (err == -NFS4ERR_WRONGSEC)
5922 err = _nfs4_proc_secinfo(dir, name, flavors, false);
5923
Trond Myklebust078ea3d2013-08-12 16:45:55 -04005924 trace_nfs4_secinfo(dir, name, err);
5925 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005926 &exception);
5927 } while (exception.retry);
5928 return err;
5929}
5930
Andy Adamson557134a2009-04-01 09:21:53 -04005931#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005932/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005933 * Check the exchange flags returned by the server for invalid flags, having
5934 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5935 * DS flags set.
5936 */
5937static int nfs4_check_cl_exchange_flags(u32 flags)
5938{
5939 if (flags & ~EXCHGID4_FLAG_MASK_R)
5940 goto out_inval;
5941 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5942 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5943 goto out_inval;
5944 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5945 goto out_inval;
5946 return NFS_OK;
5947out_inval:
5948 return -NFS4ERR_INVAL;
5949}
5950
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005951static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005952nfs41_same_server_scope(struct nfs41_server_scope *a,
5953 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005954{
5955 if (a->server_scope_sz == b->server_scope_sz &&
5956 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5957 return true;
5958
5959 return false;
5960}
5961
Andy Adamson357f54d2010-12-14 10:11:57 -05005962/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005963 * nfs4_proc_bind_conn_to_session()
5964 *
5965 * The 4.1 client currently uses the same TCP connection for the
5966 * fore and backchannel.
5967 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005968int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005969{
5970 int status;
5971 struct nfs41_bind_conn_to_session_res res;
5972 struct rpc_message msg = {
5973 .rpc_proc =
5974 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5975 .rpc_argp = clp,
5976 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005977 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005978 };
5979
5980 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005981
5982 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5983 if (unlikely(res.session == NULL)) {
5984 status = -ENOMEM;
5985 goto out;
5986 }
5987
5988 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005989 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005990 if (status == 0) {
5991 if (memcmp(res.session->sess_id.data,
5992 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5993 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5994 status = -EIO;
5995 goto out_session;
5996 }
5997 if (res.dir != NFS4_CDFS4_BOTH) {
5998 dprintk("NFS: %s: Unexpected direction from server\n",
5999 __func__);
6000 status = -EIO;
6001 goto out_session;
6002 }
6003 if (res.use_conn_in_rdma_mode) {
6004 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6005 __func__);
6006 status = -EIO;
6007 goto out_session;
6008 }
6009 }
6010out_session:
6011 kfree(res.session);
6012out:
6013 dprintk("<-- %s status= %d\n", __func__, status);
6014 return status;
6015}
6016
6017/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04006018 * nfs4_proc_exchange_id()
6019 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006020 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6021 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04006022 * Since the clientid has expired, all compounds using sessions
6023 * associated with the stale clientid will be returning
6024 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
6025 * be in some phase of session reset.
6026 */
Andy Adamson4d643d12009-12-04 15:52:24 -05006027int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04006028{
6029 nfs4_verifier verifier;
6030 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05006031 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04006032 .client = clp,
Trond Myklebust4f0b4292013-05-20 12:24:03 -04006033 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6034 EXCHGID4_FLAG_BIND_PRINC_STATEID,
Benny Halevy99fe60d2009-04-01 09:22:29 -04006035 };
6036 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04006037 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04006038 };
6039 int status;
6040 struct rpc_message msg = {
6041 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
6042 .rpc_argp = &args,
6043 .rpc_resp = &res,
6044 .rpc_cred = cred,
6045 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04006046
Chuck Leverf0920752012-05-21 22:45:41 -04006047 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04006048 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
6049 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006050 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
6051 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6052 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006053
Chuck Leveracdeb692012-05-21 22:46:16 -04006054 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006055 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04006056 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006057 status = -ENOMEM;
6058 goto out;
6059 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006060
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006061 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006062 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006063 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04006064 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04006065 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04006066 }
6067
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006068 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006069 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006070 status = -ENOMEM;
6071 goto out_server_scope;
6072 }
6073
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006074 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006075 trace_nfs4_exchange_id(clp, status);
Chuck Lever177313f2012-05-21 22:44:58 -04006076 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04006077 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006078
Chuck Lever177313f2012-05-21 22:44:58 -04006079 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04006080 clp->cl_clientid = res.clientid;
6081 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
6082 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
6083 clp->cl_seqid = res.seqid;
6084
Chuck Leveracdeb692012-05-21 22:46:16 -04006085 kfree(clp->cl_serverowner);
6086 clp->cl_serverowner = res.server_owner;
6087 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04006088
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006089 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04006090 kfree(clp->cl_implid);
6091 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006092
Chuck Lever177313f2012-05-21 22:44:58 -04006093 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006094 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006095 res.server_scope)) {
6096 dprintk("%s: server_scope mismatch detected\n",
6097 __func__);
6098 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006099 kfree(clp->cl_serverscope);
6100 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006101 }
6102
Chuck Lever177313f2012-05-21 22:44:58 -04006103 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006104 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006105 goto out;
6106 }
Trond Myklebust32b01312012-05-26 13:41:04 -04006107 } else
6108 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006109
Chuck Leveracdeb692012-05-21 22:46:16 -04006110out_server_owner:
6111 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006112out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006113 kfree(res.server_scope);
6114out:
Chuck Lever177313f2012-05-21 22:44:58 -04006115 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006116 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006117 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006118 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04006119 clp->cl_implid->date.seconds,
6120 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006121 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006122 return status;
6123}
6124
Trond Myklebust66245532012-05-25 17:18:09 -04006125static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
6126 struct rpc_cred *cred)
6127{
6128 struct rpc_message msg = {
6129 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
6130 .rpc_argp = clp,
6131 .rpc_cred = cred,
6132 };
6133 int status;
6134
6135 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006136 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04006137 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04006138 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04006139 "DESTROY_CLIENTID.", status, clp->cl_hostname);
6140 return status;
6141}
6142
6143static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
6144 struct rpc_cred *cred)
6145{
6146 unsigned int loop;
6147 int ret;
6148
6149 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
6150 ret = _nfs4_proc_destroy_clientid(clp, cred);
6151 switch (ret) {
6152 case -NFS4ERR_DELAY:
6153 case -NFS4ERR_CLIENTID_BUSY:
6154 ssleep(1);
6155 break;
6156 default:
6157 return ret;
6158 }
6159 }
6160 return 0;
6161}
6162
6163int nfs4_destroy_clientid(struct nfs_client *clp)
6164{
6165 struct rpc_cred *cred;
6166 int ret = 0;
6167
6168 if (clp->cl_mvops->minor_version < 1)
6169 goto out;
6170 if (clp->cl_exchange_flags == 0)
6171 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04006172 if (clp->cl_preserve_clid)
6173 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04006174 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04006175 ret = nfs4_proc_destroy_clientid(clp, cred);
6176 if (cred)
6177 put_rpccred(cred);
6178 switch (ret) {
6179 case 0:
6180 case -NFS4ERR_STALE_CLIENTID:
6181 clp->cl_exchange_flags = 0;
6182 }
6183out:
6184 return ret;
6185}
6186
Andy Adamson2050f0c2009-04-01 09:22:30 -04006187struct nfs4_get_lease_time_data {
6188 struct nfs4_get_lease_time_args *args;
6189 struct nfs4_get_lease_time_res *res;
6190 struct nfs_client *clp;
6191};
6192
6193static void nfs4_get_lease_time_prepare(struct rpc_task *task,
6194 void *calldata)
6195{
Andy Adamson2050f0c2009-04-01 09:22:30 -04006196 struct nfs4_get_lease_time_data *data =
6197 (struct nfs4_get_lease_time_data *)calldata;
6198
6199 dprintk("--> %s\n", __func__);
6200 /* just setup sequence, do not trigger session recovery
6201 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006202 nfs41_setup_sequence(data->clp->cl_session,
6203 &data->args->la_seq_args,
6204 &data->res->lr_seq_res,
6205 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006206 dprintk("<-- %s\n", __func__);
6207}
6208
6209/*
6210 * Called from nfs4_state_manager thread for session setup, so don't recover
6211 * from sequence operation or clientid errors.
6212 */
6213static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
6214{
6215 struct nfs4_get_lease_time_data *data =
6216 (struct nfs4_get_lease_time_data *)calldata;
6217
6218 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006219 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
6220 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04006221 switch (task->tk_status) {
6222 case -NFS4ERR_DELAY:
6223 case -NFS4ERR_GRACE:
6224 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
6225 rpc_delay(task, NFS4_POLL_RETRY_MIN);
6226 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006227 /* fall through */
6228 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006229 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006230 return;
6231 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04006232 dprintk("<-- %s\n", __func__);
6233}
6234
Trond Myklebust17280172012-03-11 13:11:00 -04006235static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04006236 .rpc_call_prepare = nfs4_get_lease_time_prepare,
6237 .rpc_call_done = nfs4_get_lease_time_done,
6238};
6239
6240int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
6241{
6242 struct rpc_task *task;
6243 struct nfs4_get_lease_time_args args;
6244 struct nfs4_get_lease_time_res res = {
6245 .lr_fsinfo = fsinfo,
6246 };
6247 struct nfs4_get_lease_time_data data = {
6248 .args = &args,
6249 .res = &res,
6250 .clp = clp,
6251 };
6252 struct rpc_message msg = {
6253 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
6254 .rpc_argp = &args,
6255 .rpc_resp = &res,
6256 };
6257 struct rpc_task_setup task_setup = {
6258 .rpc_client = clp->cl_rpcclient,
6259 .rpc_message = &msg,
6260 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006261 .callback_data = &data,
6262 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04006263 };
6264 int status;
6265
Chuck Levera9c92d62013-08-09 12:48:18 -04006266 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006267 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006268 dprintk("--> %s\n", __func__);
6269 task = rpc_run_task(&task_setup);
6270
6271 if (IS_ERR(task))
6272 status = PTR_ERR(task);
6273 else {
6274 status = task->tk_status;
6275 rpc_put_task(task);
6276 }
6277 dprintk("<-- %s return %d\n", __func__, status);
6278
6279 return status;
6280}
6281
Andy Adamsonfc931582009-04-01 09:22:31 -04006282/*
6283 * Initialize the values to be used by the client in CREATE_SESSION
6284 * If nfs4_init_session set the fore channel request and response sizes,
6285 * use them.
6286 *
6287 * Set the back channel max_resp_sz_cached to zero to force the client to
6288 * always set csa_cachethis to FALSE because the current implementation
6289 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
6290 */
6291static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
6292{
Andy Adamson18aad3d2013-06-26 12:21:49 -04006293 unsigned int max_rqst_sz, max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006294
Andy Adamson18aad3d2013-06-26 12:21:49 -04006295 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
6296 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
6297
Andy Adamsonfc931582009-04-01 09:22:31 -04006298 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04006299 args->fc_attrs.max_rqst_sz = max_rqst_sz;
6300 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006301 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05006302 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04006303
6304 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05006305 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04006306 __func__,
6307 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05006308 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04006309
6310 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04006311 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
6312 args->bc_attrs.max_resp_sz = PAGE_SIZE;
6313 args->bc_attrs.max_resp_sz_cached = 0;
6314 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
6315 args->bc_attrs.max_reqs = 1;
6316
6317 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
6318 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
6319 __func__,
6320 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
6321 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
6322 args->bc_attrs.max_reqs);
6323}
6324
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006325static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04006326{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006327 struct nfs4_channel_attrs *sent = &args->fc_attrs;
6328 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
6329
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006330 if (rcvd->max_resp_sz > sent->max_resp_sz)
6331 return -EINVAL;
6332 /*
6333 * Our requested max_ops is the minimum we need; we're not
6334 * prepared to break up compounds into smaller pieces than that.
6335 * So, no point even trying to continue if the server won't
6336 * cooperate:
6337 */
6338 if (rcvd->max_ops < sent->max_ops)
6339 return -EINVAL;
6340 if (rcvd->max_reqs == 0)
6341 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006342 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
6343 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006344 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04006345}
6346
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006347static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
6348{
6349 struct nfs4_channel_attrs *sent = &args->bc_attrs;
6350 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04006351
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006352 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
6353 return -EINVAL;
6354 if (rcvd->max_resp_sz < sent->max_resp_sz)
6355 return -EINVAL;
6356 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
6357 return -EINVAL;
6358 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006359 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006360 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006361 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006362 return -EINVAL;
6363 return 0;
6364}
Andy Adamson8d353012009-04-01 09:22:32 -04006365
Andy Adamson8d353012009-04-01 09:22:32 -04006366static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
6367 struct nfs4_session *session)
6368{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006369 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04006370
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006371 ret = nfs4_verify_fore_channel_attrs(args, session);
6372 if (ret)
6373 return ret;
6374 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04006375}
6376
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006377static int _nfs4_proc_create_session(struct nfs_client *clp,
6378 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04006379{
6380 struct nfs4_session *session = clp->cl_session;
6381 struct nfs41_create_session_args args = {
6382 .client = clp,
6383 .cb_program = NFS4_CALLBACK,
6384 };
6385 struct nfs41_create_session_res res = {
6386 .client = clp,
6387 };
6388 struct rpc_message msg = {
6389 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
6390 .rpc_argp = &args,
6391 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006392 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04006393 };
6394 int status;
6395
6396 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04006397 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04006398
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006399 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006400 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04006401
Trond Myklebust43095d32012-11-20 11:13:12 -05006402 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04006403 /* Verify the session's negotiated channel_attrs values */
6404 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04006405 /* Increment the clientid slot sequence id */
6406 clp->cl_seqid++;
6407 }
6408
6409 return status;
6410}
6411
6412/*
6413 * Issues a CREATE_SESSION operation to the server.
6414 * It is the responsibility of the caller to verify the session is
6415 * expired before calling this routine.
6416 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006417int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04006418{
6419 int status;
6420 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04006421 struct nfs4_session *session = clp->cl_session;
6422
6423 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
6424
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006425 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04006426 if (status)
6427 goto out;
6428
Andy Adamsonaacd5532011-11-09 13:58:21 -05006429 /* Init or reset the session slot tables */
6430 status = nfs4_setup_session_slot_tables(session);
6431 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04006432 if (status)
6433 goto out;
6434
6435 ptr = (unsigned *)&session->sess_id.data[0];
6436 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
6437 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04006438out:
6439 dprintk("<-- %s\n", __func__);
6440 return status;
6441}
6442
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006443/*
6444 * Issue the over-the-wire RPC DESTROY_SESSION.
6445 * The caller must serialize access to this routine.
6446 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006447int nfs4_proc_destroy_session(struct nfs4_session *session,
6448 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006449{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006450 struct rpc_message msg = {
6451 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
6452 .rpc_argp = session,
6453 .rpc_cred = cred,
6454 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006455 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006456
6457 dprintk("--> nfs4_proc_destroy_session\n");
6458
6459 /* session is still being setup */
6460 if (session->clp->cl_cons_state != NFS_CS_READY)
6461 return status;
6462
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006463 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006464 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006465
6466 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04006467 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006468 "Session has been destroyed regardless...\n", status);
6469
6470 dprintk("<-- nfs4_proc_destroy_session\n");
6471 return status;
6472}
6473
Trond Myklebust7b38c362012-05-23 13:23:31 -04006474/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006475 * Renew the cl_session lease.
6476 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006477struct nfs4_sequence_data {
6478 struct nfs_client *clp;
6479 struct nfs4_sequence_args args;
6480 struct nfs4_sequence_res res;
6481};
6482
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006483static void nfs41_sequence_release(void *data)
6484{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006485 struct nfs4_sequence_data *calldata = data;
6486 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006487
Alexandros Batsakis71358402010-02-05 03:45:05 -08006488 if (atomic_read(&clp->cl_count) > 1)
6489 nfs4_schedule_state_renewal(clp);
6490 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006491 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006492}
6493
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006494static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6495{
6496 switch(task->tk_status) {
6497 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006498 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6499 return -EAGAIN;
6500 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006501 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006502 }
6503 return 0;
6504}
6505
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006506static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006507{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006508 struct nfs4_sequence_data *calldata = data;
6509 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006510
Trond Myklebust14516c32010-07-31 14:29:06 -04006511 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6512 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006513
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006514 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006515 if (task->tk_status < 0) {
6516 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006517 if (atomic_read(&clp->cl_count) == 1)
6518 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006519
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006520 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6521 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006522 return;
6523 }
6524 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006525 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006526out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006527 dprintk("<-- %s\n", __func__);
6528}
6529
6530static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6531{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006532 struct nfs4_sequence_data *calldata = data;
6533 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006534 struct nfs4_sequence_args *args;
6535 struct nfs4_sequence_res *res;
6536
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006537 args = task->tk_msg.rpc_argp;
6538 res = task->tk_msg.rpc_resp;
6539
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006540 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006541}
6542
6543static const struct rpc_call_ops nfs41_sequence_ops = {
6544 .rpc_call_done = nfs41_sequence_call_done,
6545 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006546 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006547};
6548
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006549static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
6550 struct rpc_cred *cred,
6551 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006552{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006553 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006554 struct rpc_message msg = {
6555 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6556 .rpc_cred = cred,
6557 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006558 struct rpc_task_setup task_setup_data = {
6559 .rpc_client = clp->cl_rpcclient,
6560 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006561 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04006562 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006563 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006564
Alexandros Batsakis71358402010-02-05 03:45:05 -08006565 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006566 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006567 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006568 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006569 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006570 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006571 }
Chuck Levera9c92d62013-08-09 12:48:18 -04006572 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006573 if (is_privileged)
6574 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006575 msg.rpc_argp = &calldata->args;
6576 msg.rpc_resp = &calldata->res;
6577 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006578 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006579
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006580 return rpc_run_task(&task_setup_data);
6581}
6582
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006583static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006584{
6585 struct rpc_task *task;
6586 int ret = 0;
6587
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006588 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6589 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006590 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006591 if (IS_ERR(task))
6592 ret = PTR_ERR(task);
6593 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006594 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006595 dprintk("<-- %s status=%d\n", __func__, ret);
6596 return ret;
6597}
6598
6599static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6600{
6601 struct rpc_task *task;
6602 int ret;
6603
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006604 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006605 if (IS_ERR(task)) {
6606 ret = PTR_ERR(task);
6607 goto out;
6608 }
6609 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006610 if (!ret) {
6611 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6612
6613 if (task->tk_status == 0)
6614 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006615 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006616 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006617 rpc_put_task(task);
6618out:
6619 dprintk("<-- %s status=%d\n", __func__, ret);
6620 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006621}
6622
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006623struct nfs4_reclaim_complete_data {
6624 struct nfs_client *clp;
6625 struct nfs41_reclaim_complete_args arg;
6626 struct nfs41_reclaim_complete_res res;
6627};
6628
6629static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6630{
6631 struct nfs4_reclaim_complete_data *calldata = data;
6632
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006633 nfs41_setup_sequence(calldata->clp->cl_session,
6634 &calldata->arg.seq_args,
6635 &calldata->res.seq_res,
6636 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006637}
6638
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006639static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6640{
6641 switch(task->tk_status) {
6642 case 0:
6643 case -NFS4ERR_COMPLETE_ALREADY:
6644 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6645 break;
6646 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006647 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006648 /* fall through */
6649 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006650 return -EAGAIN;
6651 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006652 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006653 }
6654 return 0;
6655}
6656
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006657static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6658{
6659 struct nfs4_reclaim_complete_data *calldata = data;
6660 struct nfs_client *clp = calldata->clp;
6661 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6662
6663 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006664 if (!nfs41_sequence_done(task, res))
6665 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006666
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006667 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006668 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6669 rpc_restart_call_prepare(task);
6670 return;
6671 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006672 dprintk("<-- %s\n", __func__);
6673}
6674
6675static void nfs4_free_reclaim_complete_data(void *data)
6676{
6677 struct nfs4_reclaim_complete_data *calldata = data;
6678
6679 kfree(calldata);
6680}
6681
6682static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6683 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6684 .rpc_call_done = nfs4_reclaim_complete_done,
6685 .rpc_release = nfs4_free_reclaim_complete_data,
6686};
6687
6688/*
6689 * Issue a global reclaim complete.
6690 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04006691static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
6692 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006693{
6694 struct nfs4_reclaim_complete_data *calldata;
6695 struct rpc_task *task;
6696 struct rpc_message msg = {
6697 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04006698 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006699 };
6700 struct rpc_task_setup task_setup_data = {
6701 .rpc_client = clp->cl_rpcclient,
6702 .rpc_message = &msg,
6703 .callback_ops = &nfs4_reclaim_complete_call_ops,
6704 .flags = RPC_TASK_ASYNC,
6705 };
6706 int status = -ENOMEM;
6707
6708 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006709 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006710 if (calldata == NULL)
6711 goto out;
6712 calldata->clp = clp;
6713 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006714
Chuck Levera9c92d62013-08-09 12:48:18 -04006715 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006716 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006717 msg.rpc_argp = &calldata->arg;
6718 msg.rpc_resp = &calldata->res;
6719 task_setup_data.callback_data = calldata;
6720 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006721 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006722 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006723 goto out;
6724 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006725 status = nfs4_wait_for_completion_rpc_task(task);
6726 if (status == 0)
6727 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006728 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006729 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006730out:
6731 dprintk("<-- %s status=%d\n", __func__, status);
6732 return status;
6733}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006734
6735static void
6736nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6737{
6738 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006739 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006740 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006741
6742 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006743 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6744 * right now covering the LAYOUTGET we are about to send.
6745 * However, that is not so catastrophic, and there seems
6746 * to be no way to prevent it completely.
6747 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006748 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006749 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006750 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006751 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6752 NFS_I(lgp->args.inode)->layout,
6753 lgp->args.ctx->state)) {
6754 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00006755 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006756}
6757
6758static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6759{
6760 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006761 struct inode *inode = lgp->args.inode;
6762 struct nfs_server *server = NFS_SERVER(inode);
6763 struct pnfs_layout_hdr *lo;
6764 struct nfs4_state *state = NULL;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006765 unsigned long timeo, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006766
6767 dprintk("--> %s\n", __func__);
6768
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006769 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006770 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006771
6772 switch (task->tk_status) {
6773 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006774 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006775 case -NFS4ERR_LAYOUTTRYLATER:
6776 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05006777 timeo = rpc_get_timeout(task->tk_client);
6778 giveup = lgp->args.timestamp + timeo;
6779 if (time_after(giveup, jiffies))
6780 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006781 break;
6782 case -NFS4ERR_EXPIRED:
6783 case -NFS4ERR_BAD_STATEID:
6784 spin_lock(&inode->i_lock);
6785 lo = NFS_I(inode)->layout;
6786 if (!lo || list_empty(&lo->plh_segs)) {
6787 spin_unlock(&inode->i_lock);
6788 /* If the open stateid was bad, then recover it. */
6789 state = lgp->args.ctx->state;
6790 } else {
6791 LIST_HEAD(head);
6792
6793 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6794 spin_unlock(&inode->i_lock);
6795 /* Mark the bad layout state as invalid, then
6796 * retry using the open stateid. */
6797 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006798 }
6799 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006800 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6801 rpc_restart_call_prepare(task);
6802out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006803 dprintk("<-- %s\n", __func__);
6804}
6805
Idan Kedar85541162012-08-02 11:47:10 +03006806static size_t max_response_pages(struct nfs_server *server)
6807{
6808 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6809 return nfs_page_array_len(0, max_resp_sz);
6810}
6811
6812static void nfs4_free_pages(struct page **pages, size_t size)
6813{
6814 int i;
6815
6816 if (!pages)
6817 return;
6818
6819 for (i = 0; i < size; i++) {
6820 if (!pages[i])
6821 break;
6822 __free_page(pages[i]);
6823 }
6824 kfree(pages);
6825}
6826
6827static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6828{
6829 struct page **pages;
6830 int i;
6831
6832 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6833 if (!pages) {
6834 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6835 return NULL;
6836 }
6837
6838 for (i = 0; i < size; i++) {
6839 pages[i] = alloc_page(gfp_flags);
6840 if (!pages[i]) {
6841 dprintk("%s: failed to allocate page\n", __func__);
6842 nfs4_free_pages(pages, size);
6843 return NULL;
6844 }
6845 }
6846
6847 return pages;
6848}
6849
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006850static void nfs4_layoutget_release(void *calldata)
6851{
6852 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006853 struct inode *inode = lgp->args.inode;
6854 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006855 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006856
6857 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006858 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006859 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006860 put_nfs_open_context(lgp->args.ctx);
6861 kfree(calldata);
6862 dprintk("<-- %s\n", __func__);
6863}
6864
6865static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6866 .rpc_call_prepare = nfs4_layoutget_prepare,
6867 .rpc_call_done = nfs4_layoutget_done,
6868 .rpc_release = nfs4_layoutget_release,
6869};
6870
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006871struct pnfs_layout_segment *
6872nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006873{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006874 struct inode *inode = lgp->args.inode;
6875 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006876 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006877 struct rpc_task *task;
6878 struct rpc_message msg = {
6879 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6880 .rpc_argp = &lgp->args,
6881 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04006882 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006883 };
6884 struct rpc_task_setup task_setup_data = {
6885 .rpc_client = server->client,
6886 .rpc_message = &msg,
6887 .callback_ops = &nfs4_layoutget_call_ops,
6888 .callback_data = lgp,
6889 .flags = RPC_TASK_ASYNC,
6890 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006891 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006892 int status = 0;
6893
6894 dprintk("--> %s\n", __func__);
6895
Idan Kedar85541162012-08-02 11:47:10 +03006896 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6897 if (!lgp->args.layout.pages) {
6898 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006899 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006900 }
6901 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006902 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03006903
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006904 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006905 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04006906 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006907
6908 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
6909 pnfs_get_layout_hdr(NFS_I(inode)->layout);
6910
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006911 task = rpc_run_task(&task_setup_data);
6912 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006913 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006914 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006915 if (status == 0)
6916 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04006917 trace_nfs4_layoutget(lgp->args.ctx,
6918 &lgp->args.range,
6919 &lgp->res.range,
6920 status);
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05006921 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
6922 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006923 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006924 rpc_put_task(task);
6925 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006926 if (status)
6927 return ERR_PTR(status);
6928 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006929}
6930
Benny Halevycbe82602011-05-22 19:52:37 +03006931static void
6932nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6933{
6934 struct nfs4_layoutreturn *lrp = calldata;
6935
6936 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006937 nfs41_setup_sequence(lrp->clp->cl_session,
6938 &lrp->args.seq_args,
6939 &lrp->res.seq_res,
6940 task);
Benny Halevycbe82602011-05-22 19:52:37 +03006941}
6942
6943static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6944{
6945 struct nfs4_layoutreturn *lrp = calldata;
6946 struct nfs_server *server;
6947
6948 dprintk("--> %s\n", __func__);
6949
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006950 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03006951 return;
6952
6953 server = NFS_SERVER(lrp->args.inode);
6954 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006955 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006956 return;
6957 }
Benny Halevycbe82602011-05-22 19:52:37 +03006958 dprintk("<-- %s\n", __func__);
6959}
6960
6961static void nfs4_layoutreturn_release(void *calldata)
6962{
6963 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006964 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006965
6966 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006967 spin_lock(&lo->plh_inode->i_lock);
6968 if (lrp->res.lrs_present)
6969 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6970 lo->plh_block_lgets--;
6971 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006972 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006973 kfree(calldata);
6974 dprintk("<-- %s\n", __func__);
6975}
6976
6977static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6978 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6979 .rpc_call_done = nfs4_layoutreturn_done,
6980 .rpc_release = nfs4_layoutreturn_release,
6981};
6982
6983int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6984{
6985 struct rpc_task *task;
6986 struct rpc_message msg = {
6987 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6988 .rpc_argp = &lrp->args,
6989 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04006990 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03006991 };
6992 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04006993 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03006994 .rpc_message = &msg,
6995 .callback_ops = &nfs4_layoutreturn_call_ops,
6996 .callback_data = lrp,
6997 };
6998 int status;
6999
7000 dprintk("--> %s\n", __func__);
Chuck Levera9c92d62013-08-09 12:48:18 -04007001 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03007002 task = rpc_run_task(&task_setup_data);
7003 if (IS_ERR(task))
7004 return PTR_ERR(task);
7005 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007006 trace_nfs4_layoutreturn(lrp->args.inode, status);
Benny Halevycbe82602011-05-22 19:52:37 +03007007 dprintk("<-- %s status=%d\n", __func__, status);
7008 rpc_put_task(task);
7009 return status;
7010}
7011
Andy Adamson7f11d8d2011-07-30 20:52:35 -04007012/*
7013 * Retrieve the list of Data Server devices from the MDS.
7014 */
7015static int _nfs4_getdevicelist(struct nfs_server *server,
7016 const struct nfs_fh *fh,
7017 struct pnfs_devicelist *devlist)
7018{
7019 struct nfs4_getdevicelist_args args = {
7020 .fh = fh,
7021 .layoutclass = server->pnfs_curr_ld->id,
7022 };
7023 struct nfs4_getdevicelist_res res = {
7024 .devlist = devlist,
7025 };
7026 struct rpc_message msg = {
7027 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
7028 .rpc_argp = &args,
7029 .rpc_resp = &res,
7030 };
7031 int status;
7032
7033 dprintk("--> %s\n", __func__);
7034 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
7035 &res.seq_res, 0);
7036 dprintk("<-- %s status=%d\n", __func__, status);
7037 return status;
7038}
7039
7040int nfs4_proc_getdevicelist(struct nfs_server *server,
7041 const struct nfs_fh *fh,
7042 struct pnfs_devicelist *devlist)
7043{
7044 struct nfs4_exception exception = { };
7045 int err;
7046
7047 do {
7048 err = nfs4_handle_exception(server,
7049 _nfs4_getdevicelist(server, fh, devlist),
7050 &exception);
7051 } while (exception.retry);
7052
7053 dprintk("%s: err=%d, num_devs=%u\n", __func__,
7054 err, devlist->num_devs);
7055
7056 return err;
7057}
7058EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
7059
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007060static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007061_nfs4_proc_getdeviceinfo(struct nfs_server *server,
7062 struct pnfs_device *pdev,
7063 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007064{
7065 struct nfs4_getdeviceinfo_args args = {
7066 .pdev = pdev,
7067 };
7068 struct nfs4_getdeviceinfo_res res = {
7069 .pdev = pdev,
7070 };
7071 struct rpc_message msg = {
7072 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
7073 .rpc_argp = &args,
7074 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007075 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007076 };
7077 int status;
7078
7079 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00007080 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007081 dprintk("<-- %s status=%d\n", __func__, status);
7082
7083 return status;
7084}
7085
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007086int nfs4_proc_getdeviceinfo(struct nfs_server *server,
7087 struct pnfs_device *pdev,
7088 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007089{
7090 struct nfs4_exception exception = { };
7091 int err;
7092
7093 do {
7094 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007095 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007096 &exception);
7097 } while (exception.retry);
7098 return err;
7099}
7100EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
7101
Andy Adamson863a3c62011-03-23 13:27:54 +00007102static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
7103{
7104 struct nfs4_layoutcommit_data *data = calldata;
7105 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007106 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00007107
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007108 nfs41_setup_sequence(session,
7109 &data->args.seq_args,
7110 &data->res.seq_res,
7111 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00007112}
7113
7114static void
7115nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
7116{
7117 struct nfs4_layoutcommit_data *data = calldata;
7118 struct nfs_server *server = NFS_SERVER(data->args.inode);
7119
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007120 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00007121 return;
7122
7123 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007124 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
7125 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
7126 case -NFS4ERR_BADLAYOUT: /* no layout */
7127 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00007128 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007129 break;
7130 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00007131 nfs_post_op_update_inode_force_wcc(data->args.inode,
7132 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007133 break;
7134 default:
7135 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
7136 rpc_restart_call_prepare(task);
7137 return;
7138 }
7139 }
Andy Adamson863a3c62011-03-23 13:27:54 +00007140}
7141
7142static void nfs4_layoutcommit_release(void *calldata)
7143{
7144 struct nfs4_layoutcommit_data *data = calldata;
7145
Andy Adamsondb29c082011-07-30 20:52:38 -04007146 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00007147 put_rpccred(data->cred);
7148 kfree(data);
7149}
7150
7151static const struct rpc_call_ops nfs4_layoutcommit_ops = {
7152 .rpc_call_prepare = nfs4_layoutcommit_prepare,
7153 .rpc_call_done = nfs4_layoutcommit_done,
7154 .rpc_release = nfs4_layoutcommit_release,
7155};
7156
7157int
Andy Adamsonef311532011-03-12 02:58:10 -05007158nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00007159{
7160 struct rpc_message msg = {
7161 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
7162 .rpc_argp = &data->args,
7163 .rpc_resp = &data->res,
7164 .rpc_cred = data->cred,
7165 };
7166 struct rpc_task_setup task_setup_data = {
7167 .task = &data->task,
7168 .rpc_client = NFS_CLIENT(data->args.inode),
7169 .rpc_message = &msg,
7170 .callback_ops = &nfs4_layoutcommit_ops,
7171 .callback_data = data,
7172 .flags = RPC_TASK_ASYNC,
7173 };
7174 struct rpc_task *task;
7175 int status = 0;
7176
7177 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
7178 "lbw: %llu inode %lu\n",
7179 data->task.tk_pid, sync,
7180 data->args.lastbytewritten,
7181 data->args.inode->i_ino);
7182
Chuck Levera9c92d62013-08-09 12:48:18 -04007183 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00007184 task = rpc_run_task(&task_setup_data);
7185 if (IS_ERR(task))
7186 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05007187 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00007188 goto out;
7189 status = nfs4_wait_for_completion_rpc_task(task);
7190 if (status != 0)
7191 goto out;
7192 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007193 trace_nfs4_layoutcommit(data->args.inode, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00007194out:
7195 dprintk("%s: status %d\n", __func__, status);
7196 rpc_put_task(task);
7197 return status;
7198}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007199
Andy Adamson97431202013-08-08 10:57:56 -04007200/**
7201 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
7202 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
7203 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007204static int
7205_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7206 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
7207{
7208 struct nfs41_secinfo_no_name_args args = {
7209 .style = SECINFO_STYLE_CURRENT_FH,
7210 };
7211 struct nfs4_secinfo_res res = {
7212 .flavors = flavors,
7213 };
7214 struct rpc_message msg = {
7215 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
7216 .rpc_argp = &args,
7217 .rpc_resp = &res,
7218 };
Andy Adamson97431202013-08-08 10:57:56 -04007219 return nfs4_call_sync(server->nfs_client->cl_rpcclient, server, &msg,
7220 &args.seq_args, &res.seq_res, 0);
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007221}
7222
7223static int
7224nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7225 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
7226{
7227 struct nfs4_exception exception = { };
7228 int err;
7229 do {
7230 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
7231 switch (err) {
7232 case 0:
7233 case -NFS4ERR_WRONGSEC:
7234 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007235 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007236 default:
7237 err = nfs4_handle_exception(server, err, &exception);
7238 }
7239 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007240out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007241 return err;
7242}
7243
7244static int
7245nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
7246 struct nfs_fsinfo *info)
7247{
7248 int err;
7249 struct page *page;
7250 rpc_authflavor_t flavor;
7251 struct nfs4_secinfo_flavors *flavors;
7252
7253 page = alloc_page(GFP_KERNEL);
7254 if (!page) {
7255 err = -ENOMEM;
7256 goto out;
7257 }
7258
7259 flavors = page_address(page);
7260 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
7261
7262 /*
7263 * Fall back on "guess and check" method if
7264 * the server doesn't support SECINFO_NO_NAME
7265 */
7266 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
7267 err = nfs4_find_root_sec(server, fhandle, info);
7268 goto out_freepage;
7269 }
7270 if (err)
7271 goto out_freepage;
7272
7273 flavor = nfs_find_best_sec(flavors);
7274 if (err == 0)
7275 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
7276
7277out_freepage:
7278 put_page(page);
7279 if (err == -EACCES)
7280 return -EPERM;
7281out:
7282 return err;
7283}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007284
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007285static int _nfs41_test_stateid(struct nfs_server *server,
7286 nfs4_stateid *stateid,
7287 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04007288{
7289 int status;
7290 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007291 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04007292 };
7293 struct nfs41_test_stateid_res res;
7294 struct rpc_message msg = {
7295 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
7296 .rpc_argp = &args,
7297 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007298 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04007299 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007300
Chuck Lever38527b12012-07-11 16:30:23 -04007301 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04007302 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007303 nfs4_set_sequence_privileged(&args.seq_args);
7304 status = nfs4_call_sync_sequence(server->client, server, &msg,
7305 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04007306 if (status != NFS_OK) {
7307 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04007308 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04007309 }
7310 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04007311 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04007312}
7313
Chuck Lever38527b12012-07-11 16:30:23 -04007314/**
7315 * nfs41_test_stateid - perform a TEST_STATEID operation
7316 *
7317 * @server: server / transport on which to perform the operation
7318 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007319 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04007320 *
7321 * Returns NFS_OK if the server recognizes that "stateid" is valid.
7322 * Otherwise a negative NFS4ERR value is returned if the operation
7323 * failed or the state ID is not currently valid.
7324 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007325static int nfs41_test_stateid(struct nfs_server *server,
7326 nfs4_stateid *stateid,
7327 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04007328{
7329 struct nfs4_exception exception = { };
7330 int err;
7331 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007332 err = _nfs41_test_stateid(server, stateid, cred);
Chuck Lever377e5072012-07-11 16:29:45 -04007333 if (err != -NFS4ERR_DELAY)
7334 break;
7335 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04007336 } while (exception.retry);
7337 return err;
7338}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007339
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007340struct nfs_free_stateid_data {
7341 struct nfs_server *server;
7342 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007343 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007344};
7345
7346static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
7347{
7348 struct nfs_free_stateid_data *data = calldata;
7349 nfs41_setup_sequence(nfs4_get_session(data->server),
7350 &data->args.seq_args,
7351 &data->res.seq_res,
7352 task);
7353}
7354
7355static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
7356{
7357 struct nfs_free_stateid_data *data = calldata;
7358
7359 nfs41_sequence_done(task, &data->res.seq_res);
7360
7361 switch (task->tk_status) {
7362 case -NFS4ERR_DELAY:
7363 if (nfs4_async_handle_error(task, data->server, NULL) == -EAGAIN)
7364 rpc_restart_call_prepare(task);
7365 }
7366}
7367
7368static void nfs41_free_stateid_release(void *calldata)
7369{
7370 kfree(calldata);
7371}
7372
Trond Myklebust17f26b12013-08-21 15:48:42 -04007373static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007374 .rpc_call_prepare = nfs41_free_stateid_prepare,
7375 .rpc_call_done = nfs41_free_stateid_done,
7376 .rpc_release = nfs41_free_stateid_release,
7377};
7378
7379static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
7380 nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007381 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007382 bool privileged)
7383{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007384 struct rpc_message msg = {
7385 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007386 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007387 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007388 struct rpc_task_setup task_setup = {
7389 .rpc_client = server->client,
7390 .rpc_message = &msg,
7391 .callback_ops = &nfs41_free_stateid_ops,
7392 .flags = RPC_TASK_ASYNC,
7393 };
7394 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007395
Chuck Lever38527b12012-07-11 16:30:23 -04007396 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007397 data = kmalloc(sizeof(*data), GFP_NOFS);
7398 if (!data)
7399 return ERR_PTR(-ENOMEM);
7400 data->server = server;
7401 nfs4_stateid_copy(&data->args.stateid, stateid);
7402
7403 task_setup.callback_data = data;
7404
7405 msg.rpc_argp = &data->args;
7406 msg.rpc_resp = &data->res;
Chuck Levera9c92d62013-08-09 12:48:18 -04007407 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007408 if (privileged)
7409 nfs4_set_sequence_privileged(&data->args.seq_args);
7410
7411 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007412}
7413
Chuck Lever38527b12012-07-11 16:30:23 -04007414/**
7415 * nfs41_free_stateid - perform a FREE_STATEID operation
7416 *
7417 * @server: server / transport on which to perform the operation
7418 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007419 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04007420 *
7421 * Returns NFS_OK if the server freed "stateid". Otherwise a
7422 * negative NFS4ERR value is returned.
7423 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007424static int nfs41_free_stateid(struct nfs_server *server,
7425 nfs4_stateid *stateid,
7426 struct rpc_cred *cred)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007427{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007428 struct rpc_task *task;
7429 int ret;
7430
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007431 task = _nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007432 if (IS_ERR(task))
7433 return PTR_ERR(task);
7434 ret = rpc_wait_for_completion_task(task);
7435 if (!ret)
7436 ret = task->tk_status;
7437 rpc_put_task(task);
7438 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007439}
Trond Myklebust36281ca2012-03-04 18:13:56 -05007440
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007441static int nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
7442{
7443 struct rpc_task *task;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007444 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007445
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007446 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007447 nfs4_free_lock_state(server, lsp);
7448 if (IS_ERR(task))
7449 return PTR_ERR(task);
7450 rpc_put_task(task);
7451 return 0;
7452}
7453
Trond Myklebust36281ca2012-03-04 18:13:56 -05007454static bool nfs41_match_stateid(const nfs4_stateid *s1,
7455 const nfs4_stateid *s2)
7456{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007457 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007458 return false;
7459
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007460 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007461 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007462 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007463 return true;
7464
7465 return false;
7466}
7467
Andy Adamson557134a2009-04-01 09:21:53 -04007468#endif /* CONFIG_NFS_V4_1 */
7469
Trond Myklebust36281ca2012-03-04 18:13:56 -05007470static bool nfs4_match_stateid(const nfs4_stateid *s1,
7471 const nfs4_stateid *s2)
7472{
Trond Myklebustf597c532012-03-04 18:13:56 -05007473 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05007474}
7475
7476
Trond Myklebust17280172012-03-11 13:11:00 -04007477static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007478 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007479 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007480 .recover_open = nfs4_open_reclaim,
7481 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04007482 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04007483 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007484};
7485
Andy Adamson591d71c2009-04-01 09:22:47 -04007486#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007487static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007488 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
7489 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
7490 .recover_open = nfs4_open_reclaim,
7491 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05007492 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007493 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04007494 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04007495};
7496#endif /* CONFIG_NFS_V4_1 */
7497
Trond Myklebust17280172012-03-11 13:11:00 -04007498static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007499 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007500 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007501 .recover_open = nfs4_open_expired,
7502 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04007503 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007504};
7505
Andy Adamson591d71c2009-04-01 09:22:47 -04007506#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007507static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007508 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
7509 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007510 .recover_open = nfs41_open_expired,
7511 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05007512 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04007513};
7514#endif /* CONFIG_NFS_V4_1 */
7515
Trond Myklebust17280172012-03-11 13:11:00 -04007516static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007517 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04007518 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007519 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04007520};
7521
7522#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007523static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007524 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04007525 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007526 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04007527};
7528#endif
7529
Trond Myklebust97dc1352010-06-16 09:52:26 -04007530static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
7531 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04007532 .init_caps = NFS_CAP_READDIRPLUS
7533 | NFS_CAP_ATOMIC_OPEN
7534 | NFS_CAP_CHANGE_ATTR
7535 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04007536 .init_client = nfs40_init_client,
7537 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007538 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007539 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007540 .free_lock_state = nfs4_release_lockowner,
Chuck Lever9915ea72013-08-09 12:48:27 -04007541 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007542 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
7543 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
7544 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007545};
7546
7547#if defined(CONFIG_NFS_V4_1)
7548static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
7549 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04007550 .init_caps = NFS_CAP_READDIRPLUS
7551 | NFS_CAP_ATOMIC_OPEN
7552 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04007553 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04007554 | NFS_CAP_STATEID_NFSV41
7555 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04007556 .init_client = nfs41_init_client,
7557 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007558 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007559 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007560 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04007561 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007562 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7563 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7564 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007565};
7566#endif
7567
Steve Dickson42c2c422013-05-22 12:50:38 -04007568#if defined(CONFIG_NFS_V4_2)
7569static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
7570 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04007571 .init_caps = NFS_CAP_READDIRPLUS
7572 | NFS_CAP_ATOMIC_OPEN
7573 | NFS_CAP_CHANGE_ATTR
7574 | NFS_CAP_POSIX_LOCK
7575 | NFS_CAP_STATEID_NFSV41
7576 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04007577 .init_client = nfs41_init_client,
7578 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04007579 .match_stateid = nfs41_match_stateid,
7580 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04007581 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04007582 .call_sync_ops = &nfs41_call_sync_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04007583 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7584 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7585 .state_renewal_ops = &nfs41_state_renewal_ops,
7586};
7587#endif
7588
Trond Myklebust97dc1352010-06-16 09:52:26 -04007589const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
7590 [0] = &nfs_v4_0_minor_ops,
7591#if defined(CONFIG_NFS_V4_1)
7592 [1] = &nfs_v4_1_minor_ops,
7593#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04007594#if defined(CONFIG_NFS_V4_2)
7595 [2] = &nfs_v4_2_minor_ops,
7596#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04007597};
7598
Trond Myklebust17f26b12013-08-21 15:48:42 -04007599static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04007600 .create = nfs_create,
7601 .lookup = nfs_lookup,
7602 .atomic_open = nfs_atomic_open,
7603 .link = nfs_link,
7604 .unlink = nfs_unlink,
7605 .symlink = nfs_symlink,
7606 .mkdir = nfs_mkdir,
7607 .rmdir = nfs_rmdir,
7608 .mknod = nfs_mknod,
7609 .rename = nfs_rename,
7610 .permission = nfs_permission,
7611 .getattr = nfs_getattr,
7612 .setattr = nfs_setattr,
7613 .getxattr = generic_getxattr,
7614 .setxattr = generic_setxattr,
7615 .listxattr = generic_listxattr,
7616 .removexattr = generic_removexattr,
7617};
7618
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08007619static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007620 .permission = nfs_permission,
7621 .getattr = nfs_getattr,
7622 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007623 .getxattr = generic_getxattr,
7624 .setxattr = generic_setxattr,
7625 .listxattr = generic_listxattr,
7626 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007627};
7628
David Howells509de812006-08-22 20:06:11 -04007629const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007630 .version = 4, /* protocol version */
7631 .dentry_ops = &nfs4_dentry_operations,
7632 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007633 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04007634 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007635 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04007636 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04007637 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007638 .getattr = nfs4_proc_getattr,
7639 .setattr = nfs4_proc_setattr,
7640 .lookup = nfs4_proc_lookup,
7641 .access = nfs4_proc_access,
7642 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007643 .create = nfs4_proc_create,
7644 .remove = nfs4_proc_remove,
7645 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04007646 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007647 .unlink_done = nfs4_proc_unlink_done,
7648 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04007649 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04007650 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04007651 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007652 .link = nfs4_proc_link,
7653 .symlink = nfs4_proc_symlink,
7654 .mkdir = nfs4_proc_mkdir,
7655 .rmdir = nfs4_proc_remove,
7656 .readdir = nfs4_proc_readdir,
7657 .mknod = nfs4_proc_mknod,
7658 .statfs = nfs4_proc_statfs,
7659 .fsinfo = nfs4_proc_fsinfo,
7660 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04007661 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007662 .decode_dirent = nfs4_decode_dirent,
7663 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04007664 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04007665 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05007666 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007667 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04007668 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04007669 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007670 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007671 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04007672 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007673 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007674 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00007675 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04007676 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04007677 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04007678 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04007679 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04007680 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00007681 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04007682 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04007683 .create_server = nfs4_create_server,
7684 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007685};
7686
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007687static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7688 .prefix = XATTR_NAME_NFSV4_ACL,
7689 .list = nfs4_xattr_list_nfs4_acl,
7690 .get = nfs4_xattr_get_nfs4_acl,
7691 .set = nfs4_xattr_set_nfs4_acl,
7692};
7693
7694const struct xattr_handler *nfs4_xattr_handlers[] = {
7695 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04007696#ifdef CONFIG_NFS_V4_SECURITY_LABEL
7697 &nfs4_xattr_nfs4_label_handler,
7698#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007699 NULL
7700};
7701
Linus Torvalds1da177e2005-04-16 15:20:36 -07007702/*
7703 * Local variables:
7704 * c-basic-offset: 8
7705 * End:
7706 */