blob: a745f7c41758ae796b4c793e2275fa945c15739e [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
438
Trond Myklebust452e9352010-07-31 14:29:06 -0400439static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 spin_lock(&clp->cl_lock);
442 if (time_before(clp->cl_last_renewal,timestamp))
443 clp->cl_last_renewal = timestamp;
444 spin_unlock(&clp->cl_lock);
445}
446
Trond Myklebust452e9352010-07-31 14:29:06 -0400447static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
448{
449 do_renew_lease(server->nfs_client, timestamp);
450}
451
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400452#if defined(CONFIG_NFS_V4_1)
453
Trond Myklebustd185a332010-06-16 09:52:25 -0400454static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400455{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500456 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400457 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500458 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400459
Benny Halevydfb4f3092010-09-24 09:17:01 -0400460 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400461 /* just wake up the next guy waiting since
462 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500463 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500464 return;
Andy Adamson13615872009-04-01 09:22:17 -0400465 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500466 tbl = res->sr_slot->table;
467 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500468
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500469 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500470 /* Be nice to the server: try to ensure that the last transmitted
471 * value for highest_user_slotid <= target_highest_slotid
472 */
473 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
474 send_new_highest_used_slotid = true;
475
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500476 if (nfs41_wake_and_assign_slot(tbl, res->sr_slot)) {
477 send_new_highest_used_slotid = false;
478 goto out_unlock;
479 }
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500480 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500481
482 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
483 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500484out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500485 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400486 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500487 if (send_new_highest_used_slotid)
488 nfs41_server_notify_highest_slotid_update(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400489}
490
Trond Myklebust14516c32010-07-31 14:29:06 -0400491static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400492{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500493 struct nfs4_session *session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500494 struct nfs4_slot *slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400495 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500496 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500497 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400498
Bryan Schumaker468f8612011-04-18 15:57:32 -0400499 /* don't increment the sequence number if the task wasn't sent */
500 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400501 goto out;
502
Trond Myklebust933602e2012-11-16 12:12:38 -0500503 slot = res->sr_slot;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500504 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500505
Trond Myklebustac20d162012-12-15 15:36:07 -0500506 if (slot->interrupted) {
507 slot->interrupted = 0;
508 interrupted = true;
509 }
510
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400511 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500512 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400513 switch (res->sr_status) {
514 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400515 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500516 ++slot->seq_nr;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500517 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500518 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500519 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500520 if (res->sr_status_flags != 0)
521 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500522 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400523 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500524 case 1:
525 /*
526 * sr_status remains 1 if an RPC level error occurred.
527 * The server may or may not have processed the sequence
528 * operation..
529 * Mark the slot as having hosted an interrupted RPC call.
530 */
531 slot->interrupted = 1;
532 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400533 case -NFS4ERR_DELAY:
534 /* The server detected a resend of the RPC call and
535 * returned NFS4ERR_DELAY as per Section 2.10.6.2
536 * of RFC5661.
537 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500538 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400539 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500540 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500541 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400542 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500543 case -NFS4ERR_BADSLOT:
544 /*
545 * The slot id we used was probably retired. Try again
546 * using a different slot id.
547 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500548 goto retry_nowait;
549 case -NFS4ERR_SEQ_MISORDERED:
550 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500551 * Was the last operation on this sequence interrupted?
552 * If so, retry after bumping the sequence number.
553 */
554 if (interrupted) {
555 ++slot->seq_nr;
556 goto retry_nowait;
557 }
558 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500559 * Could this slot have been previously retired?
560 * If so, then the server may be expecting seq_nr = 1!
561 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500562 if (slot->seq_nr != 1) {
563 slot->seq_nr = 1;
564 goto retry_nowait;
565 }
566 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500567 case -NFS4ERR_SEQ_FALSE_RETRY:
568 ++slot->seq_nr;
569 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400570 default:
571 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500572 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400573 }
574out:
575 /* The session may be reset by one of the error handlers. */
576 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400577 nfs41_sequence_free_slot(res);
Trond Myklebust85563072012-12-11 10:31:12 -0500578 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500579retry_nowait:
580 if (rpc_restart_call_prepare(task)) {
581 task->tk_status = 0;
582 ret = 0;
583 }
584 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400585out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400586 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400587 goto out;
588 rpc_delay(task, NFS4_POLL_RETRY_MAX);
589 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400590}
591
Trond Myklebust14516c32010-07-31 14:29:06 -0400592static int nfs4_sequence_done(struct rpc_task *task,
593 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400594{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500595 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400596 return 1;
597 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400598}
599
Trond Myklebust9d12b212012-01-17 22:04:25 -0500600static void nfs41_init_sequence(struct nfs4_sequence_args *args,
601 struct nfs4_sequence_res *res, int cache_reply)
602{
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500603 args->sa_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500604 args->sa_cache_this = 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400605 args->sa_privileged = 0;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500606 if (cache_reply)
607 args->sa_cache_this = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500608 res->sr_slot = NULL;
609}
610
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400611static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
612{
613 args->sa_privileged = 1;
614}
615
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000616int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400617 struct nfs4_sequence_args *args,
618 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400619 struct rpc_task *task)
620{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400621 struct nfs4_slot *slot;
622 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400623
624 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400625 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400626 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400627 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400628
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400629 tbl = &session->fc_slot_table;
630
Trond Myklebust69d206b2012-11-22 13:21:02 -0500631 task->tk_timeout = 0;
632
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400633 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400634 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400635 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500636 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500637 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400638 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400639 }
640
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500641 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500642 if (IS_ERR(slot)) {
643 /* If out of memory, try again in 1/4 second */
644 if (slot == ERR_PTR(-ENOMEM))
645 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400646 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400647 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400648 }
649 spin_unlock(&tbl->slot_tbl_lock);
650
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500651 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400652
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500653 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
654 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400655
Benny Halevydfb4f3092010-09-24 09:17:01 -0400656 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500657 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400658 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400659 /*
660 * sr_status is only set in decode_sequence, and so will remain
661 * set to 1 if an rpc level failure occurs.
662 */
663 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400664 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400665out_success:
666 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400667 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400668out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400669 /* Privileged tasks are queued with top priority */
670 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400671 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
672 NULL, RPC_PRIORITY_PRIVILEGED);
673 else
674 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400675 spin_unlock(&tbl->slot_tbl_lock);
676 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400677}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000678EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400679
Trond Myklebust035168a2010-06-16 09:52:26 -0400680int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400681 struct nfs4_sequence_args *args,
682 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400683 struct rpc_task *task)
684{
Trond Myklebust035168a2010-06-16 09:52:26 -0400685 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400686 int ret = 0;
687
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400688 if (session == NULL) {
689 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400690 goto out;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400691 }
Trond Myklebust035168a2010-06-16 09:52:26 -0400692
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500693 dprintk("--> %s clp %p session %p sr_slot %d\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400694 __func__, session->clp, session, res->sr_slot ?
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500695 res->sr_slot->slot_nr : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400696
Trond Myklebust9d12b212012-01-17 22:04:25 -0500697 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400698out:
699 dprintk("<-- %s status=%d\n", __func__, ret);
700 return ret;
701}
702
703struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400704 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400705 struct nfs4_sequence_args *seq_args;
706 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400707};
708
709static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
710{
711 struct nfs41_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400712 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400713
Trond Myklebust035168a2010-06-16 09:52:26 -0400714 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
715
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400716 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400717}
718
Andy Adamson69ab40c2009-04-01 09:22:19 -0400719static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
720{
721 struct nfs41_call_sync_data *data = calldata;
722
Trond Myklebust14516c32010-07-31 14:29:06 -0400723 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400724}
725
Trond Myklebust17280172012-03-11 13:11:00 -0400726static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400727 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400728 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400729};
730
Bryan Schumaker7c513052011-03-24 17:12:24 +0000731static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
732 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400733 struct rpc_message *msg,
734 struct nfs4_sequence_args *args,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400735 struct nfs4_sequence_res *res)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400736{
737 int ret;
738 struct rpc_task *task;
739 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400740 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400741 .seq_args = args,
742 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400743 };
744 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000745 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400746 .rpc_message = msg,
747 .callback_ops = &nfs41_call_sync_ops,
748 .callback_data = &data
749 };
750
Andy Adamsonce5039c2009-04-01 09:22:13 -0400751 task = rpc_run_task(&task_setup);
752 if (IS_ERR(task))
753 ret = PTR_ERR(task);
754 else {
755 ret = task->tk_status;
756 rpc_put_task(task);
757 }
758 return ret;
759}
760
Trond Myklebustdf896452010-06-16 09:52:26 -0400761#else
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400762static
Trond Myklebust9d12b212012-01-17 22:04:25 -0500763void nfs41_init_sequence(struct nfs4_sequence_args *args,
764 struct nfs4_sequence_res *res, int cache_reply)
765{
766}
767
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400768static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
769{
770}
771
772
Trond Myklebust14516c32010-07-31 14:29:06 -0400773static int nfs4_sequence_done(struct rpc_task *task,
774 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400775{
Trond Myklebust14516c32010-07-31 14:29:06 -0400776 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400777}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400778#endif /* CONFIG_NFS_V4_1 */
779
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400780static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000781int _nfs4_call_sync(struct rpc_clnt *clnt,
782 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400783 struct rpc_message *msg,
784 struct nfs4_sequence_args *args,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400785 struct nfs4_sequence_res *res)
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400786{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000787 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400788}
789
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400790static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000791int nfs4_call_sync(struct rpc_clnt *clnt,
792 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000793 struct rpc_message *msg,
794 struct nfs4_sequence_args *args,
795 struct nfs4_sequence_res *res,
796 int cache_reply)
797{
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400798 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000799 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400800 args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000801}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400802
Trond Myklebust38478b22006-05-25 01:40:57 -0400803static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
Trond Myklebust38478b22006-05-25 01:40:57 -0400805 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Trond Myklebust38478b22006-05-25 01:40:57 -0400807 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400808 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700809 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400810 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700811 dir->i_version = cinfo->after;
David Howellsde242c02012-12-20 21:52:38 +0000812 nfs_fscache_invalidate(dir);
Trond Myklebust38478b22006-05-25 01:40:57 -0400813 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814}
815
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100816struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400817 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100818 struct nfs_openargs o_arg;
819 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100820 struct nfs_open_confirmargs c_arg;
821 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500822 struct nfs4_string owner_name;
823 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100824 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400825 struct nfs4_label *f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100826 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400827 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100828 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400829 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100830 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100831 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400832 unsigned int rpc_done : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -0400833 unsigned int is_recover : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100834 int rpc_status;
835 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100836};
837
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400838static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
839 int err, struct nfs4_exception *exception)
840{
841 if (err != -EINVAL)
842 return false;
843 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
844 return false;
845 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
846 exception->retry = 1;
847 return true;
848}
849
850static enum open_claim_type4
851nfs4_map_atomic_open_claim(struct nfs_server *server,
852 enum open_claim_type4 claim)
853{
854 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
855 return claim;
856 switch (claim) {
857 default:
858 return claim;
859 case NFS4_OPEN_CLAIM_FH:
860 return NFS4_OPEN_CLAIM_NULL;
861 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
862 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
863 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
864 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
865 }
866}
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400867
868static void nfs4_init_opendata_res(struct nfs4_opendata *p)
869{
870 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400871 p->o_res.f_label = p->f_label;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400872 p->o_res.seqid = p->o_arg.seqid;
873 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400874 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400875 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400876 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500877 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400878}
879
Al Viro82a2c1b2011-06-22 18:30:55 -0400880static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500881 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400882 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -0400883 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400884 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400885 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100886{
Al Viro82a2c1b2011-06-22 18:30:55 -0400887 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100888 struct inode *dir = parent->d_inode;
889 struct nfs_server *server = NFS_SERVER(dir);
890 struct nfs4_opendata *p;
891
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400892 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100893 if (p == NULL)
894 goto err;
David Quigley14c43f72013-05-22 12:50:43 -0400895
896 p->f_label = nfs4_label_alloc(server, gfp_mask);
897 if (IS_ERR(p->f_label))
898 goto err_free_p;
899
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400900 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100901 if (p->o_arg.seqid == NULL)
David Quigley14c43f72013-05-22 12:50:43 -0400902 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -0400903 nfs_sb_active(dentry->d_sb);
904 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100905 p->dir = parent;
906 p->owner = sp;
907 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500908 p->o_arg.open_flags = flags;
909 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700910 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
911 * will return permission denied for all bits until close */
912 if (!(flags & O_EXCL)) {
913 /* ask server to check for all possible rights as results
914 * are cached */
915 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
916 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700917 }
David Howells7539bba2006-08-22 20:06:09 -0400918 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400919 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
920 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400921 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100922 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -0400923 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -0400924 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
David Quigley1775fd32013-05-22 12:50:42 -0400925 p->o_arg.label = label;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400926 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
927 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400928 case NFS4_OPEN_CLAIM_NULL:
929 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
930 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
931 p->o_arg.fh = NFS_FH(dir);
932 break;
933 case NFS4_OPEN_CLAIM_PREVIOUS:
934 case NFS4_OPEN_CLAIM_FH:
935 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
936 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
937 p->o_arg.fh = NFS_FH(dentry->d_inode);
938 }
Trond Myklebust536e43d2012-01-17 22:04:26 -0500939 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -0400940 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400941
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100942 p->o_arg.u.attrs = &p->attrs;
943 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500944
945 verf[0] = jiffies;
946 verf[1] = current->pid;
947 memcpy(p->o_arg.u.verifier.data, verf,
948 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100949 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100950 p->c_arg.fh = &p->o_res.fh;
951 p->c_arg.stateid = &p->o_res.stateid;
952 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400953 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400954 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100955 return p;
David Quigley14c43f72013-05-22 12:50:43 -0400956
957err_free_label:
958 nfs4_label_free(p->f_label);
959err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100960 kfree(p);
961err:
962 dput(parent);
963 return NULL;
964}
965
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400966static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100967{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400968 struct nfs4_opendata *p = container_of(kref,
969 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400970 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400971
972 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400973 if (p->state != NULL)
974 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400975 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -0400976
977 nfs4_label_free(p->f_label);
978
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400979 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400980 dput(p->dentry);
981 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500982 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400983 kfree(p);
984}
985
986static void nfs4_opendata_put(struct nfs4_opendata *p)
987{
988 if (p != NULL)
989 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100990}
991
Trond Myklebust06f814a2006-01-03 09:55:07 +0100992static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
993{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100994 int ret;
995
Trond Myklebust06f814a2006-01-03 09:55:07 +0100996 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100997 return ret;
998}
999
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001000static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001001{
1002 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001003
Trond Myklebust536e43d2012-01-17 22:04:26 -05001004 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001005 goto out;
1006 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001007 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001008 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1009 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001010 break;
1011 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001012 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1013 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001014 break;
1015 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001016 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1017 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001018 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001019out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001020 return ret;
1021}
1022
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001023static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001024{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001025 if (delegation == NULL)
1026 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001027 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001028 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -05001029 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -04001030 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001031 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1032 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -05001033 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001034 return 1;
1035}
1036
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001037static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001038{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001039 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001040 case FMODE_WRITE:
1041 state->n_wronly++;
1042 break;
1043 case FMODE_READ:
1044 state->n_rdonly++;
1045 break;
1046 case FMODE_READ|FMODE_WRITE:
1047 state->n_rdwr++;
1048 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001049 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001050}
1051
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001052static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001053{
1054 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001055 nfs4_stateid_copy(&state->stateid, stateid);
1056 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust92b40e92013-04-20 01:25:45 -04001057 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001058 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001059 case FMODE_READ:
1060 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1061 break;
1062 case FMODE_WRITE:
1063 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1064 break;
1065 case FMODE_READ|FMODE_WRITE:
1066 set_bit(NFS_O_RDWR_STATE, &state->flags);
1067 }
1068}
1069
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001070static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001071{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001072 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001073 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001074 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001075}
1076
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001077static 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 -07001078{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001079 /*
1080 * Protect the call to nfs4_state_set_mode_locked and
1081 * serialise the stateid update
1082 */
1083 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001084 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001085 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001086 set_bit(NFS_DELEGATED_STATE, &state->flags);
1087 }
1088 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001089 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001090 write_sequnlock(&state->seqlock);
1091 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001092 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001093 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094}
1095
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001096static 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 -05001097{
1098 struct nfs_inode *nfsi = NFS_I(state->inode);
1099 struct nfs_delegation *deleg_cur;
1100 int ret = 0;
1101
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001102 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001103
1104 rcu_read_lock();
1105 deleg_cur = rcu_dereference(nfsi->delegation);
1106 if (deleg_cur == NULL)
1107 goto no_delegation;
1108
1109 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001110 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001111 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001112 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001113 goto no_delegation_unlock;
1114
1115 if (delegation == NULL)
1116 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001117 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001118 goto no_delegation_unlock;
1119
Trond Myklebustb7391f42008-12-23 15:21:52 -05001120 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001121 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001122 ret = 1;
1123no_delegation_unlock:
1124 spin_unlock(&deleg_cur->lock);
1125no_delegation:
1126 rcu_read_unlock();
1127
1128 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001129 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001130 ret = 1;
1131 }
1132
1133 return ret;
1134}
1135
1136
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001137static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001138{
1139 struct nfs_delegation *delegation;
1140
1141 rcu_read_lock();
1142 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001143 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001144 rcu_read_unlock();
1145 return;
1146 }
1147 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001148 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001149}
1150
Trond Myklebust6ee41262007-07-08 14:11:36 -04001151static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001152{
1153 struct nfs4_state *state = opendata->state;
1154 struct nfs_inode *nfsi = NFS_I(state->inode);
1155 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001156 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001157 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001158 nfs4_stateid stateid;
1159 int ret = -EAGAIN;
1160
Trond Myklebustaac00a82007-07-05 19:02:21 -04001161 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001162 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001163 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001164 if (can_open_cached(state, fmode, open_mode)) {
1165 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001166 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001167 goto out_return_state;
1168 }
1169 spin_unlock(&state->owner->so_lock);
1170 }
Trond Myklebust34310432008-12-23 15:21:38 -05001171 rcu_read_lock();
1172 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001173 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001174 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001175 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001176 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001177 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001178 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001179 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001180 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001181 if (!opendata->is_recover) {
1182 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1183 if (ret != 0)
1184 goto out;
1185 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001186 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001187
1188 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001189 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001190 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001191 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001192out:
1193 return ERR_PTR(ret);
1194out_return_state:
1195 atomic_inc(&state->count);
1196 return state;
1197}
1198
Andy Adamsone23008e2012-10-02 21:07:32 -04001199static void
1200nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1201{
1202 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1203 struct nfs_delegation *delegation;
1204 int delegation_flags = 0;
1205
1206 rcu_read_lock();
1207 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1208 if (delegation)
1209 delegation_flags = delegation->flags;
1210 rcu_read_unlock();
1211 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1212 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1213 "returning a delegation for "
1214 "OPEN(CLAIM_DELEGATE_CUR)\n",
1215 clp->cl_hostname);
1216 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1217 nfs_inode_set_delegation(state->inode,
1218 data->owner->so_cred,
1219 &data->o_res);
1220 else
1221 nfs_inode_reclaim_delegation(state->inode,
1222 data->owner->so_cred,
1223 &data->o_res);
1224}
1225
1226/*
1227 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1228 * and update the nfs4_state.
1229 */
1230static struct nfs4_state *
1231_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1232{
1233 struct inode *inode = data->state->inode;
1234 struct nfs4_state *state = data->state;
1235 int ret;
1236
1237 if (!data->rpc_done) {
1238 ret = data->rpc_status;
1239 goto err;
1240 }
1241
1242 ret = -ESTALE;
1243 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1244 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1245 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1246 goto err;
1247
1248 ret = -ENOMEM;
1249 state = nfs4_get_open_state(inode, data->owner);
1250 if (state == NULL)
1251 goto err;
1252
1253 ret = nfs_refresh_inode(inode, &data->f_attr);
1254 if (ret)
1255 goto err;
1256
David Quigleyaa9c2662013-05-22 12:50:44 -04001257 nfs_setsecurity(inode, &data->f_attr, data->f_label);
1258
Andy Adamsone23008e2012-10-02 21:07:32 -04001259 if (data->o_res.delegation_type != 0)
1260 nfs4_opendata_check_deleg(data, state);
1261 update_open_stateid(state, &data->o_res.stateid, NULL,
1262 data->o_arg.fmode);
1263
1264 return state;
1265err:
1266 return ERR_PTR(ret);
1267
1268}
1269
1270static struct nfs4_state *
1271_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001272{
1273 struct inode *inode;
1274 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001275 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001276
Trond Myklebustaac00a82007-07-05 19:02:21 -04001277 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001278 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001279 goto out;
1280 }
1281
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001282 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001283 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001284 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001285 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001286 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001287 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001288 goto err;
1289 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001290 state = nfs4_get_open_state(inode, data->owner);
1291 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001292 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001293 if (data->o_res.delegation_type != 0)
1294 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001295 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001296 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001297 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001298out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001299 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001300 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001301err_put_inode:
1302 iput(inode);
1303err:
1304 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001305}
1306
Andy Adamsone23008e2012-10-02 21:07:32 -04001307static struct nfs4_state *
1308nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1309{
1310 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1311 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1312 return _nfs4_opendata_to_nfs4_state(data);
1313}
1314
Trond Myklebust864472e2006-01-03 09:55:15 +01001315static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1316{
1317 struct nfs_inode *nfsi = NFS_I(state->inode);
1318 struct nfs_open_context *ctx;
1319
1320 spin_lock(&state->inode->i_lock);
1321 list_for_each_entry(ctx, &nfsi->open_files, list) {
1322 if (ctx->state != state)
1323 continue;
1324 get_nfs_open_context(ctx);
1325 spin_unlock(&state->inode->i_lock);
1326 return ctx;
1327 }
1328 spin_unlock(&state->inode->i_lock);
1329 return ERR_PTR(-ENOENT);
1330}
1331
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001332static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1333 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001334{
1335 struct nfs4_opendata *opendata;
1336
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001337 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001338 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001339 if (opendata == NULL)
1340 return ERR_PTR(-ENOMEM);
1341 opendata->state = state;
1342 atomic_inc(&state->count);
1343 return opendata;
1344}
1345
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001346static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001347{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001348 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001349 int ret;
1350
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001351 opendata->o_arg.open_flags = 0;
1352 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001353 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1354 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1355 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001356 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001357 if (ret != 0)
1358 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001359 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001360 if (IS_ERR(newstate))
1361 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001362 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001363 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001364 return 0;
1365}
1366
1367static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1368{
Trond Myklebust864472e2006-01-03 09:55:15 +01001369 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001370 int ret;
1371
1372 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001373 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001374 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001375 smp_rmb();
1376 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001377 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001378 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001379 if (ret != 0)
1380 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001381 if (newstate != state)
1382 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001383 }
1384 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001385 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001386 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001387 if (ret != 0)
1388 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001389 if (newstate != state)
1390 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001391 }
1392 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001393 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001394 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001395 if (ret != 0)
1396 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001397 if (newstate != state)
1398 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001399 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001400 /*
1401 * We may have performed cached opens for all three recoveries.
1402 * Check if we need to update the current stateid.
1403 */
1404 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001405 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001406 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001407 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001408 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001409 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001410 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001411 return 0;
1412}
1413
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414/*
1415 * OPEN_RECLAIM:
1416 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001418static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001420 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001421 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001422 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 int status;
1424
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001425 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1426 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001427 if (IS_ERR(opendata))
1428 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001429 rcu_read_lock();
1430 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001431 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001432 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001433 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001434 opendata->o_arg.u.delegation_type = delegation_type;
1435 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001436 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 return status;
1438}
1439
Trond Myklebust539cd032007-06-05 11:46:42 -04001440static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
1442 struct nfs_server *server = NFS_SERVER(state->inode);
1443 struct nfs4_exception exception = { };
1444 int err;
1445 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001446 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001447 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001448 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1449 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001450 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001451 break;
1452 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 } while (exception.retry);
1454 return err;
1455}
1456
Trond Myklebust864472e2006-01-03 09:55:15 +01001457static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1458{
1459 struct nfs_open_context *ctx;
1460 int ret;
1461
1462 ctx = nfs4_state_find_open_context(state);
1463 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001464 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001465 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001466 put_nfs_open_context(ctx);
1467 return ret;
1468}
1469
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001470static 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 -07001471{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001472 switch (err) {
1473 default:
1474 printk(KERN_ERR "NFS: %s: unhandled error "
1475 "%d.\n", __func__, err);
1476 case 0:
1477 case -ENOENT:
1478 case -ESTALE:
1479 break;
1480 case -NFS4ERR_BADSESSION:
1481 case -NFS4ERR_BADSLOT:
1482 case -NFS4ERR_BAD_HIGH_SLOT:
1483 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1484 case -NFS4ERR_DEADSESSION:
1485 set_bit(NFS_DELEGATED_STATE, &state->flags);
1486 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1487 return -EAGAIN;
1488 case -NFS4ERR_STALE_CLIENTID:
1489 case -NFS4ERR_STALE_STATEID:
1490 set_bit(NFS_DELEGATED_STATE, &state->flags);
1491 case -NFS4ERR_EXPIRED:
1492 /* Don't recall a delegation if it was lost */
1493 nfs4_schedule_lease_recovery(server->nfs_client);
1494 return -EAGAIN;
1495 case -NFS4ERR_DELEG_REVOKED:
1496 case -NFS4ERR_ADMIN_REVOKED:
1497 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001498 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001499 nfs_inode_find_state_and_recover(state->inode,
1500 stateid);
1501 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001502 return 0;
1503 case -NFS4ERR_DELAY:
1504 case -NFS4ERR_GRACE:
1505 set_bit(NFS_DELEGATED_STATE, &state->flags);
1506 ssleep(1);
1507 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001508 case -ENOMEM:
1509 case -NFS4ERR_DENIED:
1510 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1511 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 return err;
1514}
1515
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001516int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1517{
1518 struct nfs_server *server = NFS_SERVER(state->inode);
1519 struct nfs4_opendata *opendata;
1520 int err;
1521
1522 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1523 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1524 if (IS_ERR(opendata))
1525 return PTR_ERR(opendata);
1526 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1527 err = nfs4_open_recover(opendata, state);
1528 nfs4_opendata_put(opendata);
1529 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1530}
1531
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001532static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1533{
1534 struct nfs4_opendata *data = calldata;
1535
1536 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001537 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001538 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001539 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001540 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001541 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001542 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001543}
1544
1545static void nfs4_open_confirm_release(void *calldata)
1546{
1547 struct nfs4_opendata *data = calldata;
1548 struct nfs4_state *state = NULL;
1549
1550 /* If this request hasn't been cancelled, do nothing */
1551 if (data->cancelled == 0)
1552 goto out_free;
1553 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001554 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001555 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001556 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001557 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001558 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001559out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001560 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001561}
1562
1563static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001564 .rpc_call_done = nfs4_open_confirm_done,
1565 .rpc_release = nfs4_open_confirm_release,
1566};
1567
1568/*
1569 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1570 */
1571static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1572{
1573 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1574 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001575 struct rpc_message msg = {
1576 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1577 .rpc_argp = &data->c_arg,
1578 .rpc_resp = &data->c_res,
1579 .rpc_cred = data->owner->so_cred,
1580 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001581 struct rpc_task_setup task_setup_data = {
1582 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001583 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001584 .callback_ops = &nfs4_open_confirm_ops,
1585 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001586 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001587 .flags = RPC_TASK_ASYNC,
1588 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 int status;
1590
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001591 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001592 data->rpc_done = 0;
1593 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001594 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001595 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001596 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001597 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001598 status = nfs4_wait_for_completion_rpc_task(task);
1599 if (status != 0) {
1600 data->cancelled = 1;
1601 smp_wmb();
1602 } else
1603 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001604 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 return status;
1606}
1607
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001608static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001610 struct nfs4_opendata *data = calldata;
1611 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001612 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001613
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001614 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001615 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001616 /*
1617 * Check if we still need to send an OPEN call, or if we can use
1618 * a delegation instead.
1619 */
1620 if (data->state != NULL) {
1621 struct nfs_delegation *delegation;
1622
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001623 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001624 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001625 rcu_read_lock();
1626 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001627 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
Trond Myklebustcd4c9be2013-04-23 14:46:25 -04001628 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
Trond Myklebust652f89f2011-12-09 19:05:58 -05001629 can_open_delegated(delegation, data->o_arg.fmode))
1630 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001631 rcu_read_unlock();
1632 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001633 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04001634 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust8188df12013-04-23 14:31:19 -04001635 switch (data->o_arg.claim) {
1636 case NFS4_OPEN_CLAIM_PREVIOUS:
1637 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1638 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001639 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04001640 case NFS4_OPEN_CLAIM_FH:
1641 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001642 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1643 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001644 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001645 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001646 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001647 &data->o_res.seq_res,
1648 task) != 0)
1649 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04001650
1651 /* Set the create mode (note dependency on the session type) */
1652 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1653 if (data->o_arg.open_flags & O_EXCL) {
1654 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1655 if (nfs4_has_persistent_session(clp))
1656 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1657 else if (clp->cl_mvops->minor_version > 0)
1658 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1659 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04001660 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001661unlock_no_action:
1662 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001663out_no_action:
1664 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001665out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001666 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001667}
1668
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001669static void nfs4_open_done(struct rpc_task *task, void *calldata)
1670{
1671 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001673 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001674
Trond Myklebust14516c32010-07-31 14:29:06 -04001675 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1676 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001677
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001678 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001679 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1680 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001681 case S_IFREG:
1682 break;
1683 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001684 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001685 break;
1686 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001687 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001688 break;
1689 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001690 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001691 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001692 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001693 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001694 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1695 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001696 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001697 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001698}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001699
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001700static void nfs4_open_release(void *calldata)
1701{
1702 struct nfs4_opendata *data = calldata;
1703 struct nfs4_state *state = NULL;
1704
1705 /* If this request hasn't been cancelled, do nothing */
1706 if (data->cancelled == 0)
1707 goto out_free;
1708 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001709 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001710 goto out_free;
1711 /* In case we need an open_confirm, no cleanup! */
1712 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1713 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001714 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001715 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001716 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001717out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001718 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001719}
1720
1721static const struct rpc_call_ops nfs4_open_ops = {
1722 .rpc_call_prepare = nfs4_open_prepare,
1723 .rpc_call_done = nfs4_open_done,
1724 .rpc_release = nfs4_open_release,
1725};
1726
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001727static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001728{
1729 struct inode *dir = data->dir->d_inode;
1730 struct nfs_server *server = NFS_SERVER(dir);
1731 struct nfs_openargs *o_arg = &data->o_arg;
1732 struct nfs_openres *o_res = &data->o_res;
1733 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001734 struct rpc_message msg = {
1735 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1736 .rpc_argp = o_arg,
1737 .rpc_resp = o_res,
1738 .rpc_cred = data->owner->so_cred,
1739 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001740 struct rpc_task_setup task_setup_data = {
1741 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001742 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001743 .callback_ops = &nfs4_open_ops,
1744 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001745 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001746 .flags = RPC_TASK_ASYNC,
1747 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001748 int status;
1749
Trond Myklebust9d12b212012-01-17 22:04:25 -05001750 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001751 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001752 data->rpc_done = 0;
1753 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001754 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001755 data->is_recover = 0;
1756 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001757 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001758 data->is_recover = 1;
1759 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04001760 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001761 if (IS_ERR(task))
1762 return PTR_ERR(task);
1763 status = nfs4_wait_for_completion_rpc_task(task);
1764 if (status != 0) {
1765 data->cancelled = 1;
1766 smp_wmb();
1767 } else
1768 status = data->rpc_status;
1769 rpc_put_task(task);
1770
1771 return status;
1772}
1773
1774static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1775{
1776 struct inode *dir = data->dir->d_inode;
1777 struct nfs_openres *o_res = &data->o_res;
1778 int status;
1779
1780 status = nfs4_run_open_task(data, 1);
1781 if (status != 0 || !data->rpc_done)
1782 return status;
1783
Trond Myklebust6926afd2012-01-07 13:22:46 -05001784 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1785
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001786 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1787 status = _nfs4_proc_open_confirm(data);
1788 if (status != 0)
1789 return status;
1790 }
1791
1792 return status;
1793}
1794
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001795static int nfs4_opendata_access(struct rpc_cred *cred,
1796 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001797 struct nfs4_state *state, fmode_t fmode,
1798 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001799{
1800 struct nfs_access_entry cache;
1801 u32 mask;
1802
1803 /* access call failed or for some reason the server doesn't
1804 * support any access modes -- defer access call until later */
1805 if (opendata->o_res.access_supported == 0)
1806 return 0;
1807
1808 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001809 /* don't check MAY_WRITE - a newly created file may not have
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001810 * write mode bits, but POSIX allows the creating process to write.
1811 * use openflags to check for exec, because fmode won't
1812 * always have FMODE_EXEC set when file open for exec. */
1813 if (openflags & __FMODE_EXEC) {
1814 /* ONLY check for exec rights */
1815 mask = MAY_EXEC;
1816 } else if (fmode & FMODE_READ)
1817 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001818
1819 cache.cred = cred;
1820 cache.jiffies = jiffies;
1821 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1822 nfs_access_add_cache(state->inode, &cache);
1823
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001824 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001825 return 0;
1826
1827 /* even though OPEN succeeded, access is denied. Close the file */
1828 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001829 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001830}
1831
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001832/*
1833 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1834 */
1835static int _nfs4_proc_open(struct nfs4_opendata *data)
1836{
1837 struct inode *dir = data->dir->d_inode;
1838 struct nfs_server *server = NFS_SERVER(dir);
1839 struct nfs_openargs *o_arg = &data->o_arg;
1840 struct nfs_openres *o_res = &data->o_res;
1841 int status;
1842
1843 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001844 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001845 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001846 if (status != 0) {
1847 if (status == -NFS4ERR_BADNAME &&
1848 !(o_arg->open_flags & O_CREAT))
1849 return -ENOENT;
1850 return status;
1851 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001852
Trond Myklebust6926afd2012-01-07 13:22:46 -05001853 nfs_fattr_map_and_free_names(server, &data->f_attr);
1854
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001855 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001856 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001857 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1858 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001860 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001862 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 }
1864 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
David Quigley1775fd32013-05-22 12:50:42 -04001865 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001866 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867}
1868
Andy Adamsond83217c2011-03-01 01:34:17 +00001869static int nfs4_recover_expired_lease(struct nfs_server *server)
1870{
1871 return nfs4_client_recover_expired_lease(server->nfs_client);
1872}
1873
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874/*
1875 * OPEN_EXPIRED:
1876 * reclaim state on the server after a network partition.
1877 * Assumes caller holds the appropriate lock
1878 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001879static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001881 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001882 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001884 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001885 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001886 if (IS_ERR(opendata))
1887 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001888 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001889 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001890 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001891 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001892 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893}
1894
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001895static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001896{
Trond Myklebust539cd032007-06-05 11:46:42 -04001897 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001898 struct nfs4_exception exception = { };
1899 int err;
1900
1901 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001902 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001903 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001904 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1905 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001906 switch (err) {
1907 default:
1908 goto out;
1909 case -NFS4ERR_GRACE:
1910 case -NFS4ERR_DELAY:
1911 nfs4_handle_exception(server, err, &exception);
1912 err = 0;
1913 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001914 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001915out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001916 return err;
1917}
1918
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1920{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001922 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
Trond Myklebust864472e2006-01-03 09:55:15 +01001924 ctx = nfs4_state_find_open_context(state);
1925 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001926 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001927 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001928 put_nfs_open_context(ctx);
1929 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930}
1931
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001932#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001933static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001934{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001935 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001936 nfs4_stateid *stateid = &state->stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001937 struct nfs_delegation *delegation;
1938 struct rpc_cred *cred = NULL;
1939 int status = -NFS4ERR_BAD_STATEID;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001940
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001941 /* If a state reset has been done, test_stateid is unneeded */
1942 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1943 return;
1944
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001945 /* Get the delegation credential for use by test/free_stateid */
1946 rcu_read_lock();
1947 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1948 if (delegation != NULL &&
1949 nfs4_stateid_match(&delegation->stateid, stateid)) {
1950 cred = get_rpccred(delegation->cred);
1951 rcu_read_unlock();
1952 status = nfs41_test_stateid(server, stateid, cred);
1953 } else
1954 rcu_read_unlock();
1955
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001956 if (status != NFS_OK) {
1957 /* Free the stateid unless the server explicitly
1958 * informs us the stateid is unrecognized. */
1959 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001960 nfs41_free_stateid(server, stateid, cred);
Bryan Schumaker69388672012-09-26 15:25:52 -04001961 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001962
Bryan Schumaker69388672012-09-26 15:25:52 -04001963 write_seqlock(&state->seqlock);
1964 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1965 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001966 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1967 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001968
1969 if (cred != NULL)
1970 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001971}
1972
1973/**
1974 * nfs41_check_open_stateid - possibly free an open stateid
1975 *
1976 * @state: NFSv4 state for an inode
1977 *
1978 * Returns NFS_OK if recovery for this stateid is now finished.
1979 * Otherwise a negative NFS4ERR value is returned.
1980 */
1981static int nfs41_check_open_stateid(struct nfs4_state *state)
1982{
1983 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001984 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001985 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001986 int status;
1987
1988 /* If a state reset has been done, test_stateid is unneeded */
1989 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1990 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1991 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1992 return -NFS4ERR_BAD_STATEID;
1993
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001994 status = nfs41_test_stateid(server, stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001995 if (status != NFS_OK) {
1996 /* Free the stateid unless the server explicitly
1997 * informs us the stateid is unrecognized. */
1998 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001999 nfs41_free_stateid(server, stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002000
2001 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2002 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2003 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002004 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002005 }
2006 return status;
2007}
2008
2009static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2010{
Chuck Levereb64cf92012-07-11 16:30:05 -04002011 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002012
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002013 nfs41_clear_delegation_stateid(state);
2014 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002015 if (status != NFS_OK)
2016 status = nfs4_open_expired(sp, state);
2017 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002018}
2019#endif
2020
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002022 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2023 * fields corresponding to attributes that were used to store the verifier.
2024 * Make sure we clobber those fields in the later setattr call
2025 */
2026static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2027{
2028 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2029 !(sattr->ia_valid & ATTR_ATIME_SET))
2030 sattr->ia_valid |= ATTR_ATIME;
2031
2032 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2033 !(sattr->ia_valid & ATTR_MTIME_SET))
2034 sattr->ia_valid |= ATTR_MTIME;
2035}
2036
Trond Myklebustc21443c2013-02-07 14:26:21 -05002037static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2038 fmode_t fmode,
2039 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002040 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002041{
2042 struct nfs4_state_owner *sp = opendata->owner;
2043 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002044 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002045 struct nfs4_state *state;
2046 unsigned int seq;
2047 int ret;
2048
2049 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2050
2051 ret = _nfs4_proc_open(opendata);
2052 if (ret != 0)
2053 goto out;
2054
2055 state = nfs4_opendata_to_nfs4_state(opendata);
2056 ret = PTR_ERR(state);
2057 if (IS_ERR(state))
2058 goto out;
2059 if (server->caps & NFS_CAP_POSIX_LOCK)
2060 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2061
Trond Myklebust275bb302013-05-29 13:11:28 -04002062 dentry = opendata->dentry;
2063 if (dentry->d_inode == NULL) {
2064 /* FIXME: Is this d_drop() ever needed? */
2065 d_drop(dentry);
2066 dentry = d_add_unique(dentry, igrab(state->inode));
2067 if (dentry == NULL) {
2068 dentry = opendata->dentry;
2069 } else if (dentry != ctx->dentry) {
2070 dput(ctx->dentry);
2071 ctx->dentry = dget(dentry);
2072 }
2073 nfs_set_verifier(dentry,
2074 nfs_save_change_attribute(opendata->dir->d_inode));
2075 }
2076
Trond Myklebustc21443c2013-02-07 14:26:21 -05002077 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2078 if (ret != 0)
2079 goto out;
2080
Trond Myklebust3efb9722013-05-29 13:17:04 -04002081 ctx->state = state;
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002082 if (dentry->d_inode == state->inode) {
2083 nfs_inode_attach_open_context(ctx);
2084 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2085 nfs4_schedule_stateid_recovery(server, state);
2086 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002087out:
2088 return ret;
2089}
2090
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002091/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002092 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 */
Andy Adamson82be4172012-05-23 05:02:35 -04002094static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002095 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002096 int flags,
2097 struct iattr *sattr,
Trond Myklebust959d9212013-06-28 16:29:51 -04002098 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099{
2100 struct nfs4_state_owner *sp;
2101 struct nfs4_state *state = NULL;
2102 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002103 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002104 struct dentry *dentry = ctx->dentry;
2105 struct rpc_cred *cred = ctx->cred;
2106 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2107 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002108 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002109 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002110 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
2112 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002114 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2115 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2117 goto out_err;
2118 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002119 status = nfs4_recover_expired_lease(server);
2120 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002121 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04002122 if (dentry->d_inode != NULL)
2123 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002124 status = -ENOMEM;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002125 if (dentry->d_inode)
2126 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002127 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002128 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002129 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002130 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
David Quigley14c43f72013-05-22 12:50:43 -04002132 if (label) {
2133 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2134 if (IS_ERR(olabel)) {
2135 status = PTR_ERR(olabel);
2136 goto err_opendata_put;
2137 }
2138 }
2139
Andy Adamson82be4172012-05-23 05:02:35 -04002140 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2141 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2142 if (!opendata->f_attr.mdsthreshold)
David Quigley14c43f72013-05-22 12:50:43 -04002143 goto err_free_label;
Trond Myklebust1549210f2012-06-05 09:16:47 -04002144 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002145 }
Al Viro82a2c1b2011-06-22 18:30:55 -04002146 if (dentry->d_inode != NULL)
2147 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002148
Trond Myklebust3efb9722013-05-29 13:17:04 -04002149 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002150 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002151 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002152 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002153
Trond Myklebust549b19c2013-04-16 18:42:34 -04002154 if ((opendata->o_arg.open_flags & O_EXCL) &&
2155 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002156 nfs4_exclusive_attrset(opendata, sattr);
2157
2158 nfs_fattr_init(opendata->o_res.f_attr);
2159 status = nfs4_do_setattr(state->inode, cred,
2160 opendata->o_res.f_attr, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002161 state, label, olabel);
2162 if (status == 0) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002163 nfs_setattr_update_inode(state->inode, sattr);
David Quigley1775fd32013-05-22 12:50:42 -04002164 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
David Quigleyaa9c2662013-05-22 12:50:44 -04002165 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
David Quigley1775fd32013-05-22 12:50:42 -04002166 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002167 }
Andy Adamson82be4172012-05-23 05:02:35 -04002168
2169 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2170 *ctx_th = opendata->f_attr.mdsthreshold;
2171 else
2172 kfree(opendata->f_attr.mdsthreshold);
2173 opendata->f_attr.mdsthreshold = NULL;
2174
David Quigley14c43f72013-05-22 12:50:43 -04002175 nfs4_label_free(olabel);
2176
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002177 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002180err_free_label:
2181 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002182err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002183 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002184 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002185err_put_state_owner:
2186 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 return status;
2189}
2190
2191
Andy Adamson82be4172012-05-23 05:02:35 -04002192static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002193 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002194 int flags,
2195 struct iattr *sattr,
Trond Myklebust959d9212013-06-28 16:29:51 -04002196 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002198 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 struct nfs4_exception exception = { };
2200 struct nfs4_state *res;
2201 int status;
2202
2203 do {
Trond Myklebust959d9212013-06-28 16:29:51 -04002204 status = _nfs4_do_open(dir, ctx, flags, sattr, label);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002205 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002206 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 if (status == 0)
2208 break;
2209 /* NOTE: BAD_SEQID means the server and client disagree about the
2210 * book-keeping w.r.t. state-changing operations
2211 * (OPEN/CLOSE/LOCK/LOCKU...)
2212 * It is actually a sign of a bug on the client or on the server.
2213 *
2214 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002215 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 * have unhashed the old state_owner for us, and that we can
2217 * therefore safely retry using a new one. We should still warn
2218 * the user though...
2219 */
2220 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002221 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002222 " returned a bad sequence-id error!\n",
2223 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 exception.retry = 1;
2225 continue;
2226 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002227 /*
2228 * BAD_STATEID on OPEN means that the server cancelled our
2229 * state before it received the OPEN_CONFIRM.
2230 * Recover by retrying the request as per the discussion
2231 * on Page 181 of RFC3530.
2232 */
2233 if (status == -NFS4ERR_BAD_STATEID) {
2234 exception.retry = 1;
2235 continue;
2236 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002237 if (status == -EAGAIN) {
2238 /* We must have found a delegation */
2239 exception.retry = 1;
2240 continue;
2241 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002242 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2243 continue;
2244 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 status, &exception));
2246 } while (exception.retry);
2247 return res;
2248}
2249
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002250static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2251 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002252 struct nfs4_state *state, struct nfs4_label *ilabel,
2253 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002255 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002257 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 .iap = sattr,
2259 .server = server,
2260 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002261 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 };
2263 struct nfs_setattrres res = {
2264 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002265 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 .server = server,
2267 };
2268 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002269 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2270 .rpc_argp = &arg,
2271 .rpc_resp = &res,
2272 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002274 unsigned long timestamp = jiffies;
Trond Myklebustee3ae842013-04-29 10:35:36 -04002275 fmode_t fmode;
2276 bool truncate;
Trond Myklebust65e43082005-08-16 11:49:44 -04002277 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
David Quigleyaa9c2662013-05-22 12:50:44 -04002279 arg.bitmask = nfs4_bitmask(server, ilabel);
2280 if (ilabel)
2281 arg.bitmask = nfs4_bitmask(server, olabel);
2282
Trond Myklebust0e574af2005-10-27 22:12:38 -04002283 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
Trond Myklebustee3ae842013-04-29 10:35:36 -04002285 /* Servers should only apply open mode checks for file size changes */
2286 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2287 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2288
2289 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2290 /* Use that stateid */
2291 } else if (truncate && state != NULL && nfs4_valid_open_stateid(state)) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002292 struct nfs_lockowner lockowner = {
2293 .l_owner = current->files,
2294 .l_pid = current->tgid,
2295 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002296 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002297 &lockowner);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002298 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002299 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
Bryan Schumaker7c513052011-03-24 17:12:24 +00002301 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002302 if (status == 0 && state != NULL)
2303 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002304 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305}
2306
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002307static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2308 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002309 struct nfs4_state *state, struct nfs4_label *ilabel,
2310 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002312 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002313 struct nfs4_exception exception = {
2314 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002315 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002316 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 int err;
2318 do {
David Quigley1775fd32013-05-22 12:50:42 -04002319 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
Trond Myklebustc1578b72013-08-12 16:58:42 -04002320 trace_nfs4_setattr(inode, err);
Trond Myklebust451146b2012-04-18 16:29:11 -04002321 switch (err) {
2322 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002323 if (!(sattr->ia_valid & ATTR_SIZE)) {
2324 pr_warn_once("NFSv4: server %s is incorrectly "
2325 "applying open mode checks to "
2326 "a SETATTR that is not "
2327 "changing file size.\n",
2328 server->nfs_client->cl_hostname);
2329 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002330 if (state && !(state->state & FMODE_WRITE)) {
2331 err = -EBADF;
2332 if (sattr->ia_valid & ATTR_OPEN)
2333 err = -EACCES;
2334 goto out;
2335 }
2336 }
2337 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002339out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 return err;
2341}
2342
2343struct nfs4_closedata {
2344 struct inode *inode;
2345 struct nfs4_state *state;
2346 struct nfs_closeargs arg;
2347 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002348 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002349 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002350 bool roc;
2351 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352};
2353
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002354static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002355{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002356 struct nfs4_closedata *calldata = data;
2357 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002358 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002359
Fred Isamanf7e89172011-01-06 11:36:32 +00002360 if (calldata->roc)
2361 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002362 nfs4_put_open_state(calldata->state);
2363 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002364 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002365 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002366 kfree(calldata);
2367}
2368
Trond Myklebust88069f72009-12-08 08:33:16 -05002369static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2370 fmode_t fmode)
2371{
2372 spin_lock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002373 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002374 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
2375 case FMODE_WRITE:
2376 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2377 break;
2378 case FMODE_READ:
2379 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2380 break;
2381 case 0:
2382 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2383 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2384 clear_bit(NFS_OPEN_STATE, &state->flags);
2385 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002386 spin_unlock(&state->owner->so_lock);
2387}
2388
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002389static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002391 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 struct nfs_server *server = NFS_SERVER(calldata->inode);
2394
Chuck Levera3ca5652012-03-01 17:00:40 -05002395 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002396 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2397 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002398 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 /* hmm. we are done with the inode, and in the process of freeing
2400 * the state_owner. we keep this around to process errors
2401 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 switch (task->tk_status) {
2403 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002404 if (calldata->roc)
2405 pnfs_roc_set_barrier(state->inode,
2406 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002407 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002408 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002409 nfs4_close_clear_stateid_flags(state,
2410 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 break;
2412 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002413 case -NFS4ERR_OLD_STATEID:
2414 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002416 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002417 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002419 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2420 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002422 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002423 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002424 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425}
2426
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002427static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002429 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002430 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002431 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002432 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002433
Chuck Levera3ca5652012-03-01 17:00:40 -05002434 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002435 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002436 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002437
Trond Myklebust88069f72009-12-08 08:33:16 -05002438 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2439 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002440 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002441 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002442 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002443 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002444 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2445 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2446 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002447 }
2448 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002449 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2450 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2451 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002452 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002453 }
Trond Myklebust5d422302013-03-14 16:57:48 -04002454 if (!nfs4_valid_open_stateid(state))
2455 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002456 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002457
2458 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002459 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002460 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002461 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002462
Fred Isamanf7e89172011-01-06 11:36:32 +00002463 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002464 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002465 if (calldata->roc &&
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002466 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2467 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002468 goto out_wait;
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002469 }
Fred Isamanf7e89172011-01-06 11:36:32 +00002470 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002471
Trond Myklebust516a6af2005-10-27 22:12:41 -04002472 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002473 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002474 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002475 &calldata->arg.seq_args,
2476 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002477 task) != 0)
2478 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002479 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002480 return;
2481out_no_action:
2482 task->tk_action = NULL;
2483out_wait:
2484 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485}
2486
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002487static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002488 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002489 .rpc_call_done = nfs4_close_done,
2490 .rpc_release = nfs4_free_closedata,
2491};
2492
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493/*
2494 * It is possible for data to be read/written from a mem-mapped file
2495 * after the sys_close call (which hits the vfs layer as a flush).
2496 * This means that we can't safely call nfsv4 close on a file until
2497 * the inode is cleared. This in turn means that we are not good
2498 * NFSv4 citizens - we do not indicate to the server to update the file's
2499 * share state even when we are done with one of the three share
2500 * stateid's in the inode.
2501 *
2502 * NOTE: Caller must be holding the sp->so_owner semaphore!
2503 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002504int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002506 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002508 struct nfs4_state_owner *sp = state->owner;
2509 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002510 struct rpc_message msg = {
2511 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2512 .rpc_cred = state->owner->so_cred,
2513 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002514 struct rpc_task_setup task_setup_data = {
2515 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002516 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002517 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002518 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002519 .flags = RPC_TASK_ASYNC,
2520 };
Trond Myklebust95121352005-10-18 14:20:12 -07002521 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002523 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002525 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002526 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002527 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002529 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002530 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002532 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002533 if (calldata->arg.seqid == NULL)
2534 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002535 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002536 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002537 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002538 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002539 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002540 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002541 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002542
Trond Myklebust1174dd12010-12-21 10:52:24 -05002543 msg.rpc_argp = &calldata->arg;
2544 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002545 task_setup_data.callback_data = calldata;
2546 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002547 if (IS_ERR(task))
2548 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002549 status = 0;
2550 if (wait)
2551 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002552 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002553 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002554out_free_calldata:
2555 kfree(calldata);
2556out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002557 nfs4_put_open_state(state);
2558 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002559 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560}
2561
Trond Myklebust2b484292010-09-17 10:56:51 -04002562static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002563nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04002566 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2567
2568 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569
Trond Myklebust565277f2007-10-15 18:17:53 -04002570 /* Protect against concurrent sillydeletes */
Trond Myklebust959d9212013-06-28 16:29:51 -04002571 state = nfs4_do_open(dir, ctx, open_flags, attr, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04002572
2573 nfs4_label_release_security(label);
2574
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002575 if (IS_ERR(state))
2576 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04002577 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578}
2579
Trond Myklebust1185a552009-12-03 15:54:02 -05002580static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002581{
2582 if (ctx->state == NULL)
2583 return;
2584 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002585 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002586 else
Al Viro643168c2011-06-22 18:20:23 -04002587 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002588}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589
2590static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2591{
Benny Halevy43652ad2009-04-01 09:21:54 -04002592 struct nfs4_server_caps_arg args = {
2593 .fhandle = fhandle,
2594 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 struct nfs4_server_caps_res res = {};
2596 struct rpc_message msg = {
2597 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002598 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 .rpc_resp = &res,
2600 };
2601 int status;
2602
Bryan Schumaker7c513052011-03-24 17:12:24 +00002603 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 if (status == 0) {
2605 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002606 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2607 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2608 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2609 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2610 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2612 server->caps |= NFS_CAP_ACLS;
2613 if (res.has_links != 0)
2614 server->caps |= NFS_CAP_HARDLINKS;
2615 if (res.has_symlinks != 0)
2616 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002617 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2618 server->caps |= NFS_CAP_FILEID;
2619 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2620 server->caps |= NFS_CAP_MODE;
2621 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2622 server->caps |= NFS_CAP_NLINK;
2623 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2624 server->caps |= NFS_CAP_OWNER;
2625 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2626 server->caps |= NFS_CAP_OWNER_GROUP;
2627 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2628 server->caps |= NFS_CAP_ATIME;
2629 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2630 server->caps |= NFS_CAP_CTIME;
2631 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2632 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04002633#ifdef CONFIG_NFS_V4_SECURITY_LABEL
2634 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
2635 server->caps |= NFS_CAP_SECURITY_LABEL;
2636#endif
2637 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
2638 sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002639
David Quigleyaa9c2662013-05-22 12:50:44 -04002640 if (server->caps & NFS_CAP_SECURITY_LABEL) {
2641 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2642 res.attr_bitmask[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2643 }
Trond Myklebusta65318b2009-03-11 14:10:28 -04002644 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2645 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2646 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002648 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002650
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 return status;
2652}
2653
Trond Myklebust55a97592006-06-09 09:34:19 -04002654int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655{
2656 struct nfs4_exception exception = { };
2657 int err;
2658 do {
2659 err = nfs4_handle_exception(server,
2660 _nfs4_server_capabilities(server, fhandle),
2661 &exception);
2662 } while (exception.retry);
2663 return err;
2664}
2665
2666static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2667 struct nfs_fsinfo *info)
2668{
David Quigleyaa9c2662013-05-22 12:50:44 -04002669 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04002671 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 };
2673 struct nfs4_lookup_res res = {
2674 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002675 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 .fh = fhandle,
2677 };
2678 struct rpc_message msg = {
2679 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2680 .rpc_argp = &args,
2681 .rpc_resp = &res,
2682 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002683
David Quigleyaa9c2662013-05-22 12:50:44 -04002684 bitmask[0] = nfs4_fattr_bitmap[0];
2685 bitmask[1] = nfs4_fattr_bitmap[1];
2686 /*
2687 * Process the label in the upcoming getfattr
2688 */
2689 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
2690
Trond Myklebust0e574af2005-10-27 22:12:38 -04002691 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002692 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693}
2694
2695static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2696 struct nfs_fsinfo *info)
2697{
2698 struct nfs4_exception exception = { };
2699 int err;
2700 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002701 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04002702 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002703 switch (err) {
2704 case 0:
2705 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002706 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002707 default:
2708 err = nfs4_handle_exception(server, err, &exception);
2709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002711out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 return err;
2713}
2714
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002715static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2716 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2717{
2718 struct rpc_auth *auth;
2719 int ret;
2720
2721 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002722 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04002723 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002724 goto out;
2725 }
2726 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002727out:
2728 return ret;
2729}
2730
Chuck Lever9a744ba2013-03-16 15:56:02 -04002731/*
2732 * Retry pseudoroot lookup with various security flavors. We do this when:
2733 *
2734 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2735 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2736 *
2737 * Returns zero on success, or a negative NFS4ERR value, or a
2738 * negative errno value.
2739 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002740static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002741 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742{
Chuck Lever9a744ba2013-03-16 15:56:02 -04002743 /* Per 3530bis 15.33.5 */
2744 static const rpc_authflavor_t flav_array[] = {
2745 RPC_AUTH_GSS_KRB5P,
2746 RPC_AUTH_GSS_KRB5I,
2747 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04002748 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04002749 RPC_AUTH_NULL,
2750 };
2751 int status = -EPERM;
2752 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753
Chuck Lever9a744ba2013-03-16 15:56:02 -04002754 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002755 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002756 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002757 continue;
2758 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002759 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04002760
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002761 /*
2762 * -EACCESS could mean that the user doesn't have correct permissions
2763 * to access the mount. It could also mean that we tried to mount
2764 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2765 * existing mount programs don't handle -EACCES very well so it should
2766 * be mapped to -EPERM instead.
2767 */
2768 if (status == -EACCES)
2769 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002770 return status;
2771}
2772
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002773static int nfs4_do_find_root_sec(struct nfs_server *server,
2774 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
2775{
2776 int mv = server->nfs_client->cl_minorversion;
2777 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
2778}
2779
2780/**
2781 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
2782 * @server: initialized nfs_server handle
2783 * @fhandle: we fill in the pseudo-fs root file handle
2784 * @info: we fill in an FSINFO struct
2785 *
2786 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002787 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002788int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2789 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002790{
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002791 int status;
2792
2793 status = nfs4_lookup_root(server, fhandle, info);
2794 if ((status == -NFS4ERR_WRONGSEC) &&
2795 !(server->flags & NFS_MOUNT_SECFLAVOUR))
2796 status = nfs4_do_find_root_sec(server, fhandle, info);
2797
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 if (status == 0)
2799 status = nfs4_server_capabilities(server, fhandle);
2800 if (status == 0)
2801 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002802
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002803 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804}
2805
Bryan Schumakerbae36242012-05-10 15:07:31 -04002806static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2807 struct nfs_fsinfo *info)
2808{
2809 int error;
2810 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04002811 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04002812
2813 error = nfs4_server_capabilities(server, mntfh);
2814 if (error < 0) {
2815 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2816 return error;
2817 }
2818
David Quigley14c43f72013-05-22 12:50:43 -04002819 label = nfs4_label_alloc(server, GFP_KERNEL);
2820 if (IS_ERR(label))
2821 return PTR_ERR(label);
2822
David Quigley1775fd32013-05-22 12:50:42 -04002823 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04002824 if (error < 0) {
2825 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04002826 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04002827 }
2828
2829 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2830 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2831 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2832
David Quigley14c43f72013-05-22 12:50:43 -04002833err_free_label:
2834 nfs4_label_free(label);
2835
Bryan Schumakerbae36242012-05-10 15:07:31 -04002836 return error;
2837}
2838
Manoj Naik6b97fd32006-06-09 09:34:29 -04002839/*
2840 * Get locations and (maybe) other attributes of a referral.
2841 * Note that we'll actually follow the referral later when
2842 * we detect fsid mismatch in inode revalidation
2843 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002844static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2845 const struct qstr *name, struct nfs_fattr *fattr,
2846 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002847{
2848 int status = -ENOMEM;
2849 struct page *page = NULL;
2850 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002851
2852 page = alloc_page(GFP_KERNEL);
2853 if (page == NULL)
2854 goto out;
2855 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2856 if (locations == NULL)
2857 goto out;
2858
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002859 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002860 if (status != 0)
2861 goto out;
2862 /* Make sure server returned a different fsid for the referral */
2863 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002864 dprintk("%s: server did not return a different fsid for"
2865 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002866 status = -EIO;
2867 goto out;
2868 }
Andy Adamson533eb462011-06-13 18:25:56 -04002869 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2870 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002871
Andy Adamson533eb462011-06-13 18:25:56 -04002872 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002873 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002874 memset(fhandle, 0, sizeof(struct nfs_fh));
2875out:
2876 if (page)
2877 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002878 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002879 return status;
2880}
2881
David Quigley1775fd32013-05-22 12:50:42 -04002882static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
2883 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884{
2885 struct nfs4_getattr_arg args = {
2886 .fh = fhandle,
2887 .bitmask = server->attr_bitmask,
2888 };
2889 struct nfs4_getattr_res res = {
2890 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002891 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 .server = server,
2893 };
2894 struct rpc_message msg = {
2895 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2896 .rpc_argp = &args,
2897 .rpc_resp = &res,
2898 };
David Quigleyaa9c2662013-05-22 12:50:44 -04002899
2900 args.bitmask = nfs4_bitmask(server, label);
2901
Trond Myklebust0e574af2005-10-27 22:12:38 -04002902 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002903 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904}
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_exception exception = { };
2910 int err;
2911 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04002912 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
2913 trace_nfs4_getattr(server, fhandle, fattr, err);
2914 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 &exception);
2916 } while (exception.retry);
2917 return err;
2918}
2919
2920/*
2921 * The file is not closed if it is opened due to the a request to change
2922 * the size of the file. The open call will not be needed once the
2923 * VFS layer lookup-intents are implemented.
2924 *
2925 * Close is called when the inode is destroyed.
2926 * If we haven't opened the file for O_WRONLY, we
2927 * need to in the size_change case to obtain a stateid.
2928 *
2929 * Got race?
2930 * Because OPEN is always done by name in nfsv4, it is
2931 * possible that we opened a different file by the same
2932 * name. We can recognize this race condition, but we
2933 * can't do anything about it besides returning an error.
2934 *
2935 * This will be fixed with VFS changes (lookup-intent).
2936 */
2937static int
2938nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2939 struct iattr *sattr)
2940{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002941 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002942 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002943 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04002944 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 int status;
2946
Benny Halevy8a1636c2010-07-14 15:43:57 -04002947 if (pnfs_ld_layoutret_on_setattr(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04002948 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04002949
Trond Myklebust0e574af2005-10-27 22:12:38 -04002950 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951
Andy Adamson26699402012-05-30 16:12:24 -04002952 /* Deal with open(O_TRUNC) */
2953 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03002954 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04002955
2956 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03002957 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04002958 return 0;
2959
Trond Myklebustd5308382005-11-04 15:33:38 -05002960 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002961 if (sattr->ia_valid & ATTR_FILE) {
2962 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002963
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002964 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002965 if (ctx) {
2966 cred = ctx->cred;
2967 state = ctx->state;
2968 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002969 }
2970
David Quigley14c43f72013-05-22 12:50:43 -04002971 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
2972 if (IS_ERR(label))
2973 return PTR_ERR(label);
2974
2975 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04002976 if (status == 0) {
Trond Myklebust65e43082005-08-16 11:49:44 -04002977 nfs_setattr_update_inode(inode, sattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04002978 nfs_setsecurity(inode, fattr, label);
2979 }
David Quigley14c43f72013-05-22 12:50:43 -04002980 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 return status;
2982}
2983
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002984static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2985 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04002986 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04002987{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002988 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002989 int status;
2990 struct nfs4_lookup_arg args = {
2991 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002992 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002993 .name = name,
2994 };
2995 struct nfs4_lookup_res res = {
2996 .server = server,
2997 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04002998 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04002999 .fh = fhandle,
3000 };
3001 struct rpc_message msg = {
3002 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3003 .rpc_argp = &args,
3004 .rpc_resp = &res,
3005 };
3006
David Quigleyaa9c2662013-05-22 12:50:44 -04003007 args.bitmask = nfs4_bitmask(server, label);
3008
David Howells2b3de442006-08-22 20:06:09 -04003009 nfs_fattr_init(fattr);
3010
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003012 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 dprintk("NFS reply lookup: %d\n", status);
3014 return status;
3015}
3016
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003017static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003018{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003019 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003020 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003021 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3022 fattr->nlink = 2;
3023}
3024
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003025static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3026 struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003027 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003028{
3029 struct nfs4_exception exception = { };
3030 struct rpc_clnt *client = *clnt;
3031 int err;
3032 do {
David Quigley1775fd32013-05-22 12:50:42 -04003033 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003034 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003035 switch (err) {
3036 case -NFS4ERR_BADNAME:
3037 err = -ENOENT;
3038 goto out;
3039 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003040 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003041 goto out;
3042 case -NFS4ERR_WRONGSEC:
3043 err = -EPERM;
3044 if (client != *clnt)
3045 goto out;
3046
3047 client = nfs4_create_sec_client(client, dir, name);
3048 if (IS_ERR(client))
3049 return PTR_ERR(client);
3050
3051 exception.retry = 1;
3052 break;
3053 default:
3054 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3055 }
3056 } while (exception.retry);
3057
3058out:
3059 if (err == 0)
3060 *clnt = client;
3061 else if (client != *clnt)
3062 rpc_shutdown_client(client);
3063
3064 return err;
3065}
3066
Bryan Schumaker80a16b22012-04-27 13:27:46 -04003067static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003068 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3069 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003071 int status;
3072 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003073
David Quigley1775fd32013-05-22 12:50:42 -04003074 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003075 if (client != NFS_CLIENT(dir)) {
3076 rpc_shutdown_client(client);
3077 nfs_fixup_secinfo_attributes(fattr);
3078 }
3079 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080}
3081
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003082struct rpc_clnt *
3083nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3084 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3085{
3086 int status;
3087 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
3088
David Quigley1775fd32013-05-22 12:50:42 -04003089 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003090 if (status < 0) {
3091 rpc_shutdown_client(client);
3092 return ERR_PTR(status);
3093 }
3094 return client;
3095}
3096
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3098{
Trond Myklebust76b32992007-08-10 17:45:11 -04003099 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 struct nfs4_accessargs args = {
3101 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003102 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003104 struct nfs4_accessres res = {
3105 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003106 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 struct rpc_message msg = {
3108 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3109 .rpc_argp = &args,
3110 .rpc_resp = &res,
3111 .rpc_cred = entry->cred,
3112 };
3113 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003114 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115
3116 /*
3117 * Determine which access bits we want to ask for...
3118 */
3119 if (mode & MAY_READ)
3120 args.access |= NFS4_ACCESS_READ;
3121 if (S_ISDIR(inode->i_mode)) {
3122 if (mode & MAY_WRITE)
3123 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3124 if (mode & MAY_EXEC)
3125 args.access |= NFS4_ACCESS_LOOKUP;
3126 } else {
3127 if (mode & MAY_WRITE)
3128 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3129 if (mode & MAY_EXEC)
3130 args.access |= NFS4_ACCESS_EXECUTE;
3131 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003132
3133 res.fattr = nfs_alloc_fattr();
3134 if (res.fattr == NULL)
3135 return -ENOMEM;
3136
Bryan Schumaker7c513052011-03-24 17:12:24 +00003137 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003139 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003140 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003142 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 return status;
3144}
3145
3146static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3147{
3148 struct nfs4_exception exception = { };
3149 int err;
3150 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003151 err = _nfs4_proc_access(inode, entry);
3152 trace_nfs4_access(inode, err);
3153 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 &exception);
3155 } while (exception.retry);
3156 return err;
3157}
3158
3159/*
3160 * TODO: For the time being, we don't try to get any attributes
3161 * along with any of the zero-copy operations READ, READDIR,
3162 * READLINK, WRITE.
3163 *
3164 * In the case of the first three, we want to put the GETATTR
3165 * after the read-type operation -- this is because it is hard
3166 * to predict the length of a GETATTR response in v4, and thus
3167 * align the READ data correctly. This means that the GETATTR
3168 * may end up partially falling into the page cache, and we should
3169 * shift it into the 'tail' of the xdr_buf before processing.
3170 * To do this efficiently, we need to know the total length
3171 * of data received, which doesn't seem to be available outside
3172 * of the RPC layer.
3173 *
3174 * In the case of WRITE, we also want to put the GETATTR after
3175 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003176 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 *
3178 * Both of these changes to the XDR layer would in fact be quite
3179 * minor, but I decided to leave them for a subsequent patch.
3180 */
3181static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3182 unsigned int pgbase, unsigned int pglen)
3183{
3184 struct nfs4_readlink args = {
3185 .fh = NFS_FH(inode),
3186 .pgbase = pgbase,
3187 .pglen = pglen,
3188 .pages = &page,
3189 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003190 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 struct rpc_message msg = {
3192 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3193 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003194 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 };
3196
Bryan Schumaker7c513052011-03-24 17:12:24 +00003197 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 -07003198}
3199
3200static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3201 unsigned int pgbase, unsigned int pglen)
3202{
3203 struct nfs4_exception exception = { };
3204 int err;
3205 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003206 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3207 trace_nfs4_readlink(inode, err);
3208 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 &exception);
3210 } while (exception.retry);
3211 return err;
3212}
3213
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003215 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217static int
3218nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003219 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220{
David Quigleyaa9c2662013-05-22 12:50:44 -04003221 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003222 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 int status = 0;
3225
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003226 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3227 if (IS_ERR(ctx))
3228 return PTR_ERR(ctx);
3229
David Quigleyaa9c2662013-05-22 12:50:44 -04003230 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3231
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003232 sattr->ia_mode &= ~current_umask();
Trond Myklebust959d9212013-06-28 16:29:51 -04003233 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 if (IS_ERR(state)) {
3235 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003236 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003239 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003240 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 return status;
3242}
3243
3244static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3245{
Trond Myklebust16e42952005-10-27 22:12:44 -04003246 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003247 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003249 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003251 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003252 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003253 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003255 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3256 .rpc_argp = &args,
3257 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003259 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003260
Bryan Schumaker7c513052011-03-24 17:12:24 +00003261 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003262 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003263 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 return status;
3265}
3266
3267static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3268{
3269 struct nfs4_exception exception = { };
3270 int err;
3271 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003272 err = _nfs4_proc_remove(dir, name);
3273 trace_nfs4_remove(dir, name, err);
3274 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 &exception);
3276 } while (exception.retry);
3277 return err;
3278}
3279
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003280static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003282 struct nfs_server *server = NFS_SERVER(dir);
3283 struct nfs_removeargs *args = msg->rpc_argp;
3284 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003286 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003288 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003289
3290 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291}
3292
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003293static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3294{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003295 nfs4_setup_sequence(NFS_SERVER(data->dir),
3296 &data->args.seq_args,
3297 &data->res.seq_res,
3298 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299}
3300
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003301static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003303 struct nfs_unlinkdata *data = task->tk_calldata;
3304 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003305
Trond Myklebust14516c32010-07-31 14:29:06 -04003306 if (!nfs4_sequence_done(task, &res->seq_res))
3307 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003308 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003309 return 0;
3310 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003311 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312}
3313
Jeff Laytond3d41522010-09-17 17:31:57 -04003314static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3315{
3316 struct nfs_server *server = NFS_SERVER(dir);
3317 struct nfs_renameargs *arg = msg->rpc_argp;
3318 struct nfs_renameres *res = msg->rpc_resp;
3319
3320 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003321 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003322 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003323}
3324
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003325static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3326{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003327 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3328 &data->args.seq_args,
3329 &data->res.seq_res,
3330 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003331}
3332
3333static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3334 struct inode *new_dir)
3335{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003336 struct nfs_renamedata *data = task->tk_calldata;
3337 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04003338
3339 if (!nfs4_sequence_done(task, &res->seq_res))
3340 return 0;
3341 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3342 return 0;
3343
3344 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003345 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003346 return 1;
3347}
3348
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3350 struct inode *new_dir, struct qstr *new_name)
3351{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003352 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003353 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 .old_dir = NFS_FH(old_dir),
3355 .new_dir = NFS_FH(new_dir),
3356 .old_name = old_name,
3357 .new_name = new_name,
3358 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003359 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003360 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003361 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 struct rpc_message msg = {
3363 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3364 .rpc_argp = &arg,
3365 .rpc_resp = &res,
3366 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003367 int status = -ENOMEM;
David Quigley14c43f72013-05-22 12:50:43 -04003368
Bryan Schumaker7c513052011-03-24 17:12:24 +00003369 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 if (!status) {
3371 update_changeattr(old_dir, &res.old_cinfo);
3372 update_changeattr(new_dir, &res.new_cinfo);
3373 }
3374 return status;
3375}
3376
3377static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3378 struct inode *new_dir, struct qstr *new_name)
3379{
3380 struct nfs4_exception exception = { };
3381 int err;
3382 do {
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003383 err = _nfs4_proc_rename(old_dir, old_name,
3384 new_dir, new_name);
3385 trace_nfs4_rename(old_dir, old_name, new_dir, new_name, err);
3386 err = nfs4_handle_exception(NFS_SERVER(old_dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 &exception);
3388 } while (exception.retry);
3389 return err;
3390}
3391
3392static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3393{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003394 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 struct nfs4_link_arg arg = {
3396 .fh = NFS_FH(inode),
3397 .dir_fh = NFS_FH(dir),
3398 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003399 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003401 struct nfs4_link_res res = {
3402 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003403 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003404 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 struct rpc_message msg = {
3406 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3407 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003408 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003410 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411
Trond Myklebust136f2622010-04-16 16:22:49 -04003412 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003413 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003414 goto out;
3415
David Quigley14c43f72013-05-22 12:50:43 -04003416 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3417 if (IS_ERR(res.label)) {
3418 status = PTR_ERR(res.label);
3419 goto out;
3420 }
David Quigleyaa9c2662013-05-22 12:50:44 -04003421 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04003422
Bryan Schumaker7c513052011-03-24 17:12:24 +00003423 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003424 if (!status) {
3425 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04003426 status = nfs_post_op_update_inode(inode, res.fattr);
3427 if (!status)
3428 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003429 }
David Quigley14c43f72013-05-22 12:50:43 -04003430
3431
3432 nfs4_label_free(res.label);
3433
Trond Myklebust136f2622010-04-16 16:22:49 -04003434out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003435 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 return status;
3437}
3438
3439static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3440{
3441 struct nfs4_exception exception = { };
3442 int err;
3443 do {
3444 err = nfs4_handle_exception(NFS_SERVER(inode),
3445 _nfs4_proc_link(inode, dir, name),
3446 &exception);
3447 } while (exception.retry);
3448 return err;
3449}
3450
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003451struct nfs4_createdata {
3452 struct rpc_message msg;
3453 struct nfs4_create_arg arg;
3454 struct nfs4_create_res res;
3455 struct nfs_fh fh;
3456 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003457 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003458};
3459
3460static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3461 struct qstr *name, struct iattr *sattr, u32 ftype)
3462{
3463 struct nfs4_createdata *data;
3464
3465 data = kzalloc(sizeof(*data), GFP_KERNEL);
3466 if (data != NULL) {
3467 struct nfs_server *server = NFS_SERVER(dir);
3468
David Quigley14c43f72013-05-22 12:50:43 -04003469 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3470 if (IS_ERR(data->label))
3471 goto out_free;
3472
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003473 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3474 data->msg.rpc_argp = &data->arg;
3475 data->msg.rpc_resp = &data->res;
3476 data->arg.dir_fh = NFS_FH(dir);
3477 data->arg.server = server;
3478 data->arg.name = name;
3479 data->arg.attrs = sattr;
3480 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04003481 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003482 data->res.server = server;
3483 data->res.fh = &data->fh;
3484 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003485 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003486 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003487 }
3488 return data;
David Quigley14c43f72013-05-22 12:50:43 -04003489out_free:
3490 kfree(data);
3491 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003492}
3493
3494static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3495{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003496 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003497 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003498 if (status == 0) {
3499 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04003500 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003501 }
3502 return status;
3503}
3504
3505static void nfs4_free_createdata(struct nfs4_createdata *data)
3506{
David Quigley14c43f72013-05-22 12:50:43 -04003507 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003508 kfree(data);
3509}
3510
Chuck Lever4f390c12006-08-22 20:06:22 -04003511static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003512 struct page *page, unsigned int len, struct iattr *sattr,
3513 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003515 struct nfs4_createdata *data;
3516 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517
Chuck Lever94a6d752006-08-22 20:06:23 -04003518 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003519 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003520
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003521 status = -ENOMEM;
3522 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3523 if (data == NULL)
3524 goto out;
3525
3526 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3527 data->arg.u.symlink.pages = &page;
3528 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04003529 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003531 status = nfs4_do_create(dir, dentry, data);
3532
3533 nfs4_free_createdata(data);
3534out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 return status;
3536}
3537
Chuck Lever4f390c12006-08-22 20:06:22 -04003538static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003539 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540{
3541 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003542 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04003544
3545 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3546
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003548 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3549 trace_nfs4_symlink(dir, &dentry->d_name, err);
3550 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 &exception);
3552 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003553
3554 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 return err;
3556}
3557
3558static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003559 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003561 struct nfs4_createdata *data;
3562 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003564 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3565 if (data == NULL)
3566 goto out;
3567
David Quigley1775fd32013-05-22 12:50:42 -04003568 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003569 status = nfs4_do_create(dir, dentry, data);
3570
3571 nfs4_free_createdata(data);
3572out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 return status;
3574}
3575
3576static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3577 struct iattr *sattr)
3578{
3579 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003580 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003582
David Quigleyaa9c2662013-05-22 12:50:44 -04003583 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3584
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003585 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003587 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3588 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3589 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 &exception);
3591 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003592 nfs4_label_release_security(label);
3593
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 return err;
3595}
3596
3597static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003598 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599{
3600 struct inode *dir = dentry->d_inode;
3601 struct nfs4_readdir_arg args = {
3602 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003603 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 .pgbase = 0,
3605 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003606 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003607 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 };
3609 struct nfs4_readdir_res res;
3610 struct rpc_message msg = {
3611 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3612 .rpc_argp = &args,
3613 .rpc_resp = &res,
3614 .rpc_cred = cred,
3615 };
3616 int status;
3617
Harvey Harrison3110ff82008-05-02 13:42:44 -07003618 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003619 dentry->d_parent->d_name.name,
3620 dentry->d_name.name,
3621 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003622 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003624 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 -05003625 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003626 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003627 status += args.pgbase;
3628 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003629
3630 nfs_invalidate_atime(dir);
3631
Harvey Harrison3110ff82008-05-02 13:42:44 -07003632 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 return status;
3634}
3635
3636static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003637 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638{
3639 struct nfs4_exception exception = { };
3640 int err;
3641 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003642 err = _nfs4_proc_readdir(dentry, cred, cookie,
3643 pages, count, plus);
3644 trace_nfs4_readdir(dentry->d_inode, err);
3645 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 &exception);
3647 } while (exception.retry);
3648 return err;
3649}
3650
3651static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04003652 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003654 struct nfs4_createdata *data;
3655 int mode = sattr->ia_mode;
3656 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003658 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3659 if (data == NULL)
3660 goto out;
3661
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003663 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003665 data->arg.ftype = NF4BLK;
3666 data->arg.u.device.specdata1 = MAJOR(rdev);
3667 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 }
3669 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003670 data->arg.ftype = NF4CHR;
3671 data->arg.u.device.specdata1 = MAJOR(rdev);
3672 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003673 } else if (!S_ISSOCK(mode)) {
3674 status = -EINVAL;
3675 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 }
David Quigley1775fd32013-05-22 12:50:42 -04003677
David Quigleyaa9c2662013-05-22 12:50:44 -04003678 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003679 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003680out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003681 nfs4_free_createdata(data);
3682out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 return status;
3684}
3685
3686static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3687 struct iattr *sattr, dev_t rdev)
3688{
3689 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003690 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003692
David Quigleyaa9c2662013-05-22 12:50:44 -04003693 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3694
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003695 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003697 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
3698 trace_nfs4_mknod(dir, &dentry->d_name, err);
3699 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 &exception);
3701 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003702
3703 nfs4_label_release_security(label);
3704
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 return err;
3706}
3707
3708static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3709 struct nfs_fsstat *fsstat)
3710{
3711 struct nfs4_statfs_arg args = {
3712 .fh = fhandle,
3713 .bitmask = server->attr_bitmask,
3714 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003715 struct nfs4_statfs_res res = {
3716 .fsstat = fsstat,
3717 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 struct rpc_message msg = {
3719 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3720 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003721 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 };
3723
Trond Myklebust0e574af2005-10-27 22:12:38 -04003724 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003725 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726}
3727
3728static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3729{
3730 struct nfs4_exception exception = { };
3731 int err;
3732 do {
3733 err = nfs4_handle_exception(server,
3734 _nfs4_proc_statfs(server, fhandle, fsstat),
3735 &exception);
3736 } while (exception.retry);
3737 return err;
3738}
3739
3740static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3741 struct nfs_fsinfo *fsinfo)
3742{
3743 struct nfs4_fsinfo_arg args = {
3744 .fh = fhandle,
3745 .bitmask = server->attr_bitmask,
3746 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003747 struct nfs4_fsinfo_res res = {
3748 .fsinfo = fsinfo,
3749 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 struct rpc_message msg = {
3751 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3752 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003753 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 };
3755
Bryan Schumaker7c513052011-03-24 17:12:24 +00003756 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757}
3758
3759static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3760{
3761 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04003762 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 int err;
3764
3765 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04003766 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003767 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04003768 if (err == 0) {
3769 struct nfs_client *clp = server->nfs_client;
3770
3771 spin_lock(&clp->cl_lock);
3772 clp->cl_lease_time = fsinfo->lease_time * HZ;
3773 clp->cl_last_renewal = now;
3774 spin_unlock(&clp->cl_lock);
3775 break;
3776 }
3777 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 } while (exception.retry);
3779 return err;
3780}
3781
3782static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3783{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003784 int error;
3785
Trond Myklebust0e574af2005-10-27 22:12:38 -04003786 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003787 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003788 if (error == 0) {
3789 /* block layout checks this! */
3790 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003791 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003792 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003793
3794 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795}
3796
3797static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3798 struct nfs_pathconf *pathconf)
3799{
3800 struct nfs4_pathconf_arg args = {
3801 .fh = fhandle,
3802 .bitmask = server->attr_bitmask,
3803 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003804 struct nfs4_pathconf_res res = {
3805 .pathconf = pathconf,
3806 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 struct rpc_message msg = {
3808 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3809 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003810 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 };
3812
3813 /* None of the pathconf attributes are mandatory to implement */
3814 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3815 memset(pathconf, 0, sizeof(*pathconf));
3816 return 0;
3817 }
3818
Trond Myklebust0e574af2005-10-27 22:12:38 -04003819 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003820 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821}
3822
3823static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3824 struct nfs_pathconf *pathconf)
3825{
3826 struct nfs4_exception exception = { };
3827 int err;
3828
3829 do {
3830 err = nfs4_handle_exception(server,
3831 _nfs4_proc_pathconf(server, fhandle, pathconf),
3832 &exception);
3833 } while (exception.retry);
3834 return err;
3835}
3836
Trond Myklebust5521abf2013-03-16 20:54:34 -04003837int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04003838 const struct nfs_open_context *ctx,
3839 const struct nfs_lock_context *l_ctx,
3840 fmode_t fmode)
3841{
3842 const struct nfs_lockowner *lockowner = NULL;
3843
3844 if (l_ctx != NULL)
3845 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003846 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04003847}
3848EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
3849
Trond Myklebust5521abf2013-03-16 20:54:34 -04003850static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
3851 const struct nfs_open_context *ctx,
3852 const struct nfs_lock_context *l_ctx,
3853 fmode_t fmode)
3854{
3855 nfs4_stateid current_stateid;
3856
3857 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode))
3858 return false;
3859 return nfs4_stateid_match(stateid, &current_stateid);
3860}
3861
3862static bool nfs4_error_stateid_expired(int err)
3863{
3864 switch (err) {
3865 case -NFS4ERR_DELEG_REVOKED:
3866 case -NFS4ERR_ADMIN_REVOKED:
3867 case -NFS4ERR_BAD_STATEID:
3868 case -NFS4ERR_STALE_STATEID:
3869 case -NFS4ERR_OLD_STATEID:
3870 case -NFS4ERR_OPENMODE:
3871 case -NFS4ERR_EXPIRED:
3872 return true;
3873 }
3874 return false;
3875}
3876
Benny Halevyd20581a2011-05-22 19:52:03 +03003877void __nfs4_read_done_cb(struct nfs_read_data *data)
3878{
Fred Isamancd841602012-04-20 14:47:44 -04003879 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003880}
3881
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003882static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883{
Fred Isamancd841602012-04-20 14:47:44 -04003884 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885
Trond Myklebustcc668ab2013-08-14 15:31:28 -04003886 trace_nfs4_read(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003887 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003888 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003889 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003891
Benny Halevyd20581a2011-05-22 19:52:03 +03003892 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003894 renew_lease(server, data->timestamp);
3895 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896}
3897
Trond Myklebust5521abf2013-03-16 20:54:34 -04003898static bool nfs4_read_stateid_changed(struct rpc_task *task,
3899 struct nfs_readargs *args)
3900{
3901
3902 if (!nfs4_error_stateid_expired(task->tk_status) ||
3903 nfs4_stateid_is_current(&args->stateid,
3904 args->context,
3905 args->lock_context,
3906 FMODE_READ))
3907 return false;
3908 rpc_restart_call_prepare(task);
3909 return true;
3910}
3911
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003912static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3913{
3914
3915 dprintk("--> %s\n", __func__);
3916
3917 if (!nfs4_sequence_done(task, &data->res.seq_res))
3918 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003919 if (nfs4_read_stateid_changed(task, &data->args))
3920 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003921 return data->read_done_cb ? data->read_done_cb(task, data) :
3922 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003923}
3924
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003925static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003928 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003929 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003930 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931}
3932
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003933static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3934{
Trond Myklebust9b206142013-03-17 15:52:00 -04003935 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003936 &data->args.seq_args,
3937 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04003938 task))
3939 return;
3940 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3941 data->args.lock_context, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942}
3943
Fred Isamanb029bc92011-03-03 15:13:42 +00003944static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945{
Fred Isamancd841602012-04-20 14:47:44 -04003946 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947
Trond Myklebustcc668ab2013-08-14 15:31:28 -04003948 trace_nfs4_write(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003949 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003950 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003951 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003953 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003955 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003956 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003957 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958}
3959
Trond Myklebust5521abf2013-03-16 20:54:34 -04003960static bool nfs4_write_stateid_changed(struct rpc_task *task,
3961 struct nfs_writeargs *args)
3962{
3963
3964 if (!nfs4_error_stateid_expired(task->tk_status) ||
3965 nfs4_stateid_is_current(&args->stateid,
3966 args->context,
3967 args->lock_context,
3968 FMODE_WRITE))
3969 return false;
3970 rpc_restart_call_prepare(task);
3971 return true;
3972}
3973
Fred Isamanb029bc92011-03-03 15:13:42 +00003974static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3975{
3976 if (!nfs4_sequence_done(task, &data->res.seq_res))
3977 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003978 if (nfs4_write_stateid_changed(task, &data->args))
3979 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003980 return data->write_done_cb ? data->write_done_cb(task, data) :
3981 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003982}
3983
Trond Myklebust5a37f852012-04-28 14:55:16 -04003984static
3985bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003986{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003987 const struct nfs_pgio_header *hdr = data->header;
3988
3989 /* Don't request attributes for pNFS or O_DIRECT writes */
3990 if (data->ds_clp != NULL || hdr->dreq != NULL)
3991 return false;
3992 /* Otherwise, request attributes if and only if we don't hold
3993 * a delegation
3994 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003995 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003996}
Fred Isamana69aef12011-03-03 15:13:47 +00003997
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003998static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999{
Fred Isamancd841602012-04-20 14:47:44 -04004000 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004001
Trond Myklebust5a37f852012-04-28 14:55:16 -04004002 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00004003 data->args.bitmask = NULL;
4004 data->res.fattr = NULL;
4005 } else
4006 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004007
Fred Isamanb029bc92011-03-03 15:13:42 +00004008 if (!data->write_done_cb)
4009 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004010 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 data->timestamp = jiffies;
4012
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004013 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05004014 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015}
4016
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04004017static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
4018{
Trond Myklebust9b206142013-03-17 15:52:00 -04004019 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004020 &data->args.seq_args,
4021 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004022 task))
4023 return;
4024 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4025 data->args.lock_context, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026}
4027
Fred Isaman0b7c0152012-04-20 14:47:39 -04004028static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4029{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004030 nfs4_setup_sequence(NFS_SERVER(data->inode),
4031 &data->args.seq_args,
4032 &data->res.seq_res,
4033 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034}
4035
Fred Isaman0b7c0152012-04-20 14:47:39 -04004036static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004039
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004040 trace_nfs4_commit(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004041 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004042 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004043 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004045 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046}
4047
Fred Isaman0b7c0152012-04-20 14:47:39 -04004048static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004049{
4050 if (!nfs4_sequence_done(task, &data->res.seq_res))
4051 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004052 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004053}
4054
Fred Isaman0b7c0152012-04-20 14:47:39 -04004055static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004057 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004058
Fred Isaman0b7c0152012-04-20 14:47:39 -04004059 if (data->commit_done_cb == NULL)
4060 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004061 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004062 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05004063 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064}
4065
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004066struct nfs4_renewdata {
4067 struct nfs_client *client;
4068 unsigned long timestamp;
4069};
4070
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071/*
4072 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4073 * standalone procedure for queueing an asynchronous RENEW.
4074 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004075static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004076{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004077 struct nfs4_renewdata *data = calldata;
4078 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004079
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004080 if (atomic_read(&clp->cl_count) > 1)
4081 nfs4_schedule_state_renewal(clp);
4082 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004083 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004084}
4085
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004086static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004088 struct nfs4_renewdata *data = calldata;
4089 struct nfs_client *clp = data->client;
4090 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004092 trace_nfs4_renew_async(clp, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04004094 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004095 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4096 return;
4097 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004098 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004099 return;
4100 }
4101 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004103 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104}
4105
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004106static const struct rpc_call_ops nfs4_renew_ops = {
4107 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004108 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004109};
4110
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004111static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112{
4113 struct rpc_message msg = {
4114 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4115 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004116 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004118 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004120 if (renew_flags == 0)
4121 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004122 if (!atomic_inc_not_zero(&clp->cl_count))
4123 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004124 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004125 if (data == NULL)
4126 return -ENOMEM;
4127 data->client = clp;
4128 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004129 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004130 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131}
4132
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004133static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134{
4135 struct rpc_message msg = {
4136 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4137 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004138 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 };
4140 unsigned long now = jiffies;
4141 int status;
4142
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004143 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 if (status < 0)
4145 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004146 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 return 0;
4148}
4149
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004150static inline int nfs4_server_supports_acls(struct nfs_server *server)
4151{
4152 return (server->caps & NFS_CAP_ACLS)
4153 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
4154 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
4155}
4156
Trond Myklebust21f498c2012-08-24 10:59:25 -04004157/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4158 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004159 * the stack.
4160 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004161#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004162
Neil Hormane9e3d722011-03-04 19:26:03 -05004163static int buf_to_pages_noslab(const void *buf, size_t buflen,
4164 struct page **pages, unsigned int *pgbase)
4165{
4166 struct page *newpage, **spages;
4167 int rc = 0;
4168 size_t len;
4169 spages = pages;
4170
4171 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004172 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004173 newpage = alloc_page(GFP_KERNEL);
4174
4175 if (newpage == NULL)
4176 goto unwind;
4177 memcpy(page_address(newpage), buf, len);
4178 buf += len;
4179 buflen -= len;
4180 *pages++ = newpage;
4181 rc++;
4182 } while (buflen != 0);
4183
4184 return rc;
4185
4186unwind:
4187 for(; rc > 0; rc--)
4188 __free_page(spages[rc-1]);
4189 return -ENOMEM;
4190}
4191
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004192struct nfs4_cached_acl {
4193 int cached;
4194 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004195 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004196};
4197
4198static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004199{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004200 struct nfs_inode *nfsi = NFS_I(inode);
4201
4202 spin_lock(&inode->i_lock);
4203 kfree(nfsi->nfs4_acl);
4204 nfsi->nfs4_acl = acl;
4205 spin_unlock(&inode->i_lock);
4206}
4207
4208static void nfs4_zap_acl_attr(struct inode *inode)
4209{
4210 nfs4_set_cached_acl(inode, NULL);
4211}
4212
4213static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4214{
4215 struct nfs_inode *nfsi = NFS_I(inode);
4216 struct nfs4_cached_acl *acl;
4217 int ret = -ENOENT;
4218
4219 spin_lock(&inode->i_lock);
4220 acl = nfsi->nfs4_acl;
4221 if (acl == NULL)
4222 goto out;
4223 if (buf == NULL) /* user is just asking for length */
4224 goto out_len;
4225 if (acl->cached == 0)
4226 goto out;
4227 ret = -ERANGE; /* see getxattr(2) man page */
4228 if (acl->len > buflen)
4229 goto out;
4230 memcpy(buf, acl->data, acl->len);
4231out_len:
4232 ret = acl->len;
4233out:
4234 spin_unlock(&inode->i_lock);
4235 return ret;
4236}
4237
Sachin Prabhu5794d212012-04-17 14:36:40 +01004238static 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 +00004239{
4240 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004241 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004242
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004243 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004244 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004245 if (acl == NULL)
4246 goto out;
4247 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004248 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004249 } else {
4250 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4251 if (acl == NULL)
4252 goto out;
4253 acl->cached = 0;
4254 }
4255 acl->len = acl_len;
4256out:
4257 nfs4_set_cached_acl(inode, acl);
4258}
4259
Andy Adamsonbf118a32011-12-07 11:55:27 -05004260/*
4261 * The getxattr API returns the required buffer length when called with a
4262 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4263 * the required buf. On a NULL buf, we send a page of data to the server
4264 * guessing that the ACL request can be serviced by a page. If so, we cache
4265 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4266 * the cache. If not so, we throw away the page, and cache the required
4267 * length. The next getxattr call will then produce another round trip to
4268 * the server, this time with the input buf of the required size.
4269 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004270static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004271{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004272 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004273 struct nfs_getaclargs args = {
4274 .fh = NFS_FH(inode),
4275 .acl_pages = pages,
4276 .acl_len = buflen,
4277 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004278 struct nfs_getaclres res = {
4279 .acl_len = buflen,
4280 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004281 struct rpc_message msg = {
4282 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4283 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004284 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004285 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004286 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4287 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004288
Andy Adamsonbf118a32011-12-07 11:55:27 -05004289 /* As long as we're doing a round trip to the server anyway,
4290 * let's be prepared for a page of acl data. */
4291 if (npages == 0)
4292 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004293 if (npages > ARRAY_SIZE(pages))
4294 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004295
Andy Adamsonbf118a32011-12-07 11:55:27 -05004296 for (i = 0; i < npages; i++) {
4297 pages[i] = alloc_page(GFP_KERNEL);
4298 if (!pages[i])
4299 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004300 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004301
4302 /* for decoding across pages */
4303 res.acl_scratch = alloc_page(GFP_KERNEL);
4304 if (!res.acl_scratch)
4305 goto out_free;
4306
Andy Adamsonbf118a32011-12-07 11:55:27 -05004307 args.acl_len = npages * PAGE_SIZE;
4308 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004309
Peng Taode040be2012-01-10 22:42:47 +08004310 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004311 __func__, buf, buflen, npages, args.acl_len);
4312 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4313 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004314 if (ret)
4315 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004316
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004317 /* Handle the case where the passed-in buffer is too short */
4318 if (res.acl_flags & NFS4_ACL_TRUNC) {
4319 /* Did the user only issue a request for the acl length? */
4320 if (buf == NULL)
4321 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004322 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004323 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004324 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004325 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004326 if (buf) {
4327 if (res.acl_len > buflen) {
4328 ret = -ERANGE;
4329 goto out_free;
4330 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004331 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004332 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004333out_ok:
4334 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004335out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004336 for (i = 0; i < npages; i++)
4337 if (pages[i])
4338 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004339 if (res.acl_scratch)
4340 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004341 return ret;
4342}
4343
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004344static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4345{
4346 struct nfs4_exception exception = { };
4347 ssize_t ret;
4348 do {
4349 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04004350 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004351 if (ret >= 0)
4352 break;
4353 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4354 } while (exception.retry);
4355 return ret;
4356}
4357
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004358static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4359{
4360 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004361 int ret;
4362
4363 if (!nfs4_server_supports_acls(server))
4364 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004365 ret = nfs_revalidate_inode(server, inode);
4366 if (ret < 0)
4367 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004368 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4369 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004370 ret = nfs4_read_cached_acl(inode, buf, buflen);
4371 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004372 /* -ENOENT is returned if there is no ACL or if there is an ACL
4373 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004374 return ret;
4375 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004376}
4377
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004378static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004379{
4380 struct nfs_server *server = NFS_SERVER(inode);
4381 struct page *pages[NFS4ACL_MAXPAGES];
4382 struct nfs_setaclargs arg = {
4383 .fh = NFS_FH(inode),
4384 .acl_pages = pages,
4385 .acl_len = buflen,
4386 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004387 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004388 struct rpc_message msg = {
4389 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4390 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004391 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004392 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004393 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004394 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004395
4396 if (!nfs4_server_supports_acls(server))
4397 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004398 if (npages > ARRAY_SIZE(pages))
4399 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004400 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4401 if (i < 0)
4402 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004403 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004404 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004405
4406 /*
4407 * Free each page after tx, so the only ref left is
4408 * held by the network stack
4409 */
4410 for (; i > 0; i--)
4411 put_page(pages[i-1]);
4412
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004413 /*
4414 * Acl update can result in inode attribute update.
4415 * so mark the attribute cache invalid.
4416 */
4417 spin_lock(&inode->i_lock);
4418 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4419 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004420 nfs_access_zap_cache(inode);
4421 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004422 return ret;
4423}
4424
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004425static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4426{
4427 struct nfs4_exception exception = { };
4428 int err;
4429 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004430 err = __nfs4_proc_set_acl(inode, buf, buflen);
4431 trace_nfs4_set_acl(inode, err);
4432 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004433 &exception);
4434 } while (exception.retry);
4435 return err;
4436}
4437
David Quigleyaa9c2662013-05-22 12:50:44 -04004438#ifdef CONFIG_NFS_V4_SECURITY_LABEL
4439static int _nfs4_get_security_label(struct inode *inode, void *buf,
4440 size_t buflen)
4441{
4442 struct nfs_server *server = NFS_SERVER(inode);
4443 struct nfs_fattr fattr;
4444 struct nfs4_label label = {0, 0, buflen, buf};
4445
4446 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4447 struct nfs4_getattr_arg args = {
4448 .fh = NFS_FH(inode),
4449 .bitmask = bitmask,
4450 };
4451 struct nfs4_getattr_res res = {
4452 .fattr = &fattr,
4453 .label = &label,
4454 .server = server,
4455 };
4456 struct rpc_message msg = {
4457 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4458 .rpc_argp = &args,
4459 .rpc_resp = &res,
4460 };
4461 int ret;
4462
4463 nfs_fattr_init(&fattr);
4464
4465 ret = rpc_call_sync(server->client, &msg, 0);
4466 if (ret)
4467 return ret;
4468 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4469 return -ENOENT;
4470 if (buflen < label.len)
4471 return -ERANGE;
4472 return 0;
4473}
4474
4475static int nfs4_get_security_label(struct inode *inode, void *buf,
4476 size_t buflen)
4477{
4478 struct nfs4_exception exception = { };
4479 int err;
4480
4481 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4482 return -EOPNOTSUPP;
4483
4484 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004485 err = _nfs4_get_security_label(inode, buf, buflen);
4486 trace_nfs4_get_security_label(inode, err);
4487 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004488 &exception);
4489 } while (exception.retry);
4490 return err;
4491}
4492
4493static int _nfs4_do_set_security_label(struct inode *inode,
4494 struct nfs4_label *ilabel,
4495 struct nfs_fattr *fattr,
4496 struct nfs4_label *olabel)
4497{
4498
4499 struct iattr sattr = {0};
4500 struct nfs_server *server = NFS_SERVER(inode);
4501 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4502 struct nfs_setattrargs args = {
4503 .fh = NFS_FH(inode),
4504 .iap = &sattr,
4505 .server = server,
4506 .bitmask = bitmask,
4507 .label = ilabel,
4508 };
4509 struct nfs_setattrres res = {
4510 .fattr = fattr,
4511 .label = olabel,
4512 .server = server,
4513 };
4514 struct rpc_message msg = {
4515 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
4516 .rpc_argp = &args,
4517 .rpc_resp = &res,
4518 };
4519 int status;
4520
4521 nfs4_stateid_copy(&args.stateid, &zero_stateid);
4522
4523 status = rpc_call_sync(server->client, &msg, 0);
4524 if (status)
4525 dprintk("%s failed: %d\n", __func__, status);
4526
4527 return status;
4528}
4529
4530static int nfs4_do_set_security_label(struct inode *inode,
4531 struct nfs4_label *ilabel,
4532 struct nfs_fattr *fattr,
4533 struct nfs4_label *olabel)
4534{
4535 struct nfs4_exception exception = { };
4536 int err;
4537
4538 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004539 err = _nfs4_do_set_security_label(inode, ilabel,
4540 fattr, olabel);
4541 trace_nfs4_set_security_label(inode, err);
4542 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004543 &exception);
4544 } while (exception.retry);
4545 return err;
4546}
4547
4548static int
4549nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4550{
4551 struct nfs4_label ilabel, *olabel = NULL;
4552 struct nfs_fattr fattr;
4553 struct rpc_cred *cred;
4554 struct inode *inode = dentry->d_inode;
4555 int status;
4556
4557 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4558 return -EOPNOTSUPP;
4559
4560 nfs_fattr_init(&fattr);
4561
4562 ilabel.pi = 0;
4563 ilabel.lfs = 0;
4564 ilabel.label = (char *)buf;
4565 ilabel.len = buflen;
4566
4567 cred = rpc_lookup_cred();
4568 if (IS_ERR(cred))
4569 return PTR_ERR(cred);
4570
4571 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4572 if (IS_ERR(olabel)) {
4573 status = -PTR_ERR(olabel);
4574 goto out;
4575 }
4576
4577 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4578 if (status == 0)
4579 nfs_setsecurity(inode, &fattr, olabel);
4580
4581 nfs4_label_free(olabel);
4582out:
4583 put_rpccred(cred);
4584 return status;
4585}
4586#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4587
4588
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004590nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004592 struct nfs_client *clp = server->nfs_client;
4593
4594 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 return 0;
4596 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004597 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004598 case -NFS4ERR_ADMIN_REVOKED:
4599 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004600 if (state == NULL)
4601 break;
4602 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004603 case -NFS4ERR_OPENMODE:
4604 if (state == NULL)
4605 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004606 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4607 goto stateid_invalid;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004608 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004609 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004610 if (state != NULL) {
4611 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4612 goto stateid_invalid;
4613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004615 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004616 nfs4_schedule_lease_recovery(clp);
4617 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004618#if defined(CONFIG_NFS_V4_1)
4619 case -NFS4ERR_BADSESSION:
4620 case -NFS4ERR_BADSLOT:
4621 case -NFS4ERR_BAD_HIGH_SLOT:
4622 case -NFS4ERR_DEADSESSION:
4623 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4624 case -NFS4ERR_SEQ_FALSE_RETRY:
4625 case -NFS4ERR_SEQ_MISORDERED:
4626 dprintk("%s ERROR %d, Reset session\n", __func__,
4627 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004628 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004629 task->tk_status = 0;
4630 return -EAGAIN;
4631#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004633 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004634 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4636 task->tk_status = 0;
4637 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004638 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 case -NFS4ERR_OLD_STATEID:
4640 task->tk_status = 0;
4641 return -EAGAIN;
4642 }
4643 task->tk_status = nfs4_map_errors(task->tk_status);
4644 return 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004645stateid_invalid:
4646 task->tk_status = -EIO;
4647 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004648wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004649 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004650 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4651 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4652 task->tk_status = 0;
4653 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654}
4655
Chuck Leverf0920752012-05-21 22:45:41 -04004656static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4657 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004658{
4659 __be32 verf[2];
4660
Chuck Lever2c820d92012-05-21 22:45:33 -04004661 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4662 /* An impossible timestamp guarantees this value
4663 * will never match a generated boot time. */
4664 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04004665 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04004666 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004667 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04004668 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
4669 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04004670 }
Chuck Levercd937102012-03-02 17:14:31 -05004671 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4672}
4673
Chuck Levere984a552012-09-14 17:24:21 -04004674static unsigned int
4675nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4676 char *buf, size_t len)
4677{
4678 unsigned int result;
4679
4680 rcu_read_lock();
4681 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4682 clp->cl_ipaddr,
4683 rpc_peeraddr2str(clp->cl_rpcclient,
4684 RPC_DISPLAY_ADDR),
4685 rpc_peeraddr2str(clp->cl_rpcclient,
4686 RPC_DISPLAY_PROTO));
4687 rcu_read_unlock();
4688 return result;
4689}
4690
4691static unsigned int
4692nfs4_init_uniform_client_string(const struct nfs_client *clp,
4693 char *buf, size_t len)
4694{
Trond Myklebust55b59292013-07-24 16:36:35 -04004695 const char *nodename = clp->cl_rpcclient->cl_nodename;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004696
4697 if (nfs4_client_id_uniquifier[0] != '\0')
Trond Myklebust55b59292013-07-24 16:36:35 -04004698 return scnprintf(buf, len, "Linux NFSv%u.%u %s/%s",
4699 clp->rpc_ops->version,
4700 clp->cl_minorversion,
4701 nfs4_client_id_uniquifier,
4702 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004703 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4704 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004705 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004706}
4707
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004708/**
4709 * nfs4_proc_setclientid - Negotiate client ID
4710 * @clp: state data structure
4711 * @program: RPC program for NFSv4 callback service
4712 * @port: IP port number for NFS4 callback service
4713 * @cred: RPC credential to use for this call
4714 * @res: where to place the result
4715 *
4716 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4717 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004718int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4719 unsigned short port, struct rpc_cred *cred,
4720 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721{
4722 nfs4_verifier sc_verifier;
4723 struct nfs4_setclientid setclientid = {
4724 .sc_verifier = &sc_verifier,
4725 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004726 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727 };
4728 struct rpc_message msg = {
4729 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4730 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004731 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004732 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004734 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735
Chuck Leverde734832012-07-11 16:30:50 -04004736 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004737 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004738 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4739 setclientid.sc_name_len =
4740 nfs4_init_uniform_client_string(clp,
4741 setclientid.sc_name,
4742 sizeof(setclientid.sc_name));
4743 else
4744 setclientid.sc_name_len =
4745 nfs4_init_nonuniform_client_string(clp,
4746 setclientid.sc_name,
4747 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004748 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004749 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004750 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Djalal Harounife2d5392013-06-18 17:58:12 +01004751 sizeof(setclientid.sc_netid), "%s",
Chuck Leverd4d3c502007-12-10 14:57:09 -05004752 rpc_peeraddr2str(clp->cl_rpcclient,
4753 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004754 rcu_read_unlock();
4755 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004756 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 clp->cl_ipaddr, port >> 8, port & 255);
4758
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004759 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4760 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4761 setclientid.sc_name_len, setclientid.sc_name);
4762 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004763 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004764 dprintk("NFS reply setclientid: %d\n", status);
4765 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766}
4767
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004768/**
4769 * nfs4_proc_setclientid_confirm - Confirm client ID
4770 * @clp: state data structure
4771 * @res: result of a previous SETCLIENTID
4772 * @cred: RPC credential to use for this call
4773 *
4774 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4775 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004776int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004777 struct nfs4_setclientid_res *arg,
4778 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780 struct rpc_message msg = {
4781 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004782 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004783 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 int status;
4786
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004787 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4788 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4789 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004790 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004791 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004792 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 return status;
4794}
4795
Trond Myklebustfe650402006-01-03 09:55:18 +01004796struct nfs4_delegreturndata {
4797 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004798 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004799 struct nfs_fh fh;
4800 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004801 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004802 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004803 int rpc_status;
4804};
4805
Trond Myklebustfe650402006-01-03 09:55:18 +01004806static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4807{
4808 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004809
Trond Myklebust14516c32010-07-31 14:29:06 -04004810 if (!nfs4_sequence_done(task, &data->res.seq_res))
4811 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004812
Trond Myklebustca8acf82013-08-13 10:36:56 -04004813 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004814 switch (task->tk_status) {
4815 case -NFS4ERR_STALE_STATEID:
4816 case -NFS4ERR_EXPIRED:
4817 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004818 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004819 break;
4820 default:
4821 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4822 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004823 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004824 return;
4825 }
4826 }
4827 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004828}
4829
4830static void nfs4_delegreturn_release(void *calldata)
4831{
Trond Myklebustfe650402006-01-03 09:55:18 +01004832 kfree(calldata);
4833}
4834
Andy Adamson938e1012009-04-01 09:22:28 -04004835#if defined(CONFIG_NFS_V4_1)
4836static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4837{
4838 struct nfs4_delegreturndata *d_data;
4839
4840 d_data = (struct nfs4_delegreturndata *)data;
4841
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004842 nfs4_setup_sequence(d_data->res.server,
4843 &d_data->args.seq_args,
4844 &d_data->res.seq_res,
4845 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004846}
4847#endif /* CONFIG_NFS_V4_1 */
4848
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004849static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004850#if defined(CONFIG_NFS_V4_1)
4851 .rpc_call_prepare = nfs4_delegreturn_prepare,
4852#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004853 .rpc_call_done = nfs4_delegreturn_done,
4854 .rpc_release = nfs4_delegreturn_release,
4855};
4856
Trond Myklebuste6f81072008-01-24 18:14:34 -05004857static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004858{
4859 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004860 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004861 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004862 struct rpc_message msg = {
4863 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4864 .rpc_cred = cred,
4865 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004866 struct rpc_task_setup task_setup_data = {
4867 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004868 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004869 .callback_ops = &nfs4_delegreturn_ops,
4870 .flags = RPC_TASK_ASYNC,
4871 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004872 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004873
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004874 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004875 if (data == NULL)
4876 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004877 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004878 data->args.fhandle = &data->fh;
4879 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004880 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004881 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004882 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004883 data->res.fattr = &data->fattr;
4884 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004885 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004886 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004887 data->rpc_status = 0;
4888
Trond Myklebustc970aa82007-07-14 15:39:59 -04004889 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004890 msg.rpc_argp = &data->args;
4891 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004892 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004893 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004894 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004895 if (!issync)
4896 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004897 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004898 if (status != 0)
4899 goto out;
4900 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004901 if (status == 0)
4902 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4903 else
4904 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004905out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004906 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004907 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908}
4909
Trond Myklebuste6f81072008-01-24 18:14:34 -05004910int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911{
4912 struct nfs_server *server = NFS_SERVER(inode);
4913 struct nfs4_exception exception = { };
4914 int err;
4915 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004916 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Trond Myklebustca8acf82013-08-13 10:36:56 -04004917 trace_nfs4_delegreturn(inode, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 switch (err) {
4919 case -NFS4ERR_STALE_STATEID:
4920 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 case 0:
4922 return 0;
4923 }
4924 err = nfs4_handle_exception(server, err, &exception);
4925 } while (exception.retry);
4926 return err;
4927}
4928
4929#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4930#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4931
4932/*
4933 * sleep, with exponential backoff, and retry the LOCK operation.
4934 */
4935static unsigned long
4936nfs4_set_lock_task_retry(unsigned long timeout)
4937{
Colin Cross416ad3c2013-05-06 23:50:06 +00004938 freezable_schedule_timeout_killable_unsafe(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 timeout <<= 1;
4940 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4941 return NFS4_LOCK_MAXTIMEOUT;
4942 return timeout;
4943}
4944
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4946{
4947 struct inode *inode = state->inode;
4948 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004949 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004950 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004952 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004954 struct nfs_lockt_res res = {
4955 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 };
4957 struct rpc_message msg = {
4958 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4959 .rpc_argp = &arg,
4960 .rpc_resp = &res,
4961 .rpc_cred = state->owner->so_cred,
4962 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963 struct nfs4_lock_state *lsp;
4964 int status;
4965
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004966 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004967 status = nfs4_set_lock_state(state, request);
4968 if (status != 0)
4969 goto out;
4970 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004971 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004972 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004973 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004974 switch (status) {
4975 case 0:
4976 request->fl_type = F_UNLCK;
4977 break;
4978 case -NFS4ERR_DENIED:
4979 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004981 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004982out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 return status;
4984}
4985
4986static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4987{
4988 struct nfs4_exception exception = { };
4989 int err;
4990
4991 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04004992 err = _nfs4_proc_getlk(state, cmd, request);
4993 trace_nfs4_get_lock(request, state, cmd, err);
4994 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 &exception);
4996 } while (exception.retry);
4997 return err;
4998}
4999
5000static int do_vfs_lock(struct file *file, struct file_lock *fl)
5001{
5002 int res = 0;
5003 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5004 case FL_POSIX:
5005 res = posix_lock_file_wait(file, fl);
5006 break;
5007 case FL_FLOCK:
5008 res = flock_lock_file_wait(file, fl);
5009 break;
5010 default:
5011 BUG();
5012 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 return res;
5014}
5015
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005016struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005017 struct nfs_locku_args arg;
5018 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005019 struct nfs4_lock_state *lsp;
5020 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005021 struct file_lock fl;
5022 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005023 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005024};
5025
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005026static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5027 struct nfs_open_context *ctx,
5028 struct nfs4_lock_state *lsp,
5029 struct nfs_seqid *seqid)
5030{
5031 struct nfs4_unlockdata *p;
5032 struct inode *inode = lsp->ls_state->inode;
5033
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005034 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005035 if (p == NULL)
5036 return NULL;
5037 p->arg.fh = NFS_FH(inode);
5038 p->arg.fl = &p->fl;
5039 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005040 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005041 p->arg.stateid = &lsp->ls_stateid;
5042 p->lsp = lsp;
5043 atomic_inc(&lsp->ls_count);
5044 /* Ensure we don't close file until we're done freeing locks! */
5045 p->ctx = get_nfs_open_context(ctx);
5046 memcpy(&p->fl, fl, sizeof(p->fl));
5047 p->server = NFS_SERVER(inode);
5048 return p;
5049}
5050
Trond Myklebust06f814a2006-01-03 09:55:07 +01005051static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005052{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005053 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005054 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005055 nfs4_put_lock_state(calldata->lsp);
5056 put_nfs_open_context(calldata->ctx);
5057 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005058}
5059
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005060static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005061{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005062 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005063
Trond Myklebust14516c32010-07-31 14:29:06 -04005064 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5065 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005066 switch (task->tk_status) {
5067 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05005068 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
5069 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005070 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005071 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005072 case -NFS4ERR_BAD_STATEID:
5073 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005074 case -NFS4ERR_STALE_STATEID:
5075 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005076 break;
5077 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005078 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005079 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005080 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005081 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005082}
5083
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005084static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005085{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005086 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005088 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005089 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04005090 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005091 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005092 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005093 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005094 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005095 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005096 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005097 &calldata->res.seq_res,
5098 task) != 0)
5099 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005100 return;
5101out_no_action:
5102 task->tk_action = NULL;
5103out_wait:
5104 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105}
5106
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005107static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005108 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005109 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005110 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005111};
5112
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005113static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5114 struct nfs_open_context *ctx,
5115 struct nfs4_lock_state *lsp,
5116 struct nfs_seqid *seqid)
5117{
5118 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005119 struct rpc_message msg = {
5120 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5121 .rpc_cred = ctx->cred,
5122 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005123 struct rpc_task_setup task_setup_data = {
5124 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005125 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005126 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005127 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005128 .flags = RPC_TASK_ASYNC,
5129 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005130
Frank Filz137d6ac2007-07-09 15:32:29 -07005131 /* Ensure this is an unlock - when canceling a lock, the
5132 * canceled lock is passed in, and it won't be an unlock.
5133 */
5134 fl->fl_type = F_UNLCK;
5135
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005136 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5137 if (data == NULL) {
5138 nfs_free_seqid(seqid);
5139 return ERR_PTR(-ENOMEM);
5140 }
5141
Trond Myklebust9d12b212012-01-17 22:04:25 -05005142 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005143 msg.rpc_argp = &data->arg;
5144 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005145 task_setup_data.callback_data = data;
5146 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005147}
5148
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5150{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005151 struct inode *inode = state->inode;
5152 struct nfs4_state_owner *sp = state->owner;
5153 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005154 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005155 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005156 struct rpc_task *task;
5157 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005158 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159
Trond Myklebust9b073572006-06-29 16:38:34 -04005160 status = nfs4_set_lock_state(state, request);
5161 /* Unlock _before_ we do the RPC call */
5162 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005163 /* Exclude nfs_delegation_claim_locks() */
5164 mutex_lock(&sp->so_delegreturn_mutex);
5165 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005166 down_read(&nfsi->rwsem);
5167 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
5168 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005169 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005170 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005171 }
5172 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005173 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005174 if (status != 0)
5175 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005176 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005177 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005178 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5179 goto out;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005180 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005181 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005182 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04005183 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005184 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005185 status = PTR_ERR(task);
5186 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005187 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005188 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005189 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005190out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005191 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005192 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005193 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194}
5195
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005196struct nfs4_lockdata {
5197 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005198 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005199 struct nfs4_lock_state *lsp;
5200 struct nfs_open_context *ctx;
5201 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005202 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005203 int rpc_status;
5204 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005205 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005206};
5207
5208static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005209 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5210 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005211{
5212 struct nfs4_lockdata *p;
5213 struct inode *inode = lsp->ls_state->inode;
5214 struct nfs_server *server = NFS_SERVER(inode);
5215
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005216 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005217 if (p == NULL)
5218 return NULL;
5219
5220 p->arg.fh = NFS_FH(inode);
5221 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005222 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005223 if (p->arg.open_seqid == NULL)
5224 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005225 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005226 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005227 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005228 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04005229 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005230 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005231 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005232 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005233 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005234 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005235 atomic_inc(&lsp->ls_count);
5236 p->ctx = get_nfs_open_context(ctx);
5237 memcpy(&p->fl, fl, sizeof(p->fl));
5238 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005239out_free_seqid:
5240 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005241out_free:
5242 kfree(p);
5243 return NULL;
5244}
5245
5246static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5247{
5248 struct nfs4_lockdata *data = calldata;
5249 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250
Harvey Harrison3110ff82008-05-02 13:42:44 -07005251 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005252 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005253 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005254 /* Do we need to do an open_to_lock_owner? */
5255 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005256 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005257 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005258 }
Trond Myklebust8e472f32013-04-20 01:30:53 -04005259 data->arg.open_stateid = &state->open_stateid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005260 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005261 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005262 } else
5263 data->arg.new_lock_owner = 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04005264 if (!nfs4_valid_open_stateid(state)) {
5265 data->rpc_status = -EBADF;
5266 task->tk_action = NULL;
5267 goto out_release_open_seqid;
5268 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005269 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005270 if (nfs4_setup_sequence(data->server,
5271 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005272 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005273 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04005274 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04005275out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005276 nfs_release_seqid(data->arg.open_seqid);
5277out_release_lock_seqid:
5278 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005279out_wait:
5280 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005281 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005282}
5283
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005284static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5285{
5286 struct nfs4_lockdata *data = calldata;
5287
Harvey Harrison3110ff82008-05-02 13:42:44 -07005288 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005289
Trond Myklebust14516c32010-07-31 14:29:06 -04005290 if (!nfs4_sequence_done(task, &data->res.seq_res))
5291 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04005292
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005293 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005294 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005295 if (data->rpc_status == 0)
5296 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
5297 else
5298 goto out;
5299 }
5300 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05005301 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005302 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04005303 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005304 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005305out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07005306 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005307}
5308
5309static void nfs4_lock_release(void *calldata)
5310{
5311 struct nfs4_lockdata *data = calldata;
5312
Harvey Harrison3110ff82008-05-02 13:42:44 -07005313 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005314 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005315 if (data->cancelled != 0) {
5316 struct rpc_task *task;
5317 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5318 data->arg.lock_seqid);
5319 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08005320 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005321 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005322 } else
5323 nfs_free_seqid(data->arg.lock_seqid);
5324 nfs4_put_lock_state(data->lsp);
5325 put_nfs_open_context(data->ctx);
5326 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005327 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005328}
5329
5330static const struct rpc_call_ops nfs4_lock_ops = {
5331 .rpc_call_prepare = nfs4_lock_prepare,
5332 .rpc_call_done = nfs4_lock_done,
5333 .rpc_release = nfs4_lock_release,
5334};
5335
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005336static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5337{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005338 switch (error) {
5339 case -NFS4ERR_ADMIN_REVOKED:
5340 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05005341 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005342 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04005343 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05005344 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005345 break;
5346 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005347 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05005348 case -NFS4ERR_EXPIRED:
5349 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005350 };
5351}
5352
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005353static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005354{
5355 struct nfs4_lockdata *data;
5356 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005357 struct rpc_message msg = {
5358 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5359 .rpc_cred = state->owner->so_cred,
5360 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005361 struct rpc_task_setup task_setup_data = {
5362 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005363 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005364 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005365 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005366 .flags = RPC_TASK_ASYNC,
5367 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005368 int ret;
5369
Harvey Harrison3110ff82008-05-02 13:42:44 -07005370 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005371 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005372 fl->fl_u.nfs4_fl.owner,
5373 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005374 if (data == NULL)
5375 return -ENOMEM;
5376 if (IS_SETLKW(cmd))
5377 data->arg.block = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -05005378 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005379 msg.rpc_argp = &data->arg;
5380 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005381 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005382 if (recovery_type > NFS_LOCK_NEW) {
5383 if (recovery_type == NFS_LOCK_RECLAIM)
5384 data->arg.reclaim = NFS_LOCK_RECLAIM;
5385 nfs4_set_sequence_privileged(&data->arg.seq_args);
5386 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04005387 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005388 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005389 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005390 ret = nfs4_wait_for_completion_rpc_task(task);
5391 if (ret == 0) {
5392 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005393 if (ret)
5394 nfs4_handle_setlk_error(data->server, data->lsp,
5395 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005396 } else
5397 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005398 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005399 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005400 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401}
5402
5403static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5404{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005405 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005406 struct nfs4_exception exception = {
5407 .inode = state->inode,
5408 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005409 int err;
5410
5411 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005412 /* Cache the lock if possible... */
5413 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5414 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005415 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005416 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
Trond Myklebust168667c2010-10-19 19:47:49 -04005417 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00005418 break;
5419 nfs4_handle_exception(server, err, &exception);
5420 } while (exception.retry);
5421 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422}
5423
5424static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5425{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005426 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005427 struct nfs4_exception exception = {
5428 .inode = state->inode,
5429 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005430 int err;
5431
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005432 err = nfs4_set_lock_state(state, request);
5433 if (err != 0)
5434 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00005435 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005436 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5437 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005438 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005439 trace_nfs4_lock_expired(request, state, F_SETLK, err);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005440 switch (err) {
5441 default:
5442 goto out;
5443 case -NFS4ERR_GRACE:
5444 case -NFS4ERR_DELAY:
5445 nfs4_handle_exception(server, err, &exception);
5446 err = 0;
5447 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005448 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005449out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00005450 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451}
5452
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005453#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005454/**
5455 * nfs41_check_expired_locks - possibly free a lock stateid
5456 *
5457 * @state: NFSv4 state for an inode
5458 *
5459 * Returns NFS_OK if recovery for this stateid is now finished.
5460 * Otherwise a negative NFS4ERR value is returned.
5461 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005462static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005463{
Chuck Levereb64cf92012-07-11 16:30:05 -04005464 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005465 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005466 struct nfs_server *server = NFS_SERVER(state->inode);
5467
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005468 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04005469 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005470 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
5471
5472 status = nfs41_test_stateid(server,
5473 &lsp->ls_stateid,
5474 cred);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005475 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005476 /* Free the stateid unless the server
5477 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04005478 if (status != -NFS4ERR_BAD_STATEID)
5479 nfs41_free_stateid(server,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005480 &lsp->ls_stateid,
5481 cred);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005482 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005483 ret = status;
5484 }
5485 }
5486 };
5487
5488 return ret;
5489}
5490
5491static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5492{
5493 int status = NFS_OK;
5494
5495 if (test_bit(LK_STATE_IN_USE, &state->flags))
5496 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005497 if (status != NFS_OK)
5498 status = nfs4_lock_expired(state, request);
5499 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005500}
5501#endif
5502
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5504{
Trond Myklebust9a99af492013-02-04 20:17:49 -05005505 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005506 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005507 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005508 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005509 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005511 if ((fl_flags & FL_POSIX) &&
5512 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5513 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005514 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005515 status = nfs4_set_lock_state(state, request);
5516 if (status != 0)
5517 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005518 request->fl_flags |= FL_ACCESS;
5519 status = do_vfs_lock(request->fl_file, request);
5520 if (status < 0)
5521 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005522 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005523 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005524 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005525 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005526 request->fl_flags = fl_flags & ~FL_SLEEP;
5527 status = do_vfs_lock(request->fl_file, request);
5528 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005529 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05005530 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5531 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005532 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005533 if (status != 0)
Trond Myklebust9a99af492013-02-04 20:17:49 -05005534 goto out;
5535 down_read(&nfsi->rwsem);
5536 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5537 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005538 goto out_unlock;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005539 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005540 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005541 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005542 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005543 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5544 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005545out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005546 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005547out:
5548 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549 return status;
5550}
5551
5552static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5553{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005554 struct nfs4_exception exception = {
5555 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005556 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005557 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558 int err;
5559
5560 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005561 err = _nfs4_proc_setlk(state, cmd, request);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005562 trace_nfs4_set_lock(request, state, cmd, err);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005563 if (err == -NFS4ERR_DENIED)
5564 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005566 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567 } while (exception.retry);
5568 return err;
5569}
5570
5571static int
5572nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5573{
5574 struct nfs_open_context *ctx;
5575 struct nfs4_state *state;
5576 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5577 int status;
5578
5579 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005580 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581 state = ctx->state;
5582
5583 if (request->fl_start < 0 || request->fl_end < 0)
5584 return -EINVAL;
5585
Trond Myklebustd9531262009-07-21 19:22:38 -04005586 if (IS_GETLK(cmd)) {
5587 if (state != NULL)
5588 return nfs4_proc_getlk(state, F_GETLK, request);
5589 return 0;
5590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591
5592 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5593 return -EINVAL;
5594
Trond Myklebustd9531262009-07-21 19:22:38 -04005595 if (request->fl_type == F_UNLCK) {
5596 if (state != NULL)
5597 return nfs4_proc_unlck(state, cmd, request);
5598 return 0;
5599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600
Trond Myklebustd9531262009-07-21 19:22:38 -04005601 if (state == NULL)
5602 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005603 /*
5604 * Don't rely on the VFS having checked the file open mode,
5605 * since it won't do this for flock() locks.
5606 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005607 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005608 case F_RDLCK:
5609 if (!(filp->f_mode & FMODE_READ))
5610 return -EBADF;
5611 break;
5612 case F_WRLCK:
5613 if (!(filp->f_mode & FMODE_WRITE))
5614 return -EBADF;
5615 }
5616
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617 do {
5618 status = nfs4_proc_setlk(state, cmd, request);
5619 if ((status != -EAGAIN) || IS_SETLK(cmd))
5620 break;
5621 timeout = nfs4_set_lock_task_retry(timeout);
5622 status = -ERESTARTSYS;
5623 if (signalled())
5624 break;
5625 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626 return status;
5627}
5628
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005629int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05005630{
5631 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05005632 int err;
5633
5634 err = nfs4_set_lock_state(state, fl);
5635 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005636 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04005637 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005638 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05005639}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005640
Trond Myklebustcf470c32012-03-07 13:49:12 -05005641struct nfs_release_lockowner_data {
5642 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005643 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005644 struct nfs_release_lockowner_args args;
5645};
5646
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005647static void nfs4_release_lockowner_release(void *calldata)
5648{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005649 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005650 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005651 kfree(calldata);
5652}
5653
Trond Myklebust17280172012-03-11 13:11:00 -04005654static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005655 .rpc_release = nfs4_release_lockowner_release,
5656};
5657
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04005658static int nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005659{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005660 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005661 struct rpc_message msg = {
5662 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5663 };
5664
5665 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005666 return -EINVAL;
5667 data = kmalloc(sizeof(*data), GFP_NOFS);
5668 if (!data)
5669 return -ENOMEM;
5670 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005671 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005672 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5673 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5674 data->args.lock_owner.s_dev = server->s_dev;
5675 msg.rpc_argp = &data->args;
5676 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5677 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005678}
5679
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005680#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5681
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005682static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5683 const void *buf, size_t buflen,
5684 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005685{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005686 if (strcmp(key, "") != 0)
5687 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005688
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005689 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005690}
5691
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005692static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5693 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005694{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005695 if (strcmp(key, "") != 0)
5696 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005697
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005698 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005699}
5700
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005701static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5702 size_t list_len, const char *name,
5703 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005704{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005705 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005706
J. Bruce Fields096455a2006-03-20 23:23:42 -05005707 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5708 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005709
5710 if (list && len <= list_len)
5711 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005712 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005713}
5714
David Quigleyc9bccef2013-05-22 12:50:45 -04005715#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5716static inline int nfs4_server_supports_labels(struct nfs_server *server)
5717{
5718 return server->caps & NFS_CAP_SECURITY_LABEL;
5719}
5720
5721static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
5722 const void *buf, size_t buflen,
5723 int flags, int type)
5724{
5725 if (security_ismaclabel(key))
5726 return nfs4_set_security_label(dentry, buf, buflen);
5727
5728 return -EOPNOTSUPP;
5729}
5730
5731static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
5732 void *buf, size_t buflen, int type)
5733{
5734 if (security_ismaclabel(key))
5735 return nfs4_get_security_label(dentry->d_inode, buf, buflen);
5736 return -EOPNOTSUPP;
5737}
5738
5739static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
5740 size_t list_len, const char *name,
5741 size_t name_len, int type)
5742{
5743 size_t len = 0;
5744
5745 if (nfs_server_capable(dentry->d_inode, NFS_CAP_SECURITY_LABEL)) {
5746 len = security_inode_listsecurity(dentry->d_inode, NULL, 0);
5747 if (list && len <= list_len)
5748 security_inode_listsecurity(dentry->d_inode, list, len);
5749 }
5750 return len;
5751}
5752
5753static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
5754 .prefix = XATTR_SECURITY_PREFIX,
5755 .list = nfs4_xattr_list_nfs4_label,
5756 .get = nfs4_xattr_get_nfs4_label,
5757 .set = nfs4_xattr_set_nfs4_label,
5758};
5759#endif
5760
5761
Andy Adamson533eb462011-06-13 18:25:56 -04005762/*
5763 * nfs_fhget will use either the mounted_on_fileid or the fileid
5764 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005765static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5766{
Andy Adamson533eb462011-06-13 18:25:56 -04005767 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5768 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5769 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005770 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005771 return;
5772
5773 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005774 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005775 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5776 fattr->nlink = 2;
5777}
5778
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005779static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5780 const struct qstr *name,
5781 struct nfs4_fs_locations *fs_locations,
5782 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005783{
5784 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04005785 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005786 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005787 };
5788 struct nfs4_fs_locations_arg args = {
5789 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005790 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005791 .page = page,
5792 .bitmask = bitmask,
5793 };
Benny Halevy22958462009-04-01 09:22:02 -04005794 struct nfs4_fs_locations_res res = {
5795 .fs_locations = fs_locations,
5796 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005797 struct rpc_message msg = {
5798 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5799 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005800 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005801 };
5802 int status;
5803
Harvey Harrison3110ff82008-05-02 13:42:44 -07005804 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005805
5806 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5807 * is not supported */
5808 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5809 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5810 else
5811 bitmask[0] |= FATTR4_WORD0_FILEID;
5812
Trond Myklebustc228fd32007-01-13 02:28:11 -05005813 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005814 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005815 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005816 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005817 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005818 return status;
5819}
5820
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005821int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5822 const struct qstr *name,
5823 struct nfs4_fs_locations *fs_locations,
5824 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005825{
5826 struct nfs4_exception exception = { };
5827 int err;
5828 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04005829 err = _nfs4_proc_fs_locations(client, dir, name,
5830 fs_locations, page);
5831 trace_nfs4_get_fs_locations(dir, name, err);
5832 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005833 &exception);
5834 } while (exception.retry);
5835 return err;
5836}
5837
Andy Adamson5ec16a82013-08-08 10:57:55 -04005838/**
5839 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
5840 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
5841 */
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005842static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5843{
5844 int status;
5845 struct nfs4_secinfo_arg args = {
5846 .dir_fh = NFS_FH(dir),
5847 .name = name,
5848 };
5849 struct nfs4_secinfo_res res = {
5850 .flavors = flavors,
5851 };
5852 struct rpc_message msg = {
5853 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5854 .rpc_argp = &args,
5855 .rpc_resp = &res,
5856 };
Andy Adamson5ec16a82013-08-08 10:57:55 -04005857 struct rpc_clnt *clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005858
5859 dprintk("NFS call secinfo %s\n", name->name);
Andy Adamson5ec16a82013-08-08 10:57:55 -04005860 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005861 dprintk("NFS reply secinfo: %d\n", status);
5862 return status;
5863}
5864
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005865int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5866 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005867{
5868 struct nfs4_exception exception = { };
5869 int err;
5870 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04005871 err = _nfs4_proc_secinfo(dir, name, flavors);
5872 trace_nfs4_secinfo(dir, name, err);
5873 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005874 &exception);
5875 } while (exception.retry);
5876 return err;
5877}
5878
Andy Adamson557134a2009-04-01 09:21:53 -04005879#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005880/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005881 * Check the exchange flags returned by the server for invalid flags, having
5882 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5883 * DS flags set.
5884 */
5885static int nfs4_check_cl_exchange_flags(u32 flags)
5886{
5887 if (flags & ~EXCHGID4_FLAG_MASK_R)
5888 goto out_inval;
5889 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5890 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5891 goto out_inval;
5892 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5893 goto out_inval;
5894 return NFS_OK;
5895out_inval:
5896 return -NFS4ERR_INVAL;
5897}
5898
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005899static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005900nfs41_same_server_scope(struct nfs41_server_scope *a,
5901 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005902{
5903 if (a->server_scope_sz == b->server_scope_sz &&
5904 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5905 return true;
5906
5907 return false;
5908}
5909
Andy Adamson357f54d2010-12-14 10:11:57 -05005910/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005911 * nfs4_proc_bind_conn_to_session()
5912 *
5913 * The 4.1 client currently uses the same TCP connection for the
5914 * fore and backchannel.
5915 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005916int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005917{
5918 int status;
5919 struct nfs41_bind_conn_to_session_res res;
5920 struct rpc_message msg = {
5921 .rpc_proc =
5922 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5923 .rpc_argp = clp,
5924 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005925 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005926 };
5927
5928 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005929
5930 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5931 if (unlikely(res.session == NULL)) {
5932 status = -ENOMEM;
5933 goto out;
5934 }
5935
5936 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005937 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005938 if (status == 0) {
5939 if (memcmp(res.session->sess_id.data,
5940 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5941 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5942 status = -EIO;
5943 goto out_session;
5944 }
5945 if (res.dir != NFS4_CDFS4_BOTH) {
5946 dprintk("NFS: %s: Unexpected direction from server\n",
5947 __func__);
5948 status = -EIO;
5949 goto out_session;
5950 }
5951 if (res.use_conn_in_rdma_mode) {
5952 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5953 __func__);
5954 status = -EIO;
5955 goto out_session;
5956 }
5957 }
5958out_session:
5959 kfree(res.session);
5960out:
5961 dprintk("<-- %s status= %d\n", __func__, status);
5962 return status;
5963}
5964
5965/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005966 * nfs4_proc_exchange_id()
5967 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005968 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5969 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005970 * Since the clientid has expired, all compounds using sessions
5971 * associated with the stale clientid will be returning
5972 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5973 * be in some phase of session reset.
5974 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005975int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005976{
5977 nfs4_verifier verifier;
5978 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005979 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005980 .client = clp,
Trond Myklebust4f0b4292013-05-20 12:24:03 -04005981 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
5982 EXCHGID4_FLAG_BIND_PRINC_STATEID,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005983 };
5984 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005985 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005986 };
5987 int status;
5988 struct rpc_message msg = {
5989 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5990 .rpc_argp = &args,
5991 .rpc_resp = &res,
5992 .rpc_cred = cred,
5993 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005994
Chuck Leverf0920752012-05-21 22:45:41 -04005995 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005996 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5997 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005998 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5999 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6000 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006001
Chuck Leveracdeb692012-05-21 22:46:16 -04006002 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006003 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04006004 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006005 status = -ENOMEM;
6006 goto out;
6007 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006008
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006009 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006010 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006011 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04006012 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04006013 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04006014 }
6015
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006016 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006017 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006018 status = -ENOMEM;
6019 goto out_server_scope;
6020 }
6021
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006022 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006023 trace_nfs4_exchange_id(clp, status);
Chuck Lever177313f2012-05-21 22:44:58 -04006024 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04006025 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006026
Chuck Lever177313f2012-05-21 22:44:58 -04006027 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04006028 clp->cl_clientid = res.clientid;
6029 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
6030 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
6031 clp->cl_seqid = res.seqid;
6032
Chuck Leveracdeb692012-05-21 22:46:16 -04006033 kfree(clp->cl_serverowner);
6034 clp->cl_serverowner = res.server_owner;
6035 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04006036
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006037 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04006038 kfree(clp->cl_implid);
6039 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006040
Chuck Lever177313f2012-05-21 22:44:58 -04006041 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006042 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006043 res.server_scope)) {
6044 dprintk("%s: server_scope mismatch detected\n",
6045 __func__);
6046 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006047 kfree(clp->cl_serverscope);
6048 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006049 }
6050
Chuck Lever177313f2012-05-21 22:44:58 -04006051 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006052 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006053 goto out;
6054 }
Trond Myklebust32b01312012-05-26 13:41:04 -04006055 } else
6056 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006057
Chuck Leveracdeb692012-05-21 22:46:16 -04006058out_server_owner:
6059 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006060out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006061 kfree(res.server_scope);
6062out:
Chuck Lever177313f2012-05-21 22:44:58 -04006063 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006064 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006065 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006066 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04006067 clp->cl_implid->date.seconds,
6068 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006069 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006070 return status;
6071}
6072
Trond Myklebust66245532012-05-25 17:18:09 -04006073static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
6074 struct rpc_cred *cred)
6075{
6076 struct rpc_message msg = {
6077 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
6078 .rpc_argp = clp,
6079 .rpc_cred = cred,
6080 };
6081 int status;
6082
6083 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006084 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04006085 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04006086 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04006087 "DESTROY_CLIENTID.", status, clp->cl_hostname);
6088 return status;
6089}
6090
6091static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
6092 struct rpc_cred *cred)
6093{
6094 unsigned int loop;
6095 int ret;
6096
6097 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
6098 ret = _nfs4_proc_destroy_clientid(clp, cred);
6099 switch (ret) {
6100 case -NFS4ERR_DELAY:
6101 case -NFS4ERR_CLIENTID_BUSY:
6102 ssleep(1);
6103 break;
6104 default:
6105 return ret;
6106 }
6107 }
6108 return 0;
6109}
6110
6111int nfs4_destroy_clientid(struct nfs_client *clp)
6112{
6113 struct rpc_cred *cred;
6114 int ret = 0;
6115
6116 if (clp->cl_mvops->minor_version < 1)
6117 goto out;
6118 if (clp->cl_exchange_flags == 0)
6119 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04006120 if (clp->cl_preserve_clid)
6121 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04006122 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04006123 ret = nfs4_proc_destroy_clientid(clp, cred);
6124 if (cred)
6125 put_rpccred(cred);
6126 switch (ret) {
6127 case 0:
6128 case -NFS4ERR_STALE_CLIENTID:
6129 clp->cl_exchange_flags = 0;
6130 }
6131out:
6132 return ret;
6133}
6134
Andy Adamson2050f0c2009-04-01 09:22:30 -04006135struct nfs4_get_lease_time_data {
6136 struct nfs4_get_lease_time_args *args;
6137 struct nfs4_get_lease_time_res *res;
6138 struct nfs_client *clp;
6139};
6140
6141static void nfs4_get_lease_time_prepare(struct rpc_task *task,
6142 void *calldata)
6143{
Andy Adamson2050f0c2009-04-01 09:22:30 -04006144 struct nfs4_get_lease_time_data *data =
6145 (struct nfs4_get_lease_time_data *)calldata;
6146
6147 dprintk("--> %s\n", __func__);
6148 /* just setup sequence, do not trigger session recovery
6149 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006150 nfs41_setup_sequence(data->clp->cl_session,
6151 &data->args->la_seq_args,
6152 &data->res->lr_seq_res,
6153 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006154 dprintk("<-- %s\n", __func__);
6155}
6156
6157/*
6158 * Called from nfs4_state_manager thread for session setup, so don't recover
6159 * from sequence operation or clientid errors.
6160 */
6161static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
6162{
6163 struct nfs4_get_lease_time_data *data =
6164 (struct nfs4_get_lease_time_data *)calldata;
6165
6166 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006167 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
6168 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04006169 switch (task->tk_status) {
6170 case -NFS4ERR_DELAY:
6171 case -NFS4ERR_GRACE:
6172 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
6173 rpc_delay(task, NFS4_POLL_RETRY_MIN);
6174 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006175 /* fall through */
6176 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006177 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006178 return;
6179 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04006180 dprintk("<-- %s\n", __func__);
6181}
6182
Trond Myklebust17280172012-03-11 13:11:00 -04006183static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04006184 .rpc_call_prepare = nfs4_get_lease_time_prepare,
6185 .rpc_call_done = nfs4_get_lease_time_done,
6186};
6187
6188int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
6189{
6190 struct rpc_task *task;
6191 struct nfs4_get_lease_time_args args;
6192 struct nfs4_get_lease_time_res res = {
6193 .lr_fsinfo = fsinfo,
6194 };
6195 struct nfs4_get_lease_time_data data = {
6196 .args = &args,
6197 .res = &res,
6198 .clp = clp,
6199 };
6200 struct rpc_message msg = {
6201 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
6202 .rpc_argp = &args,
6203 .rpc_resp = &res,
6204 };
6205 struct rpc_task_setup task_setup = {
6206 .rpc_client = clp->cl_rpcclient,
6207 .rpc_message = &msg,
6208 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006209 .callback_data = &data,
6210 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04006211 };
6212 int status;
6213
Trond Myklebust9d12b212012-01-17 22:04:25 -05006214 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006215 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006216 dprintk("--> %s\n", __func__);
6217 task = rpc_run_task(&task_setup);
6218
6219 if (IS_ERR(task))
6220 status = PTR_ERR(task);
6221 else {
6222 status = task->tk_status;
6223 rpc_put_task(task);
6224 }
6225 dprintk("<-- %s return %d\n", __func__, status);
6226
6227 return status;
6228}
6229
Andy Adamsonfc931582009-04-01 09:22:31 -04006230/*
6231 * Initialize the values to be used by the client in CREATE_SESSION
6232 * If nfs4_init_session set the fore channel request and response sizes,
6233 * use them.
6234 *
6235 * Set the back channel max_resp_sz_cached to zero to force the client to
6236 * always set csa_cachethis to FALSE because the current implementation
6237 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
6238 */
6239static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
6240{
Andy Adamson18aad3d2013-06-26 12:21:49 -04006241 unsigned int max_rqst_sz, max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006242
Andy Adamson18aad3d2013-06-26 12:21:49 -04006243 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
6244 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
6245
Andy Adamsonfc931582009-04-01 09:22:31 -04006246 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04006247 args->fc_attrs.max_rqst_sz = max_rqst_sz;
6248 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006249 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05006250 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04006251
6252 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05006253 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04006254 __func__,
6255 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05006256 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04006257
6258 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04006259 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
6260 args->bc_attrs.max_resp_sz = PAGE_SIZE;
6261 args->bc_attrs.max_resp_sz_cached = 0;
6262 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
6263 args->bc_attrs.max_reqs = 1;
6264
6265 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
6266 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
6267 __func__,
6268 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
6269 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
6270 args->bc_attrs.max_reqs);
6271}
6272
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006273static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04006274{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006275 struct nfs4_channel_attrs *sent = &args->fc_attrs;
6276 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
6277
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006278 if (rcvd->max_resp_sz > sent->max_resp_sz)
6279 return -EINVAL;
6280 /*
6281 * Our requested max_ops is the minimum we need; we're not
6282 * prepared to break up compounds into smaller pieces than that.
6283 * So, no point even trying to continue if the server won't
6284 * cooperate:
6285 */
6286 if (rcvd->max_ops < sent->max_ops)
6287 return -EINVAL;
6288 if (rcvd->max_reqs == 0)
6289 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006290 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
6291 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006292 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04006293}
6294
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006295static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
6296{
6297 struct nfs4_channel_attrs *sent = &args->bc_attrs;
6298 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04006299
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006300 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
6301 return -EINVAL;
6302 if (rcvd->max_resp_sz < sent->max_resp_sz)
6303 return -EINVAL;
6304 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
6305 return -EINVAL;
6306 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006307 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006308 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006309 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006310 return -EINVAL;
6311 return 0;
6312}
Andy Adamson8d353012009-04-01 09:22:32 -04006313
Andy Adamson8d353012009-04-01 09:22:32 -04006314static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
6315 struct nfs4_session *session)
6316{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006317 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04006318
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006319 ret = nfs4_verify_fore_channel_attrs(args, session);
6320 if (ret)
6321 return ret;
6322 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04006323}
6324
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006325static int _nfs4_proc_create_session(struct nfs_client *clp,
6326 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04006327{
6328 struct nfs4_session *session = clp->cl_session;
6329 struct nfs41_create_session_args args = {
6330 .client = clp,
6331 .cb_program = NFS4_CALLBACK,
6332 };
6333 struct nfs41_create_session_res res = {
6334 .client = clp,
6335 };
6336 struct rpc_message msg = {
6337 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
6338 .rpc_argp = &args,
6339 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006340 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04006341 };
6342 int status;
6343
6344 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04006345 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04006346
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006347 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006348 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04006349
Trond Myklebust43095d32012-11-20 11:13:12 -05006350 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04006351 /* Verify the session's negotiated channel_attrs values */
6352 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04006353 /* Increment the clientid slot sequence id */
6354 clp->cl_seqid++;
6355 }
6356
6357 return status;
6358}
6359
6360/*
6361 * Issues a CREATE_SESSION operation to the server.
6362 * It is the responsibility of the caller to verify the session is
6363 * expired before calling this routine.
6364 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006365int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04006366{
6367 int status;
6368 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04006369 struct nfs4_session *session = clp->cl_session;
6370
6371 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
6372
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006373 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04006374 if (status)
6375 goto out;
6376
Andy Adamsonaacd5532011-11-09 13:58:21 -05006377 /* Init or reset the session slot tables */
6378 status = nfs4_setup_session_slot_tables(session);
6379 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04006380 if (status)
6381 goto out;
6382
6383 ptr = (unsigned *)&session->sess_id.data[0];
6384 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
6385 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04006386out:
6387 dprintk("<-- %s\n", __func__);
6388 return status;
6389}
6390
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006391/*
6392 * Issue the over-the-wire RPC DESTROY_SESSION.
6393 * The caller must serialize access to this routine.
6394 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006395int nfs4_proc_destroy_session(struct nfs4_session *session,
6396 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006397{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006398 struct rpc_message msg = {
6399 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
6400 .rpc_argp = session,
6401 .rpc_cred = cred,
6402 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006403 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006404
6405 dprintk("--> nfs4_proc_destroy_session\n");
6406
6407 /* session is still being setup */
6408 if (session->clp->cl_cons_state != NFS_CS_READY)
6409 return status;
6410
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006411 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006412 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006413
6414 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04006415 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006416 "Session has been destroyed regardless...\n", status);
6417
6418 dprintk("<-- nfs4_proc_destroy_session\n");
6419 return status;
6420}
6421
Trond Myklebust7b38c362012-05-23 13:23:31 -04006422/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006423 * Renew the cl_session lease.
6424 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006425struct nfs4_sequence_data {
6426 struct nfs_client *clp;
6427 struct nfs4_sequence_args args;
6428 struct nfs4_sequence_res res;
6429};
6430
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006431static void nfs41_sequence_release(void *data)
6432{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006433 struct nfs4_sequence_data *calldata = data;
6434 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006435
Alexandros Batsakis71358402010-02-05 03:45:05 -08006436 if (atomic_read(&clp->cl_count) > 1)
6437 nfs4_schedule_state_renewal(clp);
6438 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006439 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006440}
6441
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006442static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6443{
6444 switch(task->tk_status) {
6445 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006446 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6447 return -EAGAIN;
6448 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006449 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006450 }
6451 return 0;
6452}
6453
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006454static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006455{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006456 struct nfs4_sequence_data *calldata = data;
6457 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006458
Trond Myklebust14516c32010-07-31 14:29:06 -04006459 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6460 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006461
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006462 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006463 if (task->tk_status < 0) {
6464 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006465 if (atomic_read(&clp->cl_count) == 1)
6466 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006467
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006468 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6469 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006470 return;
6471 }
6472 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006473 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006474out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006475 dprintk("<-- %s\n", __func__);
6476}
6477
6478static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6479{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006480 struct nfs4_sequence_data *calldata = data;
6481 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006482 struct nfs4_sequence_args *args;
6483 struct nfs4_sequence_res *res;
6484
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006485 args = task->tk_msg.rpc_argp;
6486 res = task->tk_msg.rpc_resp;
6487
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006488 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006489}
6490
6491static const struct rpc_call_ops nfs41_sequence_ops = {
6492 .rpc_call_done = nfs41_sequence_call_done,
6493 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006494 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006495};
6496
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006497static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
6498 struct rpc_cred *cred,
6499 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006500{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006501 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006502 struct rpc_message msg = {
6503 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6504 .rpc_cred = cred,
6505 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006506 struct rpc_task_setup task_setup_data = {
6507 .rpc_client = clp->cl_rpcclient,
6508 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006509 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04006510 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006511 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006512
Alexandros Batsakis71358402010-02-05 03:45:05 -08006513 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006514 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006515 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006516 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006517 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006518 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006519 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05006520 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006521 if (is_privileged)
6522 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006523 msg.rpc_argp = &calldata->args;
6524 msg.rpc_resp = &calldata->res;
6525 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006526 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006527
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006528 return rpc_run_task(&task_setup_data);
6529}
6530
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006531static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006532{
6533 struct rpc_task *task;
6534 int ret = 0;
6535
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006536 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6537 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006538 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006539 if (IS_ERR(task))
6540 ret = PTR_ERR(task);
6541 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006542 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006543 dprintk("<-- %s status=%d\n", __func__, ret);
6544 return ret;
6545}
6546
6547static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6548{
6549 struct rpc_task *task;
6550 int ret;
6551
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006552 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006553 if (IS_ERR(task)) {
6554 ret = PTR_ERR(task);
6555 goto out;
6556 }
6557 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006558 if (!ret) {
6559 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6560
6561 if (task->tk_status == 0)
6562 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006563 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006564 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006565 rpc_put_task(task);
6566out:
6567 dprintk("<-- %s status=%d\n", __func__, ret);
6568 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006569}
6570
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006571struct nfs4_reclaim_complete_data {
6572 struct nfs_client *clp;
6573 struct nfs41_reclaim_complete_args arg;
6574 struct nfs41_reclaim_complete_res res;
6575};
6576
6577static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6578{
6579 struct nfs4_reclaim_complete_data *calldata = data;
6580
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006581 nfs41_setup_sequence(calldata->clp->cl_session,
6582 &calldata->arg.seq_args,
6583 &calldata->res.seq_res,
6584 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006585}
6586
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006587static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6588{
6589 switch(task->tk_status) {
6590 case 0:
6591 case -NFS4ERR_COMPLETE_ALREADY:
6592 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6593 break;
6594 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006595 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006596 /* fall through */
6597 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006598 return -EAGAIN;
6599 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006600 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006601 }
6602 return 0;
6603}
6604
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006605static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6606{
6607 struct nfs4_reclaim_complete_data *calldata = data;
6608 struct nfs_client *clp = calldata->clp;
6609 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6610
6611 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006612 if (!nfs41_sequence_done(task, res))
6613 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006614
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006615 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006616 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6617 rpc_restart_call_prepare(task);
6618 return;
6619 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006620 dprintk("<-- %s\n", __func__);
6621}
6622
6623static void nfs4_free_reclaim_complete_data(void *data)
6624{
6625 struct nfs4_reclaim_complete_data *calldata = data;
6626
6627 kfree(calldata);
6628}
6629
6630static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6631 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6632 .rpc_call_done = nfs4_reclaim_complete_done,
6633 .rpc_release = nfs4_free_reclaim_complete_data,
6634};
6635
6636/*
6637 * Issue a global reclaim complete.
6638 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04006639static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
6640 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006641{
6642 struct nfs4_reclaim_complete_data *calldata;
6643 struct rpc_task *task;
6644 struct rpc_message msg = {
6645 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04006646 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006647 };
6648 struct rpc_task_setup task_setup_data = {
6649 .rpc_client = clp->cl_rpcclient,
6650 .rpc_message = &msg,
6651 .callback_ops = &nfs4_reclaim_complete_call_ops,
6652 .flags = RPC_TASK_ASYNC,
6653 };
6654 int status = -ENOMEM;
6655
6656 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006657 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006658 if (calldata == NULL)
6659 goto out;
6660 calldata->clp = clp;
6661 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006662
Trond Myklebust9d12b212012-01-17 22:04:25 -05006663 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006664 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006665 msg.rpc_argp = &calldata->arg;
6666 msg.rpc_resp = &calldata->res;
6667 task_setup_data.callback_data = calldata;
6668 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006669 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006670 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006671 goto out;
6672 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006673 status = nfs4_wait_for_completion_rpc_task(task);
6674 if (status == 0)
6675 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006676 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006677 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006678out:
6679 dprintk("<-- %s status=%d\n", __func__, status);
6680 return status;
6681}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006682
6683static void
6684nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6685{
6686 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006687 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006688 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006689
6690 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006691 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6692 * right now covering the LAYOUTGET we are about to send.
6693 * However, that is not so catastrophic, and there seems
6694 * to be no way to prevent it completely.
6695 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006696 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006697 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006698 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006699 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6700 NFS_I(lgp->args.inode)->layout,
6701 lgp->args.ctx->state)) {
6702 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00006703 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006704}
6705
6706static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6707{
6708 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006709 struct inode *inode = lgp->args.inode;
6710 struct nfs_server *server = NFS_SERVER(inode);
6711 struct pnfs_layout_hdr *lo;
6712 struct nfs4_state *state = NULL;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006713 unsigned long timeo, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006714
6715 dprintk("--> %s\n", __func__);
6716
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006717 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006718 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006719
6720 switch (task->tk_status) {
6721 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006722 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006723 case -NFS4ERR_LAYOUTTRYLATER:
6724 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05006725 timeo = rpc_get_timeout(task->tk_client);
6726 giveup = lgp->args.timestamp + timeo;
6727 if (time_after(giveup, jiffies))
6728 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006729 break;
6730 case -NFS4ERR_EXPIRED:
6731 case -NFS4ERR_BAD_STATEID:
6732 spin_lock(&inode->i_lock);
6733 lo = NFS_I(inode)->layout;
6734 if (!lo || list_empty(&lo->plh_segs)) {
6735 spin_unlock(&inode->i_lock);
6736 /* If the open stateid was bad, then recover it. */
6737 state = lgp->args.ctx->state;
6738 } else {
6739 LIST_HEAD(head);
6740
6741 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6742 spin_unlock(&inode->i_lock);
6743 /* Mark the bad layout state as invalid, then
6744 * retry using the open stateid. */
6745 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006746 }
6747 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006748 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6749 rpc_restart_call_prepare(task);
6750out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006751 dprintk("<-- %s\n", __func__);
6752}
6753
Idan Kedar85541162012-08-02 11:47:10 +03006754static size_t max_response_pages(struct nfs_server *server)
6755{
6756 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6757 return nfs_page_array_len(0, max_resp_sz);
6758}
6759
6760static void nfs4_free_pages(struct page **pages, size_t size)
6761{
6762 int i;
6763
6764 if (!pages)
6765 return;
6766
6767 for (i = 0; i < size; i++) {
6768 if (!pages[i])
6769 break;
6770 __free_page(pages[i]);
6771 }
6772 kfree(pages);
6773}
6774
6775static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6776{
6777 struct page **pages;
6778 int i;
6779
6780 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6781 if (!pages) {
6782 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6783 return NULL;
6784 }
6785
6786 for (i = 0; i < size; i++) {
6787 pages[i] = alloc_page(gfp_flags);
6788 if (!pages[i]) {
6789 dprintk("%s: failed to allocate page\n", __func__);
6790 nfs4_free_pages(pages, size);
6791 return NULL;
6792 }
6793 }
6794
6795 return pages;
6796}
6797
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006798static void nfs4_layoutget_release(void *calldata)
6799{
6800 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006801 struct inode *inode = lgp->args.inode;
6802 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006803 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006804
6805 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006806 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006807 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006808 put_nfs_open_context(lgp->args.ctx);
6809 kfree(calldata);
6810 dprintk("<-- %s\n", __func__);
6811}
6812
6813static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6814 .rpc_call_prepare = nfs4_layoutget_prepare,
6815 .rpc_call_done = nfs4_layoutget_done,
6816 .rpc_release = nfs4_layoutget_release,
6817};
6818
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006819struct pnfs_layout_segment *
6820nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006821{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006822 struct inode *inode = lgp->args.inode;
6823 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006824 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006825 struct rpc_task *task;
6826 struct rpc_message msg = {
6827 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6828 .rpc_argp = &lgp->args,
6829 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04006830 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006831 };
6832 struct rpc_task_setup task_setup_data = {
6833 .rpc_client = server->client,
6834 .rpc_message = &msg,
6835 .callback_ops = &nfs4_layoutget_call_ops,
6836 .callback_data = lgp,
6837 .flags = RPC_TASK_ASYNC,
6838 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006839 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006840 int status = 0;
6841
6842 dprintk("--> %s\n", __func__);
6843
Idan Kedar85541162012-08-02 11:47:10 +03006844 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6845 if (!lgp->args.layout.pages) {
6846 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006847 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006848 }
6849 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006850 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03006851
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006852 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006853 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006854 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006855
6856 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
6857 pnfs_get_layout_hdr(NFS_I(inode)->layout);
6858
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006859 task = rpc_run_task(&task_setup_data);
6860 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006861 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006862 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006863 if (status == 0)
6864 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04006865 trace_nfs4_layoutget(lgp->args.ctx,
6866 &lgp->args.range,
6867 &lgp->res.range,
6868 status);
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05006869 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
6870 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006871 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006872 rpc_put_task(task);
6873 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006874 if (status)
6875 return ERR_PTR(status);
6876 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006877}
6878
Benny Halevycbe82602011-05-22 19:52:37 +03006879static void
6880nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6881{
6882 struct nfs4_layoutreturn *lrp = calldata;
6883
6884 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006885 nfs41_setup_sequence(lrp->clp->cl_session,
6886 &lrp->args.seq_args,
6887 &lrp->res.seq_res,
6888 task);
Benny Halevycbe82602011-05-22 19:52:37 +03006889}
6890
6891static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6892{
6893 struct nfs4_layoutreturn *lrp = calldata;
6894 struct nfs_server *server;
6895
6896 dprintk("--> %s\n", __func__);
6897
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006898 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03006899 return;
6900
6901 server = NFS_SERVER(lrp->args.inode);
6902 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006903 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006904 return;
6905 }
Benny Halevycbe82602011-05-22 19:52:37 +03006906 dprintk("<-- %s\n", __func__);
6907}
6908
6909static void nfs4_layoutreturn_release(void *calldata)
6910{
6911 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006912 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006913
6914 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006915 spin_lock(&lo->plh_inode->i_lock);
6916 if (lrp->res.lrs_present)
6917 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6918 lo->plh_block_lgets--;
6919 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006920 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006921 kfree(calldata);
6922 dprintk("<-- %s\n", __func__);
6923}
6924
6925static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6926 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6927 .rpc_call_done = nfs4_layoutreturn_done,
6928 .rpc_release = nfs4_layoutreturn_release,
6929};
6930
6931int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6932{
6933 struct rpc_task *task;
6934 struct rpc_message msg = {
6935 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6936 .rpc_argp = &lrp->args,
6937 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04006938 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03006939 };
6940 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04006941 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03006942 .rpc_message = &msg,
6943 .callback_ops = &nfs4_layoutreturn_call_ops,
6944 .callback_data = lrp,
6945 };
6946 int status;
6947
6948 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006949 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006950 task = rpc_run_task(&task_setup_data);
6951 if (IS_ERR(task))
6952 return PTR_ERR(task);
6953 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04006954 trace_nfs4_layoutreturn(lrp->args.inode, status);
Benny Halevycbe82602011-05-22 19:52:37 +03006955 dprintk("<-- %s status=%d\n", __func__, status);
6956 rpc_put_task(task);
6957 return status;
6958}
6959
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006960/*
6961 * Retrieve the list of Data Server devices from the MDS.
6962 */
6963static int _nfs4_getdevicelist(struct nfs_server *server,
6964 const struct nfs_fh *fh,
6965 struct pnfs_devicelist *devlist)
6966{
6967 struct nfs4_getdevicelist_args args = {
6968 .fh = fh,
6969 .layoutclass = server->pnfs_curr_ld->id,
6970 };
6971 struct nfs4_getdevicelist_res res = {
6972 .devlist = devlist,
6973 };
6974 struct rpc_message msg = {
6975 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6976 .rpc_argp = &args,
6977 .rpc_resp = &res,
6978 };
6979 int status;
6980
6981 dprintk("--> %s\n", __func__);
6982 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6983 &res.seq_res, 0);
6984 dprintk("<-- %s status=%d\n", __func__, status);
6985 return status;
6986}
6987
6988int nfs4_proc_getdevicelist(struct nfs_server *server,
6989 const struct nfs_fh *fh,
6990 struct pnfs_devicelist *devlist)
6991{
6992 struct nfs4_exception exception = { };
6993 int err;
6994
6995 do {
6996 err = nfs4_handle_exception(server,
6997 _nfs4_getdevicelist(server, fh, devlist),
6998 &exception);
6999 } while (exception.retry);
7000
7001 dprintk("%s: err=%d, num_devs=%u\n", __func__,
7002 err, devlist->num_devs);
7003
7004 return err;
7005}
7006EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
7007
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007008static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007009_nfs4_proc_getdeviceinfo(struct nfs_server *server,
7010 struct pnfs_device *pdev,
7011 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007012{
7013 struct nfs4_getdeviceinfo_args args = {
7014 .pdev = pdev,
7015 };
7016 struct nfs4_getdeviceinfo_res res = {
7017 .pdev = pdev,
7018 };
7019 struct rpc_message msg = {
7020 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
7021 .rpc_argp = &args,
7022 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007023 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007024 };
7025 int status;
7026
7027 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00007028 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007029 dprintk("<-- %s status=%d\n", __func__, status);
7030
7031 return status;
7032}
7033
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007034int nfs4_proc_getdeviceinfo(struct nfs_server *server,
7035 struct pnfs_device *pdev,
7036 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007037{
7038 struct nfs4_exception exception = { };
7039 int err;
7040
7041 do {
7042 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007043 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007044 &exception);
7045 } while (exception.retry);
7046 return err;
7047}
7048EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
7049
Andy Adamson863a3c62011-03-23 13:27:54 +00007050static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
7051{
7052 struct nfs4_layoutcommit_data *data = calldata;
7053 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007054 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00007055
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007056 nfs41_setup_sequence(session,
7057 &data->args.seq_args,
7058 &data->res.seq_res,
7059 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00007060}
7061
7062static void
7063nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
7064{
7065 struct nfs4_layoutcommit_data *data = calldata;
7066 struct nfs_server *server = NFS_SERVER(data->args.inode);
7067
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007068 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00007069 return;
7070
7071 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007072 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
7073 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
7074 case -NFS4ERR_BADLAYOUT: /* no layout */
7075 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00007076 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007077 break;
7078 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00007079 nfs_post_op_update_inode_force_wcc(data->args.inode,
7080 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007081 break;
7082 default:
7083 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
7084 rpc_restart_call_prepare(task);
7085 return;
7086 }
7087 }
Andy Adamson863a3c62011-03-23 13:27:54 +00007088}
7089
7090static void nfs4_layoutcommit_release(void *calldata)
7091{
7092 struct nfs4_layoutcommit_data *data = calldata;
7093
Andy Adamsondb29c082011-07-30 20:52:38 -04007094 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00007095 put_rpccred(data->cred);
7096 kfree(data);
7097}
7098
7099static const struct rpc_call_ops nfs4_layoutcommit_ops = {
7100 .rpc_call_prepare = nfs4_layoutcommit_prepare,
7101 .rpc_call_done = nfs4_layoutcommit_done,
7102 .rpc_release = nfs4_layoutcommit_release,
7103};
7104
7105int
Andy Adamsonef311532011-03-12 02:58:10 -05007106nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00007107{
7108 struct rpc_message msg = {
7109 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
7110 .rpc_argp = &data->args,
7111 .rpc_resp = &data->res,
7112 .rpc_cred = data->cred,
7113 };
7114 struct rpc_task_setup task_setup_data = {
7115 .task = &data->task,
7116 .rpc_client = NFS_CLIENT(data->args.inode),
7117 .rpc_message = &msg,
7118 .callback_ops = &nfs4_layoutcommit_ops,
7119 .callback_data = data,
7120 .flags = RPC_TASK_ASYNC,
7121 };
7122 struct rpc_task *task;
7123 int status = 0;
7124
7125 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
7126 "lbw: %llu inode %lu\n",
7127 data->task.tk_pid, sync,
7128 data->args.lastbytewritten,
7129 data->args.inode->i_ino);
7130
Trond Myklebust9d12b212012-01-17 22:04:25 -05007131 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00007132 task = rpc_run_task(&task_setup_data);
7133 if (IS_ERR(task))
7134 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05007135 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00007136 goto out;
7137 status = nfs4_wait_for_completion_rpc_task(task);
7138 if (status != 0)
7139 goto out;
7140 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007141 trace_nfs4_layoutcommit(data->args.inode, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00007142out:
7143 dprintk("%s: status %d\n", __func__, status);
7144 rpc_put_task(task);
7145 return status;
7146}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007147
Andy Adamson97431202013-08-08 10:57:56 -04007148/**
7149 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
7150 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
7151 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007152static int
7153_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7154 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
7155{
7156 struct nfs41_secinfo_no_name_args args = {
7157 .style = SECINFO_STYLE_CURRENT_FH,
7158 };
7159 struct nfs4_secinfo_res res = {
7160 .flavors = flavors,
7161 };
7162 struct rpc_message msg = {
7163 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
7164 .rpc_argp = &args,
7165 .rpc_resp = &res,
7166 };
Andy Adamson97431202013-08-08 10:57:56 -04007167 return nfs4_call_sync(server->nfs_client->cl_rpcclient, server, &msg,
7168 &args.seq_args, &res.seq_res, 0);
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007169}
7170
7171static int
7172nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7173 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
7174{
7175 struct nfs4_exception exception = { };
7176 int err;
7177 do {
7178 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
7179 switch (err) {
7180 case 0:
7181 case -NFS4ERR_WRONGSEC:
7182 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007183 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007184 default:
7185 err = nfs4_handle_exception(server, err, &exception);
7186 }
7187 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007188out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007189 return err;
7190}
7191
7192static int
7193nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
7194 struct nfs_fsinfo *info)
7195{
7196 int err;
7197 struct page *page;
7198 rpc_authflavor_t flavor;
7199 struct nfs4_secinfo_flavors *flavors;
7200
7201 page = alloc_page(GFP_KERNEL);
7202 if (!page) {
7203 err = -ENOMEM;
7204 goto out;
7205 }
7206
7207 flavors = page_address(page);
7208 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
7209
7210 /*
7211 * Fall back on "guess and check" method if
7212 * the server doesn't support SECINFO_NO_NAME
7213 */
7214 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
7215 err = nfs4_find_root_sec(server, fhandle, info);
7216 goto out_freepage;
7217 }
7218 if (err)
7219 goto out_freepage;
7220
7221 flavor = nfs_find_best_sec(flavors);
7222 if (err == 0)
7223 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
7224
7225out_freepage:
7226 put_page(page);
7227 if (err == -EACCES)
7228 return -EPERM;
7229out:
7230 return err;
7231}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007232
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007233static int _nfs41_test_stateid(struct nfs_server *server,
7234 nfs4_stateid *stateid,
7235 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04007236{
7237 int status;
7238 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007239 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04007240 };
7241 struct nfs41_test_stateid_res res;
7242 struct rpc_message msg = {
7243 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
7244 .rpc_argp = &args,
7245 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007246 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04007247 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007248
Chuck Lever38527b12012-07-11 16:30:23 -04007249 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05007250 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007251 nfs4_set_sequence_privileged(&args.seq_args);
7252 status = nfs4_call_sync_sequence(server->client, server, &msg,
7253 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04007254 if (status != NFS_OK) {
7255 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04007256 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04007257 }
7258 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04007259 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04007260}
7261
Chuck Lever38527b12012-07-11 16:30:23 -04007262/**
7263 * nfs41_test_stateid - perform a TEST_STATEID operation
7264 *
7265 * @server: server / transport on which to perform the operation
7266 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007267 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04007268 *
7269 * Returns NFS_OK if the server recognizes that "stateid" is valid.
7270 * Otherwise a negative NFS4ERR value is returned if the operation
7271 * failed or the state ID is not currently valid.
7272 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007273static int nfs41_test_stateid(struct nfs_server *server,
7274 nfs4_stateid *stateid,
7275 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04007276{
7277 struct nfs4_exception exception = { };
7278 int err;
7279 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007280 err = _nfs41_test_stateid(server, stateid, cred);
Chuck Lever377e5072012-07-11 16:29:45 -04007281 if (err != -NFS4ERR_DELAY)
7282 break;
7283 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04007284 } while (exception.retry);
7285 return err;
7286}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007287
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007288struct nfs_free_stateid_data {
7289 struct nfs_server *server;
7290 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007291 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007292};
7293
7294static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
7295{
7296 struct nfs_free_stateid_data *data = calldata;
7297 nfs41_setup_sequence(nfs4_get_session(data->server),
7298 &data->args.seq_args,
7299 &data->res.seq_res,
7300 task);
7301}
7302
7303static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
7304{
7305 struct nfs_free_stateid_data *data = calldata;
7306
7307 nfs41_sequence_done(task, &data->res.seq_res);
7308
7309 switch (task->tk_status) {
7310 case -NFS4ERR_DELAY:
7311 if (nfs4_async_handle_error(task, data->server, NULL) == -EAGAIN)
7312 rpc_restart_call_prepare(task);
7313 }
7314}
7315
7316static void nfs41_free_stateid_release(void *calldata)
7317{
7318 kfree(calldata);
7319}
7320
Trond Myklebust17f26b12013-08-21 15:48:42 -04007321static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007322 .rpc_call_prepare = nfs41_free_stateid_prepare,
7323 .rpc_call_done = nfs41_free_stateid_done,
7324 .rpc_release = nfs41_free_stateid_release,
7325};
7326
7327static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
7328 nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007329 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007330 bool privileged)
7331{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007332 struct rpc_message msg = {
7333 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007334 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007335 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007336 struct rpc_task_setup task_setup = {
7337 .rpc_client = server->client,
7338 .rpc_message = &msg,
7339 .callback_ops = &nfs41_free_stateid_ops,
7340 .flags = RPC_TASK_ASYNC,
7341 };
7342 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007343
Chuck Lever38527b12012-07-11 16:30:23 -04007344 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007345 data = kmalloc(sizeof(*data), GFP_NOFS);
7346 if (!data)
7347 return ERR_PTR(-ENOMEM);
7348 data->server = server;
7349 nfs4_stateid_copy(&data->args.stateid, stateid);
7350
7351 task_setup.callback_data = data;
7352
7353 msg.rpc_argp = &data->args;
7354 msg.rpc_resp = &data->res;
7355 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
7356 if (privileged)
7357 nfs4_set_sequence_privileged(&data->args.seq_args);
7358
7359 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007360}
7361
Chuck Lever38527b12012-07-11 16:30:23 -04007362/**
7363 * nfs41_free_stateid - perform a FREE_STATEID operation
7364 *
7365 * @server: server / transport on which to perform the operation
7366 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007367 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04007368 *
7369 * Returns NFS_OK if the server freed "stateid". Otherwise a
7370 * negative NFS4ERR value is returned.
7371 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007372static int nfs41_free_stateid(struct nfs_server *server,
7373 nfs4_stateid *stateid,
7374 struct rpc_cred *cred)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007375{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007376 struct rpc_task *task;
7377 int ret;
7378
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007379 task = _nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007380 if (IS_ERR(task))
7381 return PTR_ERR(task);
7382 ret = rpc_wait_for_completion_task(task);
7383 if (!ret)
7384 ret = task->tk_status;
7385 rpc_put_task(task);
7386 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007387}
Trond Myklebust36281ca2012-03-04 18:13:56 -05007388
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007389static int nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
7390{
7391 struct rpc_task *task;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007392 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007393
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007394 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007395 nfs4_free_lock_state(server, lsp);
7396 if (IS_ERR(task))
7397 return PTR_ERR(task);
7398 rpc_put_task(task);
7399 return 0;
7400}
7401
Trond Myklebust36281ca2012-03-04 18:13:56 -05007402static bool nfs41_match_stateid(const nfs4_stateid *s1,
7403 const nfs4_stateid *s2)
7404{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007405 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007406 return false;
7407
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007408 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007409 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007410 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007411 return true;
7412
7413 return false;
7414}
7415
Andy Adamson557134a2009-04-01 09:21:53 -04007416#endif /* CONFIG_NFS_V4_1 */
7417
Trond Myklebust36281ca2012-03-04 18:13:56 -05007418static bool nfs4_match_stateid(const nfs4_stateid *s1,
7419 const nfs4_stateid *s2)
7420{
Trond Myklebustf597c532012-03-04 18:13:56 -05007421 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05007422}
7423
7424
Trond Myklebust17280172012-03-11 13:11:00 -04007425static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007426 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007427 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007428 .recover_open = nfs4_open_reclaim,
7429 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04007430 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04007431 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007432};
7433
Andy Adamson591d71c2009-04-01 09:22:47 -04007434#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007435static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007436 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
7437 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
7438 .recover_open = nfs4_open_reclaim,
7439 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05007440 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007441 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04007442 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04007443};
7444#endif /* CONFIG_NFS_V4_1 */
7445
Trond Myklebust17280172012-03-11 13:11:00 -04007446static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007447 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007448 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007449 .recover_open = nfs4_open_expired,
7450 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04007451 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007452};
7453
Andy Adamson591d71c2009-04-01 09:22:47 -04007454#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007455static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007456 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
7457 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007458 .recover_open = nfs41_open_expired,
7459 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05007460 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04007461};
7462#endif /* CONFIG_NFS_V4_1 */
7463
Trond Myklebust17280172012-03-11 13:11:00 -04007464static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007465 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04007466 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007467 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04007468};
7469
7470#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007471static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007472 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04007473 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007474 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04007475};
7476#endif
7477
Trond Myklebust97dc1352010-06-16 09:52:26 -04007478static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
7479 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04007480 .init_caps = NFS_CAP_READDIRPLUS
7481 | NFS_CAP_ATOMIC_OPEN
7482 | NFS_CAP_CHANGE_ATTR
7483 | NFS_CAP_POSIX_LOCK,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007484 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007485 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007486 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007487 .free_lock_state = nfs4_release_lockowner,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007488 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
7489 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
7490 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007491};
7492
7493#if defined(CONFIG_NFS_V4_1)
7494static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
7495 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04007496 .init_caps = NFS_CAP_READDIRPLUS
7497 | NFS_CAP_ATOMIC_OPEN
7498 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04007499 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04007500 | NFS_CAP_STATEID_NFSV41
7501 | NFS_CAP_ATOMIC_OPEN_V1,
Bryan Schumaker104287c2012-11-12 14:13:13 -05007502 .call_sync = nfs4_call_sync_sequence,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007503 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007504 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007505 .free_lock_state = nfs41_free_lock_state,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007506 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7507 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7508 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007509};
7510#endif
7511
Steve Dickson42c2c422013-05-22 12:50:38 -04007512#if defined(CONFIG_NFS_V4_2)
7513static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
7514 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04007515 .init_caps = NFS_CAP_READDIRPLUS
7516 | NFS_CAP_ATOMIC_OPEN
7517 | NFS_CAP_CHANGE_ATTR
7518 | NFS_CAP_POSIX_LOCK
7519 | NFS_CAP_STATEID_NFSV41
7520 | NFS_CAP_ATOMIC_OPEN_V1,
Steve Dickson42c2c422013-05-22 12:50:38 -04007521 .call_sync = nfs4_call_sync_sequence,
7522 .match_stateid = nfs41_match_stateid,
7523 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04007524 .free_lock_state = nfs41_free_lock_state,
Steve Dickson42c2c422013-05-22 12:50:38 -04007525 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7526 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7527 .state_renewal_ops = &nfs41_state_renewal_ops,
7528};
7529#endif
7530
Trond Myklebust97dc1352010-06-16 09:52:26 -04007531const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
7532 [0] = &nfs_v4_0_minor_ops,
7533#if defined(CONFIG_NFS_V4_1)
7534 [1] = &nfs_v4_1_minor_ops,
7535#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04007536#if defined(CONFIG_NFS_V4_2)
7537 [2] = &nfs_v4_2_minor_ops,
7538#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04007539};
7540
Trond Myklebust17f26b12013-08-21 15:48:42 -04007541static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04007542 .create = nfs_create,
7543 .lookup = nfs_lookup,
7544 .atomic_open = nfs_atomic_open,
7545 .link = nfs_link,
7546 .unlink = nfs_unlink,
7547 .symlink = nfs_symlink,
7548 .mkdir = nfs_mkdir,
7549 .rmdir = nfs_rmdir,
7550 .mknod = nfs_mknod,
7551 .rename = nfs_rename,
7552 .permission = nfs_permission,
7553 .getattr = nfs_getattr,
7554 .setattr = nfs_setattr,
7555 .getxattr = generic_getxattr,
7556 .setxattr = generic_setxattr,
7557 .listxattr = generic_listxattr,
7558 .removexattr = generic_removexattr,
7559};
7560
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08007561static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007562 .permission = nfs_permission,
7563 .getattr = nfs_getattr,
7564 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007565 .getxattr = generic_getxattr,
7566 .setxattr = generic_setxattr,
7567 .listxattr = generic_listxattr,
7568 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007569};
7570
David Howells509de812006-08-22 20:06:11 -04007571const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007572 .version = 4, /* protocol version */
7573 .dentry_ops = &nfs4_dentry_operations,
7574 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007575 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04007576 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007577 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04007578 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04007579 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007580 .getattr = nfs4_proc_getattr,
7581 .setattr = nfs4_proc_setattr,
7582 .lookup = nfs4_proc_lookup,
7583 .access = nfs4_proc_access,
7584 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007585 .create = nfs4_proc_create,
7586 .remove = nfs4_proc_remove,
7587 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04007588 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007589 .unlink_done = nfs4_proc_unlink_done,
7590 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04007591 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04007592 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04007593 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007594 .link = nfs4_proc_link,
7595 .symlink = nfs4_proc_symlink,
7596 .mkdir = nfs4_proc_mkdir,
7597 .rmdir = nfs4_proc_remove,
7598 .readdir = nfs4_proc_readdir,
7599 .mknod = nfs4_proc_mknod,
7600 .statfs = nfs4_proc_statfs,
7601 .fsinfo = nfs4_proc_fsinfo,
7602 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04007603 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007604 .decode_dirent = nfs4_decode_dirent,
7605 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04007606 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04007607 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05007608 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007609 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04007610 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04007611 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007612 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007613 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04007614 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007615 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007616 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00007617 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04007618 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04007619 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04007620 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04007621 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04007622 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00007623 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04007624 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04007625 .create_server = nfs4_create_server,
7626 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007627};
7628
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007629static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7630 .prefix = XATTR_NAME_NFSV4_ACL,
7631 .list = nfs4_xattr_list_nfs4_acl,
7632 .get = nfs4_xattr_get_nfs4_acl,
7633 .set = nfs4_xattr_set_nfs4_acl,
7634};
7635
7636const struct xattr_handler *nfs4_xattr_handlers[] = {
7637 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04007638#ifdef CONFIG_NFS_V4_SECURITY_LABEL
7639 &nfs4_xattr_nfs4_label_handler,
7640#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007641 NULL
7642};
7643
Linus Torvalds1da177e2005-04-16 15:20:36 -07007644/*
7645 * Local variables:
7646 * c-basic-offset: 8
7647 * End:
7648 */