blob: e9e58cc34c618834efe11b473533fbf867fd3466 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Trond Myklebust6926afd2012-01-07 13:22:46 -050054#include <linux/nfs_idmap.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000055#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050056#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010057#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050066#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000067#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Trond Myklebustc6d01c62013-08-09 11:51:26 -040069#include "nfs4trace.h"
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define NFSDBG_FACILITY NFSDBG_PROC
72
Trond Myklebust2066fe82006-09-15 08:30:46 -040073#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define NFS4_POLL_RETRY_MAX (15*HZ)
75
Trond Myklebustcdd4e682006-01-03 09:55:12 +010076struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010077static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080078static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050080static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Chuck Lever81934dd2012-03-01 17:01:57 -050081static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040082static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
83static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040084static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
85 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -040086 struct nfs4_state *state, struct nfs4_label *ilabel,
87 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040088#ifdef CONFIG_NFS_V4_1
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040089static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
90 struct rpc_cred *);
91static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *,
92 struct rpc_cred *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040093#endif
David Quigleyaa9c2662013-05-22 12:50:44 -040094
95#ifdef CONFIG_NFS_V4_SECURITY_LABEL
96static inline struct nfs4_label *
97nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
98 struct iattr *sattr, struct nfs4_label *label)
99{
100 int err;
101
102 if (label == NULL)
103 return NULL;
104
105 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
106 return NULL;
107
108 if (NFS_SERVER(dir)->nfs_client->cl_minorversion < 2)
109 return NULL;
110
111 err = security_dentry_init_security(dentry, sattr->ia_mode,
112 &dentry->d_name, (void **)&label->label, &label->len);
113 if (err == 0)
114 return label;
115
116 return NULL;
117}
118static inline void
119nfs4_label_release_security(struct nfs4_label *label)
120{
121 if (label)
122 security_release_secctx(label->label, label->len);
123}
124static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
125{
126 if (label)
127 return server->attr_bitmask;
128
129 return server->attr_bitmask_nl;
130}
131#else
132static inline struct nfs4_label *
133nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
134 struct iattr *sattr, struct nfs4_label *l)
135{ return NULL; }
136static inline void
137nfs4_label_release_security(struct nfs4_label *label)
138{ return; }
139static inline u32 *
140nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
141{ return server->attr_bitmask; }
142#endif
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500145static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Trond Myklebust52567b02009-10-23 14:46:42 -0400147 if (err >= -1000)
148 return err;
149 switch (err) {
150 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500151 case -NFS4ERR_LAYOUTTRYLATER:
152 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400153 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000154 case -NFS4ERR_WRONGSEC:
155 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800156 case -NFS4ERR_BADOWNER:
157 case -NFS4ERR_BADNAME:
158 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400159 case -NFS4ERR_SHARE_DENIED:
160 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400161 case -NFS4ERR_MINOR_VERS_MISMATCH:
162 return -EPROTONOSUPPORT;
Weston Andros Adamson6168f622012-09-10 14:00:46 -0400163 case -NFS4ERR_ACCESS:
164 return -EACCES;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400165 case -NFS4ERR_FILE_OPEN:
166 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400167 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700169 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400170 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400172 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
175/*
176 * This is our standard bitmap for GETATTR requests.
177 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400178const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 FATTR4_WORD0_TYPE
180 | FATTR4_WORD0_CHANGE
181 | FATTR4_WORD0_SIZE
182 | FATTR4_WORD0_FSID
183 | FATTR4_WORD0_FILEID,
184 FATTR4_WORD1_MODE
185 | FATTR4_WORD1_NUMLINKS
186 | FATTR4_WORD1_OWNER
187 | FATTR4_WORD1_OWNER_GROUP
188 | FATTR4_WORD1_RAWDEV
189 | FATTR4_WORD1_SPACE_USED
190 | FATTR4_WORD1_TIME_ACCESS
191 | FATTR4_WORD1_TIME_METADATA
David Quigleyaa9c2662013-05-22 12:50:44 -0400192 | FATTR4_WORD1_TIME_MODIFY,
193#ifdef CONFIG_NFS_V4_SECURITY_LABEL
194 FATTR4_WORD2_SECURITY_LABEL
195#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196};
197
Trond Myklebust1549210f2012-06-05 09:16:47 -0400198static const u32 nfs4_pnfs_open_bitmap[3] = {
199 FATTR4_WORD0_TYPE
200 | FATTR4_WORD0_CHANGE
201 | FATTR4_WORD0_SIZE
202 | FATTR4_WORD0_FSID
203 | FATTR4_WORD0_FILEID,
204 FATTR4_WORD1_MODE
205 | FATTR4_WORD1_NUMLINKS
206 | FATTR4_WORD1_OWNER
207 | FATTR4_WORD1_OWNER_GROUP
208 | FATTR4_WORD1_RAWDEV
209 | FATTR4_WORD1_SPACE_USED
210 | FATTR4_WORD1_TIME_ACCESS
211 | FATTR4_WORD1_TIME_METADATA
212 | FATTR4_WORD1_TIME_MODIFY,
213 FATTR4_WORD2_MDSTHRESHOLD
214};
215
Andy Adamsone23008e2012-10-02 21:07:32 -0400216static const u32 nfs4_open_noattr_bitmap[3] = {
217 FATTR4_WORD0_TYPE
218 | FATTR4_WORD0_CHANGE
219 | FATTR4_WORD0_FILEID,
220};
221
David Quigleya09df2c2013-05-22 12:50:41 -0400222const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 FATTR4_WORD0_FILES_AVAIL
224 | FATTR4_WORD0_FILES_FREE
225 | FATTR4_WORD0_FILES_TOTAL,
226 FATTR4_WORD1_SPACE_AVAIL
227 | FATTR4_WORD1_SPACE_FREE
228 | FATTR4_WORD1_SPACE_TOTAL
229};
230
David Quigleya09df2c2013-05-22 12:50:41 -0400231const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 FATTR4_WORD0_MAXLINK
233 | FATTR4_WORD0_MAXNAME,
234 0
235};
236
Fred Isamandae100c2011-07-30 20:52:37 -0400237const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 | FATTR4_WORD0_MAXREAD
239 | FATTR4_WORD0_MAXWRITE
240 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700241 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400242 | FATTR4_WORD1_FS_LAYOUT_TYPES,
243 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244};
245
David Quigleya09df2c2013-05-22 12:50:41 -0400246const u32 nfs4_fs_locations_bitmap[3] = {
Manoj Naik830b8e32006-06-09 09:34:25 -0400247 FATTR4_WORD0_TYPE
248 | FATTR4_WORD0_CHANGE
249 | FATTR4_WORD0_SIZE
250 | FATTR4_WORD0_FSID
251 | FATTR4_WORD0_FILEID
252 | FATTR4_WORD0_FS_LOCATIONS,
253 FATTR4_WORD1_MODE
254 | FATTR4_WORD1_NUMLINKS
255 | FATTR4_WORD1_OWNER
256 | FATTR4_WORD1_OWNER_GROUP
257 | FATTR4_WORD1_RAWDEV
258 | FATTR4_WORD1_SPACE_USED
259 | FATTR4_WORD1_TIME_ACCESS
260 | FATTR4_WORD1_TIME_METADATA
261 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400262 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400263};
264
Al Virobc4785c2006-10-19 23:28:51 -0700265static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 struct nfs4_readdir_arg *readdir)
267{
Al Viro0dbb4c62006-10-19 23:28:49 -0700268 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000271 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
273 return;
274 }
275
276 readdir->cookie = 0;
277 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
278 if (cookie == 2)
279 return;
280
281 /*
282 * NFSv4 servers do not return entries for '.' and '..'
283 * Therefore, we fake these entries here. We let '.'
284 * have cookie 0 and '..' have cookie 1. Note that
285 * when talking to the server, we always send cookie 0
286 * instead of 1 or 2.
287 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800288 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 if (cookie == 0) {
291 *p++ = xdr_one; /* next */
292 *p++ = xdr_zero; /* cookie, first word */
293 *p++ = xdr_one; /* cookie, second word */
294 *p++ = xdr_one; /* entry len */
295 memcpy(p, ".\0\0\0", 4); /* entry */
296 p++;
297 *p++ = xdr_one; /* bitmap length */
298 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
299 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400300 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302
303 *p++ = xdr_one; /* next */
304 *p++ = xdr_zero; /* cookie, first word */
305 *p++ = xdr_two; /* cookie, second word */
306 *p++ = xdr_two; /* entry len */
307 memcpy(p, "..\0\0", 4); /* entry */
308 p++;
309 *p++ = xdr_one; /* bitmap length */
310 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
311 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400312 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 readdir->pgbase = (char *)p - (char *)start;
315 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800316 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
Trond Myklebust65de8722008-12-23 15:21:44 -0500319static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
320{
321 int res = 0;
322
323 might_sleep();
324
325 if (*timeout <= 0)
326 *timeout = NFS4_POLL_RETRY_MIN;
327 if (*timeout > NFS4_POLL_RETRY_MAX)
328 *timeout = NFS4_POLL_RETRY_MAX;
Colin Cross416ad3c2013-05-06 23:50:06 +0000329 freezable_schedule_timeout_killable_unsafe(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500330 if (fatal_signal_pending(current))
331 res = -ERESTARTSYS;
332 *timeout <<= 1;
333 return res;
334}
335
336/* This is the error handling routine for processes that are allowed
337 * to sleep.
338 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800339static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500340{
341 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500342 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500343 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500344 int ret = errorcode;
345
346 exception->retry = 0;
347 switch(errorcode) {
348 case 0:
349 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500350 case -NFS4ERR_OPENMODE:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400351 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400352 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500353 exception->retry = 1;
354 return 0;
355 }
356 if (state == NULL)
357 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400358 ret = nfs4_schedule_stateid_recovery(server, state);
359 if (ret < 0)
360 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500361 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500362 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500363 case -NFS4ERR_ADMIN_REVOKED:
364 case -NFS4ERR_BAD_STATEID:
Trond Myklebustb570a972013-04-12 15:04:51 -0400365 if (inode != NULL && nfs4_have_delegation(inode, FMODE_READ)) {
366 nfs_remove_bad_delegation(inode);
367 exception->retry = 1;
368 break;
369 }
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500370 if (state == NULL)
371 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400372 ret = nfs4_schedule_stateid_recovery(server, state);
373 if (ret < 0)
374 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500375 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400376 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -0400377 if (state != NULL) {
378 ret = nfs4_schedule_stateid_recovery(server, state);
379 if (ret < 0)
380 break;
381 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500382 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500383 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500384 nfs4_schedule_lease_recovery(clp);
385 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500386#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400387 case -NFS4ERR_BADSESSION:
388 case -NFS4ERR_BADSLOT:
389 case -NFS4ERR_BAD_HIGH_SLOT:
390 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
391 case -NFS4ERR_DEADSESSION:
392 case -NFS4ERR_SEQ_FALSE_RETRY:
393 case -NFS4ERR_SEQ_MISORDERED:
394 dprintk("%s ERROR: %d Reset session\n", __func__,
395 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400396 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400397 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500398#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500399 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500400 if (exception->timeout > HZ) {
401 /* We have retried a decent amount, time to
402 * fail
403 */
404 ret = -EBUSY;
405 break;
406 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500407 case -NFS4ERR_GRACE:
408 case -NFS4ERR_DELAY:
409 ret = nfs4_delay(server->client, &exception->timeout);
410 if (ret != 0)
411 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400412 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500413 case -NFS4ERR_OLD_STATEID:
414 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800415 break;
416 case -NFS4ERR_BADOWNER:
417 /* The following works around a Linux server bug! */
418 case -NFS4ERR_BADNAME:
419 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
420 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
421 exception->retry = 1;
422 printk(KERN_WARNING "NFS: v4 server %s "
423 "does not accept raw "
424 "uid/gids. "
425 "Reenabling the idmapper.\n",
426 server->nfs_client->cl_hostname);
427 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500428 }
429 /* We failed to handle the error */
430 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500431wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500432 ret = nfs4_wait_clnt_recover(clp);
433 if (ret == 0)
434 exception->retry = 1;
435 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500436}
437
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400438/*
439 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
440 * or 'false' otherwise.
441 */
442static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
443{
444 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
445
446 if (flavor == RPC_AUTH_GSS_KRB5I ||
447 flavor == RPC_AUTH_GSS_KRB5P)
448 return true;
449
450 return false;
451}
Trond Myklebust65de8722008-12-23 15:21:44 -0500452
Trond Myklebust452e9352010-07-31 14:29:06 -0400453static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 spin_lock(&clp->cl_lock);
456 if (time_before(clp->cl_last_renewal,timestamp))
457 clp->cl_last_renewal = timestamp;
458 spin_unlock(&clp->cl_lock);
459}
460
Trond Myklebust452e9352010-07-31 14:29:06 -0400461static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
462{
463 do_renew_lease(server->nfs_client, timestamp);
464}
465
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400466struct nfs4_call_sync_data {
467 const struct nfs_server *seq_server;
468 struct nfs4_sequence_args *seq_args;
469 struct nfs4_sequence_res *seq_res;
470};
471
Chuck Levera9c92d62013-08-09 12:48:18 -0400472static void nfs4_init_sequence(struct nfs4_sequence_args *args,
473 struct nfs4_sequence_res *res, int cache_reply)
474{
475 args->sa_slot = NULL;
476 args->sa_cache_this = cache_reply;
477 args->sa_privileged = 0;
478
479 res->sr_slot = NULL;
480}
481
482static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
483{
484 args->sa_privileged = 1;
485}
486
Chuck Lever3bd23842013-08-09 12:49:19 -0400487static int nfs40_setup_sequence(const struct nfs_server *server,
488 struct nfs4_sequence_args *args,
489 struct nfs4_sequence_res *res,
490 struct rpc_task *task)
491{
492 struct nfs4_slot_table *tbl = server->nfs_client->cl_slot_tbl;
493 struct nfs4_slot *slot;
494
495 /* slot already allocated? */
496 if (res->sr_slot != NULL)
497 goto out_start;
498
499 spin_lock(&tbl->slot_tbl_lock);
500 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
501 goto out_sleep;
502
503 slot = nfs4_alloc_slot(tbl);
504 if (IS_ERR(slot)) {
505 if (slot == ERR_PTR(-ENOMEM))
506 task->tk_timeout = HZ >> 2;
507 goto out_sleep;
508 }
509 spin_unlock(&tbl->slot_tbl_lock);
510
511 args->sa_slot = slot;
512 res->sr_slot = slot;
513
514out_start:
515 rpc_call_start(task);
516 return 0;
517
518out_sleep:
519 if (args->sa_privileged)
520 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
521 NULL, RPC_PRIORITY_PRIVILEGED);
522 else
523 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
524 spin_unlock(&tbl->slot_tbl_lock);
525 return -EAGAIN;
526}
527
528static int nfs40_sequence_done(struct rpc_task *task,
529 struct nfs4_sequence_res *res)
530{
531 struct nfs4_slot *slot = res->sr_slot;
532 struct nfs4_slot_table *tbl;
533
534 if (!RPC_WAS_SENT(task))
535 goto out;
536
537 tbl = slot->table;
538 spin_lock(&tbl->slot_tbl_lock);
539 if (!nfs41_wake_and_assign_slot(tbl, slot))
540 nfs4_free_slot(tbl, slot);
541 spin_unlock(&tbl->slot_tbl_lock);
542
543 res->sr_slot = NULL;
544out:
545 return 1;
546}
547
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400548#if defined(CONFIG_NFS_V4_1)
549
Trond Myklebustd185a332010-06-16 09:52:25 -0400550static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400551{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500552 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400553 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500554 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400555
Benny Halevydfb4f3092010-09-24 09:17:01 -0400556 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400557 /* just wake up the next guy waiting since
558 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500559 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500560 return;
Andy Adamson13615872009-04-01 09:22:17 -0400561 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500562 tbl = res->sr_slot->table;
563 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500564
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500565 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500566 /* Be nice to the server: try to ensure that the last transmitted
567 * value for highest_user_slotid <= target_highest_slotid
568 */
569 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
570 send_new_highest_used_slotid = true;
571
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500572 if (nfs41_wake_and_assign_slot(tbl, res->sr_slot)) {
573 send_new_highest_used_slotid = false;
574 goto out_unlock;
575 }
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500576 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500577
578 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
579 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500580out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500581 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400582 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500583 if (send_new_highest_used_slotid)
584 nfs41_server_notify_highest_slotid_update(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400585}
586
Trond Myklebust14516c32010-07-31 14:29:06 -0400587static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400588{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500589 struct nfs4_session *session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500590 struct nfs4_slot *slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400591 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500592 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500593 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400594
Bryan Schumaker468f8612011-04-18 15:57:32 -0400595 /* don't increment the sequence number if the task wasn't sent */
596 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400597 goto out;
598
Trond Myklebust933602e2012-11-16 12:12:38 -0500599 slot = res->sr_slot;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500600 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500601
Trond Myklebustac20d162012-12-15 15:36:07 -0500602 if (slot->interrupted) {
603 slot->interrupted = 0;
604 interrupted = true;
605 }
606
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400607 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500608 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400609 switch (res->sr_status) {
610 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400611 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500612 ++slot->seq_nr;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500613 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500614 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500615 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500616 if (res->sr_status_flags != 0)
617 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500618 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400619 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500620 case 1:
621 /*
622 * sr_status remains 1 if an RPC level error occurred.
623 * The server may or may not have processed the sequence
624 * operation..
625 * Mark the slot as having hosted an interrupted RPC call.
626 */
627 slot->interrupted = 1;
628 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400629 case -NFS4ERR_DELAY:
630 /* The server detected a resend of the RPC call and
631 * returned NFS4ERR_DELAY as per Section 2.10.6.2
632 * of RFC5661.
633 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500634 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400635 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500636 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500637 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400638 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500639 case -NFS4ERR_BADSLOT:
640 /*
641 * The slot id we used was probably retired. Try again
642 * using a different slot id.
643 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500644 goto retry_nowait;
645 case -NFS4ERR_SEQ_MISORDERED:
646 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500647 * Was the last operation on this sequence interrupted?
648 * If so, retry after bumping the sequence number.
649 */
650 if (interrupted) {
651 ++slot->seq_nr;
652 goto retry_nowait;
653 }
654 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500655 * Could this slot have been previously retired?
656 * If so, then the server may be expecting seq_nr = 1!
657 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500658 if (slot->seq_nr != 1) {
659 slot->seq_nr = 1;
660 goto retry_nowait;
661 }
662 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500663 case -NFS4ERR_SEQ_FALSE_RETRY:
664 ++slot->seq_nr;
665 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400666 default:
667 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500668 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400669 }
670out:
671 /* The session may be reset by one of the error handlers. */
672 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400673 nfs41_sequence_free_slot(res);
Trond Myklebust85563072012-12-11 10:31:12 -0500674 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500675retry_nowait:
676 if (rpc_restart_call_prepare(task)) {
677 task->tk_status = 0;
678 ret = 0;
679 }
680 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400681out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400682 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400683 goto out;
684 rpc_delay(task, NFS4_POLL_RETRY_MAX);
685 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400686}
687
Trond Myklebust14516c32010-07-31 14:29:06 -0400688static int nfs4_sequence_done(struct rpc_task *task,
689 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400690{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500691 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400692 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400693 if (!res->sr_slot->table->session)
694 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400695 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400696}
697
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000698int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400699 struct nfs4_sequence_args *args,
700 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400701 struct rpc_task *task)
702{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400703 struct nfs4_slot *slot;
704 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400705
706 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400707 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400708 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400709 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400710
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400711 tbl = &session->fc_slot_table;
712
Trond Myklebust69d206b2012-11-22 13:21:02 -0500713 task->tk_timeout = 0;
714
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400715 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400716 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400717 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500718 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500719 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400720 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400721 }
722
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500723 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500724 if (IS_ERR(slot)) {
725 /* If out of memory, try again in 1/4 second */
726 if (slot == ERR_PTR(-ENOMEM))
727 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400728 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400729 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400730 }
731 spin_unlock(&tbl->slot_tbl_lock);
732
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500733 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400734
Chuck Levere8d92382013-08-09 12:47:51 -0400735 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500736 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400737
Benny Halevydfb4f3092010-09-24 09:17:01 -0400738 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500739 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400740 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400741 /*
742 * sr_status is only set in decode_sequence, and so will remain
743 * set to 1 if an rpc level failure occurs.
744 */
745 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400746 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400747out_success:
748 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400749 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400750out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400751 /* Privileged tasks are queued with top priority */
752 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400753 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
754 NULL, RPC_PRIORITY_PRIVILEGED);
755 else
756 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400757 spin_unlock(&tbl->slot_tbl_lock);
758 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400759}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000760EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400761
Chuck Lever5a580e02013-08-09 12:48:09 -0400762static int nfs4_setup_sequence(const struct nfs_server *server,
763 struct nfs4_sequence_args *args,
764 struct nfs4_sequence_res *res,
765 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400766{
Trond Myklebust035168a2010-06-16 09:52:26 -0400767 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400768 int ret = 0;
769
Chuck Lever3bd23842013-08-09 12:49:19 -0400770 if (!session)
771 return nfs40_setup_sequence(server, args, res, task);
Trond Myklebust035168a2010-06-16 09:52:26 -0400772
Chuck Levere8d92382013-08-09 12:47:51 -0400773 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400774 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400775 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168a2010-06-16 09:52:26 -0400776
Trond Myklebust9d12b212012-01-17 22:04:25 -0500777 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400778
Andy Adamsonce5039c2009-04-01 09:22:13 -0400779 dprintk("<-- %s status=%d\n", __func__, ret);
780 return ret;
781}
782
Andy Adamsonce5039c2009-04-01 09:22:13 -0400783static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
784{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400785 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400786 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400787
Trond Myklebust035168a2010-06-16 09:52:26 -0400788 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
789
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400790 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400791}
792
Andy Adamson69ab40c2009-04-01 09:22:19 -0400793static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
794{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400795 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400796
Trond Myklebust14516c32010-07-31 14:29:06 -0400797 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400798}
799
Trond Myklebust17280172012-03-11 13:11:00 -0400800static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400801 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400802 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400803};
804
Chuck Lever3bd23842013-08-09 12:49:19 -0400805#else /* !CONFIG_NFS_V4_1 */
806
Chuck Lever5a580e02013-08-09 12:48:09 -0400807static int nfs4_setup_sequence(const struct nfs_server *server,
808 struct nfs4_sequence_args *args,
809 struct nfs4_sequence_res *res,
810 struct rpc_task *task)
811{
Chuck Lever3bd23842013-08-09 12:49:19 -0400812 return nfs40_setup_sequence(server, args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -0400813}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400814
Trond Myklebust14516c32010-07-31 14:29:06 -0400815static int nfs4_sequence_done(struct rpc_task *task,
816 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400817{
Chuck Lever3bd23842013-08-09 12:49:19 -0400818 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400819}
Chuck Lever3bd23842013-08-09 12:49:19 -0400820
821#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400822
Chuck Lever9915ea72013-08-09 12:48:27 -0400823static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
824{
825 struct nfs4_call_sync_data *data = calldata;
826 nfs4_setup_sequence(data->seq_server,
827 data->seq_args, data->seq_res, task);
828}
829
830static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
831{
832 struct nfs4_call_sync_data *data = calldata;
833 nfs4_sequence_done(task, data->seq_res);
834}
835
836static const struct rpc_call_ops nfs40_call_sync_ops = {
837 .rpc_call_prepare = nfs40_call_sync_prepare,
838 .rpc_call_done = nfs40_call_sync_done,
839};
840
841static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
842 struct nfs_server *server,
843 struct rpc_message *msg,
844 struct nfs4_sequence_args *args,
845 struct nfs4_sequence_res *res)
846{
847 int ret;
848 struct rpc_task *task;
849 struct nfs_client *clp = server->nfs_client;
850 struct nfs4_call_sync_data data = {
851 .seq_server = server,
852 .seq_args = args,
853 .seq_res = res,
854 };
855 struct rpc_task_setup task_setup = {
856 .rpc_client = clnt,
857 .rpc_message = msg,
858 .callback_ops = clp->cl_mvops->call_sync_ops,
859 .callback_data = &data
860 };
861
862 task = rpc_run_task(&task_setup);
863 if (IS_ERR(task))
864 ret = PTR_ERR(task);
865 else {
866 ret = task->tk_status;
867 rpc_put_task(task);
868 }
869 return ret;
870}
871
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400872static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000873int nfs4_call_sync(struct rpc_clnt *clnt,
874 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000875 struct rpc_message *msg,
876 struct nfs4_sequence_args *args,
877 struct nfs4_sequence_res *res,
878 int cache_reply)
879{
Chuck Levera9c92d62013-08-09 12:48:18 -0400880 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -0400881 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000882}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400883
Trond Myklebust38478b22006-05-25 01:40:57 -0400884static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
Trond Myklebust38478b22006-05-25 01:40:57 -0400886 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Trond Myklebust38478b22006-05-25 01:40:57 -0400888 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400889 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700890 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400891 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700892 dir->i_version = cinfo->after;
David Howellsde242c02012-12-20 21:52:38 +0000893 nfs_fscache_invalidate(dir);
Trond Myklebust38478b22006-05-25 01:40:57 -0400894 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895}
896
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100897struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400898 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100899 struct nfs_openargs o_arg;
900 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100901 struct nfs_open_confirmargs c_arg;
902 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500903 struct nfs4_string owner_name;
904 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100905 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400906 struct nfs4_label *f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100907 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400908 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100909 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400910 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100911 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100912 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400913 unsigned int rpc_done : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -0400914 unsigned int is_recover : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100915 int rpc_status;
916 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100917};
918
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400919static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
920 int err, struct nfs4_exception *exception)
921{
922 if (err != -EINVAL)
923 return false;
924 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
925 return false;
926 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
927 exception->retry = 1;
928 return true;
929}
930
931static enum open_claim_type4
932nfs4_map_atomic_open_claim(struct nfs_server *server,
933 enum open_claim_type4 claim)
934{
935 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
936 return claim;
937 switch (claim) {
938 default:
939 return claim;
940 case NFS4_OPEN_CLAIM_FH:
941 return NFS4_OPEN_CLAIM_NULL;
942 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
943 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
944 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
945 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
946 }
947}
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400948
949static void nfs4_init_opendata_res(struct nfs4_opendata *p)
950{
951 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400952 p->o_res.f_label = p->f_label;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400953 p->o_res.seqid = p->o_arg.seqid;
954 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400955 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400956 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400957 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500958 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400959}
960
Al Viro82a2c1b2011-06-22 18:30:55 -0400961static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500962 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400963 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -0400964 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400965 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400966 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100967{
Al Viro82a2c1b2011-06-22 18:30:55 -0400968 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100969 struct inode *dir = parent->d_inode;
970 struct nfs_server *server = NFS_SERVER(dir);
971 struct nfs4_opendata *p;
972
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400973 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100974 if (p == NULL)
975 goto err;
David Quigley14c43f72013-05-22 12:50:43 -0400976
977 p->f_label = nfs4_label_alloc(server, gfp_mask);
978 if (IS_ERR(p->f_label))
979 goto err_free_p;
980
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400981 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100982 if (p->o_arg.seqid == NULL)
David Quigley14c43f72013-05-22 12:50:43 -0400983 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -0400984 nfs_sb_active(dentry->d_sb);
985 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100986 p->dir = parent;
987 p->owner = sp;
988 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500989 p->o_arg.open_flags = flags;
990 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700991 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
992 * will return permission denied for all bits until close */
993 if (!(flags & O_EXCL)) {
994 /* ask server to check for all possible rights as results
995 * are cached */
996 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
997 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700998 }
David Howells7539bba2006-08-22 20:06:09 -0400999 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001000 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1001 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001002 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001003 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001004 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001005 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
David Quigley1775fd32013-05-22 12:50:42 -04001006 p->o_arg.label = label;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001007 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1008 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001009 case NFS4_OPEN_CLAIM_NULL:
1010 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1011 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1012 p->o_arg.fh = NFS_FH(dir);
1013 break;
1014 case NFS4_OPEN_CLAIM_PREVIOUS:
1015 case NFS4_OPEN_CLAIM_FH:
1016 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1017 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1018 p->o_arg.fh = NFS_FH(dentry->d_inode);
1019 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001020 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001021 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001022
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001023 p->o_arg.u.attrs = &p->attrs;
1024 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001025
1026 verf[0] = jiffies;
1027 verf[1] = current->pid;
1028 memcpy(p->o_arg.u.verifier.data, verf,
1029 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001030 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001031 p->c_arg.fh = &p->o_res.fh;
1032 p->c_arg.stateid = &p->o_res.stateid;
1033 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001034 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001035 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001036 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001037
1038err_free_label:
1039 nfs4_label_free(p->f_label);
1040err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001041 kfree(p);
1042err:
1043 dput(parent);
1044 return NULL;
1045}
1046
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001047static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001048{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001049 struct nfs4_opendata *p = container_of(kref,
1050 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001051 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001052
1053 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001054 if (p->state != NULL)
1055 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001056 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001057
1058 nfs4_label_free(p->f_label);
1059
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001060 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001061 dput(p->dentry);
1062 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001063 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001064 kfree(p);
1065}
1066
1067static void nfs4_opendata_put(struct nfs4_opendata *p)
1068{
1069 if (p != NULL)
1070 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001071}
1072
Trond Myklebust06f814a2006-01-03 09:55:07 +01001073static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1074{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001075 int ret;
1076
Trond Myklebust06f814a2006-01-03 09:55:07 +01001077 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001078 return ret;
1079}
1080
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001081static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001082{
1083 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001084
Trond Myklebust536e43d2012-01-17 22:04:26 -05001085 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001086 goto out;
1087 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001088 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001089 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1090 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001091 break;
1092 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001093 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1094 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001095 break;
1096 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001097 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1098 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001099 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001100out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001101 return ret;
1102}
1103
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001104static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001105{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001106 if (delegation == NULL)
1107 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001108 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001109 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -05001110 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -04001111 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001112 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1113 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -05001114 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001115 return 1;
1116}
1117
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001118static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001119{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001120 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001121 case FMODE_WRITE:
1122 state->n_wronly++;
1123 break;
1124 case FMODE_READ:
1125 state->n_rdonly++;
1126 break;
1127 case FMODE_READ|FMODE_WRITE:
1128 state->n_rdwr++;
1129 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001130 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001131}
1132
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001133static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001134{
1135 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001136 nfs4_stateid_copy(&state->stateid, stateid);
1137 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust92b40e92013-04-20 01:25:45 -04001138 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001139 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001140 case FMODE_READ:
1141 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1142 break;
1143 case FMODE_WRITE:
1144 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1145 break;
1146 case FMODE_READ|FMODE_WRITE:
1147 set_bit(NFS_O_RDWR_STATE, &state->flags);
1148 }
1149}
1150
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001151static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001152{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001153 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001154 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001155 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001156}
1157
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001158static 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 -07001159{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001160 /*
1161 * Protect the call to nfs4_state_set_mode_locked and
1162 * serialise the stateid update
1163 */
1164 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001165 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001166 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001167 set_bit(NFS_DELEGATED_STATE, &state->flags);
1168 }
1169 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001170 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001171 write_sequnlock(&state->seqlock);
1172 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001173 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001174 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175}
1176
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001177static 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 -05001178{
1179 struct nfs_inode *nfsi = NFS_I(state->inode);
1180 struct nfs_delegation *deleg_cur;
1181 int ret = 0;
1182
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001183 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001184
1185 rcu_read_lock();
1186 deleg_cur = rcu_dereference(nfsi->delegation);
1187 if (deleg_cur == NULL)
1188 goto no_delegation;
1189
1190 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001191 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001192 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001193 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001194 goto no_delegation_unlock;
1195
1196 if (delegation == NULL)
1197 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001198 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001199 goto no_delegation_unlock;
1200
Trond Myklebustb7391f42008-12-23 15:21:52 -05001201 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001202 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001203 ret = 1;
1204no_delegation_unlock:
1205 spin_unlock(&deleg_cur->lock);
1206no_delegation:
1207 rcu_read_unlock();
1208
1209 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001210 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001211 ret = 1;
1212 }
1213
1214 return ret;
1215}
1216
1217
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001218static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001219{
1220 struct nfs_delegation *delegation;
1221
1222 rcu_read_lock();
1223 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001224 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001225 rcu_read_unlock();
1226 return;
1227 }
1228 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001229 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001230}
1231
Trond Myklebust6ee41262007-07-08 14:11:36 -04001232static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001233{
1234 struct nfs4_state *state = opendata->state;
1235 struct nfs_inode *nfsi = NFS_I(state->inode);
1236 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001237 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001238 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001239 nfs4_stateid stateid;
1240 int ret = -EAGAIN;
1241
Trond Myklebustaac00a82007-07-05 19:02:21 -04001242 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001243 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001244 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001245 if (can_open_cached(state, fmode, open_mode)) {
1246 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001247 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001248 goto out_return_state;
1249 }
1250 spin_unlock(&state->owner->so_lock);
1251 }
Trond Myklebust34310432008-12-23 15:21:38 -05001252 rcu_read_lock();
1253 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001254 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001255 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001256 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001257 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001258 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001259 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001260 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001261 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001262 if (!opendata->is_recover) {
1263 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1264 if (ret != 0)
1265 goto out;
1266 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001267 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001268
1269 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001270 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001271 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001272 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001273out:
1274 return ERR_PTR(ret);
1275out_return_state:
1276 atomic_inc(&state->count);
1277 return state;
1278}
1279
Andy Adamsone23008e2012-10-02 21:07:32 -04001280static void
1281nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1282{
1283 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1284 struct nfs_delegation *delegation;
1285 int delegation_flags = 0;
1286
1287 rcu_read_lock();
1288 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1289 if (delegation)
1290 delegation_flags = delegation->flags;
1291 rcu_read_unlock();
1292 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1293 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1294 "returning a delegation for "
1295 "OPEN(CLAIM_DELEGATE_CUR)\n",
1296 clp->cl_hostname);
1297 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1298 nfs_inode_set_delegation(state->inode,
1299 data->owner->so_cred,
1300 &data->o_res);
1301 else
1302 nfs_inode_reclaim_delegation(state->inode,
1303 data->owner->so_cred,
1304 &data->o_res);
1305}
1306
1307/*
1308 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1309 * and update the nfs4_state.
1310 */
1311static struct nfs4_state *
1312_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1313{
1314 struct inode *inode = data->state->inode;
1315 struct nfs4_state *state = data->state;
1316 int ret;
1317
1318 if (!data->rpc_done) {
1319 ret = data->rpc_status;
1320 goto err;
1321 }
1322
1323 ret = -ESTALE;
1324 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1325 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1326 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1327 goto err;
1328
1329 ret = -ENOMEM;
1330 state = nfs4_get_open_state(inode, data->owner);
1331 if (state == NULL)
1332 goto err;
1333
1334 ret = nfs_refresh_inode(inode, &data->f_attr);
1335 if (ret)
1336 goto err;
1337
David Quigleyaa9c2662013-05-22 12:50:44 -04001338 nfs_setsecurity(inode, &data->f_attr, data->f_label);
1339
Andy Adamsone23008e2012-10-02 21:07:32 -04001340 if (data->o_res.delegation_type != 0)
1341 nfs4_opendata_check_deleg(data, state);
1342 update_open_stateid(state, &data->o_res.stateid, NULL,
1343 data->o_arg.fmode);
1344
1345 return state;
1346err:
1347 return ERR_PTR(ret);
1348
1349}
1350
1351static struct nfs4_state *
1352_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001353{
1354 struct inode *inode;
1355 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001356 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001357
Trond Myklebustaac00a82007-07-05 19:02:21 -04001358 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001359 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001360 goto out;
1361 }
1362
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001363 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001364 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001365 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001366 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001367 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001368 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001369 goto err;
1370 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001371 state = nfs4_get_open_state(inode, data->owner);
1372 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001373 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001374 if (data->o_res.delegation_type != 0)
1375 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001376 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001377 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001378 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001379out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001380 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001381 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001382err_put_inode:
1383 iput(inode);
1384err:
1385 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001386}
1387
Andy Adamsone23008e2012-10-02 21:07:32 -04001388static struct nfs4_state *
1389nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1390{
1391 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1392 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1393 return _nfs4_opendata_to_nfs4_state(data);
1394}
1395
Trond Myklebust864472e2006-01-03 09:55:15 +01001396static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1397{
1398 struct nfs_inode *nfsi = NFS_I(state->inode);
1399 struct nfs_open_context *ctx;
1400
1401 spin_lock(&state->inode->i_lock);
1402 list_for_each_entry(ctx, &nfsi->open_files, list) {
1403 if (ctx->state != state)
1404 continue;
1405 get_nfs_open_context(ctx);
1406 spin_unlock(&state->inode->i_lock);
1407 return ctx;
1408 }
1409 spin_unlock(&state->inode->i_lock);
1410 return ERR_PTR(-ENOENT);
1411}
1412
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001413static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1414 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001415{
1416 struct nfs4_opendata *opendata;
1417
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001418 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001419 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001420 if (opendata == NULL)
1421 return ERR_PTR(-ENOMEM);
1422 opendata->state = state;
1423 atomic_inc(&state->count);
1424 return opendata;
1425}
1426
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001427static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001428{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001429 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001430 int ret;
1431
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001432 opendata->o_arg.open_flags = 0;
1433 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001434 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1435 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1436 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001437 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001438 if (ret != 0)
1439 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001440 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001441 if (IS_ERR(newstate))
1442 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001443 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001444 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001445 return 0;
1446}
1447
1448static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1449{
Trond Myklebust864472e2006-01-03 09:55:15 +01001450 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001451 int ret;
1452
1453 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001454 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001455 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001456 smp_rmb();
1457 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001458 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001459 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001460 if (ret != 0)
1461 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001462 if (newstate != state)
1463 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001464 }
1465 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001466 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001467 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001468 if (ret != 0)
1469 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001470 if (newstate != state)
1471 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001472 }
1473 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001474 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001475 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001476 if (ret != 0)
1477 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001478 if (newstate != state)
1479 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001480 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001481 /*
1482 * We may have performed cached opens for all three recoveries.
1483 * Check if we need to update the current stateid.
1484 */
1485 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001486 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001487 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001488 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001489 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001490 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001491 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001492 return 0;
1493}
1494
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495/*
1496 * OPEN_RECLAIM:
1497 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001499static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001501 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001502 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001503 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 int status;
1505
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001506 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1507 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001508 if (IS_ERR(opendata))
1509 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001510 rcu_read_lock();
1511 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001512 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001513 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001514 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001515 opendata->o_arg.u.delegation_type = delegation_type;
1516 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001517 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 return status;
1519}
1520
Trond Myklebust539cd032007-06-05 11:46:42 -04001521static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522{
1523 struct nfs_server *server = NFS_SERVER(state->inode);
1524 struct nfs4_exception exception = { };
1525 int err;
1526 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001527 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001528 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001529 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1530 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001531 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001532 break;
1533 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 } while (exception.retry);
1535 return err;
1536}
1537
Trond Myklebust864472e2006-01-03 09:55:15 +01001538static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1539{
1540 struct nfs_open_context *ctx;
1541 int ret;
1542
1543 ctx = nfs4_state_find_open_context(state);
1544 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001545 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001546 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001547 put_nfs_open_context(ctx);
1548 return ret;
1549}
1550
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001551static 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 -07001552{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001553 switch (err) {
1554 default:
1555 printk(KERN_ERR "NFS: %s: unhandled error "
1556 "%d.\n", __func__, err);
1557 case 0:
1558 case -ENOENT:
1559 case -ESTALE:
1560 break;
1561 case -NFS4ERR_BADSESSION:
1562 case -NFS4ERR_BADSLOT:
1563 case -NFS4ERR_BAD_HIGH_SLOT:
1564 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1565 case -NFS4ERR_DEADSESSION:
1566 set_bit(NFS_DELEGATED_STATE, &state->flags);
1567 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1568 return -EAGAIN;
1569 case -NFS4ERR_STALE_CLIENTID:
1570 case -NFS4ERR_STALE_STATEID:
1571 set_bit(NFS_DELEGATED_STATE, &state->flags);
1572 case -NFS4ERR_EXPIRED:
1573 /* Don't recall a delegation if it was lost */
1574 nfs4_schedule_lease_recovery(server->nfs_client);
1575 return -EAGAIN;
1576 case -NFS4ERR_DELEG_REVOKED:
1577 case -NFS4ERR_ADMIN_REVOKED:
1578 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001579 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001580 nfs_inode_find_state_and_recover(state->inode,
1581 stateid);
1582 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001583 return 0;
1584 case -NFS4ERR_DELAY:
1585 case -NFS4ERR_GRACE:
1586 set_bit(NFS_DELEGATED_STATE, &state->flags);
1587 ssleep(1);
1588 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001589 case -ENOMEM:
1590 case -NFS4ERR_DENIED:
1591 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1592 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 return err;
1595}
1596
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001597int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1598{
1599 struct nfs_server *server = NFS_SERVER(state->inode);
1600 struct nfs4_opendata *opendata;
1601 int err;
1602
1603 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1604 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1605 if (IS_ERR(opendata))
1606 return PTR_ERR(opendata);
1607 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1608 err = nfs4_open_recover(opendata, state);
1609 nfs4_opendata_put(opendata);
1610 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1611}
1612
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001613static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1614{
1615 struct nfs4_opendata *data = calldata;
1616
1617 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001618 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001619 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001620 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001621 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001622 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001623 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001624}
1625
1626static void nfs4_open_confirm_release(void *calldata)
1627{
1628 struct nfs4_opendata *data = calldata;
1629 struct nfs4_state *state = NULL;
1630
1631 /* If this request hasn't been cancelled, do nothing */
1632 if (data->cancelled == 0)
1633 goto out_free;
1634 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001635 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001636 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001637 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001638 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001639 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001640out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001641 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001642}
1643
1644static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001645 .rpc_call_done = nfs4_open_confirm_done,
1646 .rpc_release = nfs4_open_confirm_release,
1647};
1648
1649/*
1650 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1651 */
1652static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1653{
1654 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1655 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001656 struct rpc_message msg = {
1657 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1658 .rpc_argp = &data->c_arg,
1659 .rpc_resp = &data->c_res,
1660 .rpc_cred = data->owner->so_cred,
1661 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001662 struct rpc_task_setup task_setup_data = {
1663 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001664 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001665 .callback_ops = &nfs4_open_confirm_ops,
1666 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001667 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001668 .flags = RPC_TASK_ASYNC,
1669 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 int status;
1671
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001672 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001673 data->rpc_done = 0;
1674 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001675 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001676 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001677 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001678 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001679 status = nfs4_wait_for_completion_rpc_task(task);
1680 if (status != 0) {
1681 data->cancelled = 1;
1682 smp_wmb();
1683 } else
1684 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001685 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 return status;
1687}
1688
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001689static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001691 struct nfs4_opendata *data = calldata;
1692 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001693 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001694
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001695 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001696 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001697 /*
1698 * Check if we still need to send an OPEN call, or if we can use
1699 * a delegation instead.
1700 */
1701 if (data->state != NULL) {
1702 struct nfs_delegation *delegation;
1703
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001704 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001705 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001706 rcu_read_lock();
1707 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001708 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
Trond Myklebustcd4c9be2013-04-23 14:46:25 -04001709 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
Trond Myklebust652f89f2011-12-09 19:05:58 -05001710 can_open_delegated(delegation, data->o_arg.fmode))
1711 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001712 rcu_read_unlock();
1713 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001714 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04001715 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust8188df12013-04-23 14:31:19 -04001716 switch (data->o_arg.claim) {
1717 case NFS4_OPEN_CLAIM_PREVIOUS:
1718 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1719 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001720 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04001721 case NFS4_OPEN_CLAIM_FH:
1722 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001723 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1724 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001725 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001726 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001727 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001728 &data->o_res.seq_res,
1729 task) != 0)
1730 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04001731
1732 /* Set the create mode (note dependency on the session type) */
1733 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1734 if (data->o_arg.open_flags & O_EXCL) {
1735 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1736 if (nfs4_has_persistent_session(clp))
1737 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1738 else if (clp->cl_mvops->minor_version > 0)
1739 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1740 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04001741 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001742unlock_no_action:
1743 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001744out_no_action:
1745 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001746out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001747 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001748}
1749
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001750static void nfs4_open_done(struct rpc_task *task, void *calldata)
1751{
1752 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001754 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001755
Trond Myklebust14516c32010-07-31 14:29:06 -04001756 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1757 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001758
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001759 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001760 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1761 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001762 case S_IFREG:
1763 break;
1764 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001765 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001766 break;
1767 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001768 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001769 break;
1770 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001771 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001772 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001773 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001774 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001775 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1776 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001777 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001778 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001779}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001780
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001781static void nfs4_open_release(void *calldata)
1782{
1783 struct nfs4_opendata *data = calldata;
1784 struct nfs4_state *state = NULL;
1785
1786 /* If this request hasn't been cancelled, do nothing */
1787 if (data->cancelled == 0)
1788 goto out_free;
1789 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001790 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001791 goto out_free;
1792 /* In case we need an open_confirm, no cleanup! */
1793 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1794 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001795 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001796 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001797 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001798out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001799 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001800}
1801
1802static const struct rpc_call_ops nfs4_open_ops = {
1803 .rpc_call_prepare = nfs4_open_prepare,
1804 .rpc_call_done = nfs4_open_done,
1805 .rpc_release = nfs4_open_release,
1806};
1807
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001808static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001809{
1810 struct inode *dir = data->dir->d_inode;
1811 struct nfs_server *server = NFS_SERVER(dir);
1812 struct nfs_openargs *o_arg = &data->o_arg;
1813 struct nfs_openres *o_res = &data->o_res;
1814 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001815 struct rpc_message msg = {
1816 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1817 .rpc_argp = o_arg,
1818 .rpc_resp = o_res,
1819 .rpc_cred = data->owner->so_cred,
1820 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001821 struct rpc_task_setup task_setup_data = {
1822 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001823 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001824 .callback_ops = &nfs4_open_ops,
1825 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001826 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001827 .flags = RPC_TASK_ASYNC,
1828 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001829 int status;
1830
Chuck Levera9c92d62013-08-09 12:48:18 -04001831 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001832 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001833 data->rpc_done = 0;
1834 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001835 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001836 data->is_recover = 0;
1837 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001838 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001839 data->is_recover = 1;
1840 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04001841 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001842 if (IS_ERR(task))
1843 return PTR_ERR(task);
1844 status = nfs4_wait_for_completion_rpc_task(task);
1845 if (status != 0) {
1846 data->cancelled = 1;
1847 smp_wmb();
1848 } else
1849 status = data->rpc_status;
1850 rpc_put_task(task);
1851
1852 return status;
1853}
1854
1855static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1856{
1857 struct inode *dir = data->dir->d_inode;
1858 struct nfs_openres *o_res = &data->o_res;
1859 int status;
1860
1861 status = nfs4_run_open_task(data, 1);
1862 if (status != 0 || !data->rpc_done)
1863 return status;
1864
Trond Myklebust6926afd2012-01-07 13:22:46 -05001865 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1866
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001867 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1868 status = _nfs4_proc_open_confirm(data);
1869 if (status != 0)
1870 return status;
1871 }
1872
1873 return status;
1874}
1875
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001876static int nfs4_opendata_access(struct rpc_cred *cred,
1877 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001878 struct nfs4_state *state, fmode_t fmode,
1879 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001880{
1881 struct nfs_access_entry cache;
1882 u32 mask;
1883
1884 /* access call failed or for some reason the server doesn't
1885 * support any access modes -- defer access call until later */
1886 if (opendata->o_res.access_supported == 0)
1887 return 0;
1888
1889 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001890 /* don't check MAY_WRITE - a newly created file may not have
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001891 * write mode bits, but POSIX allows the creating process to write.
1892 * use openflags to check for exec, because fmode won't
1893 * always have FMODE_EXEC set when file open for exec. */
1894 if (openflags & __FMODE_EXEC) {
1895 /* ONLY check for exec rights */
1896 mask = MAY_EXEC;
1897 } else if (fmode & FMODE_READ)
1898 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001899
1900 cache.cred = cred;
1901 cache.jiffies = jiffies;
1902 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1903 nfs_access_add_cache(state->inode, &cache);
1904
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001905 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001906 return 0;
1907
1908 /* even though OPEN succeeded, access is denied. Close the file */
1909 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001910 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001911}
1912
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001913/*
1914 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1915 */
1916static int _nfs4_proc_open(struct nfs4_opendata *data)
1917{
1918 struct inode *dir = data->dir->d_inode;
1919 struct nfs_server *server = NFS_SERVER(dir);
1920 struct nfs_openargs *o_arg = &data->o_arg;
1921 struct nfs_openres *o_res = &data->o_res;
1922 int status;
1923
1924 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001925 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001926 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001927 if (status != 0) {
1928 if (status == -NFS4ERR_BADNAME &&
1929 !(o_arg->open_flags & O_CREAT))
1930 return -ENOENT;
1931 return status;
1932 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001933
Trond Myklebust6926afd2012-01-07 13:22:46 -05001934 nfs_fattr_map_and_free_names(server, &data->f_attr);
1935
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001936 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001937 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001938 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1939 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001941 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001943 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 }
1945 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
David Quigley1775fd32013-05-22 12:50:42 -04001946 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001947 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948}
1949
Andy Adamsond83217c2011-03-01 01:34:17 +00001950static int nfs4_recover_expired_lease(struct nfs_server *server)
1951{
1952 return nfs4_client_recover_expired_lease(server->nfs_client);
1953}
1954
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955/*
1956 * OPEN_EXPIRED:
1957 * reclaim state on the server after a network partition.
1958 * Assumes caller holds the appropriate lock
1959 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001960static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001962 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001963 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001965 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001966 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001967 if (IS_ERR(opendata))
1968 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001969 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001970 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001971 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001972 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001973 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974}
1975
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001976static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001977{
Trond Myklebust539cd032007-06-05 11:46:42 -04001978 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001979 struct nfs4_exception exception = { };
1980 int err;
1981
1982 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001983 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001984 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001985 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1986 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001987 switch (err) {
1988 default:
1989 goto out;
1990 case -NFS4ERR_GRACE:
1991 case -NFS4ERR_DELAY:
1992 nfs4_handle_exception(server, err, &exception);
1993 err = 0;
1994 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001995 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001996out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001997 return err;
1998}
1999
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2001{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002003 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
Trond Myklebust864472e2006-01-03 09:55:15 +01002005 ctx = nfs4_state_find_open_context(state);
2006 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002007 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002008 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002009 put_nfs_open_context(ctx);
2010 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011}
2012
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002013#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002014static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002015{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002016 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002017 nfs4_stateid *stateid = &state->stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002018 struct nfs_delegation *delegation;
2019 struct rpc_cred *cred = NULL;
2020 int status = -NFS4ERR_BAD_STATEID;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002021
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002022 /* If a state reset has been done, test_stateid is unneeded */
2023 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2024 return;
2025
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002026 /* Get the delegation credential for use by test/free_stateid */
2027 rcu_read_lock();
2028 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2029 if (delegation != NULL &&
2030 nfs4_stateid_match(&delegation->stateid, stateid)) {
2031 cred = get_rpccred(delegation->cred);
2032 rcu_read_unlock();
2033 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002034 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002035 } else
2036 rcu_read_unlock();
2037
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002038 if (status != NFS_OK) {
2039 /* Free the stateid unless the server explicitly
2040 * informs us the stateid is unrecognized. */
2041 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002042 nfs41_free_stateid(server, stateid, cred);
Bryan Schumaker69388672012-09-26 15:25:52 -04002043 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002044
Bryan Schumaker69388672012-09-26 15:25:52 -04002045 write_seqlock(&state->seqlock);
2046 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2047 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002048 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2049 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002050
2051 if (cred != NULL)
2052 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002053}
2054
2055/**
2056 * nfs41_check_open_stateid - possibly free an open stateid
2057 *
2058 * @state: NFSv4 state for an inode
2059 *
2060 * Returns NFS_OK if recovery for this stateid is now finished.
2061 * Otherwise a negative NFS4ERR value is returned.
2062 */
2063static int nfs41_check_open_stateid(struct nfs4_state *state)
2064{
2065 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002066 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002067 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002068 int status;
2069
2070 /* If a state reset has been done, test_stateid is unneeded */
2071 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2072 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2073 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2074 return -NFS4ERR_BAD_STATEID;
2075
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002076 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002077 trace_nfs4_test_open_stateid(state, NULL, status);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002078 if (status != NFS_OK) {
2079 /* Free the stateid unless the server explicitly
2080 * informs us the stateid is unrecognized. */
2081 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002082 nfs41_free_stateid(server, stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002083
2084 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2085 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2086 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002087 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002088 }
2089 return status;
2090}
2091
2092static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2093{
Chuck Levereb64cf92012-07-11 16:30:05 -04002094 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002095
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002096 nfs41_clear_delegation_stateid(state);
2097 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002098 if (status != NFS_OK)
2099 status = nfs4_open_expired(sp, state);
2100 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002101}
2102#endif
2103
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002105 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2106 * fields corresponding to attributes that were used to store the verifier.
2107 * Make sure we clobber those fields in the later setattr call
2108 */
2109static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2110{
2111 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2112 !(sattr->ia_valid & ATTR_ATIME_SET))
2113 sattr->ia_valid |= ATTR_ATIME;
2114
2115 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2116 !(sattr->ia_valid & ATTR_MTIME_SET))
2117 sattr->ia_valid |= ATTR_MTIME;
2118}
2119
Trond Myklebustc21443c2013-02-07 14:26:21 -05002120static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2121 fmode_t fmode,
2122 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002123 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002124{
2125 struct nfs4_state_owner *sp = opendata->owner;
2126 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002127 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002128 struct nfs4_state *state;
2129 unsigned int seq;
2130 int ret;
2131
2132 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2133
2134 ret = _nfs4_proc_open(opendata);
2135 if (ret != 0)
2136 goto out;
2137
2138 state = nfs4_opendata_to_nfs4_state(opendata);
2139 ret = PTR_ERR(state);
2140 if (IS_ERR(state))
2141 goto out;
2142 if (server->caps & NFS_CAP_POSIX_LOCK)
2143 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2144
Trond Myklebust275bb302013-05-29 13:11:28 -04002145 dentry = opendata->dentry;
2146 if (dentry->d_inode == NULL) {
2147 /* FIXME: Is this d_drop() ever needed? */
2148 d_drop(dentry);
2149 dentry = d_add_unique(dentry, igrab(state->inode));
2150 if (dentry == NULL) {
2151 dentry = opendata->dentry;
2152 } else if (dentry != ctx->dentry) {
2153 dput(ctx->dentry);
2154 ctx->dentry = dget(dentry);
2155 }
2156 nfs_set_verifier(dentry,
2157 nfs_save_change_attribute(opendata->dir->d_inode));
2158 }
2159
Trond Myklebustc21443c2013-02-07 14:26:21 -05002160 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2161 if (ret != 0)
2162 goto out;
2163
Trond Myklebust3efb9722013-05-29 13:17:04 -04002164 ctx->state = state;
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002165 if (dentry->d_inode == state->inode) {
2166 nfs_inode_attach_open_context(ctx);
2167 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2168 nfs4_schedule_stateid_recovery(server, state);
2169 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002170out:
2171 return ret;
2172}
2173
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002174/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002175 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 */
Andy Adamson82be4172012-05-23 05:02:35 -04002177static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002178 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002179 int flags,
2180 struct iattr *sattr,
Trond Myklebust959d9212013-06-28 16:29:51 -04002181 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182{
2183 struct nfs4_state_owner *sp;
2184 struct nfs4_state *state = NULL;
2185 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002186 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002187 struct dentry *dentry = ctx->dentry;
2188 struct rpc_cred *cred = ctx->cred;
2189 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2190 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002191 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002192 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002193 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
2195 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002197 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2198 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2200 goto out_err;
2201 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002202 status = nfs4_recover_expired_lease(server);
2203 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002204 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04002205 if (dentry->d_inode != NULL)
2206 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002207 status = -ENOMEM;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002208 if (dentry->d_inode)
2209 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002210 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002211 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002212 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002213 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
David Quigley14c43f72013-05-22 12:50:43 -04002215 if (label) {
2216 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2217 if (IS_ERR(olabel)) {
2218 status = PTR_ERR(olabel);
2219 goto err_opendata_put;
2220 }
2221 }
2222
Andy Adamson82be4172012-05-23 05:02:35 -04002223 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2224 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2225 if (!opendata->f_attr.mdsthreshold)
David Quigley14c43f72013-05-22 12:50:43 -04002226 goto err_free_label;
Trond Myklebust1549210f2012-06-05 09:16:47 -04002227 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002228 }
Al Viro82a2c1b2011-06-22 18:30:55 -04002229 if (dentry->d_inode != NULL)
2230 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002231
Trond Myklebust3efb9722013-05-29 13:17:04 -04002232 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002233 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002234 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002235 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002236
Trond Myklebust549b19c2013-04-16 18:42:34 -04002237 if ((opendata->o_arg.open_flags & O_EXCL) &&
2238 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002239 nfs4_exclusive_attrset(opendata, sattr);
2240
2241 nfs_fattr_init(opendata->o_res.f_attr);
2242 status = nfs4_do_setattr(state->inode, cred,
2243 opendata->o_res.f_attr, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002244 state, label, olabel);
2245 if (status == 0) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002246 nfs_setattr_update_inode(state->inode, sattr);
David Quigley1775fd32013-05-22 12:50:42 -04002247 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
David Quigleyaa9c2662013-05-22 12:50:44 -04002248 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
David Quigley1775fd32013-05-22 12:50:42 -04002249 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002250 }
Andy Adamson82be4172012-05-23 05:02:35 -04002251
2252 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2253 *ctx_th = opendata->f_attr.mdsthreshold;
2254 else
2255 kfree(opendata->f_attr.mdsthreshold);
2256 opendata->f_attr.mdsthreshold = NULL;
2257
David Quigley14c43f72013-05-22 12:50:43 -04002258 nfs4_label_free(olabel);
2259
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002260 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002263err_free_label:
2264 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002265err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002266 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002267 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002268err_put_state_owner:
2269 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 return status;
2272}
2273
2274
Andy Adamson82be4172012-05-23 05:02:35 -04002275static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002276 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002277 int flags,
2278 struct iattr *sattr,
Trond Myklebust959d9212013-06-28 16:29:51 -04002279 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002281 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 struct nfs4_exception exception = { };
2283 struct nfs4_state *res;
2284 int status;
2285
2286 do {
Trond Myklebust959d9212013-06-28 16:29:51 -04002287 status = _nfs4_do_open(dir, ctx, flags, sattr, label);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002288 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002289 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 if (status == 0)
2291 break;
2292 /* NOTE: BAD_SEQID means the server and client disagree about the
2293 * book-keeping w.r.t. state-changing operations
2294 * (OPEN/CLOSE/LOCK/LOCKU...)
2295 * It is actually a sign of a bug on the client or on the server.
2296 *
2297 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002298 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 * have unhashed the old state_owner for us, and that we can
2300 * therefore safely retry using a new one. We should still warn
2301 * the user though...
2302 */
2303 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002304 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002305 " returned a bad sequence-id error!\n",
2306 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 exception.retry = 1;
2308 continue;
2309 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002310 /*
2311 * BAD_STATEID on OPEN means that the server cancelled our
2312 * state before it received the OPEN_CONFIRM.
2313 * Recover by retrying the request as per the discussion
2314 * on Page 181 of RFC3530.
2315 */
2316 if (status == -NFS4ERR_BAD_STATEID) {
2317 exception.retry = 1;
2318 continue;
2319 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002320 if (status == -EAGAIN) {
2321 /* We must have found a delegation */
2322 exception.retry = 1;
2323 continue;
2324 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002325 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2326 continue;
2327 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 status, &exception));
2329 } while (exception.retry);
2330 return res;
2331}
2332
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002333static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2334 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002335 struct nfs4_state *state, struct nfs4_label *ilabel,
2336 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002338 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002340 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 .iap = sattr,
2342 .server = server,
2343 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002344 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 };
2346 struct nfs_setattrres res = {
2347 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002348 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 .server = server,
2350 };
2351 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002352 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2353 .rpc_argp = &arg,
2354 .rpc_resp = &res,
2355 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002357 unsigned long timestamp = jiffies;
Trond Myklebustee3ae842013-04-29 10:35:36 -04002358 fmode_t fmode;
2359 bool truncate;
Trond Myklebust65e43082005-08-16 11:49:44 -04002360 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
David Quigleyaa9c2662013-05-22 12:50:44 -04002362 arg.bitmask = nfs4_bitmask(server, ilabel);
2363 if (ilabel)
2364 arg.bitmask = nfs4_bitmask(server, olabel);
2365
Trond Myklebust0e574af2005-10-27 22:12:38 -04002366 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
Trond Myklebustee3ae842013-04-29 10:35:36 -04002368 /* Servers should only apply open mode checks for file size changes */
2369 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2370 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2371
2372 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2373 /* Use that stateid */
2374 } else if (truncate && state != NULL && nfs4_valid_open_stateid(state)) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002375 struct nfs_lockowner lockowner = {
2376 .l_owner = current->files,
2377 .l_pid = current->tgid,
2378 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002379 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002380 &lockowner);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002381 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002382 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
Bryan Schumaker7c513052011-03-24 17:12:24 +00002384 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002385 if (status == 0 && state != NULL)
2386 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002387 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388}
2389
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002390static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2391 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002392 struct nfs4_state *state, struct nfs4_label *ilabel,
2393 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002395 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002396 struct nfs4_exception exception = {
2397 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002398 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002399 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 int err;
2401 do {
David Quigley1775fd32013-05-22 12:50:42 -04002402 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
Trond Myklebustc1578b72013-08-12 16:58:42 -04002403 trace_nfs4_setattr(inode, err);
Trond Myklebust451146b2012-04-18 16:29:11 -04002404 switch (err) {
2405 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002406 if (!(sattr->ia_valid & ATTR_SIZE)) {
2407 pr_warn_once("NFSv4: server %s is incorrectly "
2408 "applying open mode checks to "
2409 "a SETATTR that is not "
2410 "changing file size.\n",
2411 server->nfs_client->cl_hostname);
2412 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002413 if (state && !(state->state & FMODE_WRITE)) {
2414 err = -EBADF;
2415 if (sattr->ia_valid & ATTR_OPEN)
2416 err = -EACCES;
2417 goto out;
2418 }
2419 }
2420 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002422out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 return err;
2424}
2425
2426struct nfs4_closedata {
2427 struct inode *inode;
2428 struct nfs4_state *state;
2429 struct nfs_closeargs arg;
2430 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002431 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002432 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002433 bool roc;
2434 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435};
2436
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002437static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002438{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002439 struct nfs4_closedata *calldata = data;
2440 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002441 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002442
Fred Isamanf7e89172011-01-06 11:36:32 +00002443 if (calldata->roc)
2444 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002445 nfs4_put_open_state(calldata->state);
2446 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002447 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002448 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002449 kfree(calldata);
2450}
2451
Trond Myklebust88069f72009-12-08 08:33:16 -05002452static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2453 fmode_t fmode)
2454{
2455 spin_lock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002456 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002457 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
2458 case FMODE_WRITE:
2459 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2460 break;
2461 case FMODE_READ:
2462 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2463 break;
2464 case 0:
2465 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2466 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2467 clear_bit(NFS_OPEN_STATE, &state->flags);
2468 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002469 spin_unlock(&state->owner->so_lock);
2470}
2471
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002472static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002474 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 struct nfs_server *server = NFS_SERVER(calldata->inode);
2477
Chuck Levera3ca5652012-03-01 17:00:40 -05002478 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002479 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2480 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002481 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 /* hmm. we are done with the inode, and in the process of freeing
2483 * the state_owner. we keep this around to process errors
2484 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 switch (task->tk_status) {
2486 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002487 if (calldata->roc)
2488 pnfs_roc_set_barrier(state->inode,
2489 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002490 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002491 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002492 nfs4_close_clear_stateid_flags(state,
2493 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 break;
2495 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002496 case -NFS4ERR_OLD_STATEID:
2497 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002499 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002500 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002502 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2503 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002505 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002506 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002507 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508}
2509
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002510static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002512 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002513 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002514 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002515 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002516
Chuck Levera3ca5652012-03-01 17:00:40 -05002517 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002518 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002519 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002520
Trond Myklebust88069f72009-12-08 08:33:16 -05002521 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2522 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002523 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002524 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002525 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002526 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002527 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2528 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2529 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002530 }
2531 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002532 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2533 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2534 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002535 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002536 }
Trond Myklebust5d422302013-03-14 16:57:48 -04002537 if (!nfs4_valid_open_stateid(state))
2538 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002539 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002540
2541 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002542 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002543 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002544 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002545
Fred Isamanf7e89172011-01-06 11:36:32 +00002546 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002547 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002548 if (calldata->roc &&
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002549 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2550 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002551 goto out_wait;
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002552 }
Fred Isamanf7e89172011-01-06 11:36:32 +00002553 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002554
Trond Myklebust516a6af2005-10-27 22:12:41 -04002555 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002556 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002557 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002558 &calldata->arg.seq_args,
2559 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002560 task) != 0)
2561 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002562 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002563 return;
2564out_no_action:
2565 task->tk_action = NULL;
2566out_wait:
2567 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568}
2569
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002570static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002571 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002572 .rpc_call_done = nfs4_close_done,
2573 .rpc_release = nfs4_free_closedata,
2574};
2575
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576/*
2577 * It is possible for data to be read/written from a mem-mapped file
2578 * after the sys_close call (which hits the vfs layer as a flush).
2579 * This means that we can't safely call nfsv4 close on a file until
2580 * the inode is cleared. This in turn means that we are not good
2581 * NFSv4 citizens - we do not indicate to the server to update the file's
2582 * share state even when we are done with one of the three share
2583 * stateid's in the inode.
2584 *
2585 * NOTE: Caller must be holding the sp->so_owner semaphore!
2586 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002587int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002589 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002591 struct nfs4_state_owner *sp = state->owner;
2592 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002593 struct rpc_message msg = {
2594 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2595 .rpc_cred = state->owner->so_cred,
2596 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002597 struct rpc_task_setup task_setup_data = {
2598 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002599 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002600 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002601 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002602 .flags = RPC_TASK_ASYNC,
2603 };
Trond Myklebust95121352005-10-18 14:20:12 -07002604 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002606 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002608 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04002609 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002610 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002612 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002613 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002615 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002616 if (calldata->arg.seqid == NULL)
2617 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002618 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002619 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002620 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002621 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002622 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002623 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002624 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002625
Trond Myklebust1174dd12010-12-21 10:52:24 -05002626 msg.rpc_argp = &calldata->arg;
2627 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002628 task_setup_data.callback_data = calldata;
2629 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002630 if (IS_ERR(task))
2631 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002632 status = 0;
2633 if (wait)
2634 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002635 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002636 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002637out_free_calldata:
2638 kfree(calldata);
2639out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002640 nfs4_put_open_state(state);
2641 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002642 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643}
2644
Trond Myklebust2b484292010-09-17 10:56:51 -04002645static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002646nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04002649 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2650
2651 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652
Trond Myklebust565277f2007-10-15 18:17:53 -04002653 /* Protect against concurrent sillydeletes */
Trond Myklebust959d9212013-06-28 16:29:51 -04002654 state = nfs4_do_open(dir, ctx, open_flags, attr, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04002655
2656 nfs4_label_release_security(label);
2657
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002658 if (IS_ERR(state))
2659 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04002660 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661}
2662
Trond Myklebust1185a552009-12-03 15:54:02 -05002663static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002664{
2665 if (ctx->state == NULL)
2666 return;
2667 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002668 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002669 else
Al Viro643168c2011-06-22 18:20:23 -04002670 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002671}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672
2673static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2674{
Benny Halevy43652ad2009-04-01 09:21:54 -04002675 struct nfs4_server_caps_arg args = {
2676 .fhandle = fhandle,
2677 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 struct nfs4_server_caps_res res = {};
2679 struct rpc_message msg = {
2680 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002681 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 .rpc_resp = &res,
2683 };
2684 int status;
2685
Bryan Schumaker7c513052011-03-24 17:12:24 +00002686 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 if (status == 0) {
2688 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002689 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2690 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2691 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2692 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2693 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2695 server->caps |= NFS_CAP_ACLS;
2696 if (res.has_links != 0)
2697 server->caps |= NFS_CAP_HARDLINKS;
2698 if (res.has_symlinks != 0)
2699 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002700 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2701 server->caps |= NFS_CAP_FILEID;
2702 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2703 server->caps |= NFS_CAP_MODE;
2704 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2705 server->caps |= NFS_CAP_NLINK;
2706 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2707 server->caps |= NFS_CAP_OWNER;
2708 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2709 server->caps |= NFS_CAP_OWNER_GROUP;
2710 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2711 server->caps |= NFS_CAP_ATIME;
2712 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2713 server->caps |= NFS_CAP_CTIME;
2714 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2715 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04002716#ifdef CONFIG_NFS_V4_SECURITY_LABEL
2717 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
2718 server->caps |= NFS_CAP_SECURITY_LABEL;
2719#endif
2720 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
2721 sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002722
David Quigleyaa9c2662013-05-22 12:50:44 -04002723 if (server->caps & NFS_CAP_SECURITY_LABEL) {
2724 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2725 res.attr_bitmask[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2726 }
Trond Myklebusta65318b2009-03-11 14:10:28 -04002727 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2728 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2729 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002731 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002733
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 return status;
2735}
2736
Trond Myklebust55a97592006-06-09 09:34:19 -04002737int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738{
2739 struct nfs4_exception exception = { };
2740 int err;
2741 do {
2742 err = nfs4_handle_exception(server,
2743 _nfs4_server_capabilities(server, fhandle),
2744 &exception);
2745 } while (exception.retry);
2746 return err;
2747}
2748
2749static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2750 struct nfs_fsinfo *info)
2751{
David Quigleyaa9c2662013-05-22 12:50:44 -04002752 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04002754 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 };
2756 struct nfs4_lookup_res res = {
2757 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002758 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 .fh = fhandle,
2760 };
2761 struct rpc_message msg = {
2762 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2763 .rpc_argp = &args,
2764 .rpc_resp = &res,
2765 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002766
David Quigleyaa9c2662013-05-22 12:50:44 -04002767 bitmask[0] = nfs4_fattr_bitmap[0];
2768 bitmask[1] = nfs4_fattr_bitmap[1];
2769 /*
2770 * Process the label in the upcoming getfattr
2771 */
2772 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
2773
Trond Myklebust0e574af2005-10-27 22:12:38 -04002774 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002775 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776}
2777
2778static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2779 struct nfs_fsinfo *info)
2780{
2781 struct nfs4_exception exception = { };
2782 int err;
2783 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002784 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04002785 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002786 switch (err) {
2787 case 0:
2788 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002789 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002790 default:
2791 err = nfs4_handle_exception(server, err, &exception);
2792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002794out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 return err;
2796}
2797
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002798static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2799 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2800{
Trond Myklebustc2190662013-08-26 19:23:04 -04002801 struct rpc_auth_create_args auth_args = {
2802 .pseudoflavor = flavor,
2803 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002804 struct rpc_auth *auth;
2805 int ret;
2806
Trond Myklebustc2190662013-08-26 19:23:04 -04002807 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002808 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04002809 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002810 goto out;
2811 }
2812 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002813out:
2814 return ret;
2815}
2816
Chuck Lever9a744ba2013-03-16 15:56:02 -04002817/*
2818 * Retry pseudoroot lookup with various security flavors. We do this when:
2819 *
2820 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2821 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2822 *
2823 * Returns zero on success, or a negative NFS4ERR value, or a
2824 * negative errno value.
2825 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002826static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002827 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828{
Chuck Lever9a744ba2013-03-16 15:56:02 -04002829 /* Per 3530bis 15.33.5 */
2830 static const rpc_authflavor_t flav_array[] = {
2831 RPC_AUTH_GSS_KRB5P,
2832 RPC_AUTH_GSS_KRB5I,
2833 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04002834 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04002835 RPC_AUTH_NULL,
2836 };
2837 int status = -EPERM;
2838 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839
Chuck Lever9a744ba2013-03-16 15:56:02 -04002840 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002841 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002842 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002843 continue;
2844 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002845 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04002846
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002847 /*
2848 * -EACCESS could mean that the user doesn't have correct permissions
2849 * to access the mount. It could also mean that we tried to mount
2850 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2851 * existing mount programs don't handle -EACCES very well so it should
2852 * be mapped to -EPERM instead.
2853 */
2854 if (status == -EACCES)
2855 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002856 return status;
2857}
2858
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002859static int nfs4_do_find_root_sec(struct nfs_server *server,
2860 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
2861{
2862 int mv = server->nfs_client->cl_minorversion;
2863 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
2864}
2865
2866/**
2867 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
2868 * @server: initialized nfs_server handle
2869 * @fhandle: we fill in the pseudo-fs root file handle
2870 * @info: we fill in an FSINFO struct
2871 *
2872 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002873 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002874int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2875 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002876{
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002877 int status;
2878
2879 status = nfs4_lookup_root(server, fhandle, info);
2880 if ((status == -NFS4ERR_WRONGSEC) &&
2881 !(server->flags & NFS_MOUNT_SECFLAVOUR))
2882 status = nfs4_do_find_root_sec(server, fhandle, info);
2883
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 if (status == 0)
2885 status = nfs4_server_capabilities(server, fhandle);
2886 if (status == 0)
2887 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002888
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002889 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890}
2891
Bryan Schumakerbae36242012-05-10 15:07:31 -04002892static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2893 struct nfs_fsinfo *info)
2894{
2895 int error;
2896 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04002897 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04002898
2899 error = nfs4_server_capabilities(server, mntfh);
2900 if (error < 0) {
2901 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2902 return error;
2903 }
2904
David Quigley14c43f72013-05-22 12:50:43 -04002905 label = nfs4_label_alloc(server, GFP_KERNEL);
2906 if (IS_ERR(label))
2907 return PTR_ERR(label);
2908
David Quigley1775fd32013-05-22 12:50:42 -04002909 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04002910 if (error < 0) {
2911 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04002912 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04002913 }
2914
2915 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2916 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2917 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2918
David Quigley14c43f72013-05-22 12:50:43 -04002919err_free_label:
2920 nfs4_label_free(label);
2921
Bryan Schumakerbae36242012-05-10 15:07:31 -04002922 return error;
2923}
2924
Manoj Naik6b97fd32006-06-09 09:34:29 -04002925/*
2926 * Get locations and (maybe) other attributes of a referral.
2927 * Note that we'll actually follow the referral later when
2928 * we detect fsid mismatch in inode revalidation
2929 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002930static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2931 const struct qstr *name, struct nfs_fattr *fattr,
2932 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002933{
2934 int status = -ENOMEM;
2935 struct page *page = NULL;
2936 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002937
2938 page = alloc_page(GFP_KERNEL);
2939 if (page == NULL)
2940 goto out;
2941 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2942 if (locations == NULL)
2943 goto out;
2944
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002945 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002946 if (status != 0)
2947 goto out;
2948 /* Make sure server returned a different fsid for the referral */
2949 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002950 dprintk("%s: server did not return a different fsid for"
2951 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002952 status = -EIO;
2953 goto out;
2954 }
Andy Adamson533eb462011-06-13 18:25:56 -04002955 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2956 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002957
Andy Adamson533eb462011-06-13 18:25:56 -04002958 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002959 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002960 memset(fhandle, 0, sizeof(struct nfs_fh));
2961out:
2962 if (page)
2963 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002964 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002965 return status;
2966}
2967
David Quigley1775fd32013-05-22 12:50:42 -04002968static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
2969 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970{
2971 struct nfs4_getattr_arg args = {
2972 .fh = fhandle,
2973 .bitmask = server->attr_bitmask,
2974 };
2975 struct nfs4_getattr_res res = {
2976 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002977 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 .server = server,
2979 };
2980 struct rpc_message msg = {
2981 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2982 .rpc_argp = &args,
2983 .rpc_resp = &res,
2984 };
David Quigleyaa9c2662013-05-22 12:50:44 -04002985
2986 args.bitmask = nfs4_bitmask(server, label);
2987
Trond Myklebust0e574af2005-10-27 22:12:38 -04002988 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002989 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990}
2991
David Quigley1775fd32013-05-22 12:50:42 -04002992static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
2993 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994{
2995 struct nfs4_exception exception = { };
2996 int err;
2997 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04002998 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
2999 trace_nfs4_getattr(server, fhandle, fattr, err);
3000 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 &exception);
3002 } while (exception.retry);
3003 return err;
3004}
3005
3006/*
3007 * The file is not closed if it is opened due to the a request to change
3008 * the size of the file. The open call will not be needed once the
3009 * VFS layer lookup-intents are implemented.
3010 *
3011 * Close is called when the inode is destroyed.
3012 * If we haven't opened the file for O_WRONLY, we
3013 * need to in the size_change case to obtain a stateid.
3014 *
3015 * Got race?
3016 * Because OPEN is always done by name in nfsv4, it is
3017 * possible that we opened a different file by the same
3018 * name. We can recognize this race condition, but we
3019 * can't do anything about it besides returning an error.
3020 *
3021 * This will be fixed with VFS changes (lookup-intent).
3022 */
3023static int
3024nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3025 struct iattr *sattr)
3026{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003027 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003028 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003029 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003030 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 int status;
3032
Benny Halevy8a1636c2010-07-14 15:43:57 -04003033 if (pnfs_ld_layoutret_on_setattr(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003034 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003035
Trond Myklebust0e574af2005-10-27 22:12:38 -04003036 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037
Andy Adamson26699402012-05-30 16:12:24 -04003038 /* Deal with open(O_TRUNC) */
3039 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003040 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003041
3042 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003043 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003044 return 0;
3045
Trond Myklebustd5308382005-11-04 15:33:38 -05003046 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003047 if (sattr->ia_valid & ATTR_FILE) {
3048 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003049
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003050 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003051 if (ctx) {
3052 cred = ctx->cred;
3053 state = ctx->state;
3054 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003055 }
3056
David Quigley14c43f72013-05-22 12:50:43 -04003057 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3058 if (IS_ERR(label))
3059 return PTR_ERR(label);
3060
3061 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003062 if (status == 0) {
Trond Myklebust65e43082005-08-16 11:49:44 -04003063 nfs_setattr_update_inode(inode, sattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003064 nfs_setsecurity(inode, fattr, label);
3065 }
David Quigley14c43f72013-05-22 12:50:43 -04003066 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 return status;
3068}
3069
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003070static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3071 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003072 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003073{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003074 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003075 int status;
3076 struct nfs4_lookup_arg args = {
3077 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003078 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003079 .name = name,
3080 };
3081 struct nfs4_lookup_res res = {
3082 .server = server,
3083 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003084 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003085 .fh = fhandle,
3086 };
3087 struct rpc_message msg = {
3088 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3089 .rpc_argp = &args,
3090 .rpc_resp = &res,
3091 };
3092
David Quigleyaa9c2662013-05-22 12:50:44 -04003093 args.bitmask = nfs4_bitmask(server, label);
3094
David Howells2b3de442006-08-22 20:06:09 -04003095 nfs_fattr_init(fattr);
3096
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003098 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 dprintk("NFS reply lookup: %d\n", status);
3100 return status;
3101}
3102
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003103static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003104{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003105 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003106 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003107 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3108 fattr->nlink = 2;
3109}
3110
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003111static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3112 struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003113 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003114{
3115 struct nfs4_exception exception = { };
3116 struct rpc_clnt *client = *clnt;
3117 int err;
3118 do {
David Quigley1775fd32013-05-22 12:50:42 -04003119 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003120 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003121 switch (err) {
3122 case -NFS4ERR_BADNAME:
3123 err = -ENOENT;
3124 goto out;
3125 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003126 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003127 goto out;
3128 case -NFS4ERR_WRONGSEC:
3129 err = -EPERM;
3130 if (client != *clnt)
3131 goto out;
3132
3133 client = nfs4_create_sec_client(client, dir, name);
3134 if (IS_ERR(client))
3135 return PTR_ERR(client);
3136
3137 exception.retry = 1;
3138 break;
3139 default:
3140 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3141 }
3142 } while (exception.retry);
3143
3144out:
3145 if (err == 0)
3146 *clnt = client;
3147 else if (client != *clnt)
3148 rpc_shutdown_client(client);
3149
3150 return err;
3151}
3152
Bryan Schumaker80a16b22012-04-27 13:27:46 -04003153static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003154 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3155 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003157 int status;
3158 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003159
David Quigley1775fd32013-05-22 12:50:42 -04003160 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003161 if (client != NFS_CLIENT(dir)) {
3162 rpc_shutdown_client(client);
3163 nfs_fixup_secinfo_attributes(fattr);
3164 }
3165 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166}
3167
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003168struct rpc_clnt *
3169nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3170 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3171{
3172 int status;
3173 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
3174
David Quigley1775fd32013-05-22 12:50:42 -04003175 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003176 if (status < 0) {
3177 rpc_shutdown_client(client);
3178 return ERR_PTR(status);
3179 }
3180 return client;
3181}
3182
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3184{
Trond Myklebust76b32992007-08-10 17:45:11 -04003185 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 struct nfs4_accessargs args = {
3187 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003188 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003190 struct nfs4_accessres res = {
3191 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003192 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 struct rpc_message msg = {
3194 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3195 .rpc_argp = &args,
3196 .rpc_resp = &res,
3197 .rpc_cred = entry->cred,
3198 };
3199 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003200 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201
3202 /*
3203 * Determine which access bits we want to ask for...
3204 */
3205 if (mode & MAY_READ)
3206 args.access |= NFS4_ACCESS_READ;
3207 if (S_ISDIR(inode->i_mode)) {
3208 if (mode & MAY_WRITE)
3209 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3210 if (mode & MAY_EXEC)
3211 args.access |= NFS4_ACCESS_LOOKUP;
3212 } else {
3213 if (mode & MAY_WRITE)
3214 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3215 if (mode & MAY_EXEC)
3216 args.access |= NFS4_ACCESS_EXECUTE;
3217 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003218
3219 res.fattr = nfs_alloc_fattr();
3220 if (res.fattr == NULL)
3221 return -ENOMEM;
3222
Bryan Schumaker7c513052011-03-24 17:12:24 +00003223 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003225 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003226 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003228 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 return status;
3230}
3231
3232static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3233{
3234 struct nfs4_exception exception = { };
3235 int err;
3236 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003237 err = _nfs4_proc_access(inode, entry);
3238 trace_nfs4_access(inode, err);
3239 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 &exception);
3241 } while (exception.retry);
3242 return err;
3243}
3244
3245/*
3246 * TODO: For the time being, we don't try to get any attributes
3247 * along with any of the zero-copy operations READ, READDIR,
3248 * READLINK, WRITE.
3249 *
3250 * In the case of the first three, we want to put the GETATTR
3251 * after the read-type operation -- this is because it is hard
3252 * to predict the length of a GETATTR response in v4, and thus
3253 * align the READ data correctly. This means that the GETATTR
3254 * may end up partially falling into the page cache, and we should
3255 * shift it into the 'tail' of the xdr_buf before processing.
3256 * To do this efficiently, we need to know the total length
3257 * of data received, which doesn't seem to be available outside
3258 * of the RPC layer.
3259 *
3260 * In the case of WRITE, we also want to put the GETATTR after
3261 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003262 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 *
3264 * Both of these changes to the XDR layer would in fact be quite
3265 * minor, but I decided to leave them for a subsequent patch.
3266 */
3267static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3268 unsigned int pgbase, unsigned int pglen)
3269{
3270 struct nfs4_readlink args = {
3271 .fh = NFS_FH(inode),
3272 .pgbase = pgbase,
3273 .pglen = pglen,
3274 .pages = &page,
3275 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003276 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 struct rpc_message msg = {
3278 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3279 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003280 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 };
3282
Bryan Schumaker7c513052011-03-24 17:12:24 +00003283 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 -07003284}
3285
3286static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3287 unsigned int pgbase, unsigned int pglen)
3288{
3289 struct nfs4_exception exception = { };
3290 int err;
3291 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003292 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3293 trace_nfs4_readlink(inode, err);
3294 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 &exception);
3296 } while (exception.retry);
3297 return err;
3298}
3299
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003301 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303static int
3304nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003305 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306{
David Quigleyaa9c2662013-05-22 12:50:44 -04003307 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003308 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 int status = 0;
3311
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003312 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3313 if (IS_ERR(ctx))
3314 return PTR_ERR(ctx);
3315
David Quigleyaa9c2662013-05-22 12:50:44 -04003316 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3317
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003318 sattr->ia_mode &= ~current_umask();
Trond Myklebust959d9212013-06-28 16:29:51 -04003319 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 if (IS_ERR(state)) {
3321 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003322 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003325 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003326 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 return status;
3328}
3329
3330static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3331{
Trond Myklebust16e42952005-10-27 22:12:44 -04003332 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003333 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003335 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003337 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003338 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003339 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003341 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3342 .rpc_argp = &args,
3343 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003345 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003346
Bryan Schumaker7c513052011-03-24 17:12:24 +00003347 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003348 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003349 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 return status;
3351}
3352
3353static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3354{
3355 struct nfs4_exception exception = { };
3356 int err;
3357 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003358 err = _nfs4_proc_remove(dir, name);
3359 trace_nfs4_remove(dir, name, err);
3360 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 &exception);
3362 } while (exception.retry);
3363 return err;
3364}
3365
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003366static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003368 struct nfs_server *server = NFS_SERVER(dir);
3369 struct nfs_removeargs *args = msg->rpc_argp;
3370 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003372 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04003374 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003375
3376 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377}
3378
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003379static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3380{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003381 nfs4_setup_sequence(NFS_SERVER(data->dir),
3382 &data->args.seq_args,
3383 &data->res.seq_res,
3384 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385}
3386
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003387static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003389 struct nfs_unlinkdata *data = task->tk_calldata;
3390 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003391
Trond Myklebust14516c32010-07-31 14:29:06 -04003392 if (!nfs4_sequence_done(task, &res->seq_res))
3393 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003394 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003395 return 0;
3396 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003397 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398}
3399
Jeff Laytond3d41522010-09-17 17:31:57 -04003400static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3401{
3402 struct nfs_server *server = NFS_SERVER(dir);
3403 struct nfs_renameargs *arg = msg->rpc_argp;
3404 struct nfs_renameres *res = msg->rpc_resp;
3405
3406 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003407 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04003408 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003409}
3410
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003411static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3412{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003413 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3414 &data->args.seq_args,
3415 &data->res.seq_res,
3416 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003417}
3418
3419static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3420 struct inode *new_dir)
3421{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003422 struct nfs_renamedata *data = task->tk_calldata;
3423 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04003424
3425 if (!nfs4_sequence_done(task, &res->seq_res))
3426 return 0;
3427 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3428 return 0;
3429
3430 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003431 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003432 return 1;
3433}
3434
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3436 struct inode *new_dir, struct qstr *new_name)
3437{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003438 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003439 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 .old_dir = NFS_FH(old_dir),
3441 .new_dir = NFS_FH(new_dir),
3442 .old_name = old_name,
3443 .new_name = new_name,
3444 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003445 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003446 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003447 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 struct rpc_message msg = {
3449 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3450 .rpc_argp = &arg,
3451 .rpc_resp = &res,
3452 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003453 int status = -ENOMEM;
David Quigley14c43f72013-05-22 12:50:43 -04003454
Bryan Schumaker7c513052011-03-24 17:12:24 +00003455 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 if (!status) {
3457 update_changeattr(old_dir, &res.old_cinfo);
3458 update_changeattr(new_dir, &res.new_cinfo);
3459 }
3460 return status;
3461}
3462
3463static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3464 struct inode *new_dir, struct qstr *new_name)
3465{
3466 struct nfs4_exception exception = { };
3467 int err;
3468 do {
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003469 err = _nfs4_proc_rename(old_dir, old_name,
3470 new_dir, new_name);
3471 trace_nfs4_rename(old_dir, old_name, new_dir, new_name, err);
3472 err = nfs4_handle_exception(NFS_SERVER(old_dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 &exception);
3474 } while (exception.retry);
3475 return err;
3476}
3477
3478static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3479{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003480 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 struct nfs4_link_arg arg = {
3482 .fh = NFS_FH(inode),
3483 .dir_fh = NFS_FH(dir),
3484 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003485 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003487 struct nfs4_link_res res = {
3488 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003489 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003490 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 struct rpc_message msg = {
3492 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3493 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003494 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003496 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497
Trond Myklebust136f2622010-04-16 16:22:49 -04003498 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003499 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003500 goto out;
3501
David Quigley14c43f72013-05-22 12:50:43 -04003502 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3503 if (IS_ERR(res.label)) {
3504 status = PTR_ERR(res.label);
3505 goto out;
3506 }
David Quigleyaa9c2662013-05-22 12:50:44 -04003507 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04003508
Bryan Schumaker7c513052011-03-24 17:12:24 +00003509 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003510 if (!status) {
3511 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04003512 status = nfs_post_op_update_inode(inode, res.fattr);
3513 if (!status)
3514 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003515 }
David Quigley14c43f72013-05-22 12:50:43 -04003516
3517
3518 nfs4_label_free(res.label);
3519
Trond Myklebust136f2622010-04-16 16:22:49 -04003520out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003521 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 return status;
3523}
3524
3525static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3526{
3527 struct nfs4_exception exception = { };
3528 int err;
3529 do {
3530 err = nfs4_handle_exception(NFS_SERVER(inode),
3531 _nfs4_proc_link(inode, dir, name),
3532 &exception);
3533 } while (exception.retry);
3534 return err;
3535}
3536
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003537struct nfs4_createdata {
3538 struct rpc_message msg;
3539 struct nfs4_create_arg arg;
3540 struct nfs4_create_res res;
3541 struct nfs_fh fh;
3542 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003543 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003544};
3545
3546static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3547 struct qstr *name, struct iattr *sattr, u32 ftype)
3548{
3549 struct nfs4_createdata *data;
3550
3551 data = kzalloc(sizeof(*data), GFP_KERNEL);
3552 if (data != NULL) {
3553 struct nfs_server *server = NFS_SERVER(dir);
3554
David Quigley14c43f72013-05-22 12:50:43 -04003555 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3556 if (IS_ERR(data->label))
3557 goto out_free;
3558
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003559 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3560 data->msg.rpc_argp = &data->arg;
3561 data->msg.rpc_resp = &data->res;
3562 data->arg.dir_fh = NFS_FH(dir);
3563 data->arg.server = server;
3564 data->arg.name = name;
3565 data->arg.attrs = sattr;
3566 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04003567 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003568 data->res.server = server;
3569 data->res.fh = &data->fh;
3570 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003571 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003572 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003573 }
3574 return data;
David Quigley14c43f72013-05-22 12:50:43 -04003575out_free:
3576 kfree(data);
3577 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003578}
3579
3580static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3581{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003582 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003583 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003584 if (status == 0) {
3585 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04003586 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003587 }
3588 return status;
3589}
3590
3591static void nfs4_free_createdata(struct nfs4_createdata *data)
3592{
David Quigley14c43f72013-05-22 12:50:43 -04003593 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003594 kfree(data);
3595}
3596
Chuck Lever4f390c12006-08-22 20:06:22 -04003597static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003598 struct page *page, unsigned int len, struct iattr *sattr,
3599 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003601 struct nfs4_createdata *data;
3602 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603
Chuck Lever94a6d752006-08-22 20:06:23 -04003604 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003605 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003606
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003607 status = -ENOMEM;
3608 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3609 if (data == NULL)
3610 goto out;
3611
3612 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3613 data->arg.u.symlink.pages = &page;
3614 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04003615 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003617 status = nfs4_do_create(dir, dentry, data);
3618
3619 nfs4_free_createdata(data);
3620out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 return status;
3622}
3623
Chuck Lever4f390c12006-08-22 20:06:22 -04003624static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003625 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626{
3627 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003628 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04003630
3631 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3632
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003634 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3635 trace_nfs4_symlink(dir, &dentry->d_name, err);
3636 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 &exception);
3638 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003639
3640 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 return err;
3642}
3643
3644static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003645 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003647 struct nfs4_createdata *data;
3648 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003650 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3651 if (data == NULL)
3652 goto out;
3653
David Quigley1775fd32013-05-22 12:50:42 -04003654 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003655 status = nfs4_do_create(dir, dentry, data);
3656
3657 nfs4_free_createdata(data);
3658out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 return status;
3660}
3661
3662static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3663 struct iattr *sattr)
3664{
3665 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003666 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003668
David Quigleyaa9c2662013-05-22 12:50:44 -04003669 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3670
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003671 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003673 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3674 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3675 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 &exception);
3677 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003678 nfs4_label_release_security(label);
3679
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 return err;
3681}
3682
3683static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003684 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685{
3686 struct inode *dir = dentry->d_inode;
3687 struct nfs4_readdir_arg args = {
3688 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003689 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 .pgbase = 0,
3691 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003692 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003693 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 };
3695 struct nfs4_readdir_res res;
3696 struct rpc_message msg = {
3697 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3698 .rpc_argp = &args,
3699 .rpc_resp = &res,
3700 .rpc_cred = cred,
3701 };
3702 int status;
3703
Harvey Harrison3110ff82008-05-02 13:42:44 -07003704 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003705 dentry->d_parent->d_name.name,
3706 dentry->d_name.name,
3707 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003708 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003710 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 -05003711 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003712 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003713 status += args.pgbase;
3714 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003715
3716 nfs_invalidate_atime(dir);
3717
Harvey Harrison3110ff82008-05-02 13:42:44 -07003718 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 return status;
3720}
3721
3722static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003723 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724{
3725 struct nfs4_exception exception = { };
3726 int err;
3727 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003728 err = _nfs4_proc_readdir(dentry, cred, cookie,
3729 pages, count, plus);
3730 trace_nfs4_readdir(dentry->d_inode, err);
3731 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 &exception);
3733 } while (exception.retry);
3734 return err;
3735}
3736
3737static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04003738 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003740 struct nfs4_createdata *data;
3741 int mode = sattr->ia_mode;
3742 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003744 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3745 if (data == NULL)
3746 goto out;
3747
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003749 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003751 data->arg.ftype = NF4BLK;
3752 data->arg.u.device.specdata1 = MAJOR(rdev);
3753 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 }
3755 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003756 data->arg.ftype = NF4CHR;
3757 data->arg.u.device.specdata1 = MAJOR(rdev);
3758 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003759 } else if (!S_ISSOCK(mode)) {
3760 status = -EINVAL;
3761 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 }
David Quigley1775fd32013-05-22 12:50:42 -04003763
David Quigleyaa9c2662013-05-22 12:50:44 -04003764 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003765 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003766out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003767 nfs4_free_createdata(data);
3768out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 return status;
3770}
3771
3772static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3773 struct iattr *sattr, dev_t rdev)
3774{
3775 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003776 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003778
David Quigleyaa9c2662013-05-22 12:50:44 -04003779 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3780
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003781 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003783 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
3784 trace_nfs4_mknod(dir, &dentry->d_name, err);
3785 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 &exception);
3787 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003788
3789 nfs4_label_release_security(label);
3790
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 return err;
3792}
3793
3794static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3795 struct nfs_fsstat *fsstat)
3796{
3797 struct nfs4_statfs_arg args = {
3798 .fh = fhandle,
3799 .bitmask = server->attr_bitmask,
3800 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003801 struct nfs4_statfs_res res = {
3802 .fsstat = fsstat,
3803 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 struct rpc_message msg = {
3805 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3806 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003807 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 };
3809
Trond Myklebust0e574af2005-10-27 22:12:38 -04003810 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003811 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812}
3813
3814static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3815{
3816 struct nfs4_exception exception = { };
3817 int err;
3818 do {
3819 err = nfs4_handle_exception(server,
3820 _nfs4_proc_statfs(server, fhandle, fsstat),
3821 &exception);
3822 } while (exception.retry);
3823 return err;
3824}
3825
3826static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3827 struct nfs_fsinfo *fsinfo)
3828{
3829 struct nfs4_fsinfo_arg args = {
3830 .fh = fhandle,
3831 .bitmask = server->attr_bitmask,
3832 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003833 struct nfs4_fsinfo_res res = {
3834 .fsinfo = fsinfo,
3835 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 struct rpc_message msg = {
3837 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3838 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003839 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840 };
3841
Bryan Schumaker7c513052011-03-24 17:12:24 +00003842 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843}
3844
3845static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3846{
3847 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04003848 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 int err;
3850
3851 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04003852 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003853 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04003854 if (err == 0) {
3855 struct nfs_client *clp = server->nfs_client;
3856
3857 spin_lock(&clp->cl_lock);
3858 clp->cl_lease_time = fsinfo->lease_time * HZ;
3859 clp->cl_last_renewal = now;
3860 spin_unlock(&clp->cl_lock);
3861 break;
3862 }
3863 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 } while (exception.retry);
3865 return err;
3866}
3867
3868static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3869{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003870 int error;
3871
Trond Myklebust0e574af2005-10-27 22:12:38 -04003872 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003873 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003874 if (error == 0) {
3875 /* block layout checks this! */
3876 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003877 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003878 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003879
3880 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881}
3882
3883static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3884 struct nfs_pathconf *pathconf)
3885{
3886 struct nfs4_pathconf_arg args = {
3887 .fh = fhandle,
3888 .bitmask = server->attr_bitmask,
3889 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003890 struct nfs4_pathconf_res res = {
3891 .pathconf = pathconf,
3892 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 struct rpc_message msg = {
3894 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3895 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003896 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897 };
3898
3899 /* None of the pathconf attributes are mandatory to implement */
3900 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3901 memset(pathconf, 0, sizeof(*pathconf));
3902 return 0;
3903 }
3904
Trond Myklebust0e574af2005-10-27 22:12:38 -04003905 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003906 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907}
3908
3909static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3910 struct nfs_pathconf *pathconf)
3911{
3912 struct nfs4_exception exception = { };
3913 int err;
3914
3915 do {
3916 err = nfs4_handle_exception(server,
3917 _nfs4_proc_pathconf(server, fhandle, pathconf),
3918 &exception);
3919 } while (exception.retry);
3920 return err;
3921}
3922
Trond Myklebust5521abf2013-03-16 20:54:34 -04003923int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04003924 const struct nfs_open_context *ctx,
3925 const struct nfs_lock_context *l_ctx,
3926 fmode_t fmode)
3927{
3928 const struct nfs_lockowner *lockowner = NULL;
3929
3930 if (l_ctx != NULL)
3931 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003932 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04003933}
3934EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
3935
Trond Myklebust5521abf2013-03-16 20:54:34 -04003936static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
3937 const struct nfs_open_context *ctx,
3938 const struct nfs_lock_context *l_ctx,
3939 fmode_t fmode)
3940{
3941 nfs4_stateid current_stateid;
3942
3943 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode))
3944 return false;
3945 return nfs4_stateid_match(stateid, &current_stateid);
3946}
3947
3948static bool nfs4_error_stateid_expired(int err)
3949{
3950 switch (err) {
3951 case -NFS4ERR_DELEG_REVOKED:
3952 case -NFS4ERR_ADMIN_REVOKED:
3953 case -NFS4ERR_BAD_STATEID:
3954 case -NFS4ERR_STALE_STATEID:
3955 case -NFS4ERR_OLD_STATEID:
3956 case -NFS4ERR_OPENMODE:
3957 case -NFS4ERR_EXPIRED:
3958 return true;
3959 }
3960 return false;
3961}
3962
Benny Halevyd20581a2011-05-22 19:52:03 +03003963void __nfs4_read_done_cb(struct nfs_read_data *data)
3964{
Fred Isamancd841602012-04-20 14:47:44 -04003965 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003966}
3967
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003968static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969{
Fred Isamancd841602012-04-20 14:47:44 -04003970 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971
Trond Myklebustcc668ab2013-08-14 15:31:28 -04003972 trace_nfs4_read(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003973 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003974 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003975 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003977
Benny Halevyd20581a2011-05-22 19:52:03 +03003978 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003980 renew_lease(server, data->timestamp);
3981 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982}
3983
Trond Myklebust5521abf2013-03-16 20:54:34 -04003984static bool nfs4_read_stateid_changed(struct rpc_task *task,
3985 struct nfs_readargs *args)
3986{
3987
3988 if (!nfs4_error_stateid_expired(task->tk_status) ||
3989 nfs4_stateid_is_current(&args->stateid,
3990 args->context,
3991 args->lock_context,
3992 FMODE_READ))
3993 return false;
3994 rpc_restart_call_prepare(task);
3995 return true;
3996}
3997
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003998static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3999{
4000
4001 dprintk("--> %s\n", __func__);
4002
4003 if (!nfs4_sequence_done(task, &data->res.seq_res))
4004 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004005 if (nfs4_read_stateid_changed(task, &data->args))
4006 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03004007 return data->read_done_cb ? data->read_done_cb(task, data) :
4008 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004009}
4010
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004011static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004014 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004015 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Chuck Levera9c92d62013-08-09 12:48:18 -04004016 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017}
4018
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004019static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
4020{
Trond Myklebust9b206142013-03-17 15:52:00 -04004021 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004022 &data->args.seq_args,
4023 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004024 task))
4025 return;
4026 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4027 data->args.lock_context, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028}
4029
Fred Isamanb029bc92011-03-03 15:13:42 +00004030static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031{
Fred Isamancd841602012-04-20 14:47:44 -04004032 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004034 trace_nfs4_write(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004035 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004036 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004037 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004039 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04004041 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004042 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004043 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044}
4045
Trond Myklebust5521abf2013-03-16 20:54:34 -04004046static bool nfs4_write_stateid_changed(struct rpc_task *task,
4047 struct nfs_writeargs *args)
4048{
4049
4050 if (!nfs4_error_stateid_expired(task->tk_status) ||
4051 nfs4_stateid_is_current(&args->stateid,
4052 args->context,
4053 args->lock_context,
4054 FMODE_WRITE))
4055 return false;
4056 rpc_restart_call_prepare(task);
4057 return true;
4058}
4059
Fred Isamanb029bc92011-03-03 15:13:42 +00004060static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
4061{
4062 if (!nfs4_sequence_done(task, &data->res.seq_res))
4063 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004064 if (nfs4_write_stateid_changed(task, &data->args))
4065 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03004066 return data->write_done_cb ? data->write_done_cb(task, data) :
4067 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00004068}
4069
Trond Myklebust5a37f852012-04-28 14:55:16 -04004070static
4071bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00004072{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004073 const struct nfs_pgio_header *hdr = data->header;
4074
4075 /* Don't request attributes for pNFS or O_DIRECT writes */
4076 if (data->ds_clp != NULL || hdr->dreq != NULL)
4077 return false;
4078 /* Otherwise, request attributes if and only if we don't hold
4079 * a delegation
4080 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004081 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004082}
Fred Isamana69aef12011-03-03 15:13:47 +00004083
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004084static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085{
Fred Isamancd841602012-04-20 14:47:44 -04004086 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004087
Trond Myklebust5a37f852012-04-28 14:55:16 -04004088 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00004089 data->args.bitmask = NULL;
4090 data->res.fattr = NULL;
4091 } else
4092 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004093
Fred Isamanb029bc92011-03-03 15:13:42 +00004094 if (!data->write_done_cb)
4095 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004096 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 data->timestamp = jiffies;
4098
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004099 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004100 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101}
4102
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04004103static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
4104{
Trond Myklebust9b206142013-03-17 15:52:00 -04004105 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004106 &data->args.seq_args,
4107 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004108 task))
4109 return;
4110 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4111 data->args.lock_context, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112}
4113
Fred Isaman0b7c0152012-04-20 14:47:39 -04004114static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4115{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004116 nfs4_setup_sequence(NFS_SERVER(data->inode),
4117 &data->args.seq_args,
4118 &data->res.seq_res,
4119 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120}
4121
Fred Isaman0b7c0152012-04-20 14:47:39 -04004122static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004125
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004126 trace_nfs4_commit(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004127 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004128 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004129 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004131 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132}
4133
Fred Isaman0b7c0152012-04-20 14:47:39 -04004134static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004135{
4136 if (!nfs4_sequence_done(task, &data->res.seq_res))
4137 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004138 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004139}
4140
Fred Isaman0b7c0152012-04-20 14:47:39 -04004141static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004143 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004144
Fred Isaman0b7c0152012-04-20 14:47:39 -04004145 if (data->commit_done_cb == NULL)
4146 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004147 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004148 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004149 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150}
4151
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004152struct nfs4_renewdata {
4153 struct nfs_client *client;
4154 unsigned long timestamp;
4155};
4156
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157/*
4158 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4159 * standalone procedure for queueing an asynchronous RENEW.
4160 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004161static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004162{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004163 struct nfs4_renewdata *data = calldata;
4164 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004165
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004166 if (atomic_read(&clp->cl_count) > 1)
4167 nfs4_schedule_state_renewal(clp);
4168 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004169 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004170}
4171
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004172static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004174 struct nfs4_renewdata *data = calldata;
4175 struct nfs_client *clp = data->client;
4176 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004178 trace_nfs4_renew_async(clp, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04004180 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004181 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4182 return;
4183 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004184 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004185 return;
4186 }
4187 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004189 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190}
4191
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004192static const struct rpc_call_ops nfs4_renew_ops = {
4193 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004194 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004195};
4196
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004197static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198{
4199 struct rpc_message msg = {
4200 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4201 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004202 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004204 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004206 if (renew_flags == 0)
4207 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004208 if (!atomic_inc_not_zero(&clp->cl_count))
4209 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004210 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004211 if (data == NULL)
4212 return -ENOMEM;
4213 data->client = clp;
4214 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004215 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004216 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217}
4218
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004219static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220{
4221 struct rpc_message msg = {
4222 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4223 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004224 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 };
4226 unsigned long now = jiffies;
4227 int status;
4228
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004229 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 if (status < 0)
4231 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004232 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 return 0;
4234}
4235
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004236static inline int nfs4_server_supports_acls(struct nfs_server *server)
4237{
4238 return (server->caps & NFS_CAP_ACLS)
4239 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
4240 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
4241}
4242
Trond Myklebust21f498c2012-08-24 10:59:25 -04004243/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4244 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004245 * the stack.
4246 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004247#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004248
Neil Hormane9e3d722011-03-04 19:26:03 -05004249static int buf_to_pages_noslab(const void *buf, size_t buflen,
4250 struct page **pages, unsigned int *pgbase)
4251{
4252 struct page *newpage, **spages;
4253 int rc = 0;
4254 size_t len;
4255 spages = pages;
4256
4257 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004258 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004259 newpage = alloc_page(GFP_KERNEL);
4260
4261 if (newpage == NULL)
4262 goto unwind;
4263 memcpy(page_address(newpage), buf, len);
4264 buf += len;
4265 buflen -= len;
4266 *pages++ = newpage;
4267 rc++;
4268 } while (buflen != 0);
4269
4270 return rc;
4271
4272unwind:
4273 for(; rc > 0; rc--)
4274 __free_page(spages[rc-1]);
4275 return -ENOMEM;
4276}
4277
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004278struct nfs4_cached_acl {
4279 int cached;
4280 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004281 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004282};
4283
4284static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004285{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004286 struct nfs_inode *nfsi = NFS_I(inode);
4287
4288 spin_lock(&inode->i_lock);
4289 kfree(nfsi->nfs4_acl);
4290 nfsi->nfs4_acl = acl;
4291 spin_unlock(&inode->i_lock);
4292}
4293
4294static void nfs4_zap_acl_attr(struct inode *inode)
4295{
4296 nfs4_set_cached_acl(inode, NULL);
4297}
4298
4299static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4300{
4301 struct nfs_inode *nfsi = NFS_I(inode);
4302 struct nfs4_cached_acl *acl;
4303 int ret = -ENOENT;
4304
4305 spin_lock(&inode->i_lock);
4306 acl = nfsi->nfs4_acl;
4307 if (acl == NULL)
4308 goto out;
4309 if (buf == NULL) /* user is just asking for length */
4310 goto out_len;
4311 if (acl->cached == 0)
4312 goto out;
4313 ret = -ERANGE; /* see getxattr(2) man page */
4314 if (acl->len > buflen)
4315 goto out;
4316 memcpy(buf, acl->data, acl->len);
4317out_len:
4318 ret = acl->len;
4319out:
4320 spin_unlock(&inode->i_lock);
4321 return ret;
4322}
4323
Sachin Prabhu5794d212012-04-17 14:36:40 +01004324static 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 +00004325{
4326 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004327 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004328
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004329 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004330 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004331 if (acl == NULL)
4332 goto out;
4333 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004334 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004335 } else {
4336 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4337 if (acl == NULL)
4338 goto out;
4339 acl->cached = 0;
4340 }
4341 acl->len = acl_len;
4342out:
4343 nfs4_set_cached_acl(inode, acl);
4344}
4345
Andy Adamsonbf118a32011-12-07 11:55:27 -05004346/*
4347 * The getxattr API returns the required buffer length when called with a
4348 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4349 * the required buf. On a NULL buf, we send a page of data to the server
4350 * guessing that the ACL request can be serviced by a page. If so, we cache
4351 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4352 * the cache. If not so, we throw away the page, and cache the required
4353 * length. The next getxattr call will then produce another round trip to
4354 * the server, this time with the input buf of the required size.
4355 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004356static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004357{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004358 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004359 struct nfs_getaclargs args = {
4360 .fh = NFS_FH(inode),
4361 .acl_pages = pages,
4362 .acl_len = buflen,
4363 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004364 struct nfs_getaclres res = {
4365 .acl_len = buflen,
4366 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004367 struct rpc_message msg = {
4368 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4369 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004370 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004371 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004372 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4373 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004374
Andy Adamsonbf118a32011-12-07 11:55:27 -05004375 /* As long as we're doing a round trip to the server anyway,
4376 * let's be prepared for a page of acl data. */
4377 if (npages == 0)
4378 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004379 if (npages > ARRAY_SIZE(pages))
4380 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004381
Andy Adamsonbf118a32011-12-07 11:55:27 -05004382 for (i = 0; i < npages; i++) {
4383 pages[i] = alloc_page(GFP_KERNEL);
4384 if (!pages[i])
4385 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004386 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004387
4388 /* for decoding across pages */
4389 res.acl_scratch = alloc_page(GFP_KERNEL);
4390 if (!res.acl_scratch)
4391 goto out_free;
4392
Andy Adamsonbf118a32011-12-07 11:55:27 -05004393 args.acl_len = npages * PAGE_SIZE;
4394 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004395
Peng Taode040be2012-01-10 22:42:47 +08004396 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004397 __func__, buf, buflen, npages, args.acl_len);
4398 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4399 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004400 if (ret)
4401 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004402
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004403 /* Handle the case where the passed-in buffer is too short */
4404 if (res.acl_flags & NFS4_ACL_TRUNC) {
4405 /* Did the user only issue a request for the acl length? */
4406 if (buf == NULL)
4407 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004408 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004409 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004410 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004411 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004412 if (buf) {
4413 if (res.acl_len > buflen) {
4414 ret = -ERANGE;
4415 goto out_free;
4416 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004417 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004418 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004419out_ok:
4420 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004421out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004422 for (i = 0; i < npages; i++)
4423 if (pages[i])
4424 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004425 if (res.acl_scratch)
4426 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004427 return ret;
4428}
4429
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004430static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4431{
4432 struct nfs4_exception exception = { };
4433 ssize_t ret;
4434 do {
4435 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04004436 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004437 if (ret >= 0)
4438 break;
4439 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4440 } while (exception.retry);
4441 return ret;
4442}
4443
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004444static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4445{
4446 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004447 int ret;
4448
4449 if (!nfs4_server_supports_acls(server))
4450 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004451 ret = nfs_revalidate_inode(server, inode);
4452 if (ret < 0)
4453 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004454 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4455 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004456 ret = nfs4_read_cached_acl(inode, buf, buflen);
4457 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004458 /* -ENOENT is returned if there is no ACL or if there is an ACL
4459 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004460 return ret;
4461 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004462}
4463
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004464static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004465{
4466 struct nfs_server *server = NFS_SERVER(inode);
4467 struct page *pages[NFS4ACL_MAXPAGES];
4468 struct nfs_setaclargs arg = {
4469 .fh = NFS_FH(inode),
4470 .acl_pages = pages,
4471 .acl_len = buflen,
4472 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004473 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004474 struct rpc_message msg = {
4475 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4476 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004477 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004478 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004479 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004480 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004481
4482 if (!nfs4_server_supports_acls(server))
4483 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004484 if (npages > ARRAY_SIZE(pages))
4485 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004486 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4487 if (i < 0)
4488 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004489 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004490 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004491
4492 /*
4493 * Free each page after tx, so the only ref left is
4494 * held by the network stack
4495 */
4496 for (; i > 0; i--)
4497 put_page(pages[i-1]);
4498
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004499 /*
4500 * Acl update can result in inode attribute update.
4501 * so mark the attribute cache invalid.
4502 */
4503 spin_lock(&inode->i_lock);
4504 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4505 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004506 nfs_access_zap_cache(inode);
4507 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004508 return ret;
4509}
4510
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004511static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4512{
4513 struct nfs4_exception exception = { };
4514 int err;
4515 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004516 err = __nfs4_proc_set_acl(inode, buf, buflen);
4517 trace_nfs4_set_acl(inode, err);
4518 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004519 &exception);
4520 } while (exception.retry);
4521 return err;
4522}
4523
David Quigleyaa9c2662013-05-22 12:50:44 -04004524#ifdef CONFIG_NFS_V4_SECURITY_LABEL
4525static int _nfs4_get_security_label(struct inode *inode, void *buf,
4526 size_t buflen)
4527{
4528 struct nfs_server *server = NFS_SERVER(inode);
4529 struct nfs_fattr fattr;
4530 struct nfs4_label label = {0, 0, buflen, buf};
4531
4532 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4533 struct nfs4_getattr_arg args = {
4534 .fh = NFS_FH(inode),
4535 .bitmask = bitmask,
4536 };
4537 struct nfs4_getattr_res res = {
4538 .fattr = &fattr,
4539 .label = &label,
4540 .server = server,
4541 };
4542 struct rpc_message msg = {
4543 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4544 .rpc_argp = &args,
4545 .rpc_resp = &res,
4546 };
4547 int ret;
4548
4549 nfs_fattr_init(&fattr);
4550
4551 ret = rpc_call_sync(server->client, &msg, 0);
4552 if (ret)
4553 return ret;
4554 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4555 return -ENOENT;
4556 if (buflen < label.len)
4557 return -ERANGE;
4558 return 0;
4559}
4560
4561static int nfs4_get_security_label(struct inode *inode, void *buf,
4562 size_t buflen)
4563{
4564 struct nfs4_exception exception = { };
4565 int err;
4566
4567 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4568 return -EOPNOTSUPP;
4569
4570 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004571 err = _nfs4_get_security_label(inode, buf, buflen);
4572 trace_nfs4_get_security_label(inode, err);
4573 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004574 &exception);
4575 } while (exception.retry);
4576 return err;
4577}
4578
4579static int _nfs4_do_set_security_label(struct inode *inode,
4580 struct nfs4_label *ilabel,
4581 struct nfs_fattr *fattr,
4582 struct nfs4_label *olabel)
4583{
4584
4585 struct iattr sattr = {0};
4586 struct nfs_server *server = NFS_SERVER(inode);
4587 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4588 struct nfs_setattrargs args = {
4589 .fh = NFS_FH(inode),
4590 .iap = &sattr,
4591 .server = server,
4592 .bitmask = bitmask,
4593 .label = ilabel,
4594 };
4595 struct nfs_setattrres res = {
4596 .fattr = fattr,
4597 .label = olabel,
4598 .server = server,
4599 };
4600 struct rpc_message msg = {
4601 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
4602 .rpc_argp = &args,
4603 .rpc_resp = &res,
4604 };
4605 int status;
4606
4607 nfs4_stateid_copy(&args.stateid, &zero_stateid);
4608
4609 status = rpc_call_sync(server->client, &msg, 0);
4610 if (status)
4611 dprintk("%s failed: %d\n", __func__, status);
4612
4613 return status;
4614}
4615
4616static int nfs4_do_set_security_label(struct inode *inode,
4617 struct nfs4_label *ilabel,
4618 struct nfs_fattr *fattr,
4619 struct nfs4_label *olabel)
4620{
4621 struct nfs4_exception exception = { };
4622 int err;
4623
4624 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004625 err = _nfs4_do_set_security_label(inode, ilabel,
4626 fattr, olabel);
4627 trace_nfs4_set_security_label(inode, err);
4628 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004629 &exception);
4630 } while (exception.retry);
4631 return err;
4632}
4633
4634static int
4635nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4636{
4637 struct nfs4_label ilabel, *olabel = NULL;
4638 struct nfs_fattr fattr;
4639 struct rpc_cred *cred;
4640 struct inode *inode = dentry->d_inode;
4641 int status;
4642
4643 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4644 return -EOPNOTSUPP;
4645
4646 nfs_fattr_init(&fattr);
4647
4648 ilabel.pi = 0;
4649 ilabel.lfs = 0;
4650 ilabel.label = (char *)buf;
4651 ilabel.len = buflen;
4652
4653 cred = rpc_lookup_cred();
4654 if (IS_ERR(cred))
4655 return PTR_ERR(cred);
4656
4657 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4658 if (IS_ERR(olabel)) {
4659 status = -PTR_ERR(olabel);
4660 goto out;
4661 }
4662
4663 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4664 if (status == 0)
4665 nfs_setsecurity(inode, &fattr, olabel);
4666
4667 nfs4_label_free(olabel);
4668out:
4669 put_rpccred(cred);
4670 return status;
4671}
4672#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4673
4674
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004676nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004678 struct nfs_client *clp = server->nfs_client;
4679
4680 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 return 0;
4682 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004683 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004684 case -NFS4ERR_ADMIN_REVOKED:
4685 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004686 if (state == NULL)
4687 break;
4688 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004689 case -NFS4ERR_OPENMODE:
4690 if (state == NULL)
4691 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004692 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4693 goto stateid_invalid;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004694 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004695 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004696 if (state != NULL) {
4697 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4698 goto stateid_invalid;
4699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004701 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004702 nfs4_schedule_lease_recovery(clp);
4703 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004704#if defined(CONFIG_NFS_V4_1)
4705 case -NFS4ERR_BADSESSION:
4706 case -NFS4ERR_BADSLOT:
4707 case -NFS4ERR_BAD_HIGH_SLOT:
4708 case -NFS4ERR_DEADSESSION:
4709 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4710 case -NFS4ERR_SEQ_FALSE_RETRY:
4711 case -NFS4ERR_SEQ_MISORDERED:
4712 dprintk("%s ERROR %d, Reset session\n", __func__,
4713 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004714 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004715 task->tk_status = 0;
4716 return -EAGAIN;
4717#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004719 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004720 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4722 task->tk_status = 0;
4723 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004724 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 case -NFS4ERR_OLD_STATEID:
4726 task->tk_status = 0;
4727 return -EAGAIN;
4728 }
4729 task->tk_status = nfs4_map_errors(task->tk_status);
4730 return 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004731stateid_invalid:
4732 task->tk_status = -EIO;
4733 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004734wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004735 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004736 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4737 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4738 task->tk_status = 0;
4739 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740}
4741
Chuck Leverf0920752012-05-21 22:45:41 -04004742static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4743 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004744{
4745 __be32 verf[2];
4746
Chuck Lever2c820d92012-05-21 22:45:33 -04004747 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4748 /* An impossible timestamp guarantees this value
4749 * will never match a generated boot time. */
4750 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04004751 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04004752 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004753 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04004754 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
4755 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04004756 }
Chuck Levercd937102012-03-02 17:14:31 -05004757 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4758}
4759
Chuck Levere984a552012-09-14 17:24:21 -04004760static unsigned int
4761nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4762 char *buf, size_t len)
4763{
4764 unsigned int result;
4765
4766 rcu_read_lock();
4767 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4768 clp->cl_ipaddr,
4769 rpc_peeraddr2str(clp->cl_rpcclient,
4770 RPC_DISPLAY_ADDR),
4771 rpc_peeraddr2str(clp->cl_rpcclient,
4772 RPC_DISPLAY_PROTO));
4773 rcu_read_unlock();
4774 return result;
4775}
4776
4777static unsigned int
4778nfs4_init_uniform_client_string(const struct nfs_client *clp,
4779 char *buf, size_t len)
4780{
Trond Myklebust55b59292013-07-24 16:36:35 -04004781 const char *nodename = clp->cl_rpcclient->cl_nodename;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004782
4783 if (nfs4_client_id_uniquifier[0] != '\0')
Trond Myklebust55b59292013-07-24 16:36:35 -04004784 return scnprintf(buf, len, "Linux NFSv%u.%u %s/%s",
4785 clp->rpc_ops->version,
4786 clp->cl_minorversion,
4787 nfs4_client_id_uniquifier,
4788 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004789 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4790 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004791 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004792}
4793
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004794/**
4795 * nfs4_proc_setclientid - Negotiate client ID
4796 * @clp: state data structure
4797 * @program: RPC program for NFSv4 callback service
4798 * @port: IP port number for NFS4 callback service
4799 * @cred: RPC credential to use for this call
4800 * @res: where to place the result
4801 *
4802 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4803 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004804int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4805 unsigned short port, struct rpc_cred *cred,
4806 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807{
4808 nfs4_verifier sc_verifier;
4809 struct nfs4_setclientid setclientid = {
4810 .sc_verifier = &sc_verifier,
4811 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004812 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 };
4814 struct rpc_message msg = {
4815 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4816 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004817 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004818 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004820 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821
Chuck Leverde734832012-07-11 16:30:50 -04004822 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004823 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004824 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4825 setclientid.sc_name_len =
4826 nfs4_init_uniform_client_string(clp,
4827 setclientid.sc_name,
4828 sizeof(setclientid.sc_name));
4829 else
4830 setclientid.sc_name_len =
4831 nfs4_init_nonuniform_client_string(clp,
4832 setclientid.sc_name,
4833 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004834 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004835 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004836 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Djalal Harounife2d5392013-06-18 17:58:12 +01004837 sizeof(setclientid.sc_netid), "%s",
Chuck Leverd4d3c502007-12-10 14:57:09 -05004838 rpc_peeraddr2str(clp->cl_rpcclient,
4839 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004840 rcu_read_unlock();
4841 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004842 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 clp->cl_ipaddr, port >> 8, port & 255);
4844
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004845 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4846 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4847 setclientid.sc_name_len, setclientid.sc_name);
4848 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004849 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004850 dprintk("NFS reply setclientid: %d\n", status);
4851 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852}
4853
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004854/**
4855 * nfs4_proc_setclientid_confirm - Confirm client ID
4856 * @clp: state data structure
4857 * @res: result of a previous SETCLIENTID
4858 * @cred: RPC credential to use for this call
4859 *
4860 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4861 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004862int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004863 struct nfs4_setclientid_res *arg,
4864 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866 struct rpc_message msg = {
4867 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004868 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004869 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 int status;
4872
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004873 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4874 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4875 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004876 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004877 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004878 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879 return status;
4880}
4881
Trond Myklebustfe650402006-01-03 09:55:18 +01004882struct nfs4_delegreturndata {
4883 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004884 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004885 struct nfs_fh fh;
4886 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004887 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004888 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004889 int rpc_status;
4890};
4891
Trond Myklebustfe650402006-01-03 09:55:18 +01004892static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4893{
4894 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004895
Trond Myklebust14516c32010-07-31 14:29:06 -04004896 if (!nfs4_sequence_done(task, &data->res.seq_res))
4897 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004898
Trond Myklebustca8acf82013-08-13 10:36:56 -04004899 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004900 switch (task->tk_status) {
4901 case -NFS4ERR_STALE_STATEID:
4902 case -NFS4ERR_EXPIRED:
4903 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004904 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004905 break;
4906 default:
4907 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4908 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004909 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004910 return;
4911 }
4912 }
4913 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004914}
4915
4916static void nfs4_delegreturn_release(void *calldata)
4917{
Trond Myklebustfe650402006-01-03 09:55:18 +01004918 kfree(calldata);
4919}
4920
Andy Adamson938e1012009-04-01 09:22:28 -04004921#if defined(CONFIG_NFS_V4_1)
4922static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4923{
4924 struct nfs4_delegreturndata *d_data;
4925
4926 d_data = (struct nfs4_delegreturndata *)data;
4927
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004928 nfs4_setup_sequence(d_data->res.server,
4929 &d_data->args.seq_args,
4930 &d_data->res.seq_res,
4931 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004932}
4933#endif /* CONFIG_NFS_V4_1 */
4934
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004935static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004936#if defined(CONFIG_NFS_V4_1)
4937 .rpc_call_prepare = nfs4_delegreturn_prepare,
4938#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004939 .rpc_call_done = nfs4_delegreturn_done,
4940 .rpc_release = nfs4_delegreturn_release,
4941};
4942
Trond Myklebuste6f81072008-01-24 18:14:34 -05004943static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004944{
4945 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004946 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004947 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004948 struct rpc_message msg = {
4949 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4950 .rpc_cred = cred,
4951 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004952 struct rpc_task_setup task_setup_data = {
4953 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004954 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004955 .callback_ops = &nfs4_delegreturn_ops,
4956 .flags = RPC_TASK_ASYNC,
4957 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004958 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004959
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004960 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004961 if (data == NULL)
4962 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04004963 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004964 data->args.fhandle = &data->fh;
4965 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004966 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004967 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004968 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004969 data->res.fattr = &data->fattr;
4970 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004971 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004972 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004973 data->rpc_status = 0;
4974
Trond Myklebustc970aa82007-07-14 15:39:59 -04004975 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004976 msg.rpc_argp = &data->args;
4977 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004978 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004979 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004980 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004981 if (!issync)
4982 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004983 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004984 if (status != 0)
4985 goto out;
4986 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004987 if (status == 0)
4988 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4989 else
4990 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004991out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004992 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004993 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994}
4995
Trond Myklebuste6f81072008-01-24 18:14:34 -05004996int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997{
4998 struct nfs_server *server = NFS_SERVER(inode);
4999 struct nfs4_exception exception = { };
5000 int err;
5001 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005002 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Trond Myklebustca8acf82013-08-13 10:36:56 -04005003 trace_nfs4_delegreturn(inode, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004 switch (err) {
5005 case -NFS4ERR_STALE_STATEID:
5006 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007 case 0:
5008 return 0;
5009 }
5010 err = nfs4_handle_exception(server, err, &exception);
5011 } while (exception.retry);
5012 return err;
5013}
5014
5015#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5016#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5017
5018/*
5019 * sleep, with exponential backoff, and retry the LOCK operation.
5020 */
5021static unsigned long
5022nfs4_set_lock_task_retry(unsigned long timeout)
5023{
Colin Cross416ad3c2013-05-06 23:50:06 +00005024 freezable_schedule_timeout_killable_unsafe(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025 timeout <<= 1;
5026 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5027 return NFS4_LOCK_MAXTIMEOUT;
5028 return timeout;
5029}
5030
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5032{
5033 struct inode *inode = state->inode;
5034 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005035 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005036 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005038 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005040 struct nfs_lockt_res res = {
5041 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042 };
5043 struct rpc_message msg = {
5044 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5045 .rpc_argp = &arg,
5046 .rpc_resp = &res,
5047 .rpc_cred = state->owner->so_cred,
5048 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049 struct nfs4_lock_state *lsp;
5050 int status;
5051
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005052 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005053 status = nfs4_set_lock_state(state, request);
5054 if (status != 0)
5055 goto out;
5056 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005057 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005058 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005059 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005060 switch (status) {
5061 case 0:
5062 request->fl_type = F_UNLCK;
5063 break;
5064 case -NFS4ERR_DENIED:
5065 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005067 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005068out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 return status;
5070}
5071
5072static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5073{
5074 struct nfs4_exception exception = { };
5075 int err;
5076
5077 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005078 err = _nfs4_proc_getlk(state, cmd, request);
5079 trace_nfs4_get_lock(request, state, cmd, err);
5080 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 &exception);
5082 } while (exception.retry);
5083 return err;
5084}
5085
5086static int do_vfs_lock(struct file *file, struct file_lock *fl)
5087{
5088 int res = 0;
5089 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5090 case FL_POSIX:
5091 res = posix_lock_file_wait(file, fl);
5092 break;
5093 case FL_FLOCK:
5094 res = flock_lock_file_wait(file, fl);
5095 break;
5096 default:
5097 BUG();
5098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099 return res;
5100}
5101
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005102struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005103 struct nfs_locku_args arg;
5104 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005105 struct nfs4_lock_state *lsp;
5106 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005107 struct file_lock fl;
5108 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005109 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005110};
5111
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005112static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5113 struct nfs_open_context *ctx,
5114 struct nfs4_lock_state *lsp,
5115 struct nfs_seqid *seqid)
5116{
5117 struct nfs4_unlockdata *p;
5118 struct inode *inode = lsp->ls_state->inode;
5119
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005120 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005121 if (p == NULL)
5122 return NULL;
5123 p->arg.fh = NFS_FH(inode);
5124 p->arg.fl = &p->fl;
5125 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005126 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005127 p->arg.stateid = &lsp->ls_stateid;
5128 p->lsp = lsp;
5129 atomic_inc(&lsp->ls_count);
5130 /* Ensure we don't close file until we're done freeing locks! */
5131 p->ctx = get_nfs_open_context(ctx);
5132 memcpy(&p->fl, fl, sizeof(p->fl));
5133 p->server = NFS_SERVER(inode);
5134 return p;
5135}
5136
Trond Myklebust06f814a2006-01-03 09:55:07 +01005137static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005138{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005139 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005140 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005141 nfs4_put_lock_state(calldata->lsp);
5142 put_nfs_open_context(calldata->ctx);
5143 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005144}
5145
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005146static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005147{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005148 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005149
Trond Myklebust14516c32010-07-31 14:29:06 -04005150 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5151 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005152 switch (task->tk_status) {
5153 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05005154 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
5155 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005156 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005157 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005158 case -NFS4ERR_BAD_STATEID:
5159 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005160 case -NFS4ERR_STALE_STATEID:
5161 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005162 break;
5163 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005164 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005165 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005166 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005167 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005168}
5169
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005170static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005171{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005172 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005174 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005175 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04005176 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005177 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005178 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005179 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005180 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005181 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005182 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005183 &calldata->res.seq_res,
5184 task) != 0)
5185 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005186 return;
5187out_no_action:
5188 task->tk_action = NULL;
5189out_wait:
5190 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191}
5192
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005193static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005194 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005195 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005196 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005197};
5198
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005199static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5200 struct nfs_open_context *ctx,
5201 struct nfs4_lock_state *lsp,
5202 struct nfs_seqid *seqid)
5203{
5204 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005205 struct rpc_message msg = {
5206 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5207 .rpc_cred = ctx->cred,
5208 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005209 struct rpc_task_setup task_setup_data = {
5210 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005211 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005212 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005213 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005214 .flags = RPC_TASK_ASYNC,
5215 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005216
Frank Filz137d6ac2007-07-09 15:32:29 -07005217 /* Ensure this is an unlock - when canceling a lock, the
5218 * canceled lock is passed in, and it won't be an unlock.
5219 */
5220 fl->fl_type = F_UNLCK;
5221
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005222 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5223 if (data == NULL) {
5224 nfs_free_seqid(seqid);
5225 return ERR_PTR(-ENOMEM);
5226 }
5227
Chuck Levera9c92d62013-08-09 12:48:18 -04005228 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005229 msg.rpc_argp = &data->arg;
5230 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005231 task_setup_data.callback_data = data;
5232 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005233}
5234
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5236{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005237 struct inode *inode = state->inode;
5238 struct nfs4_state_owner *sp = state->owner;
5239 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005240 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005241 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005242 struct rpc_task *task;
5243 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005244 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245
Trond Myklebust9b073572006-06-29 16:38:34 -04005246 status = nfs4_set_lock_state(state, request);
5247 /* Unlock _before_ we do the RPC call */
5248 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005249 /* Exclude nfs_delegation_claim_locks() */
5250 mutex_lock(&sp->so_delegreturn_mutex);
5251 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005252 down_read(&nfsi->rwsem);
5253 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
5254 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005255 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005256 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005257 }
5258 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005259 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005260 if (status != 0)
5261 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005262 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005263 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005264 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5265 goto out;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005266 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005267 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005268 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04005269 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005270 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005271 status = PTR_ERR(task);
5272 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005273 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005274 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005275 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005276out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005277 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005278 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005279 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280}
5281
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005282struct nfs4_lockdata {
5283 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005284 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005285 struct nfs4_lock_state *lsp;
5286 struct nfs_open_context *ctx;
5287 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005288 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005289 int rpc_status;
5290 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005291 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005292};
5293
5294static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005295 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5296 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005297{
5298 struct nfs4_lockdata *p;
5299 struct inode *inode = lsp->ls_state->inode;
5300 struct nfs_server *server = NFS_SERVER(inode);
5301
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005302 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005303 if (p == NULL)
5304 return NULL;
5305
5306 p->arg.fh = NFS_FH(inode);
5307 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005308 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005309 if (p->arg.open_seqid == NULL)
5310 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005311 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005312 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005313 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005314 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04005315 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005316 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005317 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005318 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005319 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005320 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005321 atomic_inc(&lsp->ls_count);
5322 p->ctx = get_nfs_open_context(ctx);
5323 memcpy(&p->fl, fl, sizeof(p->fl));
5324 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005325out_free_seqid:
5326 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005327out_free:
5328 kfree(p);
5329 return NULL;
5330}
5331
5332static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5333{
5334 struct nfs4_lockdata *data = calldata;
5335 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336
Harvey Harrison3110ff82008-05-02 13:42:44 -07005337 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005338 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005339 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005340 /* Do we need to do an open_to_lock_owner? */
5341 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005342 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005343 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005344 }
Trond Myklebust8e472f32013-04-20 01:30:53 -04005345 data->arg.open_stateid = &state->open_stateid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005346 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005347 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005348 } else
5349 data->arg.new_lock_owner = 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04005350 if (!nfs4_valid_open_stateid(state)) {
5351 data->rpc_status = -EBADF;
5352 task->tk_action = NULL;
5353 goto out_release_open_seqid;
5354 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005355 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005356 if (nfs4_setup_sequence(data->server,
5357 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005358 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005359 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04005360 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04005361out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005362 nfs_release_seqid(data->arg.open_seqid);
5363out_release_lock_seqid:
5364 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005365out_wait:
5366 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005367 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005368}
5369
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005370static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5371{
5372 struct nfs4_lockdata *data = calldata;
5373
Harvey Harrison3110ff82008-05-02 13:42:44 -07005374 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005375
Trond Myklebust14516c32010-07-31 14:29:06 -04005376 if (!nfs4_sequence_done(task, &data->res.seq_res))
5377 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04005378
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005379 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005380 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005381 if (data->rpc_status == 0)
5382 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
5383 else
5384 goto out;
5385 }
5386 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05005387 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005388 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04005389 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005390 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005391out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07005392 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005393}
5394
5395static void nfs4_lock_release(void *calldata)
5396{
5397 struct nfs4_lockdata *data = calldata;
5398
Harvey Harrison3110ff82008-05-02 13:42:44 -07005399 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005400 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005401 if (data->cancelled != 0) {
5402 struct rpc_task *task;
5403 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5404 data->arg.lock_seqid);
5405 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08005406 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005407 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005408 } else
5409 nfs_free_seqid(data->arg.lock_seqid);
5410 nfs4_put_lock_state(data->lsp);
5411 put_nfs_open_context(data->ctx);
5412 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005413 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005414}
5415
5416static const struct rpc_call_ops nfs4_lock_ops = {
5417 .rpc_call_prepare = nfs4_lock_prepare,
5418 .rpc_call_done = nfs4_lock_done,
5419 .rpc_release = nfs4_lock_release,
5420};
5421
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005422static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5423{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005424 switch (error) {
5425 case -NFS4ERR_ADMIN_REVOKED:
5426 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05005427 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005428 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04005429 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05005430 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005431 break;
5432 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005433 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05005434 case -NFS4ERR_EXPIRED:
5435 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005436 };
5437}
5438
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005439static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005440{
5441 struct nfs4_lockdata *data;
5442 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005443 struct rpc_message msg = {
5444 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5445 .rpc_cred = state->owner->so_cred,
5446 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005447 struct rpc_task_setup task_setup_data = {
5448 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005449 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005450 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005451 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005452 .flags = RPC_TASK_ASYNC,
5453 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005454 int ret;
5455
Harvey Harrison3110ff82008-05-02 13:42:44 -07005456 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005457 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005458 fl->fl_u.nfs4_fl.owner,
5459 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005460 if (data == NULL)
5461 return -ENOMEM;
5462 if (IS_SETLKW(cmd))
5463 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04005464 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005465 msg.rpc_argp = &data->arg;
5466 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005467 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005468 if (recovery_type > NFS_LOCK_NEW) {
5469 if (recovery_type == NFS_LOCK_RECLAIM)
5470 data->arg.reclaim = NFS_LOCK_RECLAIM;
5471 nfs4_set_sequence_privileged(&data->arg.seq_args);
5472 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04005473 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005474 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005475 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005476 ret = nfs4_wait_for_completion_rpc_task(task);
5477 if (ret == 0) {
5478 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005479 if (ret)
5480 nfs4_handle_setlk_error(data->server, data->lsp,
5481 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005482 } else
5483 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005484 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005485 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005486 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487}
5488
5489static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5490{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005491 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005492 struct nfs4_exception exception = {
5493 .inode = state->inode,
5494 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005495 int err;
5496
5497 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005498 /* Cache the lock if possible... */
5499 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5500 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005501 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005502 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
Trond Myklebust168667c2010-10-19 19:47:49 -04005503 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00005504 break;
5505 nfs4_handle_exception(server, err, &exception);
5506 } while (exception.retry);
5507 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508}
5509
5510static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5511{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005512 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005513 struct nfs4_exception exception = {
5514 .inode = state->inode,
5515 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005516 int err;
5517
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005518 err = nfs4_set_lock_state(state, request);
5519 if (err != 0)
5520 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00005521 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005522 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5523 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005524 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005525 trace_nfs4_lock_expired(request, state, F_SETLK, err);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005526 switch (err) {
5527 default:
5528 goto out;
5529 case -NFS4ERR_GRACE:
5530 case -NFS4ERR_DELAY:
5531 nfs4_handle_exception(server, err, &exception);
5532 err = 0;
5533 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005534 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005535out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00005536 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537}
5538
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005539#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005540/**
5541 * nfs41_check_expired_locks - possibly free a lock stateid
5542 *
5543 * @state: NFSv4 state for an inode
5544 *
5545 * Returns NFS_OK if recovery for this stateid is now finished.
5546 * Otherwise a negative NFS4ERR value is returned.
5547 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005548static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005549{
Chuck Levereb64cf92012-07-11 16:30:05 -04005550 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005551 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005552 struct nfs_server *server = NFS_SERVER(state->inode);
5553
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005554 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04005555 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005556 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
5557
5558 status = nfs41_test_stateid(server,
5559 &lsp->ls_stateid,
5560 cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04005561 trace_nfs4_test_lock_stateid(state, lsp, status);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005562 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005563 /* Free the stateid unless the server
5564 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04005565 if (status != -NFS4ERR_BAD_STATEID)
5566 nfs41_free_stateid(server,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005567 &lsp->ls_stateid,
5568 cred);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005569 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005570 ret = status;
5571 }
5572 }
5573 };
5574
5575 return ret;
5576}
5577
5578static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5579{
5580 int status = NFS_OK;
5581
5582 if (test_bit(LK_STATE_IN_USE, &state->flags))
5583 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005584 if (status != NFS_OK)
5585 status = nfs4_lock_expired(state, request);
5586 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005587}
5588#endif
5589
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5591{
Trond Myklebust9a99af492013-02-04 20:17:49 -05005592 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005593 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005594 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005595 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005596 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005598 if ((fl_flags & FL_POSIX) &&
5599 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5600 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005601 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005602 status = nfs4_set_lock_state(state, request);
5603 if (status != 0)
5604 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005605 request->fl_flags |= FL_ACCESS;
5606 status = do_vfs_lock(request->fl_file, request);
5607 if (status < 0)
5608 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005609 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005610 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005611 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005612 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005613 request->fl_flags = fl_flags & ~FL_SLEEP;
5614 status = do_vfs_lock(request->fl_file, request);
5615 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005616 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05005617 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5618 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005619 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005620 if (status != 0)
Trond Myklebust9a99af492013-02-04 20:17:49 -05005621 goto out;
5622 down_read(&nfsi->rwsem);
5623 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5624 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005625 goto out_unlock;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005626 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005627 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005628 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005629 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005630 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5631 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005632out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005633 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005634out:
5635 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636 return status;
5637}
5638
5639static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5640{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005641 struct nfs4_exception exception = {
5642 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005643 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005644 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645 int err;
5646
5647 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005648 err = _nfs4_proc_setlk(state, cmd, request);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005649 trace_nfs4_set_lock(request, state, cmd, err);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005650 if (err == -NFS4ERR_DENIED)
5651 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005653 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654 } while (exception.retry);
5655 return err;
5656}
5657
5658static int
5659nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5660{
5661 struct nfs_open_context *ctx;
5662 struct nfs4_state *state;
5663 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5664 int status;
5665
5666 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005667 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668 state = ctx->state;
5669
5670 if (request->fl_start < 0 || request->fl_end < 0)
5671 return -EINVAL;
5672
Trond Myklebustd9531262009-07-21 19:22:38 -04005673 if (IS_GETLK(cmd)) {
5674 if (state != NULL)
5675 return nfs4_proc_getlk(state, F_GETLK, request);
5676 return 0;
5677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678
5679 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5680 return -EINVAL;
5681
Trond Myklebustd9531262009-07-21 19:22:38 -04005682 if (request->fl_type == F_UNLCK) {
5683 if (state != NULL)
5684 return nfs4_proc_unlck(state, cmd, request);
5685 return 0;
5686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687
Trond Myklebustd9531262009-07-21 19:22:38 -04005688 if (state == NULL)
5689 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005690 /*
5691 * Don't rely on the VFS having checked the file open mode,
5692 * since it won't do this for flock() locks.
5693 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005694 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005695 case F_RDLCK:
5696 if (!(filp->f_mode & FMODE_READ))
5697 return -EBADF;
5698 break;
5699 case F_WRLCK:
5700 if (!(filp->f_mode & FMODE_WRITE))
5701 return -EBADF;
5702 }
5703
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704 do {
5705 status = nfs4_proc_setlk(state, cmd, request);
5706 if ((status != -EAGAIN) || IS_SETLK(cmd))
5707 break;
5708 timeout = nfs4_set_lock_task_retry(timeout);
5709 status = -ERESTARTSYS;
5710 if (signalled())
5711 break;
5712 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713 return status;
5714}
5715
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005716int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05005717{
5718 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05005719 int err;
5720
5721 err = nfs4_set_lock_state(state, fl);
5722 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005723 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04005724 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005725 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05005726}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005727
Trond Myklebustcf470c32012-03-07 13:49:12 -05005728struct nfs_release_lockowner_data {
5729 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005730 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005731 struct nfs_release_lockowner_args args;
5732};
5733
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005734static void nfs4_release_lockowner_release(void *calldata)
5735{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005736 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005737 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005738 kfree(calldata);
5739}
5740
Trond Myklebust17280172012-03-11 13:11:00 -04005741static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005742 .rpc_release = nfs4_release_lockowner_release,
5743};
5744
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04005745static int nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005746{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005747 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005748 struct rpc_message msg = {
5749 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5750 };
5751
5752 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005753 return -EINVAL;
5754 data = kmalloc(sizeof(*data), GFP_NOFS);
5755 if (!data)
5756 return -ENOMEM;
5757 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005758 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005759 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5760 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5761 data->args.lock_owner.s_dev = server->s_dev;
5762 msg.rpc_argp = &data->args;
5763 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5764 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005765}
5766
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005767#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5768
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005769static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5770 const void *buf, size_t buflen,
5771 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005772{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005773 if (strcmp(key, "") != 0)
5774 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005775
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005776 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005777}
5778
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005779static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5780 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005781{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005782 if (strcmp(key, "") != 0)
5783 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005784
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005785 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005786}
5787
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005788static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5789 size_t list_len, const char *name,
5790 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005791{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005792 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005793
J. Bruce Fields096455a2006-03-20 23:23:42 -05005794 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5795 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005796
5797 if (list && len <= list_len)
5798 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005799 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005800}
5801
David Quigleyc9bccef2013-05-22 12:50:45 -04005802#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5803static inline int nfs4_server_supports_labels(struct nfs_server *server)
5804{
5805 return server->caps & NFS_CAP_SECURITY_LABEL;
5806}
5807
5808static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
5809 const void *buf, size_t buflen,
5810 int flags, int type)
5811{
5812 if (security_ismaclabel(key))
5813 return nfs4_set_security_label(dentry, buf, buflen);
5814
5815 return -EOPNOTSUPP;
5816}
5817
5818static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
5819 void *buf, size_t buflen, int type)
5820{
5821 if (security_ismaclabel(key))
5822 return nfs4_get_security_label(dentry->d_inode, buf, buflen);
5823 return -EOPNOTSUPP;
5824}
5825
5826static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
5827 size_t list_len, const char *name,
5828 size_t name_len, int type)
5829{
5830 size_t len = 0;
5831
5832 if (nfs_server_capable(dentry->d_inode, NFS_CAP_SECURITY_LABEL)) {
5833 len = security_inode_listsecurity(dentry->d_inode, NULL, 0);
5834 if (list && len <= list_len)
5835 security_inode_listsecurity(dentry->d_inode, list, len);
5836 }
5837 return len;
5838}
5839
5840static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
5841 .prefix = XATTR_SECURITY_PREFIX,
5842 .list = nfs4_xattr_list_nfs4_label,
5843 .get = nfs4_xattr_get_nfs4_label,
5844 .set = nfs4_xattr_set_nfs4_label,
5845};
5846#endif
5847
5848
Andy Adamson533eb462011-06-13 18:25:56 -04005849/*
5850 * nfs_fhget will use either the mounted_on_fileid or the fileid
5851 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005852static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5853{
Andy Adamson533eb462011-06-13 18:25:56 -04005854 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5855 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5856 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005857 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005858 return;
5859
5860 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005861 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005862 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5863 fattr->nlink = 2;
5864}
5865
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005866static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5867 const struct qstr *name,
5868 struct nfs4_fs_locations *fs_locations,
5869 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005870{
5871 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04005872 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005873 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005874 };
5875 struct nfs4_fs_locations_arg args = {
5876 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005877 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005878 .page = page,
5879 .bitmask = bitmask,
5880 };
Benny Halevy22958462009-04-01 09:22:02 -04005881 struct nfs4_fs_locations_res res = {
5882 .fs_locations = fs_locations,
5883 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005884 struct rpc_message msg = {
5885 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5886 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005887 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005888 };
5889 int status;
5890
Harvey Harrison3110ff82008-05-02 13:42:44 -07005891 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005892
5893 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5894 * is not supported */
5895 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5896 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5897 else
5898 bitmask[0] |= FATTR4_WORD0_FILEID;
5899
Trond Myklebustc228fd32007-01-13 02:28:11 -05005900 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005901 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005902 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005903 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005904 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005905 return status;
5906}
5907
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005908int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5909 const struct qstr *name,
5910 struct nfs4_fs_locations *fs_locations,
5911 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005912{
5913 struct nfs4_exception exception = { };
5914 int err;
5915 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04005916 err = _nfs4_proc_fs_locations(client, dir, name,
5917 fs_locations, page);
5918 trace_nfs4_get_fs_locations(dir, name, err);
5919 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005920 &exception);
5921 } while (exception.retry);
5922 return err;
5923}
5924
Andy Adamson5ec16a82013-08-08 10:57:55 -04005925/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04005926 * If 'use_integrity' is true and the state managment nfs_client
5927 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
5928 * and the machine credential as per RFC3530bis and RFC5661 Security
5929 * Considerations sections. Otherwise, just use the user cred with the
5930 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04005931 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04005932static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005933{
5934 int status;
5935 struct nfs4_secinfo_arg args = {
5936 .dir_fh = NFS_FH(dir),
5937 .name = name,
5938 };
5939 struct nfs4_secinfo_res res = {
5940 .flavors = flavors,
5941 };
5942 struct rpc_message msg = {
5943 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5944 .rpc_argp = &args,
5945 .rpc_resp = &res,
5946 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04005947 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
5948
5949 if (use_integrity) {
5950 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
5951 msg.rpc_cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
5952 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005953
5954 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04005955 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
5956 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005957 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04005958
5959 if (msg.rpc_cred)
5960 put_rpccred(msg.rpc_cred);
5961
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005962 return status;
5963}
5964
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005965int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5966 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005967{
5968 struct nfs4_exception exception = { };
5969 int err;
5970 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04005971 err = -NFS4ERR_WRONGSEC;
5972
5973 /* try to use integrity protection with machine cred */
5974 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
5975 err = _nfs4_proc_secinfo(dir, name, flavors, true);
5976
5977 /*
5978 * if unable to use integrity protection, or SECINFO with
5979 * integrity protection returns NFS4ERR_WRONGSEC (which is
5980 * disallowed by spec, but exists in deployed servers) use
5981 * the current filesystem's rpc_client and the user cred.
5982 */
5983 if (err == -NFS4ERR_WRONGSEC)
5984 err = _nfs4_proc_secinfo(dir, name, flavors, false);
5985
Trond Myklebust078ea3d2013-08-12 16:45:55 -04005986 trace_nfs4_secinfo(dir, name, err);
5987 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005988 &exception);
5989 } while (exception.retry);
5990 return err;
5991}
5992
Andy Adamson557134a2009-04-01 09:21:53 -04005993#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005994/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005995 * Check the exchange flags returned by the server for invalid flags, having
5996 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5997 * DS flags set.
5998 */
5999static int nfs4_check_cl_exchange_flags(u32 flags)
6000{
6001 if (flags & ~EXCHGID4_FLAG_MASK_R)
6002 goto out_inval;
6003 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6004 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6005 goto out_inval;
6006 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6007 goto out_inval;
6008 return NFS_OK;
6009out_inval:
6010 return -NFS4ERR_INVAL;
6011}
6012
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006013static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006014nfs41_same_server_scope(struct nfs41_server_scope *a,
6015 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006016{
6017 if (a->server_scope_sz == b->server_scope_sz &&
6018 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6019 return true;
6020
6021 return false;
6022}
6023
Andy Adamson357f54d2010-12-14 10:11:57 -05006024/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006025 * nfs4_proc_bind_conn_to_session()
6026 *
6027 * The 4.1 client currently uses the same TCP connection for the
6028 * fore and backchannel.
6029 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006030int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006031{
6032 int status;
6033 struct nfs41_bind_conn_to_session_res res;
6034 struct rpc_message msg = {
6035 .rpc_proc =
6036 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
6037 .rpc_argp = clp,
6038 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006039 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006040 };
6041
6042 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006043
6044 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
6045 if (unlikely(res.session == NULL)) {
6046 status = -ENOMEM;
6047 goto out;
6048 }
6049
6050 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006051 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006052 if (status == 0) {
6053 if (memcmp(res.session->sess_id.data,
6054 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6055 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6056 status = -EIO;
6057 goto out_session;
6058 }
6059 if (res.dir != NFS4_CDFS4_BOTH) {
6060 dprintk("NFS: %s: Unexpected direction from server\n",
6061 __func__);
6062 status = -EIO;
6063 goto out_session;
6064 }
6065 if (res.use_conn_in_rdma_mode) {
6066 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6067 __func__);
6068 status = -EIO;
6069 goto out_session;
6070 }
6071 }
6072out_session:
6073 kfree(res.session);
6074out:
6075 dprintk("<-- %s status= %d\n", __func__, status);
6076 return status;
6077}
6078
6079/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04006080 * nfs4_proc_exchange_id()
6081 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006082 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6083 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04006084 * Since the clientid has expired, all compounds using sessions
6085 * associated with the stale clientid will be returning
6086 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
6087 * be in some phase of session reset.
6088 */
Andy Adamson4d643d12009-12-04 15:52:24 -05006089int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04006090{
6091 nfs4_verifier verifier;
6092 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05006093 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04006094 .client = clp,
Trond Myklebust4f0b4292013-05-20 12:24:03 -04006095 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6096 EXCHGID4_FLAG_BIND_PRINC_STATEID,
Benny Halevy99fe60d2009-04-01 09:22:29 -04006097 };
6098 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04006099 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04006100 };
6101 int status;
6102 struct rpc_message msg = {
6103 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
6104 .rpc_argp = &args,
6105 .rpc_resp = &res,
6106 .rpc_cred = cred,
6107 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04006108
Chuck Leverf0920752012-05-21 22:45:41 -04006109 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04006110 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
6111 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006112 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
6113 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6114 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006115
Chuck Leveracdeb692012-05-21 22:46:16 -04006116 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006117 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04006118 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006119 status = -ENOMEM;
6120 goto out;
6121 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006122
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006123 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006124 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006125 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04006126 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04006127 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04006128 }
6129
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006130 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006131 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006132 status = -ENOMEM;
6133 goto out_server_scope;
6134 }
6135
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006136 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006137 trace_nfs4_exchange_id(clp, status);
Chuck Lever177313f2012-05-21 22:44:58 -04006138 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04006139 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006140
Chuck Lever177313f2012-05-21 22:44:58 -04006141 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04006142 clp->cl_clientid = res.clientid;
6143 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
6144 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
6145 clp->cl_seqid = res.seqid;
6146
Chuck Leveracdeb692012-05-21 22:46:16 -04006147 kfree(clp->cl_serverowner);
6148 clp->cl_serverowner = res.server_owner;
6149 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04006150
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006151 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04006152 kfree(clp->cl_implid);
6153 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006154
Chuck Lever177313f2012-05-21 22:44:58 -04006155 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006156 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006157 res.server_scope)) {
6158 dprintk("%s: server_scope mismatch detected\n",
6159 __func__);
6160 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006161 kfree(clp->cl_serverscope);
6162 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006163 }
6164
Chuck Lever177313f2012-05-21 22:44:58 -04006165 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006166 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006167 goto out;
6168 }
Trond Myklebust32b01312012-05-26 13:41:04 -04006169 } else
6170 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006171
Chuck Leveracdeb692012-05-21 22:46:16 -04006172out_server_owner:
6173 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006174out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006175 kfree(res.server_scope);
6176out:
Chuck Lever177313f2012-05-21 22:44:58 -04006177 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006178 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006179 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006180 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04006181 clp->cl_implid->date.seconds,
6182 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006183 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006184 return status;
6185}
6186
Trond Myklebust66245532012-05-25 17:18:09 -04006187static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
6188 struct rpc_cred *cred)
6189{
6190 struct rpc_message msg = {
6191 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
6192 .rpc_argp = clp,
6193 .rpc_cred = cred,
6194 };
6195 int status;
6196
6197 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006198 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04006199 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04006200 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04006201 "DESTROY_CLIENTID.", status, clp->cl_hostname);
6202 return status;
6203}
6204
6205static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
6206 struct rpc_cred *cred)
6207{
6208 unsigned int loop;
6209 int ret;
6210
6211 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
6212 ret = _nfs4_proc_destroy_clientid(clp, cred);
6213 switch (ret) {
6214 case -NFS4ERR_DELAY:
6215 case -NFS4ERR_CLIENTID_BUSY:
6216 ssleep(1);
6217 break;
6218 default:
6219 return ret;
6220 }
6221 }
6222 return 0;
6223}
6224
6225int nfs4_destroy_clientid(struct nfs_client *clp)
6226{
6227 struct rpc_cred *cred;
6228 int ret = 0;
6229
6230 if (clp->cl_mvops->minor_version < 1)
6231 goto out;
6232 if (clp->cl_exchange_flags == 0)
6233 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04006234 if (clp->cl_preserve_clid)
6235 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04006236 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04006237 ret = nfs4_proc_destroy_clientid(clp, cred);
6238 if (cred)
6239 put_rpccred(cred);
6240 switch (ret) {
6241 case 0:
6242 case -NFS4ERR_STALE_CLIENTID:
6243 clp->cl_exchange_flags = 0;
6244 }
6245out:
6246 return ret;
6247}
6248
Andy Adamson2050f0c2009-04-01 09:22:30 -04006249struct nfs4_get_lease_time_data {
6250 struct nfs4_get_lease_time_args *args;
6251 struct nfs4_get_lease_time_res *res;
6252 struct nfs_client *clp;
6253};
6254
6255static void nfs4_get_lease_time_prepare(struct rpc_task *task,
6256 void *calldata)
6257{
Andy Adamson2050f0c2009-04-01 09:22:30 -04006258 struct nfs4_get_lease_time_data *data =
6259 (struct nfs4_get_lease_time_data *)calldata;
6260
6261 dprintk("--> %s\n", __func__);
6262 /* just setup sequence, do not trigger session recovery
6263 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006264 nfs41_setup_sequence(data->clp->cl_session,
6265 &data->args->la_seq_args,
6266 &data->res->lr_seq_res,
6267 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006268 dprintk("<-- %s\n", __func__);
6269}
6270
6271/*
6272 * Called from nfs4_state_manager thread for session setup, so don't recover
6273 * from sequence operation or clientid errors.
6274 */
6275static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
6276{
6277 struct nfs4_get_lease_time_data *data =
6278 (struct nfs4_get_lease_time_data *)calldata;
6279
6280 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006281 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
6282 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04006283 switch (task->tk_status) {
6284 case -NFS4ERR_DELAY:
6285 case -NFS4ERR_GRACE:
6286 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
6287 rpc_delay(task, NFS4_POLL_RETRY_MIN);
6288 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006289 /* fall through */
6290 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006291 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006292 return;
6293 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04006294 dprintk("<-- %s\n", __func__);
6295}
6296
Trond Myklebust17280172012-03-11 13:11:00 -04006297static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04006298 .rpc_call_prepare = nfs4_get_lease_time_prepare,
6299 .rpc_call_done = nfs4_get_lease_time_done,
6300};
6301
6302int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
6303{
6304 struct rpc_task *task;
6305 struct nfs4_get_lease_time_args args;
6306 struct nfs4_get_lease_time_res res = {
6307 .lr_fsinfo = fsinfo,
6308 };
6309 struct nfs4_get_lease_time_data data = {
6310 .args = &args,
6311 .res = &res,
6312 .clp = clp,
6313 };
6314 struct rpc_message msg = {
6315 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
6316 .rpc_argp = &args,
6317 .rpc_resp = &res,
6318 };
6319 struct rpc_task_setup task_setup = {
6320 .rpc_client = clp->cl_rpcclient,
6321 .rpc_message = &msg,
6322 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006323 .callback_data = &data,
6324 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04006325 };
6326 int status;
6327
Chuck Levera9c92d62013-08-09 12:48:18 -04006328 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006329 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006330 dprintk("--> %s\n", __func__);
6331 task = rpc_run_task(&task_setup);
6332
6333 if (IS_ERR(task))
6334 status = PTR_ERR(task);
6335 else {
6336 status = task->tk_status;
6337 rpc_put_task(task);
6338 }
6339 dprintk("<-- %s return %d\n", __func__, status);
6340
6341 return status;
6342}
6343
Andy Adamsonfc931582009-04-01 09:22:31 -04006344/*
6345 * Initialize the values to be used by the client in CREATE_SESSION
6346 * If nfs4_init_session set the fore channel request and response sizes,
6347 * use them.
6348 *
6349 * Set the back channel max_resp_sz_cached to zero to force the client to
6350 * always set csa_cachethis to FALSE because the current implementation
6351 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
6352 */
6353static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
6354{
Andy Adamson18aad3d2013-06-26 12:21:49 -04006355 unsigned int max_rqst_sz, max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006356
Andy Adamson18aad3d2013-06-26 12:21:49 -04006357 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
6358 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
6359
Andy Adamsonfc931582009-04-01 09:22:31 -04006360 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04006361 args->fc_attrs.max_rqst_sz = max_rqst_sz;
6362 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006363 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05006364 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04006365
6366 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05006367 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04006368 __func__,
6369 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05006370 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04006371
6372 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04006373 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
6374 args->bc_attrs.max_resp_sz = PAGE_SIZE;
6375 args->bc_attrs.max_resp_sz_cached = 0;
6376 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
6377 args->bc_attrs.max_reqs = 1;
6378
6379 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
6380 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
6381 __func__,
6382 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
6383 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
6384 args->bc_attrs.max_reqs);
6385}
6386
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006387static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04006388{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006389 struct nfs4_channel_attrs *sent = &args->fc_attrs;
6390 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
6391
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006392 if (rcvd->max_resp_sz > sent->max_resp_sz)
6393 return -EINVAL;
6394 /*
6395 * Our requested max_ops is the minimum we need; we're not
6396 * prepared to break up compounds into smaller pieces than that.
6397 * So, no point even trying to continue if the server won't
6398 * cooperate:
6399 */
6400 if (rcvd->max_ops < sent->max_ops)
6401 return -EINVAL;
6402 if (rcvd->max_reqs == 0)
6403 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006404 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
6405 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006406 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04006407}
6408
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006409static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
6410{
6411 struct nfs4_channel_attrs *sent = &args->bc_attrs;
6412 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04006413
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006414 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
6415 return -EINVAL;
6416 if (rcvd->max_resp_sz < sent->max_resp_sz)
6417 return -EINVAL;
6418 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
6419 return -EINVAL;
6420 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006421 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006422 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006423 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006424 return -EINVAL;
6425 return 0;
6426}
Andy Adamson8d353012009-04-01 09:22:32 -04006427
Andy Adamson8d353012009-04-01 09:22:32 -04006428static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
6429 struct nfs4_session *session)
6430{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006431 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04006432
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006433 ret = nfs4_verify_fore_channel_attrs(args, session);
6434 if (ret)
6435 return ret;
6436 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04006437}
6438
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006439static int _nfs4_proc_create_session(struct nfs_client *clp,
6440 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04006441{
6442 struct nfs4_session *session = clp->cl_session;
6443 struct nfs41_create_session_args args = {
6444 .client = clp,
6445 .cb_program = NFS4_CALLBACK,
6446 };
6447 struct nfs41_create_session_res res = {
6448 .client = clp,
6449 };
6450 struct rpc_message msg = {
6451 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
6452 .rpc_argp = &args,
6453 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006454 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04006455 };
6456 int status;
6457
6458 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04006459 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04006460
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006461 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006462 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04006463
Trond Myklebust43095d32012-11-20 11:13:12 -05006464 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04006465 /* Verify the session's negotiated channel_attrs values */
6466 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04006467 /* Increment the clientid slot sequence id */
6468 clp->cl_seqid++;
6469 }
6470
6471 return status;
6472}
6473
6474/*
6475 * Issues a CREATE_SESSION operation to the server.
6476 * It is the responsibility of the caller to verify the session is
6477 * expired before calling this routine.
6478 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006479int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04006480{
6481 int status;
6482 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04006483 struct nfs4_session *session = clp->cl_session;
6484
6485 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
6486
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006487 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04006488 if (status)
6489 goto out;
6490
Andy Adamsonaacd5532011-11-09 13:58:21 -05006491 /* Init or reset the session slot tables */
6492 status = nfs4_setup_session_slot_tables(session);
6493 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04006494 if (status)
6495 goto out;
6496
6497 ptr = (unsigned *)&session->sess_id.data[0];
6498 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
6499 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04006500out:
6501 dprintk("<-- %s\n", __func__);
6502 return status;
6503}
6504
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006505/*
6506 * Issue the over-the-wire RPC DESTROY_SESSION.
6507 * The caller must serialize access to this routine.
6508 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006509int nfs4_proc_destroy_session(struct nfs4_session *session,
6510 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006511{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006512 struct rpc_message msg = {
6513 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
6514 .rpc_argp = session,
6515 .rpc_cred = cred,
6516 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006517 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006518
6519 dprintk("--> nfs4_proc_destroy_session\n");
6520
6521 /* session is still being setup */
6522 if (session->clp->cl_cons_state != NFS_CS_READY)
6523 return status;
6524
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006525 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006526 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006527
6528 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04006529 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006530 "Session has been destroyed regardless...\n", status);
6531
6532 dprintk("<-- nfs4_proc_destroy_session\n");
6533 return status;
6534}
6535
Trond Myklebust7b38c362012-05-23 13:23:31 -04006536/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006537 * Renew the cl_session lease.
6538 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006539struct nfs4_sequence_data {
6540 struct nfs_client *clp;
6541 struct nfs4_sequence_args args;
6542 struct nfs4_sequence_res res;
6543};
6544
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006545static void nfs41_sequence_release(void *data)
6546{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006547 struct nfs4_sequence_data *calldata = data;
6548 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006549
Alexandros Batsakis71358402010-02-05 03:45:05 -08006550 if (atomic_read(&clp->cl_count) > 1)
6551 nfs4_schedule_state_renewal(clp);
6552 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006553 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006554}
6555
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006556static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6557{
6558 switch(task->tk_status) {
6559 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006560 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6561 return -EAGAIN;
6562 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006563 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006564 }
6565 return 0;
6566}
6567
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006568static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006569{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006570 struct nfs4_sequence_data *calldata = data;
6571 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006572
Trond Myklebust14516c32010-07-31 14:29:06 -04006573 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6574 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006575
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006576 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006577 if (task->tk_status < 0) {
6578 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006579 if (atomic_read(&clp->cl_count) == 1)
6580 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006581
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006582 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6583 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006584 return;
6585 }
6586 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006587 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006588out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006589 dprintk("<-- %s\n", __func__);
6590}
6591
6592static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6593{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006594 struct nfs4_sequence_data *calldata = data;
6595 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006596 struct nfs4_sequence_args *args;
6597 struct nfs4_sequence_res *res;
6598
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006599 args = task->tk_msg.rpc_argp;
6600 res = task->tk_msg.rpc_resp;
6601
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006602 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006603}
6604
6605static const struct rpc_call_ops nfs41_sequence_ops = {
6606 .rpc_call_done = nfs41_sequence_call_done,
6607 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006608 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006609};
6610
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006611static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
6612 struct rpc_cred *cred,
6613 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006614{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006615 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006616 struct rpc_message msg = {
6617 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6618 .rpc_cred = cred,
6619 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006620 struct rpc_task_setup task_setup_data = {
6621 .rpc_client = clp->cl_rpcclient,
6622 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006623 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04006624 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006625 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006626
Alexandros Batsakis71358402010-02-05 03:45:05 -08006627 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006628 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006629 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006630 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006631 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006632 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006633 }
Chuck Levera9c92d62013-08-09 12:48:18 -04006634 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006635 if (is_privileged)
6636 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006637 msg.rpc_argp = &calldata->args;
6638 msg.rpc_resp = &calldata->res;
6639 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006640 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006641
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006642 return rpc_run_task(&task_setup_data);
6643}
6644
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006645static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006646{
6647 struct rpc_task *task;
6648 int ret = 0;
6649
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006650 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6651 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006652 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006653 if (IS_ERR(task))
6654 ret = PTR_ERR(task);
6655 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006656 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006657 dprintk("<-- %s status=%d\n", __func__, ret);
6658 return ret;
6659}
6660
6661static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6662{
6663 struct rpc_task *task;
6664 int ret;
6665
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006666 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006667 if (IS_ERR(task)) {
6668 ret = PTR_ERR(task);
6669 goto out;
6670 }
6671 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006672 if (!ret) {
6673 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6674
6675 if (task->tk_status == 0)
6676 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006677 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006678 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006679 rpc_put_task(task);
6680out:
6681 dprintk("<-- %s status=%d\n", __func__, ret);
6682 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006683}
6684
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006685struct nfs4_reclaim_complete_data {
6686 struct nfs_client *clp;
6687 struct nfs41_reclaim_complete_args arg;
6688 struct nfs41_reclaim_complete_res res;
6689};
6690
6691static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6692{
6693 struct nfs4_reclaim_complete_data *calldata = data;
6694
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006695 nfs41_setup_sequence(calldata->clp->cl_session,
6696 &calldata->arg.seq_args,
6697 &calldata->res.seq_res,
6698 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006699}
6700
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006701static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6702{
6703 switch(task->tk_status) {
6704 case 0:
6705 case -NFS4ERR_COMPLETE_ALREADY:
6706 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6707 break;
6708 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006709 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006710 /* fall through */
6711 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006712 return -EAGAIN;
6713 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006714 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006715 }
6716 return 0;
6717}
6718
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006719static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6720{
6721 struct nfs4_reclaim_complete_data *calldata = data;
6722 struct nfs_client *clp = calldata->clp;
6723 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6724
6725 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006726 if (!nfs41_sequence_done(task, res))
6727 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006728
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006729 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006730 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6731 rpc_restart_call_prepare(task);
6732 return;
6733 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006734 dprintk("<-- %s\n", __func__);
6735}
6736
6737static void nfs4_free_reclaim_complete_data(void *data)
6738{
6739 struct nfs4_reclaim_complete_data *calldata = data;
6740
6741 kfree(calldata);
6742}
6743
6744static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6745 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6746 .rpc_call_done = nfs4_reclaim_complete_done,
6747 .rpc_release = nfs4_free_reclaim_complete_data,
6748};
6749
6750/*
6751 * Issue a global reclaim complete.
6752 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04006753static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
6754 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006755{
6756 struct nfs4_reclaim_complete_data *calldata;
6757 struct rpc_task *task;
6758 struct rpc_message msg = {
6759 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04006760 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006761 };
6762 struct rpc_task_setup task_setup_data = {
6763 .rpc_client = clp->cl_rpcclient,
6764 .rpc_message = &msg,
6765 .callback_ops = &nfs4_reclaim_complete_call_ops,
6766 .flags = RPC_TASK_ASYNC,
6767 };
6768 int status = -ENOMEM;
6769
6770 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006771 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006772 if (calldata == NULL)
6773 goto out;
6774 calldata->clp = clp;
6775 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006776
Chuck Levera9c92d62013-08-09 12:48:18 -04006777 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006778 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006779 msg.rpc_argp = &calldata->arg;
6780 msg.rpc_resp = &calldata->res;
6781 task_setup_data.callback_data = calldata;
6782 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006783 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006784 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006785 goto out;
6786 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006787 status = nfs4_wait_for_completion_rpc_task(task);
6788 if (status == 0)
6789 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006790 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006791 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006792out:
6793 dprintk("<-- %s status=%d\n", __func__, status);
6794 return status;
6795}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006796
6797static void
6798nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6799{
6800 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006801 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006802 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006803
6804 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006805 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6806 * right now covering the LAYOUTGET we are about to send.
6807 * However, that is not so catastrophic, and there seems
6808 * to be no way to prevent it completely.
6809 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006810 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006811 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006812 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006813 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6814 NFS_I(lgp->args.inode)->layout,
6815 lgp->args.ctx->state)) {
6816 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00006817 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006818}
6819
6820static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6821{
6822 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006823 struct inode *inode = lgp->args.inode;
6824 struct nfs_server *server = NFS_SERVER(inode);
6825 struct pnfs_layout_hdr *lo;
6826 struct nfs4_state *state = NULL;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006827 unsigned long timeo, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006828
6829 dprintk("--> %s\n", __func__);
6830
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006831 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006832 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006833
6834 switch (task->tk_status) {
6835 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006836 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006837 case -NFS4ERR_LAYOUTTRYLATER:
6838 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05006839 timeo = rpc_get_timeout(task->tk_client);
6840 giveup = lgp->args.timestamp + timeo;
6841 if (time_after(giveup, jiffies))
6842 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006843 break;
6844 case -NFS4ERR_EXPIRED:
6845 case -NFS4ERR_BAD_STATEID:
6846 spin_lock(&inode->i_lock);
6847 lo = NFS_I(inode)->layout;
6848 if (!lo || list_empty(&lo->plh_segs)) {
6849 spin_unlock(&inode->i_lock);
6850 /* If the open stateid was bad, then recover it. */
6851 state = lgp->args.ctx->state;
6852 } else {
6853 LIST_HEAD(head);
6854
6855 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6856 spin_unlock(&inode->i_lock);
6857 /* Mark the bad layout state as invalid, then
6858 * retry using the open stateid. */
6859 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006860 }
6861 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006862 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6863 rpc_restart_call_prepare(task);
6864out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006865 dprintk("<-- %s\n", __func__);
6866}
6867
Idan Kedar85541162012-08-02 11:47:10 +03006868static size_t max_response_pages(struct nfs_server *server)
6869{
6870 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6871 return nfs_page_array_len(0, max_resp_sz);
6872}
6873
6874static void nfs4_free_pages(struct page **pages, size_t size)
6875{
6876 int i;
6877
6878 if (!pages)
6879 return;
6880
6881 for (i = 0; i < size; i++) {
6882 if (!pages[i])
6883 break;
6884 __free_page(pages[i]);
6885 }
6886 kfree(pages);
6887}
6888
6889static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6890{
6891 struct page **pages;
6892 int i;
6893
6894 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6895 if (!pages) {
6896 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6897 return NULL;
6898 }
6899
6900 for (i = 0; i < size; i++) {
6901 pages[i] = alloc_page(gfp_flags);
6902 if (!pages[i]) {
6903 dprintk("%s: failed to allocate page\n", __func__);
6904 nfs4_free_pages(pages, size);
6905 return NULL;
6906 }
6907 }
6908
6909 return pages;
6910}
6911
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006912static void nfs4_layoutget_release(void *calldata)
6913{
6914 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006915 struct inode *inode = lgp->args.inode;
6916 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006917 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006918
6919 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006920 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006921 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006922 put_nfs_open_context(lgp->args.ctx);
6923 kfree(calldata);
6924 dprintk("<-- %s\n", __func__);
6925}
6926
6927static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6928 .rpc_call_prepare = nfs4_layoutget_prepare,
6929 .rpc_call_done = nfs4_layoutget_done,
6930 .rpc_release = nfs4_layoutget_release,
6931};
6932
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006933struct pnfs_layout_segment *
6934nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006935{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006936 struct inode *inode = lgp->args.inode;
6937 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006938 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006939 struct rpc_task *task;
6940 struct rpc_message msg = {
6941 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6942 .rpc_argp = &lgp->args,
6943 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04006944 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006945 };
6946 struct rpc_task_setup task_setup_data = {
6947 .rpc_client = server->client,
6948 .rpc_message = &msg,
6949 .callback_ops = &nfs4_layoutget_call_ops,
6950 .callback_data = lgp,
6951 .flags = RPC_TASK_ASYNC,
6952 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006953 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006954 int status = 0;
6955
6956 dprintk("--> %s\n", __func__);
6957
Idan Kedar85541162012-08-02 11:47:10 +03006958 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6959 if (!lgp->args.layout.pages) {
6960 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006961 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006962 }
6963 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006964 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03006965
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006966 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006967 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04006968 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006969
6970 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
6971 pnfs_get_layout_hdr(NFS_I(inode)->layout);
6972
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006973 task = rpc_run_task(&task_setup_data);
6974 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006975 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006976 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006977 if (status == 0)
6978 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04006979 trace_nfs4_layoutget(lgp->args.ctx,
6980 &lgp->args.range,
6981 &lgp->res.range,
6982 status);
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05006983 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
6984 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006985 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006986 rpc_put_task(task);
6987 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006988 if (status)
6989 return ERR_PTR(status);
6990 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006991}
6992
Benny Halevycbe82602011-05-22 19:52:37 +03006993static void
6994nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6995{
6996 struct nfs4_layoutreturn *lrp = calldata;
6997
6998 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006999 nfs41_setup_sequence(lrp->clp->cl_session,
7000 &lrp->args.seq_args,
7001 &lrp->res.seq_res,
7002 task);
Benny Halevycbe82602011-05-22 19:52:37 +03007003}
7004
7005static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
7006{
7007 struct nfs4_layoutreturn *lrp = calldata;
7008 struct nfs_server *server;
7009
7010 dprintk("--> %s\n", __func__);
7011
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007012 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03007013 return;
7014
7015 server = NFS_SERVER(lrp->args.inode);
7016 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007017 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03007018 return;
7019 }
Benny Halevycbe82602011-05-22 19:52:37 +03007020 dprintk("<-- %s\n", __func__);
7021}
7022
7023static void nfs4_layoutreturn_release(void *calldata)
7024{
7025 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04007026 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03007027
7028 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04007029 spin_lock(&lo->plh_inode->i_lock);
7030 if (lrp->res.lrs_present)
7031 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
7032 lo->plh_block_lgets--;
7033 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04007034 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03007035 kfree(calldata);
7036 dprintk("<-- %s\n", __func__);
7037}
7038
7039static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
7040 .rpc_call_prepare = nfs4_layoutreturn_prepare,
7041 .rpc_call_done = nfs4_layoutreturn_done,
7042 .rpc_release = nfs4_layoutreturn_release,
7043};
7044
7045int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
7046{
7047 struct rpc_task *task;
7048 struct rpc_message msg = {
7049 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
7050 .rpc_argp = &lrp->args,
7051 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04007052 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03007053 };
7054 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04007055 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03007056 .rpc_message = &msg,
7057 .callback_ops = &nfs4_layoutreturn_call_ops,
7058 .callback_data = lrp,
7059 };
7060 int status;
7061
7062 dprintk("--> %s\n", __func__);
Chuck Levera9c92d62013-08-09 12:48:18 -04007063 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03007064 task = rpc_run_task(&task_setup_data);
7065 if (IS_ERR(task))
7066 return PTR_ERR(task);
7067 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007068 trace_nfs4_layoutreturn(lrp->args.inode, status);
Benny Halevycbe82602011-05-22 19:52:37 +03007069 dprintk("<-- %s status=%d\n", __func__, status);
7070 rpc_put_task(task);
7071 return status;
7072}
7073
Andy Adamson7f11d8d2011-07-30 20:52:35 -04007074/*
7075 * Retrieve the list of Data Server devices from the MDS.
7076 */
7077static int _nfs4_getdevicelist(struct nfs_server *server,
7078 const struct nfs_fh *fh,
7079 struct pnfs_devicelist *devlist)
7080{
7081 struct nfs4_getdevicelist_args args = {
7082 .fh = fh,
7083 .layoutclass = server->pnfs_curr_ld->id,
7084 };
7085 struct nfs4_getdevicelist_res res = {
7086 .devlist = devlist,
7087 };
7088 struct rpc_message msg = {
7089 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
7090 .rpc_argp = &args,
7091 .rpc_resp = &res,
7092 };
7093 int status;
7094
7095 dprintk("--> %s\n", __func__);
7096 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
7097 &res.seq_res, 0);
7098 dprintk("<-- %s status=%d\n", __func__, status);
7099 return status;
7100}
7101
7102int nfs4_proc_getdevicelist(struct nfs_server *server,
7103 const struct nfs_fh *fh,
7104 struct pnfs_devicelist *devlist)
7105{
7106 struct nfs4_exception exception = { };
7107 int err;
7108
7109 do {
7110 err = nfs4_handle_exception(server,
7111 _nfs4_getdevicelist(server, fh, devlist),
7112 &exception);
7113 } while (exception.retry);
7114
7115 dprintk("%s: err=%d, num_devs=%u\n", __func__,
7116 err, devlist->num_devs);
7117
7118 return err;
7119}
7120EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
7121
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007122static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007123_nfs4_proc_getdeviceinfo(struct nfs_server *server,
7124 struct pnfs_device *pdev,
7125 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007126{
7127 struct nfs4_getdeviceinfo_args args = {
7128 .pdev = pdev,
7129 };
7130 struct nfs4_getdeviceinfo_res res = {
7131 .pdev = pdev,
7132 };
7133 struct rpc_message msg = {
7134 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
7135 .rpc_argp = &args,
7136 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007137 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007138 };
7139 int status;
7140
7141 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00007142 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007143 dprintk("<-- %s status=%d\n", __func__, status);
7144
7145 return status;
7146}
7147
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007148int nfs4_proc_getdeviceinfo(struct nfs_server *server,
7149 struct pnfs_device *pdev,
7150 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007151{
7152 struct nfs4_exception exception = { };
7153 int err;
7154
7155 do {
7156 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007157 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007158 &exception);
7159 } while (exception.retry);
7160 return err;
7161}
7162EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
7163
Andy Adamson863a3c62011-03-23 13:27:54 +00007164static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
7165{
7166 struct nfs4_layoutcommit_data *data = calldata;
7167 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007168 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00007169
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007170 nfs41_setup_sequence(session,
7171 &data->args.seq_args,
7172 &data->res.seq_res,
7173 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00007174}
7175
7176static void
7177nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
7178{
7179 struct nfs4_layoutcommit_data *data = calldata;
7180 struct nfs_server *server = NFS_SERVER(data->args.inode);
7181
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007182 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00007183 return;
7184
7185 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007186 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
7187 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
7188 case -NFS4ERR_BADLAYOUT: /* no layout */
7189 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00007190 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007191 break;
7192 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00007193 nfs_post_op_update_inode_force_wcc(data->args.inode,
7194 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007195 break;
7196 default:
7197 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
7198 rpc_restart_call_prepare(task);
7199 return;
7200 }
7201 }
Andy Adamson863a3c62011-03-23 13:27:54 +00007202}
7203
7204static void nfs4_layoutcommit_release(void *calldata)
7205{
7206 struct nfs4_layoutcommit_data *data = calldata;
7207
Andy Adamsondb29c082011-07-30 20:52:38 -04007208 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00007209 put_rpccred(data->cred);
7210 kfree(data);
7211}
7212
7213static const struct rpc_call_ops nfs4_layoutcommit_ops = {
7214 .rpc_call_prepare = nfs4_layoutcommit_prepare,
7215 .rpc_call_done = nfs4_layoutcommit_done,
7216 .rpc_release = nfs4_layoutcommit_release,
7217};
7218
7219int
Andy Adamsonef311532011-03-12 02:58:10 -05007220nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00007221{
7222 struct rpc_message msg = {
7223 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
7224 .rpc_argp = &data->args,
7225 .rpc_resp = &data->res,
7226 .rpc_cred = data->cred,
7227 };
7228 struct rpc_task_setup task_setup_data = {
7229 .task = &data->task,
7230 .rpc_client = NFS_CLIENT(data->args.inode),
7231 .rpc_message = &msg,
7232 .callback_ops = &nfs4_layoutcommit_ops,
7233 .callback_data = data,
7234 .flags = RPC_TASK_ASYNC,
7235 };
7236 struct rpc_task *task;
7237 int status = 0;
7238
7239 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
7240 "lbw: %llu inode %lu\n",
7241 data->task.tk_pid, sync,
7242 data->args.lastbytewritten,
7243 data->args.inode->i_ino);
7244
Chuck Levera9c92d62013-08-09 12:48:18 -04007245 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00007246 task = rpc_run_task(&task_setup_data);
7247 if (IS_ERR(task))
7248 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05007249 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00007250 goto out;
7251 status = nfs4_wait_for_completion_rpc_task(task);
7252 if (status != 0)
7253 goto out;
7254 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007255 trace_nfs4_layoutcommit(data->args.inode, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00007256out:
7257 dprintk("%s: status %d\n", __func__, status);
7258 rpc_put_task(task);
7259 return status;
7260}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007261
Andy Adamson97431202013-08-08 10:57:56 -04007262/**
7263 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
7264 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
7265 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007266static int
7267_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7268 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
7269{
7270 struct nfs41_secinfo_no_name_args args = {
7271 .style = SECINFO_STYLE_CURRENT_FH,
7272 };
7273 struct nfs4_secinfo_res res = {
7274 .flavors = flavors,
7275 };
7276 struct rpc_message msg = {
7277 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
7278 .rpc_argp = &args,
7279 .rpc_resp = &res,
7280 };
Andy Adamson97431202013-08-08 10:57:56 -04007281 return nfs4_call_sync(server->nfs_client->cl_rpcclient, server, &msg,
7282 &args.seq_args, &res.seq_res, 0);
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007283}
7284
7285static int
7286nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7287 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
7288{
7289 struct nfs4_exception exception = { };
7290 int err;
7291 do {
7292 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
7293 switch (err) {
7294 case 0:
7295 case -NFS4ERR_WRONGSEC:
7296 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007297 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007298 default:
7299 err = nfs4_handle_exception(server, err, &exception);
7300 }
7301 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007302out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007303 return err;
7304}
7305
7306static int
7307nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
7308 struct nfs_fsinfo *info)
7309{
7310 int err;
7311 struct page *page;
7312 rpc_authflavor_t flavor;
7313 struct nfs4_secinfo_flavors *flavors;
7314
7315 page = alloc_page(GFP_KERNEL);
7316 if (!page) {
7317 err = -ENOMEM;
7318 goto out;
7319 }
7320
7321 flavors = page_address(page);
7322 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
7323
7324 /*
7325 * Fall back on "guess and check" method if
7326 * the server doesn't support SECINFO_NO_NAME
7327 */
7328 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
7329 err = nfs4_find_root_sec(server, fhandle, info);
7330 goto out_freepage;
7331 }
7332 if (err)
7333 goto out_freepage;
7334
7335 flavor = nfs_find_best_sec(flavors);
7336 if (err == 0)
7337 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
7338
7339out_freepage:
7340 put_page(page);
7341 if (err == -EACCES)
7342 return -EPERM;
7343out:
7344 return err;
7345}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007346
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007347static int _nfs41_test_stateid(struct nfs_server *server,
7348 nfs4_stateid *stateid,
7349 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04007350{
7351 int status;
7352 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007353 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04007354 };
7355 struct nfs41_test_stateid_res res;
7356 struct rpc_message msg = {
7357 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
7358 .rpc_argp = &args,
7359 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007360 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04007361 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007362
Chuck Lever38527b12012-07-11 16:30:23 -04007363 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04007364 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007365 nfs4_set_sequence_privileged(&args.seq_args);
7366 status = nfs4_call_sync_sequence(server->client, server, &msg,
7367 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04007368 if (status != NFS_OK) {
7369 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04007370 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04007371 }
7372 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04007373 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04007374}
7375
Chuck Lever38527b12012-07-11 16:30:23 -04007376/**
7377 * nfs41_test_stateid - perform a TEST_STATEID operation
7378 *
7379 * @server: server / transport on which to perform the operation
7380 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007381 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04007382 *
7383 * Returns NFS_OK if the server recognizes that "stateid" is valid.
7384 * Otherwise a negative NFS4ERR value is returned if the operation
7385 * failed or the state ID is not currently valid.
7386 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007387static int nfs41_test_stateid(struct nfs_server *server,
7388 nfs4_stateid *stateid,
7389 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04007390{
7391 struct nfs4_exception exception = { };
7392 int err;
7393 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007394 err = _nfs41_test_stateid(server, stateid, cred);
Chuck Lever377e5072012-07-11 16:29:45 -04007395 if (err != -NFS4ERR_DELAY)
7396 break;
7397 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04007398 } while (exception.retry);
7399 return err;
7400}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007401
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007402struct nfs_free_stateid_data {
7403 struct nfs_server *server;
7404 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007405 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007406};
7407
7408static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
7409{
7410 struct nfs_free_stateid_data *data = calldata;
7411 nfs41_setup_sequence(nfs4_get_session(data->server),
7412 &data->args.seq_args,
7413 &data->res.seq_res,
7414 task);
7415}
7416
7417static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
7418{
7419 struct nfs_free_stateid_data *data = calldata;
7420
7421 nfs41_sequence_done(task, &data->res.seq_res);
7422
7423 switch (task->tk_status) {
7424 case -NFS4ERR_DELAY:
7425 if (nfs4_async_handle_error(task, data->server, NULL) == -EAGAIN)
7426 rpc_restart_call_prepare(task);
7427 }
7428}
7429
7430static void nfs41_free_stateid_release(void *calldata)
7431{
7432 kfree(calldata);
7433}
7434
Trond Myklebust17f26b12013-08-21 15:48:42 -04007435static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007436 .rpc_call_prepare = nfs41_free_stateid_prepare,
7437 .rpc_call_done = nfs41_free_stateid_done,
7438 .rpc_release = nfs41_free_stateid_release,
7439};
7440
7441static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
7442 nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007443 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007444 bool privileged)
7445{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007446 struct rpc_message msg = {
7447 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007448 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007449 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007450 struct rpc_task_setup task_setup = {
7451 .rpc_client = server->client,
7452 .rpc_message = &msg,
7453 .callback_ops = &nfs41_free_stateid_ops,
7454 .flags = RPC_TASK_ASYNC,
7455 };
7456 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007457
Chuck Lever38527b12012-07-11 16:30:23 -04007458 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007459 data = kmalloc(sizeof(*data), GFP_NOFS);
7460 if (!data)
7461 return ERR_PTR(-ENOMEM);
7462 data->server = server;
7463 nfs4_stateid_copy(&data->args.stateid, stateid);
7464
7465 task_setup.callback_data = data;
7466
7467 msg.rpc_argp = &data->args;
7468 msg.rpc_resp = &data->res;
Chuck Levera9c92d62013-08-09 12:48:18 -04007469 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007470 if (privileged)
7471 nfs4_set_sequence_privileged(&data->args.seq_args);
7472
7473 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007474}
7475
Chuck Lever38527b12012-07-11 16:30:23 -04007476/**
7477 * nfs41_free_stateid - perform a FREE_STATEID operation
7478 *
7479 * @server: server / transport on which to perform the operation
7480 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007481 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04007482 *
7483 * Returns NFS_OK if the server freed "stateid". Otherwise a
7484 * negative NFS4ERR value is returned.
7485 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007486static int nfs41_free_stateid(struct nfs_server *server,
7487 nfs4_stateid *stateid,
7488 struct rpc_cred *cred)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007489{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007490 struct rpc_task *task;
7491 int ret;
7492
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007493 task = _nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007494 if (IS_ERR(task))
7495 return PTR_ERR(task);
7496 ret = rpc_wait_for_completion_task(task);
7497 if (!ret)
7498 ret = task->tk_status;
7499 rpc_put_task(task);
7500 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007501}
Trond Myklebust36281ca2012-03-04 18:13:56 -05007502
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007503static int nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
7504{
7505 struct rpc_task *task;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007506 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007507
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007508 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007509 nfs4_free_lock_state(server, lsp);
7510 if (IS_ERR(task))
7511 return PTR_ERR(task);
7512 rpc_put_task(task);
7513 return 0;
7514}
7515
Trond Myklebust36281ca2012-03-04 18:13:56 -05007516static bool nfs41_match_stateid(const nfs4_stateid *s1,
7517 const nfs4_stateid *s2)
7518{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007519 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007520 return false;
7521
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007522 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007523 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007524 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007525 return true;
7526
7527 return false;
7528}
7529
Andy Adamson557134a2009-04-01 09:21:53 -04007530#endif /* CONFIG_NFS_V4_1 */
7531
Trond Myklebust36281ca2012-03-04 18:13:56 -05007532static bool nfs4_match_stateid(const nfs4_stateid *s1,
7533 const nfs4_stateid *s2)
7534{
Trond Myklebustf597c532012-03-04 18:13:56 -05007535 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05007536}
7537
7538
Trond Myklebust17280172012-03-11 13:11:00 -04007539static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007540 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007541 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007542 .recover_open = nfs4_open_reclaim,
7543 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04007544 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04007545 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007546};
7547
Andy Adamson591d71c2009-04-01 09:22:47 -04007548#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007549static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007550 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
7551 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
7552 .recover_open = nfs4_open_reclaim,
7553 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05007554 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007555 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04007556 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04007557};
7558#endif /* CONFIG_NFS_V4_1 */
7559
Trond Myklebust17280172012-03-11 13:11:00 -04007560static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007561 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007562 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007563 .recover_open = nfs4_open_expired,
7564 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04007565 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007566};
7567
Andy Adamson591d71c2009-04-01 09:22:47 -04007568#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007569static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007570 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
7571 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007572 .recover_open = nfs41_open_expired,
7573 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05007574 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04007575};
7576#endif /* CONFIG_NFS_V4_1 */
7577
Trond Myklebust17280172012-03-11 13:11:00 -04007578static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007579 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04007580 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007581 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04007582};
7583
7584#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007585static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007586 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04007587 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007588 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04007589};
7590#endif
7591
Trond Myklebust97dc1352010-06-16 09:52:26 -04007592static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
7593 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04007594 .init_caps = NFS_CAP_READDIRPLUS
7595 | NFS_CAP_ATOMIC_OPEN
7596 | NFS_CAP_CHANGE_ATTR
7597 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04007598 .init_client = nfs40_init_client,
7599 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007600 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007601 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007602 .free_lock_state = nfs4_release_lockowner,
Chuck Lever9915ea72013-08-09 12:48:27 -04007603 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007604 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
7605 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
7606 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007607};
7608
7609#if defined(CONFIG_NFS_V4_1)
7610static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
7611 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04007612 .init_caps = NFS_CAP_READDIRPLUS
7613 | NFS_CAP_ATOMIC_OPEN
7614 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04007615 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04007616 | NFS_CAP_STATEID_NFSV41
7617 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04007618 .init_client = nfs41_init_client,
7619 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007620 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007621 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007622 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04007623 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007624 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7625 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7626 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007627};
7628#endif
7629
Steve Dickson42c2c422013-05-22 12:50:38 -04007630#if defined(CONFIG_NFS_V4_2)
7631static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
7632 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04007633 .init_caps = NFS_CAP_READDIRPLUS
7634 | NFS_CAP_ATOMIC_OPEN
7635 | NFS_CAP_CHANGE_ATTR
7636 | NFS_CAP_POSIX_LOCK
7637 | NFS_CAP_STATEID_NFSV41
7638 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04007639 .init_client = nfs41_init_client,
7640 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04007641 .match_stateid = nfs41_match_stateid,
7642 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04007643 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04007644 .call_sync_ops = &nfs41_call_sync_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04007645 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7646 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7647 .state_renewal_ops = &nfs41_state_renewal_ops,
7648};
7649#endif
7650
Trond Myklebust97dc1352010-06-16 09:52:26 -04007651const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
7652 [0] = &nfs_v4_0_minor_ops,
7653#if defined(CONFIG_NFS_V4_1)
7654 [1] = &nfs_v4_1_minor_ops,
7655#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04007656#if defined(CONFIG_NFS_V4_2)
7657 [2] = &nfs_v4_2_minor_ops,
7658#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04007659};
7660
Trond Myklebust17f26b12013-08-21 15:48:42 -04007661static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04007662 .create = nfs_create,
7663 .lookup = nfs_lookup,
7664 .atomic_open = nfs_atomic_open,
7665 .link = nfs_link,
7666 .unlink = nfs_unlink,
7667 .symlink = nfs_symlink,
7668 .mkdir = nfs_mkdir,
7669 .rmdir = nfs_rmdir,
7670 .mknod = nfs_mknod,
7671 .rename = nfs_rename,
7672 .permission = nfs_permission,
7673 .getattr = nfs_getattr,
7674 .setattr = nfs_setattr,
7675 .getxattr = generic_getxattr,
7676 .setxattr = generic_setxattr,
7677 .listxattr = generic_listxattr,
7678 .removexattr = generic_removexattr,
7679};
7680
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08007681static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007682 .permission = nfs_permission,
7683 .getattr = nfs_getattr,
7684 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007685 .getxattr = generic_getxattr,
7686 .setxattr = generic_setxattr,
7687 .listxattr = generic_listxattr,
7688 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007689};
7690
David Howells509de812006-08-22 20:06:11 -04007691const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007692 .version = 4, /* protocol version */
7693 .dentry_ops = &nfs4_dentry_operations,
7694 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007695 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04007696 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007697 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04007698 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04007699 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007700 .getattr = nfs4_proc_getattr,
7701 .setattr = nfs4_proc_setattr,
7702 .lookup = nfs4_proc_lookup,
7703 .access = nfs4_proc_access,
7704 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007705 .create = nfs4_proc_create,
7706 .remove = nfs4_proc_remove,
7707 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04007708 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007709 .unlink_done = nfs4_proc_unlink_done,
7710 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04007711 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04007712 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04007713 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007714 .link = nfs4_proc_link,
7715 .symlink = nfs4_proc_symlink,
7716 .mkdir = nfs4_proc_mkdir,
7717 .rmdir = nfs4_proc_remove,
7718 .readdir = nfs4_proc_readdir,
7719 .mknod = nfs4_proc_mknod,
7720 .statfs = nfs4_proc_statfs,
7721 .fsinfo = nfs4_proc_fsinfo,
7722 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04007723 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007724 .decode_dirent = nfs4_decode_dirent,
7725 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04007726 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04007727 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05007728 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007729 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04007730 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04007731 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007732 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007733 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04007734 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007735 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007736 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00007737 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04007738 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04007739 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04007740 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04007741 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04007742 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00007743 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04007744 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04007745 .create_server = nfs4_create_server,
7746 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007747};
7748
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007749static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7750 .prefix = XATTR_NAME_NFSV4_ACL,
7751 .list = nfs4_xattr_list_nfs4_acl,
7752 .get = nfs4_xattr_get_nfs4_acl,
7753 .set = nfs4_xattr_set_nfs4_acl,
7754};
7755
7756const struct xattr_handler *nfs4_xattr_handlers[] = {
7757 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04007758#ifdef CONFIG_NFS_V4_SECURITY_LABEL
7759 &nfs4_xattr_nfs4_label_handler,
7760#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007761 NULL
7762};
7763
Linus Torvalds1da177e2005-04-16 15:20:36 -07007764/*
7765 * Local variables:
7766 * c-basic-offset: 8
7767 * End:
7768 */