blob: f6be2264e39cb341a66c18970ed0999e6a71d6f6 [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
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400472#if defined(CONFIG_NFS_V4_1)
473
Trond Myklebustd185a332010-06-16 09:52:25 -0400474static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400475{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500476 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400477 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500478 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400479
Benny Halevydfb4f3092010-09-24 09:17:01 -0400480 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400481 /* just wake up the next guy waiting since
482 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500483 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500484 return;
Andy Adamson13615872009-04-01 09:22:17 -0400485 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500486 tbl = res->sr_slot->table;
487 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500488
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500489 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500490 /* Be nice to the server: try to ensure that the last transmitted
491 * value for highest_user_slotid <= target_highest_slotid
492 */
493 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
494 send_new_highest_used_slotid = true;
495
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500496 if (nfs41_wake_and_assign_slot(tbl, res->sr_slot)) {
497 send_new_highest_used_slotid = false;
498 goto out_unlock;
499 }
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500500 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500501
502 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
503 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500504out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500505 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400506 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500507 if (send_new_highest_used_slotid)
508 nfs41_server_notify_highest_slotid_update(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400509}
510
Trond Myklebust14516c32010-07-31 14:29:06 -0400511static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400512{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500513 struct nfs4_session *session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500514 struct nfs4_slot *slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400515 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500516 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500517 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400518
Bryan Schumaker468f8612011-04-18 15:57:32 -0400519 /* don't increment the sequence number if the task wasn't sent */
520 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400521 goto out;
522
Trond Myklebust933602e2012-11-16 12:12:38 -0500523 slot = res->sr_slot;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500524 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500525
Trond Myklebustac20d162012-12-15 15:36:07 -0500526 if (slot->interrupted) {
527 slot->interrupted = 0;
528 interrupted = true;
529 }
530
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400531 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500532 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400533 switch (res->sr_status) {
534 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400535 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500536 ++slot->seq_nr;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500537 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500538 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500539 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500540 if (res->sr_status_flags != 0)
541 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500542 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400543 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500544 case 1:
545 /*
546 * sr_status remains 1 if an RPC level error occurred.
547 * The server may or may not have processed the sequence
548 * operation..
549 * Mark the slot as having hosted an interrupted RPC call.
550 */
551 slot->interrupted = 1;
552 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400553 case -NFS4ERR_DELAY:
554 /* The server detected a resend of the RPC call and
555 * returned NFS4ERR_DELAY as per Section 2.10.6.2
556 * of RFC5661.
557 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500558 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400559 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500560 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500561 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400562 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500563 case -NFS4ERR_BADSLOT:
564 /*
565 * The slot id we used was probably retired. Try again
566 * using a different slot id.
567 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500568 goto retry_nowait;
569 case -NFS4ERR_SEQ_MISORDERED:
570 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500571 * Was the last operation on this sequence interrupted?
572 * If so, retry after bumping the sequence number.
573 */
574 if (interrupted) {
575 ++slot->seq_nr;
576 goto retry_nowait;
577 }
578 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500579 * Could this slot have been previously retired?
580 * If so, then the server may be expecting seq_nr = 1!
581 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500582 if (slot->seq_nr != 1) {
583 slot->seq_nr = 1;
584 goto retry_nowait;
585 }
586 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500587 case -NFS4ERR_SEQ_FALSE_RETRY:
588 ++slot->seq_nr;
589 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400590 default:
591 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500592 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400593 }
594out:
595 /* The session may be reset by one of the error handlers. */
596 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400597 nfs41_sequence_free_slot(res);
Trond Myklebust85563072012-12-11 10:31:12 -0500598 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500599retry_nowait:
600 if (rpc_restart_call_prepare(task)) {
601 task->tk_status = 0;
602 ret = 0;
603 }
604 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400605out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400606 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400607 goto out;
608 rpc_delay(task, NFS4_POLL_RETRY_MAX);
609 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400610}
611
Trond Myklebust14516c32010-07-31 14:29:06 -0400612static int nfs4_sequence_done(struct rpc_task *task,
613 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400614{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500615 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400616 return 1;
617 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400618}
619
Trond Myklebust9d12b212012-01-17 22:04:25 -0500620static void nfs41_init_sequence(struct nfs4_sequence_args *args,
621 struct nfs4_sequence_res *res, int cache_reply)
622{
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500623 args->sa_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500624 args->sa_cache_this = 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400625 args->sa_privileged = 0;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500626 if (cache_reply)
627 args->sa_cache_this = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500628 res->sr_slot = NULL;
629}
630
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400631static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
632{
633 args->sa_privileged = 1;
634}
635
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000636int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400637 struct nfs4_sequence_args *args,
638 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400639 struct rpc_task *task)
640{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400641 struct nfs4_slot *slot;
642 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400643
644 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400645 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400646 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400647 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400648
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400649 tbl = &session->fc_slot_table;
650
Trond Myklebust69d206b2012-11-22 13:21:02 -0500651 task->tk_timeout = 0;
652
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400653 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400654 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400655 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500656 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500657 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400658 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400659 }
660
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500661 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500662 if (IS_ERR(slot)) {
663 /* If out of memory, try again in 1/4 second */
664 if (slot == ERR_PTR(-ENOMEM))
665 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400666 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400667 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400668 }
669 spin_unlock(&tbl->slot_tbl_lock);
670
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500671 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400672
Chuck Levere8d92382013-08-09 12:47:51 -0400673 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500674 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400675
Benny Halevydfb4f3092010-09-24 09:17:01 -0400676 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500677 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400678 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400679 /*
680 * sr_status is only set in decode_sequence, and so will remain
681 * set to 1 if an rpc level failure occurs.
682 */
683 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400684 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400685out_success:
686 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400687 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400688out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400689 /* Privileged tasks are queued with top priority */
690 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400691 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
692 NULL, RPC_PRIORITY_PRIVILEGED);
693 else
694 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400695 spin_unlock(&tbl->slot_tbl_lock);
696 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400697}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000698EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400699
Chuck Lever5a580e02013-08-09 12:48:09 -0400700static int nfs4_setup_sequence(const struct nfs_server *server,
701 struct nfs4_sequence_args *args,
702 struct nfs4_sequence_res *res,
703 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400704{
Trond Myklebust035168a2010-06-16 09:52:26 -0400705 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400706 int ret = 0;
707
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400708 if (session == NULL) {
709 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400710 goto out;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400711 }
Trond Myklebust035168a2010-06-16 09:52:26 -0400712
Chuck Levere8d92382013-08-09 12:47:51 -0400713 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400714 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400715 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168a2010-06-16 09:52:26 -0400716
Trond Myklebust9d12b212012-01-17 22:04:25 -0500717 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400718out:
719 dprintk("<-- %s status=%d\n", __func__, ret);
720 return ret;
721}
722
Andy Adamsonce5039c2009-04-01 09:22:13 -0400723static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
724{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400725 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400726 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400727
Trond Myklebust035168a2010-06-16 09:52:26 -0400728 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
729
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400730 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400731}
732
Andy Adamson69ab40c2009-04-01 09:22:19 -0400733static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
734{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400735 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400736
Trond Myklebust14516c32010-07-31 14:29:06 -0400737 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400738}
739
Trond Myklebust17280172012-03-11 13:11:00 -0400740static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400741 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400742 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400743};
744
Bryan Schumaker7c513052011-03-24 17:12:24 +0000745static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
746 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400747 struct rpc_message *msg,
748 struct nfs4_sequence_args *args,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400749 struct nfs4_sequence_res *res)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400750{
751 int ret;
752 struct rpc_task *task;
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400753 struct nfs4_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400754 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400755 .seq_args = args,
756 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400757 };
758 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000759 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400760 .rpc_message = msg,
761 .callback_ops = &nfs41_call_sync_ops,
762 .callback_data = &data
763 };
764
Andy Adamsonce5039c2009-04-01 09:22:13 -0400765 task = rpc_run_task(&task_setup);
766 if (IS_ERR(task))
767 ret = PTR_ERR(task);
768 else {
769 ret = task->tk_status;
770 rpc_put_task(task);
771 }
772 return ret;
773}
774
Trond Myklebustdf896452010-06-16 09:52:26 -0400775#else
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400776static
Trond Myklebust9d12b212012-01-17 22:04:25 -0500777void nfs41_init_sequence(struct nfs4_sequence_args *args,
778 struct nfs4_sequence_res *res, int cache_reply)
779{
780}
781
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400782static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
783{
784}
785
Chuck Lever5a580e02013-08-09 12:48:09 -0400786static int nfs4_setup_sequence(const struct nfs_server *server,
787 struct nfs4_sequence_args *args,
788 struct nfs4_sequence_res *res,
789 struct rpc_task *task)
790{
791 rpc_call_start(task);
792 return 0;
793}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400794
Trond Myklebust14516c32010-07-31 14:29:06 -0400795static int nfs4_sequence_done(struct rpc_task *task,
796 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400797{
Trond Myklebust14516c32010-07-31 14:29:06 -0400798 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400799}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400800#endif /* CONFIG_NFS_V4_1 */
801
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400802static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000803int _nfs4_call_sync(struct rpc_clnt *clnt,
804 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400805 struct rpc_message *msg,
806 struct nfs4_sequence_args *args,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400807 struct nfs4_sequence_res *res)
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400808{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000809 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400810}
811
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400812static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000813int nfs4_call_sync(struct rpc_clnt *clnt,
814 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000815 struct rpc_message *msg,
816 struct nfs4_sequence_args *args,
817 struct nfs4_sequence_res *res,
818 int cache_reply)
819{
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400820 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000821 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400822 args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000823}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400824
Trond Myklebust38478b22006-05-25 01:40:57 -0400825static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
Trond Myklebust38478b22006-05-25 01:40:57 -0400827 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Trond Myklebust38478b22006-05-25 01:40:57 -0400829 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400830 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700831 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400832 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700833 dir->i_version = cinfo->after;
David Howellsde242c02012-12-20 21:52:38 +0000834 nfs_fscache_invalidate(dir);
Trond Myklebust38478b22006-05-25 01:40:57 -0400835 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836}
837
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100838struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400839 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100840 struct nfs_openargs o_arg;
841 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100842 struct nfs_open_confirmargs c_arg;
843 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500844 struct nfs4_string owner_name;
845 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100846 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400847 struct nfs4_label *f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100848 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400849 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100850 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400851 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100852 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100853 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400854 unsigned int rpc_done : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -0400855 unsigned int is_recover : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100856 int rpc_status;
857 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100858};
859
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400860static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
861 int err, struct nfs4_exception *exception)
862{
863 if (err != -EINVAL)
864 return false;
865 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
866 return false;
867 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
868 exception->retry = 1;
869 return true;
870}
871
872static enum open_claim_type4
873nfs4_map_atomic_open_claim(struct nfs_server *server,
874 enum open_claim_type4 claim)
875{
876 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
877 return claim;
878 switch (claim) {
879 default:
880 return claim;
881 case NFS4_OPEN_CLAIM_FH:
882 return NFS4_OPEN_CLAIM_NULL;
883 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
884 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
885 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
886 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
887 }
888}
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400889
890static void nfs4_init_opendata_res(struct nfs4_opendata *p)
891{
892 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400893 p->o_res.f_label = p->f_label;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400894 p->o_res.seqid = p->o_arg.seqid;
895 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400896 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400897 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400898 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500899 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400900}
901
Al Viro82a2c1b2011-06-22 18:30:55 -0400902static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500903 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400904 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -0400905 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400906 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400907 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100908{
Al Viro82a2c1b2011-06-22 18:30:55 -0400909 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100910 struct inode *dir = parent->d_inode;
911 struct nfs_server *server = NFS_SERVER(dir);
912 struct nfs4_opendata *p;
913
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400914 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100915 if (p == NULL)
916 goto err;
David Quigley14c43f72013-05-22 12:50:43 -0400917
918 p->f_label = nfs4_label_alloc(server, gfp_mask);
919 if (IS_ERR(p->f_label))
920 goto err_free_p;
921
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400922 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100923 if (p->o_arg.seqid == NULL)
David Quigley14c43f72013-05-22 12:50:43 -0400924 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -0400925 nfs_sb_active(dentry->d_sb);
926 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100927 p->dir = parent;
928 p->owner = sp;
929 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500930 p->o_arg.open_flags = flags;
931 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700932 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
933 * will return permission denied for all bits until close */
934 if (!(flags & O_EXCL)) {
935 /* ask server to check for all possible rights as results
936 * are cached */
937 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
938 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700939 }
David Howells7539bba2006-08-22 20:06:09 -0400940 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400941 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
942 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400943 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100944 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -0400945 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -0400946 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
David Quigley1775fd32013-05-22 12:50:42 -0400947 p->o_arg.label = label;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400948 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
949 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400950 case NFS4_OPEN_CLAIM_NULL:
951 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
952 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
953 p->o_arg.fh = NFS_FH(dir);
954 break;
955 case NFS4_OPEN_CLAIM_PREVIOUS:
956 case NFS4_OPEN_CLAIM_FH:
957 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
958 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
959 p->o_arg.fh = NFS_FH(dentry->d_inode);
960 }
Trond Myklebust536e43d2012-01-17 22:04:26 -0500961 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -0400962 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400963
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100964 p->o_arg.u.attrs = &p->attrs;
965 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500966
967 verf[0] = jiffies;
968 verf[1] = current->pid;
969 memcpy(p->o_arg.u.verifier.data, verf,
970 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100971 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100972 p->c_arg.fh = &p->o_res.fh;
973 p->c_arg.stateid = &p->o_res.stateid;
974 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400975 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400976 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100977 return p;
David Quigley14c43f72013-05-22 12:50:43 -0400978
979err_free_label:
980 nfs4_label_free(p->f_label);
981err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100982 kfree(p);
983err:
984 dput(parent);
985 return NULL;
986}
987
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400988static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100989{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400990 struct nfs4_opendata *p = container_of(kref,
991 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400992 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400993
994 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400995 if (p->state != NULL)
996 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400997 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -0400998
999 nfs4_label_free(p->f_label);
1000
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001001 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001002 dput(p->dentry);
1003 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001004 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001005 kfree(p);
1006}
1007
1008static void nfs4_opendata_put(struct nfs4_opendata *p)
1009{
1010 if (p != NULL)
1011 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001012}
1013
Trond Myklebust06f814a2006-01-03 09:55:07 +01001014static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1015{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001016 int ret;
1017
Trond Myklebust06f814a2006-01-03 09:55:07 +01001018 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001019 return ret;
1020}
1021
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001022static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001023{
1024 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001025
Trond Myklebust536e43d2012-01-17 22:04:26 -05001026 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001027 goto out;
1028 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001029 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001030 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1031 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001032 break;
1033 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001034 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1035 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001036 break;
1037 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001038 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1039 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001040 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001041out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001042 return ret;
1043}
1044
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001045static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001046{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001047 if (delegation == NULL)
1048 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001049 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001050 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -05001051 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -04001052 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001053 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1054 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -05001055 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001056 return 1;
1057}
1058
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001059static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001060{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001061 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001062 case FMODE_WRITE:
1063 state->n_wronly++;
1064 break;
1065 case FMODE_READ:
1066 state->n_rdonly++;
1067 break;
1068 case FMODE_READ|FMODE_WRITE:
1069 state->n_rdwr++;
1070 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001071 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001072}
1073
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001074static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001075{
1076 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001077 nfs4_stateid_copy(&state->stateid, stateid);
1078 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust92b40e92013-04-20 01:25:45 -04001079 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001080 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001081 case FMODE_READ:
1082 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1083 break;
1084 case FMODE_WRITE:
1085 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1086 break;
1087 case FMODE_READ|FMODE_WRITE:
1088 set_bit(NFS_O_RDWR_STATE, &state->flags);
1089 }
1090}
1091
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001092static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001093{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001094 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001095 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001096 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001097}
1098
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001099static 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 -07001100{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001101 /*
1102 * Protect the call to nfs4_state_set_mode_locked and
1103 * serialise the stateid update
1104 */
1105 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001106 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001107 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001108 set_bit(NFS_DELEGATED_STATE, &state->flags);
1109 }
1110 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001111 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001112 write_sequnlock(&state->seqlock);
1113 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001114 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001115 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116}
1117
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001118static 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 -05001119{
1120 struct nfs_inode *nfsi = NFS_I(state->inode);
1121 struct nfs_delegation *deleg_cur;
1122 int ret = 0;
1123
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001124 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001125
1126 rcu_read_lock();
1127 deleg_cur = rcu_dereference(nfsi->delegation);
1128 if (deleg_cur == NULL)
1129 goto no_delegation;
1130
1131 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001132 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001133 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001134 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001135 goto no_delegation_unlock;
1136
1137 if (delegation == NULL)
1138 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001139 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001140 goto no_delegation_unlock;
1141
Trond Myklebustb7391f42008-12-23 15:21:52 -05001142 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001143 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001144 ret = 1;
1145no_delegation_unlock:
1146 spin_unlock(&deleg_cur->lock);
1147no_delegation:
1148 rcu_read_unlock();
1149
1150 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001151 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001152 ret = 1;
1153 }
1154
1155 return ret;
1156}
1157
1158
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001159static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001160{
1161 struct nfs_delegation *delegation;
1162
1163 rcu_read_lock();
1164 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001165 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001166 rcu_read_unlock();
1167 return;
1168 }
1169 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001170 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001171}
1172
Trond Myklebust6ee41262007-07-08 14:11:36 -04001173static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001174{
1175 struct nfs4_state *state = opendata->state;
1176 struct nfs_inode *nfsi = NFS_I(state->inode);
1177 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001178 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001179 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001180 nfs4_stateid stateid;
1181 int ret = -EAGAIN;
1182
Trond Myklebustaac00a82007-07-05 19:02:21 -04001183 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001184 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001185 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001186 if (can_open_cached(state, fmode, open_mode)) {
1187 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001188 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001189 goto out_return_state;
1190 }
1191 spin_unlock(&state->owner->so_lock);
1192 }
Trond Myklebust34310432008-12-23 15:21:38 -05001193 rcu_read_lock();
1194 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001195 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001196 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001197 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001198 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001199 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001200 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001201 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001202 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001203 if (!opendata->is_recover) {
1204 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1205 if (ret != 0)
1206 goto out;
1207 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001208 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001209
1210 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001211 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001212 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001213 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001214out:
1215 return ERR_PTR(ret);
1216out_return_state:
1217 atomic_inc(&state->count);
1218 return state;
1219}
1220
Andy Adamsone23008e2012-10-02 21:07:32 -04001221static void
1222nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1223{
1224 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1225 struct nfs_delegation *delegation;
1226 int delegation_flags = 0;
1227
1228 rcu_read_lock();
1229 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1230 if (delegation)
1231 delegation_flags = delegation->flags;
1232 rcu_read_unlock();
1233 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1234 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1235 "returning a delegation for "
1236 "OPEN(CLAIM_DELEGATE_CUR)\n",
1237 clp->cl_hostname);
1238 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1239 nfs_inode_set_delegation(state->inode,
1240 data->owner->so_cred,
1241 &data->o_res);
1242 else
1243 nfs_inode_reclaim_delegation(state->inode,
1244 data->owner->so_cred,
1245 &data->o_res);
1246}
1247
1248/*
1249 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1250 * and update the nfs4_state.
1251 */
1252static struct nfs4_state *
1253_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1254{
1255 struct inode *inode = data->state->inode;
1256 struct nfs4_state *state = data->state;
1257 int ret;
1258
1259 if (!data->rpc_done) {
1260 ret = data->rpc_status;
1261 goto err;
1262 }
1263
1264 ret = -ESTALE;
1265 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1266 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1267 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1268 goto err;
1269
1270 ret = -ENOMEM;
1271 state = nfs4_get_open_state(inode, data->owner);
1272 if (state == NULL)
1273 goto err;
1274
1275 ret = nfs_refresh_inode(inode, &data->f_attr);
1276 if (ret)
1277 goto err;
1278
David Quigleyaa9c2662013-05-22 12:50:44 -04001279 nfs_setsecurity(inode, &data->f_attr, data->f_label);
1280
Andy Adamsone23008e2012-10-02 21:07:32 -04001281 if (data->o_res.delegation_type != 0)
1282 nfs4_opendata_check_deleg(data, state);
1283 update_open_stateid(state, &data->o_res.stateid, NULL,
1284 data->o_arg.fmode);
1285
1286 return state;
1287err:
1288 return ERR_PTR(ret);
1289
1290}
1291
1292static struct nfs4_state *
1293_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001294{
1295 struct inode *inode;
1296 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001297 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001298
Trond Myklebustaac00a82007-07-05 19:02:21 -04001299 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001300 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001301 goto out;
1302 }
1303
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001304 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001305 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001306 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001307 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001308 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001309 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001310 goto err;
1311 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001312 state = nfs4_get_open_state(inode, data->owner);
1313 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001314 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001315 if (data->o_res.delegation_type != 0)
1316 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001317 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001318 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001319 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001320out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001321 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001322 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001323err_put_inode:
1324 iput(inode);
1325err:
1326 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001327}
1328
Andy Adamsone23008e2012-10-02 21:07:32 -04001329static struct nfs4_state *
1330nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1331{
1332 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1333 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1334 return _nfs4_opendata_to_nfs4_state(data);
1335}
1336
Trond Myklebust864472e2006-01-03 09:55:15 +01001337static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1338{
1339 struct nfs_inode *nfsi = NFS_I(state->inode);
1340 struct nfs_open_context *ctx;
1341
1342 spin_lock(&state->inode->i_lock);
1343 list_for_each_entry(ctx, &nfsi->open_files, list) {
1344 if (ctx->state != state)
1345 continue;
1346 get_nfs_open_context(ctx);
1347 spin_unlock(&state->inode->i_lock);
1348 return ctx;
1349 }
1350 spin_unlock(&state->inode->i_lock);
1351 return ERR_PTR(-ENOENT);
1352}
1353
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001354static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1355 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001356{
1357 struct nfs4_opendata *opendata;
1358
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001359 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001360 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001361 if (opendata == NULL)
1362 return ERR_PTR(-ENOMEM);
1363 opendata->state = state;
1364 atomic_inc(&state->count);
1365 return opendata;
1366}
1367
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001368static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001369{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001370 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001371 int ret;
1372
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001373 opendata->o_arg.open_flags = 0;
1374 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001375 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1376 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1377 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001378 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001379 if (ret != 0)
1380 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001381 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001382 if (IS_ERR(newstate))
1383 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001384 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001385 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001386 return 0;
1387}
1388
1389static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1390{
Trond Myklebust864472e2006-01-03 09:55:15 +01001391 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001392 int ret;
1393
1394 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001395 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001396 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001397 smp_rmb();
1398 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001399 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001400 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001401 if (ret != 0)
1402 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001403 if (newstate != state)
1404 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001405 }
1406 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001407 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001408 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001409 if (ret != 0)
1410 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001411 if (newstate != state)
1412 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001413 }
1414 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001415 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001416 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001417 if (ret != 0)
1418 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001419 if (newstate != state)
1420 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001421 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001422 /*
1423 * We may have performed cached opens for all three recoveries.
1424 * Check if we need to update the current stateid.
1425 */
1426 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001427 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001428 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001429 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001430 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001431 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001432 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001433 return 0;
1434}
1435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436/*
1437 * OPEN_RECLAIM:
1438 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001440static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001442 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001443 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001444 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 int status;
1446
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001447 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1448 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001449 if (IS_ERR(opendata))
1450 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001451 rcu_read_lock();
1452 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001453 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001454 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001455 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001456 opendata->o_arg.u.delegation_type = delegation_type;
1457 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001458 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 return status;
1460}
1461
Trond Myklebust539cd032007-06-05 11:46:42 -04001462static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
1464 struct nfs_server *server = NFS_SERVER(state->inode);
1465 struct nfs4_exception exception = { };
1466 int err;
1467 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001468 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001469 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001470 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1471 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001472 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001473 break;
1474 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 } while (exception.retry);
1476 return err;
1477}
1478
Trond Myklebust864472e2006-01-03 09:55:15 +01001479static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1480{
1481 struct nfs_open_context *ctx;
1482 int ret;
1483
1484 ctx = nfs4_state_find_open_context(state);
1485 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001486 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001487 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001488 put_nfs_open_context(ctx);
1489 return ret;
1490}
1491
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001492static 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 -07001493{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001494 switch (err) {
1495 default:
1496 printk(KERN_ERR "NFS: %s: unhandled error "
1497 "%d.\n", __func__, err);
1498 case 0:
1499 case -ENOENT:
1500 case -ESTALE:
1501 break;
1502 case -NFS4ERR_BADSESSION:
1503 case -NFS4ERR_BADSLOT:
1504 case -NFS4ERR_BAD_HIGH_SLOT:
1505 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1506 case -NFS4ERR_DEADSESSION:
1507 set_bit(NFS_DELEGATED_STATE, &state->flags);
1508 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1509 return -EAGAIN;
1510 case -NFS4ERR_STALE_CLIENTID:
1511 case -NFS4ERR_STALE_STATEID:
1512 set_bit(NFS_DELEGATED_STATE, &state->flags);
1513 case -NFS4ERR_EXPIRED:
1514 /* Don't recall a delegation if it was lost */
1515 nfs4_schedule_lease_recovery(server->nfs_client);
1516 return -EAGAIN;
1517 case -NFS4ERR_DELEG_REVOKED:
1518 case -NFS4ERR_ADMIN_REVOKED:
1519 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001520 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001521 nfs_inode_find_state_and_recover(state->inode,
1522 stateid);
1523 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001524 return 0;
1525 case -NFS4ERR_DELAY:
1526 case -NFS4ERR_GRACE:
1527 set_bit(NFS_DELEGATED_STATE, &state->flags);
1528 ssleep(1);
1529 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001530 case -ENOMEM:
1531 case -NFS4ERR_DENIED:
1532 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1533 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 return err;
1536}
1537
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001538int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1539{
1540 struct nfs_server *server = NFS_SERVER(state->inode);
1541 struct nfs4_opendata *opendata;
1542 int err;
1543
1544 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1545 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1546 if (IS_ERR(opendata))
1547 return PTR_ERR(opendata);
1548 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1549 err = nfs4_open_recover(opendata, state);
1550 nfs4_opendata_put(opendata);
1551 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1552}
1553
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001554static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1555{
1556 struct nfs4_opendata *data = calldata;
1557
1558 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001559 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001560 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001561 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001562 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001563 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001564 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001565}
1566
1567static void nfs4_open_confirm_release(void *calldata)
1568{
1569 struct nfs4_opendata *data = calldata;
1570 struct nfs4_state *state = NULL;
1571
1572 /* If this request hasn't been cancelled, do nothing */
1573 if (data->cancelled == 0)
1574 goto out_free;
1575 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001576 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001577 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001578 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001579 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001580 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001581out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001582 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001583}
1584
1585static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001586 .rpc_call_done = nfs4_open_confirm_done,
1587 .rpc_release = nfs4_open_confirm_release,
1588};
1589
1590/*
1591 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1592 */
1593static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1594{
1595 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1596 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001597 struct rpc_message msg = {
1598 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1599 .rpc_argp = &data->c_arg,
1600 .rpc_resp = &data->c_res,
1601 .rpc_cred = data->owner->so_cred,
1602 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001603 struct rpc_task_setup task_setup_data = {
1604 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001605 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001606 .callback_ops = &nfs4_open_confirm_ops,
1607 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001608 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001609 .flags = RPC_TASK_ASYNC,
1610 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 int status;
1612
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001613 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001614 data->rpc_done = 0;
1615 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001616 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001617 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001618 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001619 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001620 status = nfs4_wait_for_completion_rpc_task(task);
1621 if (status != 0) {
1622 data->cancelled = 1;
1623 smp_wmb();
1624 } else
1625 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001626 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 return status;
1628}
1629
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001630static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001632 struct nfs4_opendata *data = calldata;
1633 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001634 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001635
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001636 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001637 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001638 /*
1639 * Check if we still need to send an OPEN call, or if we can use
1640 * a delegation instead.
1641 */
1642 if (data->state != NULL) {
1643 struct nfs_delegation *delegation;
1644
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001645 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001646 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001647 rcu_read_lock();
1648 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001649 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
Trond Myklebustcd4c9be2013-04-23 14:46:25 -04001650 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
Trond Myklebust652f89f2011-12-09 19:05:58 -05001651 can_open_delegated(delegation, data->o_arg.fmode))
1652 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001653 rcu_read_unlock();
1654 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001655 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04001656 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust8188df12013-04-23 14:31:19 -04001657 switch (data->o_arg.claim) {
1658 case NFS4_OPEN_CLAIM_PREVIOUS:
1659 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1660 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001661 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04001662 case NFS4_OPEN_CLAIM_FH:
1663 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001664 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1665 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001666 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001667 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001668 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001669 &data->o_res.seq_res,
1670 task) != 0)
1671 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04001672
1673 /* Set the create mode (note dependency on the session type) */
1674 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1675 if (data->o_arg.open_flags & O_EXCL) {
1676 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1677 if (nfs4_has_persistent_session(clp))
1678 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1679 else if (clp->cl_mvops->minor_version > 0)
1680 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1681 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04001682 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001683unlock_no_action:
1684 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001685out_no_action:
1686 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001687out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001688 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001689}
1690
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001691static void nfs4_open_done(struct rpc_task *task, void *calldata)
1692{
1693 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001695 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001696
Trond Myklebust14516c32010-07-31 14:29:06 -04001697 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1698 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001699
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001700 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001701 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1702 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001703 case S_IFREG:
1704 break;
1705 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001706 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001707 break;
1708 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001709 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001710 break;
1711 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001712 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001713 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001714 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001715 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001716 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1717 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001718 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001719 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001720}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001721
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001722static void nfs4_open_release(void *calldata)
1723{
1724 struct nfs4_opendata *data = calldata;
1725 struct nfs4_state *state = NULL;
1726
1727 /* If this request hasn't been cancelled, do nothing */
1728 if (data->cancelled == 0)
1729 goto out_free;
1730 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001731 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001732 goto out_free;
1733 /* In case we need an open_confirm, no cleanup! */
1734 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1735 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001736 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001737 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001738 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001739out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001740 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001741}
1742
1743static const struct rpc_call_ops nfs4_open_ops = {
1744 .rpc_call_prepare = nfs4_open_prepare,
1745 .rpc_call_done = nfs4_open_done,
1746 .rpc_release = nfs4_open_release,
1747};
1748
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001749static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001750{
1751 struct inode *dir = data->dir->d_inode;
1752 struct nfs_server *server = NFS_SERVER(dir);
1753 struct nfs_openargs *o_arg = &data->o_arg;
1754 struct nfs_openres *o_res = &data->o_res;
1755 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001756 struct rpc_message msg = {
1757 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1758 .rpc_argp = o_arg,
1759 .rpc_resp = o_res,
1760 .rpc_cred = data->owner->so_cred,
1761 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001762 struct rpc_task_setup task_setup_data = {
1763 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001764 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001765 .callback_ops = &nfs4_open_ops,
1766 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001767 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001768 .flags = RPC_TASK_ASYNC,
1769 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001770 int status;
1771
Trond Myklebust9d12b212012-01-17 22:04:25 -05001772 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001773 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001774 data->rpc_done = 0;
1775 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001776 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001777 data->is_recover = 0;
1778 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001779 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001780 data->is_recover = 1;
1781 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04001782 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001783 if (IS_ERR(task))
1784 return PTR_ERR(task);
1785 status = nfs4_wait_for_completion_rpc_task(task);
1786 if (status != 0) {
1787 data->cancelled = 1;
1788 smp_wmb();
1789 } else
1790 status = data->rpc_status;
1791 rpc_put_task(task);
1792
1793 return status;
1794}
1795
1796static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1797{
1798 struct inode *dir = data->dir->d_inode;
1799 struct nfs_openres *o_res = &data->o_res;
1800 int status;
1801
1802 status = nfs4_run_open_task(data, 1);
1803 if (status != 0 || !data->rpc_done)
1804 return status;
1805
Trond Myklebust6926afd2012-01-07 13:22:46 -05001806 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1807
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001808 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1809 status = _nfs4_proc_open_confirm(data);
1810 if (status != 0)
1811 return status;
1812 }
1813
1814 return status;
1815}
1816
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001817static int nfs4_opendata_access(struct rpc_cred *cred,
1818 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001819 struct nfs4_state *state, fmode_t fmode,
1820 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001821{
1822 struct nfs_access_entry cache;
1823 u32 mask;
1824
1825 /* access call failed or for some reason the server doesn't
1826 * support any access modes -- defer access call until later */
1827 if (opendata->o_res.access_supported == 0)
1828 return 0;
1829
1830 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001831 /* don't check MAY_WRITE - a newly created file may not have
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001832 * write mode bits, but POSIX allows the creating process to write.
1833 * use openflags to check for exec, because fmode won't
1834 * always have FMODE_EXEC set when file open for exec. */
1835 if (openflags & __FMODE_EXEC) {
1836 /* ONLY check for exec rights */
1837 mask = MAY_EXEC;
1838 } else if (fmode & FMODE_READ)
1839 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001840
1841 cache.cred = cred;
1842 cache.jiffies = jiffies;
1843 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1844 nfs_access_add_cache(state->inode, &cache);
1845
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001846 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001847 return 0;
1848
1849 /* even though OPEN succeeded, access is denied. Close the file */
1850 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001851 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001852}
1853
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001854/*
1855 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1856 */
1857static int _nfs4_proc_open(struct nfs4_opendata *data)
1858{
1859 struct inode *dir = data->dir->d_inode;
1860 struct nfs_server *server = NFS_SERVER(dir);
1861 struct nfs_openargs *o_arg = &data->o_arg;
1862 struct nfs_openres *o_res = &data->o_res;
1863 int status;
1864
1865 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001866 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001867 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001868 if (status != 0) {
1869 if (status == -NFS4ERR_BADNAME &&
1870 !(o_arg->open_flags & O_CREAT))
1871 return -ENOENT;
1872 return status;
1873 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001874
Trond Myklebust6926afd2012-01-07 13:22:46 -05001875 nfs_fattr_map_and_free_names(server, &data->f_attr);
1876
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001877 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001878 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001879 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1880 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001882 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001884 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 }
1886 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
David Quigley1775fd32013-05-22 12:50:42 -04001887 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001888 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889}
1890
Andy Adamsond83217c2011-03-01 01:34:17 +00001891static int nfs4_recover_expired_lease(struct nfs_server *server)
1892{
1893 return nfs4_client_recover_expired_lease(server->nfs_client);
1894}
1895
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896/*
1897 * OPEN_EXPIRED:
1898 * reclaim state on the server after a network partition.
1899 * Assumes caller holds the appropriate lock
1900 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001901static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001903 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001904 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001906 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001907 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001908 if (IS_ERR(opendata))
1909 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001910 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001911 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001912 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001913 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001914 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915}
1916
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001917static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001918{
Trond Myklebust539cd032007-06-05 11:46:42 -04001919 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001920 struct nfs4_exception exception = { };
1921 int err;
1922
1923 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001924 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001925 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001926 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1927 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001928 switch (err) {
1929 default:
1930 goto out;
1931 case -NFS4ERR_GRACE:
1932 case -NFS4ERR_DELAY:
1933 nfs4_handle_exception(server, err, &exception);
1934 err = 0;
1935 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001936 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001937out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001938 return err;
1939}
1940
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1942{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001944 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
Trond Myklebust864472e2006-01-03 09:55:15 +01001946 ctx = nfs4_state_find_open_context(state);
1947 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001948 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001949 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001950 put_nfs_open_context(ctx);
1951 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952}
1953
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001954#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001955static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001956{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001957 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001958 nfs4_stateid *stateid = &state->stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001959 struct nfs_delegation *delegation;
1960 struct rpc_cred *cred = NULL;
1961 int status = -NFS4ERR_BAD_STATEID;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001962
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001963 /* If a state reset has been done, test_stateid is unneeded */
1964 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1965 return;
1966
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001967 /* Get the delegation credential for use by test/free_stateid */
1968 rcu_read_lock();
1969 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1970 if (delegation != NULL &&
1971 nfs4_stateid_match(&delegation->stateid, stateid)) {
1972 cred = get_rpccred(delegation->cred);
1973 rcu_read_unlock();
1974 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04001975 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001976 } else
1977 rcu_read_unlock();
1978
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001979 if (status != NFS_OK) {
1980 /* Free the stateid unless the server explicitly
1981 * informs us the stateid is unrecognized. */
1982 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001983 nfs41_free_stateid(server, stateid, cred);
Bryan Schumaker69388672012-09-26 15:25:52 -04001984 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001985
Bryan Schumaker69388672012-09-26 15:25:52 -04001986 write_seqlock(&state->seqlock);
1987 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1988 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001989 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1990 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001991
1992 if (cred != NULL)
1993 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001994}
1995
1996/**
1997 * nfs41_check_open_stateid - possibly free an open stateid
1998 *
1999 * @state: NFSv4 state for an inode
2000 *
2001 * Returns NFS_OK if recovery for this stateid is now finished.
2002 * Otherwise a negative NFS4ERR value is returned.
2003 */
2004static int nfs41_check_open_stateid(struct nfs4_state *state)
2005{
2006 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002007 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002008 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002009 int status;
2010
2011 /* If a state reset has been done, test_stateid is unneeded */
2012 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2013 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2014 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2015 return -NFS4ERR_BAD_STATEID;
2016
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002017 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002018 trace_nfs4_test_open_stateid(state, NULL, status);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002019 if (status != NFS_OK) {
2020 /* Free the stateid unless the server explicitly
2021 * informs us the stateid is unrecognized. */
2022 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002023 nfs41_free_stateid(server, stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002024
2025 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2026 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2027 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002028 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002029 }
2030 return status;
2031}
2032
2033static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2034{
Chuck Levereb64cf92012-07-11 16:30:05 -04002035 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002036
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002037 nfs41_clear_delegation_stateid(state);
2038 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002039 if (status != NFS_OK)
2040 status = nfs4_open_expired(sp, state);
2041 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002042}
2043#endif
2044
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002046 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2047 * fields corresponding to attributes that were used to store the verifier.
2048 * Make sure we clobber those fields in the later setattr call
2049 */
2050static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2051{
2052 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2053 !(sattr->ia_valid & ATTR_ATIME_SET))
2054 sattr->ia_valid |= ATTR_ATIME;
2055
2056 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2057 !(sattr->ia_valid & ATTR_MTIME_SET))
2058 sattr->ia_valid |= ATTR_MTIME;
2059}
2060
Trond Myklebustc21443c2013-02-07 14:26:21 -05002061static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2062 fmode_t fmode,
2063 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002064 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002065{
2066 struct nfs4_state_owner *sp = opendata->owner;
2067 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002068 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002069 struct nfs4_state *state;
2070 unsigned int seq;
2071 int ret;
2072
2073 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2074
2075 ret = _nfs4_proc_open(opendata);
2076 if (ret != 0)
2077 goto out;
2078
2079 state = nfs4_opendata_to_nfs4_state(opendata);
2080 ret = PTR_ERR(state);
2081 if (IS_ERR(state))
2082 goto out;
2083 if (server->caps & NFS_CAP_POSIX_LOCK)
2084 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2085
Trond Myklebust275bb302013-05-29 13:11:28 -04002086 dentry = opendata->dentry;
2087 if (dentry->d_inode == NULL) {
2088 /* FIXME: Is this d_drop() ever needed? */
2089 d_drop(dentry);
2090 dentry = d_add_unique(dentry, igrab(state->inode));
2091 if (dentry == NULL) {
2092 dentry = opendata->dentry;
2093 } else if (dentry != ctx->dentry) {
2094 dput(ctx->dentry);
2095 ctx->dentry = dget(dentry);
2096 }
2097 nfs_set_verifier(dentry,
2098 nfs_save_change_attribute(opendata->dir->d_inode));
2099 }
2100
Trond Myklebustc21443c2013-02-07 14:26:21 -05002101 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2102 if (ret != 0)
2103 goto out;
2104
Trond Myklebust3efb9722013-05-29 13:17:04 -04002105 ctx->state = state;
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002106 if (dentry->d_inode == state->inode) {
2107 nfs_inode_attach_open_context(ctx);
2108 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2109 nfs4_schedule_stateid_recovery(server, state);
2110 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002111out:
2112 return ret;
2113}
2114
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002115/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002116 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 */
Andy Adamson82be4172012-05-23 05:02:35 -04002118static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002119 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002120 int flags,
2121 struct iattr *sattr,
Trond Myklebust959d9212013-06-28 16:29:51 -04002122 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123{
2124 struct nfs4_state_owner *sp;
2125 struct nfs4_state *state = NULL;
2126 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002127 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002128 struct dentry *dentry = ctx->dentry;
2129 struct rpc_cred *cred = ctx->cred;
2130 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2131 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002132 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002133 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002134 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
2136 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002138 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2139 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2141 goto out_err;
2142 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002143 status = nfs4_recover_expired_lease(server);
2144 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002145 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04002146 if (dentry->d_inode != NULL)
2147 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002148 status = -ENOMEM;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002149 if (dentry->d_inode)
2150 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002151 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002152 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002153 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002154 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
David Quigley14c43f72013-05-22 12:50:43 -04002156 if (label) {
2157 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2158 if (IS_ERR(olabel)) {
2159 status = PTR_ERR(olabel);
2160 goto err_opendata_put;
2161 }
2162 }
2163
Andy Adamson82be4172012-05-23 05:02:35 -04002164 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2165 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2166 if (!opendata->f_attr.mdsthreshold)
David Quigley14c43f72013-05-22 12:50:43 -04002167 goto err_free_label;
Trond Myklebust1549210f2012-06-05 09:16:47 -04002168 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002169 }
Al Viro82a2c1b2011-06-22 18:30:55 -04002170 if (dentry->d_inode != NULL)
2171 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002172
Trond Myklebust3efb9722013-05-29 13:17:04 -04002173 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002174 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002175 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002176 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002177
Trond Myklebust549b19c2013-04-16 18:42:34 -04002178 if ((opendata->o_arg.open_flags & O_EXCL) &&
2179 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002180 nfs4_exclusive_attrset(opendata, sattr);
2181
2182 nfs_fattr_init(opendata->o_res.f_attr);
2183 status = nfs4_do_setattr(state->inode, cred,
2184 opendata->o_res.f_attr, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002185 state, label, olabel);
2186 if (status == 0) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002187 nfs_setattr_update_inode(state->inode, sattr);
David Quigley1775fd32013-05-22 12:50:42 -04002188 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
David Quigleyaa9c2662013-05-22 12:50:44 -04002189 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
David Quigley1775fd32013-05-22 12:50:42 -04002190 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002191 }
Andy Adamson82be4172012-05-23 05:02:35 -04002192
2193 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2194 *ctx_th = opendata->f_attr.mdsthreshold;
2195 else
2196 kfree(opendata->f_attr.mdsthreshold);
2197 opendata->f_attr.mdsthreshold = NULL;
2198
David Quigley14c43f72013-05-22 12:50:43 -04002199 nfs4_label_free(olabel);
2200
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002201 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002204err_free_label:
2205 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002206err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002207 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002208 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002209err_put_state_owner:
2210 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 return status;
2213}
2214
2215
Andy Adamson82be4172012-05-23 05:02:35 -04002216static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002217 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002218 int flags,
2219 struct iattr *sattr,
Trond Myklebust959d9212013-06-28 16:29:51 -04002220 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002222 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 struct nfs4_exception exception = { };
2224 struct nfs4_state *res;
2225 int status;
2226
2227 do {
Trond Myklebust959d9212013-06-28 16:29:51 -04002228 status = _nfs4_do_open(dir, ctx, flags, sattr, label);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002229 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002230 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 if (status == 0)
2232 break;
2233 /* NOTE: BAD_SEQID means the server and client disagree about the
2234 * book-keeping w.r.t. state-changing operations
2235 * (OPEN/CLOSE/LOCK/LOCKU...)
2236 * It is actually a sign of a bug on the client or on the server.
2237 *
2238 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002239 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 * have unhashed the old state_owner for us, and that we can
2241 * therefore safely retry using a new one. We should still warn
2242 * the user though...
2243 */
2244 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002245 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002246 " returned a bad sequence-id error!\n",
2247 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 exception.retry = 1;
2249 continue;
2250 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002251 /*
2252 * BAD_STATEID on OPEN means that the server cancelled our
2253 * state before it received the OPEN_CONFIRM.
2254 * Recover by retrying the request as per the discussion
2255 * on Page 181 of RFC3530.
2256 */
2257 if (status == -NFS4ERR_BAD_STATEID) {
2258 exception.retry = 1;
2259 continue;
2260 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002261 if (status == -EAGAIN) {
2262 /* We must have found a delegation */
2263 exception.retry = 1;
2264 continue;
2265 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002266 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2267 continue;
2268 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 status, &exception));
2270 } while (exception.retry);
2271 return res;
2272}
2273
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002274static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2275 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002276 struct nfs4_state *state, struct nfs4_label *ilabel,
2277 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002279 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002281 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 .iap = sattr,
2283 .server = server,
2284 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002285 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 };
2287 struct nfs_setattrres res = {
2288 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002289 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 .server = server,
2291 };
2292 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002293 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2294 .rpc_argp = &arg,
2295 .rpc_resp = &res,
2296 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002298 unsigned long timestamp = jiffies;
Trond Myklebustee3ae842013-04-29 10:35:36 -04002299 fmode_t fmode;
2300 bool truncate;
Trond Myklebust65e43082005-08-16 11:49:44 -04002301 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
David Quigleyaa9c2662013-05-22 12:50:44 -04002303 arg.bitmask = nfs4_bitmask(server, ilabel);
2304 if (ilabel)
2305 arg.bitmask = nfs4_bitmask(server, olabel);
2306
Trond Myklebust0e574af2005-10-27 22:12:38 -04002307 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
Trond Myklebustee3ae842013-04-29 10:35:36 -04002309 /* Servers should only apply open mode checks for file size changes */
2310 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2311 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2312
2313 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2314 /* Use that stateid */
2315 } else if (truncate && state != NULL && nfs4_valid_open_stateid(state)) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002316 struct nfs_lockowner lockowner = {
2317 .l_owner = current->files,
2318 .l_pid = current->tgid,
2319 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002320 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002321 &lockowner);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002322 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002323 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
Bryan Schumaker7c513052011-03-24 17:12:24 +00002325 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002326 if (status == 0 && state != NULL)
2327 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002328 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329}
2330
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002331static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2332 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002333 struct nfs4_state *state, struct nfs4_label *ilabel,
2334 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002336 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002337 struct nfs4_exception exception = {
2338 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002339 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002340 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 int err;
2342 do {
David Quigley1775fd32013-05-22 12:50:42 -04002343 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
Trond Myklebustc1578b72013-08-12 16:58:42 -04002344 trace_nfs4_setattr(inode, err);
Trond Myklebust451146b2012-04-18 16:29:11 -04002345 switch (err) {
2346 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002347 if (!(sattr->ia_valid & ATTR_SIZE)) {
2348 pr_warn_once("NFSv4: server %s is incorrectly "
2349 "applying open mode checks to "
2350 "a SETATTR that is not "
2351 "changing file size.\n",
2352 server->nfs_client->cl_hostname);
2353 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002354 if (state && !(state->state & FMODE_WRITE)) {
2355 err = -EBADF;
2356 if (sattr->ia_valid & ATTR_OPEN)
2357 err = -EACCES;
2358 goto out;
2359 }
2360 }
2361 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002363out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 return err;
2365}
2366
2367struct nfs4_closedata {
2368 struct inode *inode;
2369 struct nfs4_state *state;
2370 struct nfs_closeargs arg;
2371 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002372 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002373 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002374 bool roc;
2375 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376};
2377
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002378static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002379{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002380 struct nfs4_closedata *calldata = data;
2381 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002382 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002383
Fred Isamanf7e89172011-01-06 11:36:32 +00002384 if (calldata->roc)
2385 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002386 nfs4_put_open_state(calldata->state);
2387 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002388 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002389 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002390 kfree(calldata);
2391}
2392
Trond Myklebust88069f72009-12-08 08:33:16 -05002393static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2394 fmode_t fmode)
2395{
2396 spin_lock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002397 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002398 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
2399 case FMODE_WRITE:
2400 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2401 break;
2402 case FMODE_READ:
2403 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2404 break;
2405 case 0:
2406 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2407 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2408 clear_bit(NFS_OPEN_STATE, &state->flags);
2409 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002410 spin_unlock(&state->owner->so_lock);
2411}
2412
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002413static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002415 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 struct nfs_server *server = NFS_SERVER(calldata->inode);
2418
Chuck Levera3ca5652012-03-01 17:00:40 -05002419 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002420 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2421 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002422 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 /* hmm. we are done with the inode, and in the process of freeing
2424 * the state_owner. we keep this around to process errors
2425 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 switch (task->tk_status) {
2427 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002428 if (calldata->roc)
2429 pnfs_roc_set_barrier(state->inode,
2430 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002431 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002432 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002433 nfs4_close_clear_stateid_flags(state,
2434 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 break;
2436 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002437 case -NFS4ERR_OLD_STATEID:
2438 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002440 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002441 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002443 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2444 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002446 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002447 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002448 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449}
2450
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002451static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002453 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002454 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002455 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002456 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002457
Chuck Levera3ca5652012-03-01 17:00:40 -05002458 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002459 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002460 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002461
Trond Myklebust88069f72009-12-08 08:33:16 -05002462 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2463 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002464 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002465 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002466 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002467 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002468 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2469 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2470 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002471 }
2472 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002473 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2474 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2475 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002476 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002477 }
Trond Myklebust5d422302013-03-14 16:57:48 -04002478 if (!nfs4_valid_open_stateid(state))
2479 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002480 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002481
2482 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002483 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002484 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002485 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002486
Fred Isamanf7e89172011-01-06 11:36:32 +00002487 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002488 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002489 if (calldata->roc &&
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002490 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2491 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002492 goto out_wait;
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002493 }
Fred Isamanf7e89172011-01-06 11:36:32 +00002494 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002495
Trond Myklebust516a6af2005-10-27 22:12:41 -04002496 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002497 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002498 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002499 &calldata->arg.seq_args,
2500 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002501 task) != 0)
2502 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002503 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002504 return;
2505out_no_action:
2506 task->tk_action = NULL;
2507out_wait:
2508 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509}
2510
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002511static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002512 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002513 .rpc_call_done = nfs4_close_done,
2514 .rpc_release = nfs4_free_closedata,
2515};
2516
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517/*
2518 * It is possible for data to be read/written from a mem-mapped file
2519 * after the sys_close call (which hits the vfs layer as a flush).
2520 * This means that we can't safely call nfsv4 close on a file until
2521 * the inode is cleared. This in turn means that we are not good
2522 * NFSv4 citizens - we do not indicate to the server to update the file's
2523 * share state even when we are done with one of the three share
2524 * stateid's in the inode.
2525 *
2526 * NOTE: Caller must be holding the sp->so_owner semaphore!
2527 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002528int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002530 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002532 struct nfs4_state_owner *sp = state->owner;
2533 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002534 struct rpc_message msg = {
2535 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2536 .rpc_cred = state->owner->so_cred,
2537 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002538 struct rpc_task_setup task_setup_data = {
2539 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002540 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002541 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002542 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002543 .flags = RPC_TASK_ASYNC,
2544 };
Trond Myklebust95121352005-10-18 14:20:12 -07002545 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002547 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002549 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002550 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002551 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002553 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002554 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002556 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002557 if (calldata->arg.seqid == NULL)
2558 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002559 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002560 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002561 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002562 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002563 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002564 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002565 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002566
Trond Myklebust1174dd12010-12-21 10:52:24 -05002567 msg.rpc_argp = &calldata->arg;
2568 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002569 task_setup_data.callback_data = calldata;
2570 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002571 if (IS_ERR(task))
2572 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002573 status = 0;
2574 if (wait)
2575 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002576 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002577 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002578out_free_calldata:
2579 kfree(calldata);
2580out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002581 nfs4_put_open_state(state);
2582 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002583 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584}
2585
Trond Myklebust2b484292010-09-17 10:56:51 -04002586static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002587nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04002590 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2591
2592 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593
Trond Myklebust565277f2007-10-15 18:17:53 -04002594 /* Protect against concurrent sillydeletes */
Trond Myklebust959d9212013-06-28 16:29:51 -04002595 state = nfs4_do_open(dir, ctx, open_flags, attr, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04002596
2597 nfs4_label_release_security(label);
2598
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002599 if (IS_ERR(state))
2600 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04002601 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602}
2603
Trond Myklebust1185a552009-12-03 15:54:02 -05002604static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002605{
2606 if (ctx->state == NULL)
2607 return;
2608 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002609 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002610 else
Al Viro643168c2011-06-22 18:20:23 -04002611 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002612}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613
2614static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2615{
Benny Halevy43652ad2009-04-01 09:21:54 -04002616 struct nfs4_server_caps_arg args = {
2617 .fhandle = fhandle,
2618 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 struct nfs4_server_caps_res res = {};
2620 struct rpc_message msg = {
2621 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002622 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 .rpc_resp = &res,
2624 };
2625 int status;
2626
Bryan Schumaker7c513052011-03-24 17:12:24 +00002627 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 if (status == 0) {
2629 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002630 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2631 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2632 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2633 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2634 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2636 server->caps |= NFS_CAP_ACLS;
2637 if (res.has_links != 0)
2638 server->caps |= NFS_CAP_HARDLINKS;
2639 if (res.has_symlinks != 0)
2640 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002641 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2642 server->caps |= NFS_CAP_FILEID;
2643 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2644 server->caps |= NFS_CAP_MODE;
2645 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2646 server->caps |= NFS_CAP_NLINK;
2647 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2648 server->caps |= NFS_CAP_OWNER;
2649 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2650 server->caps |= NFS_CAP_OWNER_GROUP;
2651 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2652 server->caps |= NFS_CAP_ATIME;
2653 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2654 server->caps |= NFS_CAP_CTIME;
2655 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2656 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04002657#ifdef CONFIG_NFS_V4_SECURITY_LABEL
2658 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
2659 server->caps |= NFS_CAP_SECURITY_LABEL;
2660#endif
2661 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
2662 sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002663
David Quigleyaa9c2662013-05-22 12:50:44 -04002664 if (server->caps & NFS_CAP_SECURITY_LABEL) {
2665 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2666 res.attr_bitmask[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2667 }
Trond Myklebusta65318b2009-03-11 14:10:28 -04002668 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2669 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2670 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002672 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002674
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 return status;
2676}
2677
Trond Myklebust55a97592006-06-09 09:34:19 -04002678int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679{
2680 struct nfs4_exception exception = { };
2681 int err;
2682 do {
2683 err = nfs4_handle_exception(server,
2684 _nfs4_server_capabilities(server, fhandle),
2685 &exception);
2686 } while (exception.retry);
2687 return err;
2688}
2689
2690static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2691 struct nfs_fsinfo *info)
2692{
David Quigleyaa9c2662013-05-22 12:50:44 -04002693 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04002695 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 };
2697 struct nfs4_lookup_res res = {
2698 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002699 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 .fh = fhandle,
2701 };
2702 struct rpc_message msg = {
2703 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2704 .rpc_argp = &args,
2705 .rpc_resp = &res,
2706 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002707
David Quigleyaa9c2662013-05-22 12:50:44 -04002708 bitmask[0] = nfs4_fattr_bitmap[0];
2709 bitmask[1] = nfs4_fattr_bitmap[1];
2710 /*
2711 * Process the label in the upcoming getfattr
2712 */
2713 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
2714
Trond Myklebust0e574af2005-10-27 22:12:38 -04002715 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002716 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717}
2718
2719static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2720 struct nfs_fsinfo *info)
2721{
2722 struct nfs4_exception exception = { };
2723 int err;
2724 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002725 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04002726 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002727 switch (err) {
2728 case 0:
2729 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002730 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002731 default:
2732 err = nfs4_handle_exception(server, err, &exception);
2733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002735out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 return err;
2737}
2738
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002739static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2740 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2741{
Trond Myklebustc2190662013-08-26 19:23:04 -04002742 struct rpc_auth_create_args auth_args = {
2743 .pseudoflavor = flavor,
2744 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002745 struct rpc_auth *auth;
2746 int ret;
2747
Trond Myklebustc2190662013-08-26 19:23:04 -04002748 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002749 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04002750 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002751 goto out;
2752 }
2753 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002754out:
2755 return ret;
2756}
2757
Chuck Lever9a744ba2013-03-16 15:56:02 -04002758/*
2759 * Retry pseudoroot lookup with various security flavors. We do this when:
2760 *
2761 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2762 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2763 *
2764 * Returns zero on success, or a negative NFS4ERR value, or a
2765 * negative errno value.
2766 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002767static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002768 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769{
Chuck Lever9a744ba2013-03-16 15:56:02 -04002770 /* Per 3530bis 15.33.5 */
2771 static const rpc_authflavor_t flav_array[] = {
2772 RPC_AUTH_GSS_KRB5P,
2773 RPC_AUTH_GSS_KRB5I,
2774 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04002775 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04002776 RPC_AUTH_NULL,
2777 };
2778 int status = -EPERM;
2779 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780
Chuck Lever9a744ba2013-03-16 15:56:02 -04002781 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002782 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002783 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002784 continue;
2785 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002786 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04002787
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002788 /*
2789 * -EACCESS could mean that the user doesn't have correct permissions
2790 * to access the mount. It could also mean that we tried to mount
2791 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2792 * existing mount programs don't handle -EACCES very well so it should
2793 * be mapped to -EPERM instead.
2794 */
2795 if (status == -EACCES)
2796 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002797 return status;
2798}
2799
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002800static int nfs4_do_find_root_sec(struct nfs_server *server,
2801 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
2802{
2803 int mv = server->nfs_client->cl_minorversion;
2804 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
2805}
2806
2807/**
2808 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
2809 * @server: initialized nfs_server handle
2810 * @fhandle: we fill in the pseudo-fs root file handle
2811 * @info: we fill in an FSINFO struct
2812 *
2813 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002814 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002815int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2816 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002817{
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002818 int status;
2819
2820 status = nfs4_lookup_root(server, fhandle, info);
2821 if ((status == -NFS4ERR_WRONGSEC) &&
2822 !(server->flags & NFS_MOUNT_SECFLAVOUR))
2823 status = nfs4_do_find_root_sec(server, fhandle, info);
2824
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 if (status == 0)
2826 status = nfs4_server_capabilities(server, fhandle);
2827 if (status == 0)
2828 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002829
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002830 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831}
2832
Bryan Schumakerbae36242012-05-10 15:07:31 -04002833static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2834 struct nfs_fsinfo *info)
2835{
2836 int error;
2837 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04002838 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04002839
2840 error = nfs4_server_capabilities(server, mntfh);
2841 if (error < 0) {
2842 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2843 return error;
2844 }
2845
David Quigley14c43f72013-05-22 12:50:43 -04002846 label = nfs4_label_alloc(server, GFP_KERNEL);
2847 if (IS_ERR(label))
2848 return PTR_ERR(label);
2849
David Quigley1775fd32013-05-22 12:50:42 -04002850 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04002851 if (error < 0) {
2852 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04002853 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04002854 }
2855
2856 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2857 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2858 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2859
David Quigley14c43f72013-05-22 12:50:43 -04002860err_free_label:
2861 nfs4_label_free(label);
2862
Bryan Schumakerbae36242012-05-10 15:07:31 -04002863 return error;
2864}
2865
Manoj Naik6b97fd32006-06-09 09:34:29 -04002866/*
2867 * Get locations and (maybe) other attributes of a referral.
2868 * Note that we'll actually follow the referral later when
2869 * we detect fsid mismatch in inode revalidation
2870 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002871static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2872 const struct qstr *name, struct nfs_fattr *fattr,
2873 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002874{
2875 int status = -ENOMEM;
2876 struct page *page = NULL;
2877 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002878
2879 page = alloc_page(GFP_KERNEL);
2880 if (page == NULL)
2881 goto out;
2882 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2883 if (locations == NULL)
2884 goto out;
2885
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002886 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002887 if (status != 0)
2888 goto out;
2889 /* Make sure server returned a different fsid for the referral */
2890 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002891 dprintk("%s: server did not return a different fsid for"
2892 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002893 status = -EIO;
2894 goto out;
2895 }
Andy Adamson533eb462011-06-13 18:25:56 -04002896 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2897 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002898
Andy Adamson533eb462011-06-13 18:25:56 -04002899 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002900 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002901 memset(fhandle, 0, sizeof(struct nfs_fh));
2902out:
2903 if (page)
2904 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002905 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002906 return status;
2907}
2908
David Quigley1775fd32013-05-22 12:50:42 -04002909static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
2910 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911{
2912 struct nfs4_getattr_arg args = {
2913 .fh = fhandle,
2914 .bitmask = server->attr_bitmask,
2915 };
2916 struct nfs4_getattr_res res = {
2917 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002918 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 .server = server,
2920 };
2921 struct rpc_message msg = {
2922 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2923 .rpc_argp = &args,
2924 .rpc_resp = &res,
2925 };
David Quigleyaa9c2662013-05-22 12:50:44 -04002926
2927 args.bitmask = nfs4_bitmask(server, label);
2928
Trond Myklebust0e574af2005-10-27 22:12:38 -04002929 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002930 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931}
2932
David Quigley1775fd32013-05-22 12:50:42 -04002933static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
2934 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935{
2936 struct nfs4_exception exception = { };
2937 int err;
2938 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04002939 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
2940 trace_nfs4_getattr(server, fhandle, fattr, err);
2941 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 &exception);
2943 } while (exception.retry);
2944 return err;
2945}
2946
2947/*
2948 * The file is not closed if it is opened due to the a request to change
2949 * the size of the file. The open call will not be needed once the
2950 * VFS layer lookup-intents are implemented.
2951 *
2952 * Close is called when the inode is destroyed.
2953 * If we haven't opened the file for O_WRONLY, we
2954 * need to in the size_change case to obtain a stateid.
2955 *
2956 * Got race?
2957 * Because OPEN is always done by name in nfsv4, it is
2958 * possible that we opened a different file by the same
2959 * name. We can recognize this race condition, but we
2960 * can't do anything about it besides returning an error.
2961 *
2962 * This will be fixed with VFS changes (lookup-intent).
2963 */
2964static int
2965nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2966 struct iattr *sattr)
2967{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002968 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002969 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002970 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04002971 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 int status;
2973
Benny Halevy8a1636c2010-07-14 15:43:57 -04002974 if (pnfs_ld_layoutret_on_setattr(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04002975 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04002976
Trond Myklebust0e574af2005-10-27 22:12:38 -04002977 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
Andy Adamson26699402012-05-30 16:12:24 -04002979 /* Deal with open(O_TRUNC) */
2980 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03002981 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04002982
2983 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03002984 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04002985 return 0;
2986
Trond Myklebustd5308382005-11-04 15:33:38 -05002987 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002988 if (sattr->ia_valid & ATTR_FILE) {
2989 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002990
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002991 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002992 if (ctx) {
2993 cred = ctx->cred;
2994 state = ctx->state;
2995 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002996 }
2997
David Quigley14c43f72013-05-22 12:50:43 -04002998 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
2999 if (IS_ERR(label))
3000 return PTR_ERR(label);
3001
3002 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003003 if (status == 0) {
Trond Myklebust65e43082005-08-16 11:49:44 -04003004 nfs_setattr_update_inode(inode, sattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003005 nfs_setsecurity(inode, fattr, label);
3006 }
David Quigley14c43f72013-05-22 12:50:43 -04003007 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 return status;
3009}
3010
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003011static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3012 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003013 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003014{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003015 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003016 int status;
3017 struct nfs4_lookup_arg args = {
3018 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003019 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003020 .name = name,
3021 };
3022 struct nfs4_lookup_res res = {
3023 .server = server,
3024 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003025 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003026 .fh = fhandle,
3027 };
3028 struct rpc_message msg = {
3029 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3030 .rpc_argp = &args,
3031 .rpc_resp = &res,
3032 };
3033
David Quigleyaa9c2662013-05-22 12:50:44 -04003034 args.bitmask = nfs4_bitmask(server, label);
3035
David Howells2b3de442006-08-22 20:06:09 -04003036 nfs_fattr_init(fattr);
3037
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003039 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 dprintk("NFS reply lookup: %d\n", status);
3041 return status;
3042}
3043
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003044static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003045{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003046 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003047 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003048 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3049 fattr->nlink = 2;
3050}
3051
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003052static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3053 struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003054 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003055{
3056 struct nfs4_exception exception = { };
3057 struct rpc_clnt *client = *clnt;
3058 int err;
3059 do {
David Quigley1775fd32013-05-22 12:50:42 -04003060 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003061 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003062 switch (err) {
3063 case -NFS4ERR_BADNAME:
3064 err = -ENOENT;
3065 goto out;
3066 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003067 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003068 goto out;
3069 case -NFS4ERR_WRONGSEC:
3070 err = -EPERM;
3071 if (client != *clnt)
3072 goto out;
3073
3074 client = nfs4_create_sec_client(client, dir, name);
3075 if (IS_ERR(client))
3076 return PTR_ERR(client);
3077
3078 exception.retry = 1;
3079 break;
3080 default:
3081 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3082 }
3083 } while (exception.retry);
3084
3085out:
3086 if (err == 0)
3087 *clnt = client;
3088 else if (client != *clnt)
3089 rpc_shutdown_client(client);
3090
3091 return err;
3092}
3093
Bryan Schumaker80a16b22012-04-27 13:27:46 -04003094static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003095 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3096 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003098 int status;
3099 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003100
David Quigley1775fd32013-05-22 12:50:42 -04003101 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003102 if (client != NFS_CLIENT(dir)) {
3103 rpc_shutdown_client(client);
3104 nfs_fixup_secinfo_attributes(fattr);
3105 }
3106 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107}
3108
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003109struct rpc_clnt *
3110nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3111 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3112{
3113 int status;
3114 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
3115
David Quigley1775fd32013-05-22 12:50:42 -04003116 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003117 if (status < 0) {
3118 rpc_shutdown_client(client);
3119 return ERR_PTR(status);
3120 }
3121 return client;
3122}
3123
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3125{
Trond Myklebust76b32992007-08-10 17:45:11 -04003126 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 struct nfs4_accessargs args = {
3128 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003129 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003131 struct nfs4_accessres res = {
3132 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003133 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 struct rpc_message msg = {
3135 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3136 .rpc_argp = &args,
3137 .rpc_resp = &res,
3138 .rpc_cred = entry->cred,
3139 };
3140 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003141 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142
3143 /*
3144 * Determine which access bits we want to ask for...
3145 */
3146 if (mode & MAY_READ)
3147 args.access |= NFS4_ACCESS_READ;
3148 if (S_ISDIR(inode->i_mode)) {
3149 if (mode & MAY_WRITE)
3150 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3151 if (mode & MAY_EXEC)
3152 args.access |= NFS4_ACCESS_LOOKUP;
3153 } else {
3154 if (mode & MAY_WRITE)
3155 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3156 if (mode & MAY_EXEC)
3157 args.access |= NFS4_ACCESS_EXECUTE;
3158 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003159
3160 res.fattr = nfs_alloc_fattr();
3161 if (res.fattr == NULL)
3162 return -ENOMEM;
3163
Bryan Schumaker7c513052011-03-24 17:12:24 +00003164 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003166 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003167 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003169 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 return status;
3171}
3172
3173static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3174{
3175 struct nfs4_exception exception = { };
3176 int err;
3177 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003178 err = _nfs4_proc_access(inode, entry);
3179 trace_nfs4_access(inode, err);
3180 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 &exception);
3182 } while (exception.retry);
3183 return err;
3184}
3185
3186/*
3187 * TODO: For the time being, we don't try to get any attributes
3188 * along with any of the zero-copy operations READ, READDIR,
3189 * READLINK, WRITE.
3190 *
3191 * In the case of the first three, we want to put the GETATTR
3192 * after the read-type operation -- this is because it is hard
3193 * to predict the length of a GETATTR response in v4, and thus
3194 * align the READ data correctly. This means that the GETATTR
3195 * may end up partially falling into the page cache, and we should
3196 * shift it into the 'tail' of the xdr_buf before processing.
3197 * To do this efficiently, we need to know the total length
3198 * of data received, which doesn't seem to be available outside
3199 * of the RPC layer.
3200 *
3201 * In the case of WRITE, we also want to put the GETATTR after
3202 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003203 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 *
3205 * Both of these changes to the XDR layer would in fact be quite
3206 * minor, but I decided to leave them for a subsequent patch.
3207 */
3208static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3209 unsigned int pgbase, unsigned int pglen)
3210{
3211 struct nfs4_readlink args = {
3212 .fh = NFS_FH(inode),
3213 .pgbase = pgbase,
3214 .pglen = pglen,
3215 .pages = &page,
3216 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003217 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 struct rpc_message msg = {
3219 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3220 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003221 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 };
3223
Bryan Schumaker7c513052011-03-24 17:12:24 +00003224 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 -07003225}
3226
3227static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3228 unsigned int pgbase, unsigned int pglen)
3229{
3230 struct nfs4_exception exception = { };
3231 int err;
3232 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003233 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3234 trace_nfs4_readlink(inode, err);
3235 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 &exception);
3237 } while (exception.retry);
3238 return err;
3239}
3240
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003242 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244static int
3245nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003246 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247{
David Quigleyaa9c2662013-05-22 12:50:44 -04003248 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003249 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 int status = 0;
3252
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003253 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3254 if (IS_ERR(ctx))
3255 return PTR_ERR(ctx);
3256
David Quigleyaa9c2662013-05-22 12:50:44 -04003257 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3258
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003259 sattr->ia_mode &= ~current_umask();
Trond Myklebust959d9212013-06-28 16:29:51 -04003260 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 if (IS_ERR(state)) {
3262 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003263 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003266 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003267 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 return status;
3269}
3270
3271static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3272{
Trond Myklebust16e42952005-10-27 22:12:44 -04003273 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003274 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003276 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003278 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003279 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003280 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003282 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3283 .rpc_argp = &args,
3284 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003286 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003287
Bryan Schumaker7c513052011-03-24 17:12:24 +00003288 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003289 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003290 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 return status;
3292}
3293
3294static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3295{
3296 struct nfs4_exception exception = { };
3297 int err;
3298 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003299 err = _nfs4_proc_remove(dir, name);
3300 trace_nfs4_remove(dir, name, err);
3301 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 &exception);
3303 } while (exception.retry);
3304 return err;
3305}
3306
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003307static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003309 struct nfs_server *server = NFS_SERVER(dir);
3310 struct nfs_removeargs *args = msg->rpc_argp;
3311 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003313 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003315 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003316
3317 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318}
3319
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003320static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3321{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003322 nfs4_setup_sequence(NFS_SERVER(data->dir),
3323 &data->args.seq_args,
3324 &data->res.seq_res,
3325 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326}
3327
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003328static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003330 struct nfs_unlinkdata *data = task->tk_calldata;
3331 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003332
Trond Myklebust14516c32010-07-31 14:29:06 -04003333 if (!nfs4_sequence_done(task, &res->seq_res))
3334 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003335 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003336 return 0;
3337 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003338 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339}
3340
Jeff Laytond3d41522010-09-17 17:31:57 -04003341static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3342{
3343 struct nfs_server *server = NFS_SERVER(dir);
3344 struct nfs_renameargs *arg = msg->rpc_argp;
3345 struct nfs_renameres *res = msg->rpc_resp;
3346
3347 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003348 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003349 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003350}
3351
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003352static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3353{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003354 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3355 &data->args.seq_args,
3356 &data->res.seq_res,
3357 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003358}
3359
3360static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3361 struct inode *new_dir)
3362{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003363 struct nfs_renamedata *data = task->tk_calldata;
3364 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04003365
3366 if (!nfs4_sequence_done(task, &res->seq_res))
3367 return 0;
3368 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3369 return 0;
3370
3371 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003372 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003373 return 1;
3374}
3375
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3377 struct inode *new_dir, struct qstr *new_name)
3378{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003379 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003380 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 .old_dir = NFS_FH(old_dir),
3382 .new_dir = NFS_FH(new_dir),
3383 .old_name = old_name,
3384 .new_name = new_name,
3385 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003386 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003387 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003388 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 struct rpc_message msg = {
3390 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3391 .rpc_argp = &arg,
3392 .rpc_resp = &res,
3393 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003394 int status = -ENOMEM;
David Quigley14c43f72013-05-22 12:50:43 -04003395
Bryan Schumaker7c513052011-03-24 17:12:24 +00003396 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 if (!status) {
3398 update_changeattr(old_dir, &res.old_cinfo);
3399 update_changeattr(new_dir, &res.new_cinfo);
3400 }
3401 return status;
3402}
3403
3404static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3405 struct inode *new_dir, struct qstr *new_name)
3406{
3407 struct nfs4_exception exception = { };
3408 int err;
3409 do {
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003410 err = _nfs4_proc_rename(old_dir, old_name,
3411 new_dir, new_name);
3412 trace_nfs4_rename(old_dir, old_name, new_dir, new_name, err);
3413 err = nfs4_handle_exception(NFS_SERVER(old_dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 &exception);
3415 } while (exception.retry);
3416 return err;
3417}
3418
3419static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3420{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003421 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 struct nfs4_link_arg arg = {
3423 .fh = NFS_FH(inode),
3424 .dir_fh = NFS_FH(dir),
3425 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003426 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003428 struct nfs4_link_res res = {
3429 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003430 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003431 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 struct rpc_message msg = {
3433 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3434 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003435 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003437 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438
Trond Myklebust136f2622010-04-16 16:22:49 -04003439 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003440 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003441 goto out;
3442
David Quigley14c43f72013-05-22 12:50:43 -04003443 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3444 if (IS_ERR(res.label)) {
3445 status = PTR_ERR(res.label);
3446 goto out;
3447 }
David Quigleyaa9c2662013-05-22 12:50:44 -04003448 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04003449
Bryan Schumaker7c513052011-03-24 17:12:24 +00003450 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003451 if (!status) {
3452 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04003453 status = nfs_post_op_update_inode(inode, res.fattr);
3454 if (!status)
3455 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003456 }
David Quigley14c43f72013-05-22 12:50:43 -04003457
3458
3459 nfs4_label_free(res.label);
3460
Trond Myklebust136f2622010-04-16 16:22:49 -04003461out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003462 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 return status;
3464}
3465
3466static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3467{
3468 struct nfs4_exception exception = { };
3469 int err;
3470 do {
3471 err = nfs4_handle_exception(NFS_SERVER(inode),
3472 _nfs4_proc_link(inode, dir, name),
3473 &exception);
3474 } while (exception.retry);
3475 return err;
3476}
3477
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003478struct nfs4_createdata {
3479 struct rpc_message msg;
3480 struct nfs4_create_arg arg;
3481 struct nfs4_create_res res;
3482 struct nfs_fh fh;
3483 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003484 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003485};
3486
3487static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3488 struct qstr *name, struct iattr *sattr, u32 ftype)
3489{
3490 struct nfs4_createdata *data;
3491
3492 data = kzalloc(sizeof(*data), GFP_KERNEL);
3493 if (data != NULL) {
3494 struct nfs_server *server = NFS_SERVER(dir);
3495
David Quigley14c43f72013-05-22 12:50:43 -04003496 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3497 if (IS_ERR(data->label))
3498 goto out_free;
3499
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003500 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3501 data->msg.rpc_argp = &data->arg;
3502 data->msg.rpc_resp = &data->res;
3503 data->arg.dir_fh = NFS_FH(dir);
3504 data->arg.server = server;
3505 data->arg.name = name;
3506 data->arg.attrs = sattr;
3507 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04003508 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003509 data->res.server = server;
3510 data->res.fh = &data->fh;
3511 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003512 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003513 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003514 }
3515 return data;
David Quigley14c43f72013-05-22 12:50:43 -04003516out_free:
3517 kfree(data);
3518 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003519}
3520
3521static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3522{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003523 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003524 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003525 if (status == 0) {
3526 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04003527 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003528 }
3529 return status;
3530}
3531
3532static void nfs4_free_createdata(struct nfs4_createdata *data)
3533{
David Quigley14c43f72013-05-22 12:50:43 -04003534 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003535 kfree(data);
3536}
3537
Chuck Lever4f390c12006-08-22 20:06:22 -04003538static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003539 struct page *page, unsigned int len, struct iattr *sattr,
3540 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003542 struct nfs4_createdata *data;
3543 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544
Chuck Lever94a6d752006-08-22 20:06:23 -04003545 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003546 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003547
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003548 status = -ENOMEM;
3549 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3550 if (data == NULL)
3551 goto out;
3552
3553 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3554 data->arg.u.symlink.pages = &page;
3555 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04003556 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003558 status = nfs4_do_create(dir, dentry, data);
3559
3560 nfs4_free_createdata(data);
3561out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 return status;
3563}
3564
Chuck Lever4f390c12006-08-22 20:06:22 -04003565static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003566 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567{
3568 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003569 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04003571
3572 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3573
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003575 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3576 trace_nfs4_symlink(dir, &dentry->d_name, err);
3577 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 &exception);
3579 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003580
3581 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 return err;
3583}
3584
3585static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003586 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003588 struct nfs4_createdata *data;
3589 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003591 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3592 if (data == NULL)
3593 goto out;
3594
David Quigley1775fd32013-05-22 12:50:42 -04003595 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003596 status = nfs4_do_create(dir, dentry, data);
3597
3598 nfs4_free_createdata(data);
3599out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 return status;
3601}
3602
3603static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3604 struct iattr *sattr)
3605{
3606 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003607 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003609
David Quigleyaa9c2662013-05-22 12:50:44 -04003610 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3611
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003612 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003614 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3615 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3616 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 &exception);
3618 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003619 nfs4_label_release_security(label);
3620
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 return err;
3622}
3623
3624static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003625 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626{
3627 struct inode *dir = dentry->d_inode;
3628 struct nfs4_readdir_arg args = {
3629 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003630 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 .pgbase = 0,
3632 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003633 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003634 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 };
3636 struct nfs4_readdir_res res;
3637 struct rpc_message msg = {
3638 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3639 .rpc_argp = &args,
3640 .rpc_resp = &res,
3641 .rpc_cred = cred,
3642 };
3643 int status;
3644
Harvey Harrison3110ff82008-05-02 13:42:44 -07003645 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003646 dentry->d_parent->d_name.name,
3647 dentry->d_name.name,
3648 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003649 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003651 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 -05003652 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003653 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003654 status += args.pgbase;
3655 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003656
3657 nfs_invalidate_atime(dir);
3658
Harvey Harrison3110ff82008-05-02 13:42:44 -07003659 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 return status;
3661}
3662
3663static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003664 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665{
3666 struct nfs4_exception exception = { };
3667 int err;
3668 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003669 err = _nfs4_proc_readdir(dentry, cred, cookie,
3670 pages, count, plus);
3671 trace_nfs4_readdir(dentry->d_inode, err);
3672 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 &exception);
3674 } while (exception.retry);
3675 return err;
3676}
3677
3678static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04003679 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003681 struct nfs4_createdata *data;
3682 int mode = sattr->ia_mode;
3683 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003685 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3686 if (data == NULL)
3687 goto out;
3688
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003690 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003692 data->arg.ftype = NF4BLK;
3693 data->arg.u.device.specdata1 = MAJOR(rdev);
3694 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 }
3696 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003697 data->arg.ftype = NF4CHR;
3698 data->arg.u.device.specdata1 = MAJOR(rdev);
3699 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003700 } else if (!S_ISSOCK(mode)) {
3701 status = -EINVAL;
3702 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 }
David Quigley1775fd32013-05-22 12:50:42 -04003704
David Quigleyaa9c2662013-05-22 12:50:44 -04003705 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003706 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003707out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003708 nfs4_free_createdata(data);
3709out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 return status;
3711}
3712
3713static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3714 struct iattr *sattr, dev_t rdev)
3715{
3716 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003717 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003719
David Quigleyaa9c2662013-05-22 12:50:44 -04003720 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3721
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003722 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003724 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
3725 trace_nfs4_mknod(dir, &dentry->d_name, err);
3726 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 &exception);
3728 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003729
3730 nfs4_label_release_security(label);
3731
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 return err;
3733}
3734
3735static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3736 struct nfs_fsstat *fsstat)
3737{
3738 struct nfs4_statfs_arg args = {
3739 .fh = fhandle,
3740 .bitmask = server->attr_bitmask,
3741 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003742 struct nfs4_statfs_res res = {
3743 .fsstat = fsstat,
3744 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 struct rpc_message msg = {
3746 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3747 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003748 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 };
3750
Trond Myklebust0e574af2005-10-27 22:12:38 -04003751 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003752 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753}
3754
3755static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3756{
3757 struct nfs4_exception exception = { };
3758 int err;
3759 do {
3760 err = nfs4_handle_exception(server,
3761 _nfs4_proc_statfs(server, fhandle, fsstat),
3762 &exception);
3763 } while (exception.retry);
3764 return err;
3765}
3766
3767static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3768 struct nfs_fsinfo *fsinfo)
3769{
3770 struct nfs4_fsinfo_arg args = {
3771 .fh = fhandle,
3772 .bitmask = server->attr_bitmask,
3773 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003774 struct nfs4_fsinfo_res res = {
3775 .fsinfo = fsinfo,
3776 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 struct rpc_message msg = {
3778 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3779 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003780 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 };
3782
Bryan Schumaker7c513052011-03-24 17:12:24 +00003783 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784}
3785
3786static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3787{
3788 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04003789 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 int err;
3791
3792 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04003793 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003794 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04003795 if (err == 0) {
3796 struct nfs_client *clp = server->nfs_client;
3797
3798 spin_lock(&clp->cl_lock);
3799 clp->cl_lease_time = fsinfo->lease_time * HZ;
3800 clp->cl_last_renewal = now;
3801 spin_unlock(&clp->cl_lock);
3802 break;
3803 }
3804 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 } while (exception.retry);
3806 return err;
3807}
3808
3809static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3810{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003811 int error;
3812
Trond Myklebust0e574af2005-10-27 22:12:38 -04003813 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003814 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003815 if (error == 0) {
3816 /* block layout checks this! */
3817 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003818 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003819 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003820
3821 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822}
3823
3824static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3825 struct nfs_pathconf *pathconf)
3826{
3827 struct nfs4_pathconf_arg args = {
3828 .fh = fhandle,
3829 .bitmask = server->attr_bitmask,
3830 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003831 struct nfs4_pathconf_res res = {
3832 .pathconf = pathconf,
3833 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 struct rpc_message msg = {
3835 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3836 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003837 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 };
3839
3840 /* None of the pathconf attributes are mandatory to implement */
3841 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3842 memset(pathconf, 0, sizeof(*pathconf));
3843 return 0;
3844 }
3845
Trond Myklebust0e574af2005-10-27 22:12:38 -04003846 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003847 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848}
3849
3850static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3851 struct nfs_pathconf *pathconf)
3852{
3853 struct nfs4_exception exception = { };
3854 int err;
3855
3856 do {
3857 err = nfs4_handle_exception(server,
3858 _nfs4_proc_pathconf(server, fhandle, pathconf),
3859 &exception);
3860 } while (exception.retry);
3861 return err;
3862}
3863
Trond Myklebust5521abf2013-03-16 20:54:34 -04003864int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04003865 const struct nfs_open_context *ctx,
3866 const struct nfs_lock_context *l_ctx,
3867 fmode_t fmode)
3868{
3869 const struct nfs_lockowner *lockowner = NULL;
3870
3871 if (l_ctx != NULL)
3872 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003873 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04003874}
3875EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
3876
Trond Myklebust5521abf2013-03-16 20:54:34 -04003877static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
3878 const struct nfs_open_context *ctx,
3879 const struct nfs_lock_context *l_ctx,
3880 fmode_t fmode)
3881{
3882 nfs4_stateid current_stateid;
3883
3884 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode))
3885 return false;
3886 return nfs4_stateid_match(stateid, &current_stateid);
3887}
3888
3889static bool nfs4_error_stateid_expired(int err)
3890{
3891 switch (err) {
3892 case -NFS4ERR_DELEG_REVOKED:
3893 case -NFS4ERR_ADMIN_REVOKED:
3894 case -NFS4ERR_BAD_STATEID:
3895 case -NFS4ERR_STALE_STATEID:
3896 case -NFS4ERR_OLD_STATEID:
3897 case -NFS4ERR_OPENMODE:
3898 case -NFS4ERR_EXPIRED:
3899 return true;
3900 }
3901 return false;
3902}
3903
Benny Halevyd20581a2011-05-22 19:52:03 +03003904void __nfs4_read_done_cb(struct nfs_read_data *data)
3905{
Fred Isamancd841602012-04-20 14:47:44 -04003906 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003907}
3908
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003909static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910{
Fred Isamancd841602012-04-20 14:47:44 -04003911 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912
Trond Myklebustcc668ab2013-08-14 15:31:28 -04003913 trace_nfs4_read(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003914 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003915 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003916 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003918
Benny Halevyd20581a2011-05-22 19:52:03 +03003919 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003921 renew_lease(server, data->timestamp);
3922 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923}
3924
Trond Myklebust5521abf2013-03-16 20:54:34 -04003925static bool nfs4_read_stateid_changed(struct rpc_task *task,
3926 struct nfs_readargs *args)
3927{
3928
3929 if (!nfs4_error_stateid_expired(task->tk_status) ||
3930 nfs4_stateid_is_current(&args->stateid,
3931 args->context,
3932 args->lock_context,
3933 FMODE_READ))
3934 return false;
3935 rpc_restart_call_prepare(task);
3936 return true;
3937}
3938
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003939static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3940{
3941
3942 dprintk("--> %s\n", __func__);
3943
3944 if (!nfs4_sequence_done(task, &data->res.seq_res))
3945 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003946 if (nfs4_read_stateid_changed(task, &data->args))
3947 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003948 return data->read_done_cb ? data->read_done_cb(task, data) :
3949 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003950}
3951
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003952static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003955 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003956 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003957 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958}
3959
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003960static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3961{
Trond Myklebust9b206142013-03-17 15:52:00 -04003962 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003963 &data->args.seq_args,
3964 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04003965 task))
3966 return;
3967 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3968 data->args.lock_context, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969}
3970
Fred Isamanb029bc92011-03-03 15:13:42 +00003971static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972{
Fred Isamancd841602012-04-20 14:47:44 -04003973 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974
Trond Myklebustcc668ab2013-08-14 15:31:28 -04003975 trace_nfs4_write(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003976 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003977 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003978 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003980 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003982 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003983 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003984 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985}
3986
Trond Myklebust5521abf2013-03-16 20:54:34 -04003987static bool nfs4_write_stateid_changed(struct rpc_task *task,
3988 struct nfs_writeargs *args)
3989{
3990
3991 if (!nfs4_error_stateid_expired(task->tk_status) ||
3992 nfs4_stateid_is_current(&args->stateid,
3993 args->context,
3994 args->lock_context,
3995 FMODE_WRITE))
3996 return false;
3997 rpc_restart_call_prepare(task);
3998 return true;
3999}
4000
Fred Isamanb029bc92011-03-03 15:13:42 +00004001static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
4002{
4003 if (!nfs4_sequence_done(task, &data->res.seq_res))
4004 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004005 if (nfs4_write_stateid_changed(task, &data->args))
4006 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03004007 return data->write_done_cb ? data->write_done_cb(task, data) :
4008 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00004009}
4010
Trond Myklebust5a37f852012-04-28 14:55:16 -04004011static
4012bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00004013{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004014 const struct nfs_pgio_header *hdr = data->header;
4015
4016 /* Don't request attributes for pNFS or O_DIRECT writes */
4017 if (data->ds_clp != NULL || hdr->dreq != NULL)
4018 return false;
4019 /* Otherwise, request attributes if and only if we don't hold
4020 * a delegation
4021 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004022 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004023}
Fred Isamana69aef12011-03-03 15:13:47 +00004024
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004025static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026{
Fred Isamancd841602012-04-20 14:47:44 -04004027 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004028
Trond Myklebust5a37f852012-04-28 14:55:16 -04004029 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00004030 data->args.bitmask = NULL;
4031 data->res.fattr = NULL;
4032 } else
4033 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004034
Fred Isamanb029bc92011-03-03 15:13:42 +00004035 if (!data->write_done_cb)
4036 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004037 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 data->timestamp = jiffies;
4039
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004040 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05004041 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042}
4043
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04004044static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
4045{
Trond Myklebust9b206142013-03-17 15:52:00 -04004046 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004047 &data->args.seq_args,
4048 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004049 task))
4050 return;
4051 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4052 data->args.lock_context, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053}
4054
Fred Isaman0b7c0152012-04-20 14:47:39 -04004055static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4056{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004057 nfs4_setup_sequence(NFS_SERVER(data->inode),
4058 &data->args.seq_args,
4059 &data->res.seq_res,
4060 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061}
4062
Fred Isaman0b7c0152012-04-20 14:47:39 -04004063static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004066
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004067 trace_nfs4_commit(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004068 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004069 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004070 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004072 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073}
4074
Fred Isaman0b7c0152012-04-20 14:47:39 -04004075static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004076{
4077 if (!nfs4_sequence_done(task, &data->res.seq_res))
4078 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004079 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004080}
4081
Fred Isaman0b7c0152012-04-20 14:47:39 -04004082static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004084 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004085
Fred Isaman0b7c0152012-04-20 14:47:39 -04004086 if (data->commit_done_cb == NULL)
4087 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004088 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004089 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05004090 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091}
4092
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004093struct nfs4_renewdata {
4094 struct nfs_client *client;
4095 unsigned long timestamp;
4096};
4097
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098/*
4099 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4100 * standalone procedure for queueing an asynchronous RENEW.
4101 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004102static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004103{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004104 struct nfs4_renewdata *data = calldata;
4105 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004106
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004107 if (atomic_read(&clp->cl_count) > 1)
4108 nfs4_schedule_state_renewal(clp);
4109 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004110 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004111}
4112
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004113static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004115 struct nfs4_renewdata *data = calldata;
4116 struct nfs_client *clp = data->client;
4117 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004119 trace_nfs4_renew_async(clp, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04004121 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004122 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4123 return;
4124 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004125 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004126 return;
4127 }
4128 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004130 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131}
4132
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004133static const struct rpc_call_ops nfs4_renew_ops = {
4134 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004135 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004136};
4137
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004138static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139{
4140 struct rpc_message msg = {
4141 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4142 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004143 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004145 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004147 if (renew_flags == 0)
4148 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004149 if (!atomic_inc_not_zero(&clp->cl_count))
4150 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004151 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004152 if (data == NULL)
4153 return -ENOMEM;
4154 data->client = clp;
4155 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004156 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004157 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158}
4159
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004160static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161{
4162 struct rpc_message msg = {
4163 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4164 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004165 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 };
4167 unsigned long now = jiffies;
4168 int status;
4169
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004170 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 if (status < 0)
4172 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004173 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 return 0;
4175}
4176
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004177static inline int nfs4_server_supports_acls(struct nfs_server *server)
4178{
4179 return (server->caps & NFS_CAP_ACLS)
4180 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
4181 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
4182}
4183
Trond Myklebust21f498c2012-08-24 10:59:25 -04004184/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4185 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004186 * the stack.
4187 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004188#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004189
Neil Hormane9e3d722011-03-04 19:26:03 -05004190static int buf_to_pages_noslab(const void *buf, size_t buflen,
4191 struct page **pages, unsigned int *pgbase)
4192{
4193 struct page *newpage, **spages;
4194 int rc = 0;
4195 size_t len;
4196 spages = pages;
4197
4198 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004199 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004200 newpage = alloc_page(GFP_KERNEL);
4201
4202 if (newpage == NULL)
4203 goto unwind;
4204 memcpy(page_address(newpage), buf, len);
4205 buf += len;
4206 buflen -= len;
4207 *pages++ = newpage;
4208 rc++;
4209 } while (buflen != 0);
4210
4211 return rc;
4212
4213unwind:
4214 for(; rc > 0; rc--)
4215 __free_page(spages[rc-1]);
4216 return -ENOMEM;
4217}
4218
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004219struct nfs4_cached_acl {
4220 int cached;
4221 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004222 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004223};
4224
4225static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004226{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004227 struct nfs_inode *nfsi = NFS_I(inode);
4228
4229 spin_lock(&inode->i_lock);
4230 kfree(nfsi->nfs4_acl);
4231 nfsi->nfs4_acl = acl;
4232 spin_unlock(&inode->i_lock);
4233}
4234
4235static void nfs4_zap_acl_attr(struct inode *inode)
4236{
4237 nfs4_set_cached_acl(inode, NULL);
4238}
4239
4240static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4241{
4242 struct nfs_inode *nfsi = NFS_I(inode);
4243 struct nfs4_cached_acl *acl;
4244 int ret = -ENOENT;
4245
4246 spin_lock(&inode->i_lock);
4247 acl = nfsi->nfs4_acl;
4248 if (acl == NULL)
4249 goto out;
4250 if (buf == NULL) /* user is just asking for length */
4251 goto out_len;
4252 if (acl->cached == 0)
4253 goto out;
4254 ret = -ERANGE; /* see getxattr(2) man page */
4255 if (acl->len > buflen)
4256 goto out;
4257 memcpy(buf, acl->data, acl->len);
4258out_len:
4259 ret = acl->len;
4260out:
4261 spin_unlock(&inode->i_lock);
4262 return ret;
4263}
4264
Sachin Prabhu5794d212012-04-17 14:36:40 +01004265static 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 +00004266{
4267 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004268 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004269
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004270 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004271 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004272 if (acl == NULL)
4273 goto out;
4274 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004275 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004276 } else {
4277 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4278 if (acl == NULL)
4279 goto out;
4280 acl->cached = 0;
4281 }
4282 acl->len = acl_len;
4283out:
4284 nfs4_set_cached_acl(inode, acl);
4285}
4286
Andy Adamsonbf118a32011-12-07 11:55:27 -05004287/*
4288 * The getxattr API returns the required buffer length when called with a
4289 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4290 * the required buf. On a NULL buf, we send a page of data to the server
4291 * guessing that the ACL request can be serviced by a page. If so, we cache
4292 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4293 * the cache. If not so, we throw away the page, and cache the required
4294 * length. The next getxattr call will then produce another round trip to
4295 * the server, this time with the input buf of the required size.
4296 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004297static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004298{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004299 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004300 struct nfs_getaclargs args = {
4301 .fh = NFS_FH(inode),
4302 .acl_pages = pages,
4303 .acl_len = buflen,
4304 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004305 struct nfs_getaclres res = {
4306 .acl_len = buflen,
4307 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004308 struct rpc_message msg = {
4309 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4310 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004311 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004312 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004313 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4314 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004315
Andy Adamsonbf118a32011-12-07 11:55:27 -05004316 /* As long as we're doing a round trip to the server anyway,
4317 * let's be prepared for a page of acl data. */
4318 if (npages == 0)
4319 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004320 if (npages > ARRAY_SIZE(pages))
4321 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004322
Andy Adamsonbf118a32011-12-07 11:55:27 -05004323 for (i = 0; i < npages; i++) {
4324 pages[i] = alloc_page(GFP_KERNEL);
4325 if (!pages[i])
4326 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004327 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004328
4329 /* for decoding across pages */
4330 res.acl_scratch = alloc_page(GFP_KERNEL);
4331 if (!res.acl_scratch)
4332 goto out_free;
4333
Andy Adamsonbf118a32011-12-07 11:55:27 -05004334 args.acl_len = npages * PAGE_SIZE;
4335 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004336
Peng Taode040be2012-01-10 22:42:47 +08004337 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004338 __func__, buf, buflen, npages, args.acl_len);
4339 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4340 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004341 if (ret)
4342 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004343
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004344 /* Handle the case where the passed-in buffer is too short */
4345 if (res.acl_flags & NFS4_ACL_TRUNC) {
4346 /* Did the user only issue a request for the acl length? */
4347 if (buf == NULL)
4348 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004349 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004350 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004351 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004352 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004353 if (buf) {
4354 if (res.acl_len > buflen) {
4355 ret = -ERANGE;
4356 goto out_free;
4357 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004358 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004359 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004360out_ok:
4361 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004362out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004363 for (i = 0; i < npages; i++)
4364 if (pages[i])
4365 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004366 if (res.acl_scratch)
4367 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004368 return ret;
4369}
4370
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004371static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4372{
4373 struct nfs4_exception exception = { };
4374 ssize_t ret;
4375 do {
4376 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04004377 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004378 if (ret >= 0)
4379 break;
4380 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4381 } while (exception.retry);
4382 return ret;
4383}
4384
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004385static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4386{
4387 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004388 int ret;
4389
4390 if (!nfs4_server_supports_acls(server))
4391 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004392 ret = nfs_revalidate_inode(server, inode);
4393 if (ret < 0)
4394 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004395 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4396 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004397 ret = nfs4_read_cached_acl(inode, buf, buflen);
4398 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004399 /* -ENOENT is returned if there is no ACL or if there is an ACL
4400 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004401 return ret;
4402 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004403}
4404
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004405static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004406{
4407 struct nfs_server *server = NFS_SERVER(inode);
4408 struct page *pages[NFS4ACL_MAXPAGES];
4409 struct nfs_setaclargs arg = {
4410 .fh = NFS_FH(inode),
4411 .acl_pages = pages,
4412 .acl_len = buflen,
4413 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004414 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004415 struct rpc_message msg = {
4416 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4417 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004418 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004419 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004420 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004421 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004422
4423 if (!nfs4_server_supports_acls(server))
4424 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004425 if (npages > ARRAY_SIZE(pages))
4426 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004427 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4428 if (i < 0)
4429 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004430 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004431 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004432
4433 /*
4434 * Free each page after tx, so the only ref left is
4435 * held by the network stack
4436 */
4437 for (; i > 0; i--)
4438 put_page(pages[i-1]);
4439
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004440 /*
4441 * Acl update can result in inode attribute update.
4442 * so mark the attribute cache invalid.
4443 */
4444 spin_lock(&inode->i_lock);
4445 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4446 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004447 nfs_access_zap_cache(inode);
4448 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004449 return ret;
4450}
4451
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004452static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4453{
4454 struct nfs4_exception exception = { };
4455 int err;
4456 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004457 err = __nfs4_proc_set_acl(inode, buf, buflen);
4458 trace_nfs4_set_acl(inode, err);
4459 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004460 &exception);
4461 } while (exception.retry);
4462 return err;
4463}
4464
David Quigleyaa9c2662013-05-22 12:50:44 -04004465#ifdef CONFIG_NFS_V4_SECURITY_LABEL
4466static int _nfs4_get_security_label(struct inode *inode, void *buf,
4467 size_t buflen)
4468{
4469 struct nfs_server *server = NFS_SERVER(inode);
4470 struct nfs_fattr fattr;
4471 struct nfs4_label label = {0, 0, buflen, buf};
4472
4473 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4474 struct nfs4_getattr_arg args = {
4475 .fh = NFS_FH(inode),
4476 .bitmask = bitmask,
4477 };
4478 struct nfs4_getattr_res res = {
4479 .fattr = &fattr,
4480 .label = &label,
4481 .server = server,
4482 };
4483 struct rpc_message msg = {
4484 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4485 .rpc_argp = &args,
4486 .rpc_resp = &res,
4487 };
4488 int ret;
4489
4490 nfs_fattr_init(&fattr);
4491
4492 ret = rpc_call_sync(server->client, &msg, 0);
4493 if (ret)
4494 return ret;
4495 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4496 return -ENOENT;
4497 if (buflen < label.len)
4498 return -ERANGE;
4499 return 0;
4500}
4501
4502static int nfs4_get_security_label(struct inode *inode, void *buf,
4503 size_t buflen)
4504{
4505 struct nfs4_exception exception = { };
4506 int err;
4507
4508 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4509 return -EOPNOTSUPP;
4510
4511 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004512 err = _nfs4_get_security_label(inode, buf, buflen);
4513 trace_nfs4_get_security_label(inode, err);
4514 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004515 &exception);
4516 } while (exception.retry);
4517 return err;
4518}
4519
4520static int _nfs4_do_set_security_label(struct inode *inode,
4521 struct nfs4_label *ilabel,
4522 struct nfs_fattr *fattr,
4523 struct nfs4_label *olabel)
4524{
4525
4526 struct iattr sattr = {0};
4527 struct nfs_server *server = NFS_SERVER(inode);
4528 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4529 struct nfs_setattrargs args = {
4530 .fh = NFS_FH(inode),
4531 .iap = &sattr,
4532 .server = server,
4533 .bitmask = bitmask,
4534 .label = ilabel,
4535 };
4536 struct nfs_setattrres res = {
4537 .fattr = fattr,
4538 .label = olabel,
4539 .server = server,
4540 };
4541 struct rpc_message msg = {
4542 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
4543 .rpc_argp = &args,
4544 .rpc_resp = &res,
4545 };
4546 int status;
4547
4548 nfs4_stateid_copy(&args.stateid, &zero_stateid);
4549
4550 status = rpc_call_sync(server->client, &msg, 0);
4551 if (status)
4552 dprintk("%s failed: %d\n", __func__, status);
4553
4554 return status;
4555}
4556
4557static int nfs4_do_set_security_label(struct inode *inode,
4558 struct nfs4_label *ilabel,
4559 struct nfs_fattr *fattr,
4560 struct nfs4_label *olabel)
4561{
4562 struct nfs4_exception exception = { };
4563 int err;
4564
4565 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004566 err = _nfs4_do_set_security_label(inode, ilabel,
4567 fattr, olabel);
4568 trace_nfs4_set_security_label(inode, err);
4569 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004570 &exception);
4571 } while (exception.retry);
4572 return err;
4573}
4574
4575static int
4576nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4577{
4578 struct nfs4_label ilabel, *olabel = NULL;
4579 struct nfs_fattr fattr;
4580 struct rpc_cred *cred;
4581 struct inode *inode = dentry->d_inode;
4582 int status;
4583
4584 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4585 return -EOPNOTSUPP;
4586
4587 nfs_fattr_init(&fattr);
4588
4589 ilabel.pi = 0;
4590 ilabel.lfs = 0;
4591 ilabel.label = (char *)buf;
4592 ilabel.len = buflen;
4593
4594 cred = rpc_lookup_cred();
4595 if (IS_ERR(cred))
4596 return PTR_ERR(cred);
4597
4598 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4599 if (IS_ERR(olabel)) {
4600 status = -PTR_ERR(olabel);
4601 goto out;
4602 }
4603
4604 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4605 if (status == 0)
4606 nfs_setsecurity(inode, &fattr, olabel);
4607
4608 nfs4_label_free(olabel);
4609out:
4610 put_rpccred(cred);
4611 return status;
4612}
4613#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4614
4615
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004617nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004619 struct nfs_client *clp = server->nfs_client;
4620
4621 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 return 0;
4623 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004624 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004625 case -NFS4ERR_ADMIN_REVOKED:
4626 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004627 if (state == NULL)
4628 break;
4629 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004630 case -NFS4ERR_OPENMODE:
4631 if (state == NULL)
4632 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004633 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4634 goto stateid_invalid;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004635 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004636 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004637 if (state != NULL) {
4638 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4639 goto stateid_invalid;
4640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004642 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004643 nfs4_schedule_lease_recovery(clp);
4644 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004645#if defined(CONFIG_NFS_V4_1)
4646 case -NFS4ERR_BADSESSION:
4647 case -NFS4ERR_BADSLOT:
4648 case -NFS4ERR_BAD_HIGH_SLOT:
4649 case -NFS4ERR_DEADSESSION:
4650 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4651 case -NFS4ERR_SEQ_FALSE_RETRY:
4652 case -NFS4ERR_SEQ_MISORDERED:
4653 dprintk("%s ERROR %d, Reset session\n", __func__,
4654 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004655 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004656 task->tk_status = 0;
4657 return -EAGAIN;
4658#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004660 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004661 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4663 task->tk_status = 0;
4664 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004665 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 case -NFS4ERR_OLD_STATEID:
4667 task->tk_status = 0;
4668 return -EAGAIN;
4669 }
4670 task->tk_status = nfs4_map_errors(task->tk_status);
4671 return 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004672stateid_invalid:
4673 task->tk_status = -EIO;
4674 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004675wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004676 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004677 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4678 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4679 task->tk_status = 0;
4680 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681}
4682
Chuck Leverf0920752012-05-21 22:45:41 -04004683static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4684 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004685{
4686 __be32 verf[2];
4687
Chuck Lever2c820d92012-05-21 22:45:33 -04004688 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4689 /* An impossible timestamp guarantees this value
4690 * will never match a generated boot time. */
4691 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04004692 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04004693 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004694 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04004695 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
4696 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04004697 }
Chuck Levercd937102012-03-02 17:14:31 -05004698 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4699}
4700
Chuck Levere984a552012-09-14 17:24:21 -04004701static unsigned int
4702nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4703 char *buf, size_t len)
4704{
4705 unsigned int result;
4706
4707 rcu_read_lock();
4708 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4709 clp->cl_ipaddr,
4710 rpc_peeraddr2str(clp->cl_rpcclient,
4711 RPC_DISPLAY_ADDR),
4712 rpc_peeraddr2str(clp->cl_rpcclient,
4713 RPC_DISPLAY_PROTO));
4714 rcu_read_unlock();
4715 return result;
4716}
4717
4718static unsigned int
4719nfs4_init_uniform_client_string(const struct nfs_client *clp,
4720 char *buf, size_t len)
4721{
Trond Myklebust55b59292013-07-24 16:36:35 -04004722 const char *nodename = clp->cl_rpcclient->cl_nodename;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004723
4724 if (nfs4_client_id_uniquifier[0] != '\0')
Trond Myklebust55b59292013-07-24 16:36:35 -04004725 return scnprintf(buf, len, "Linux NFSv%u.%u %s/%s",
4726 clp->rpc_ops->version,
4727 clp->cl_minorversion,
4728 nfs4_client_id_uniquifier,
4729 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004730 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4731 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004732 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004733}
4734
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004735/**
4736 * nfs4_proc_setclientid - Negotiate client ID
4737 * @clp: state data structure
4738 * @program: RPC program for NFSv4 callback service
4739 * @port: IP port number for NFS4 callback service
4740 * @cred: RPC credential to use for this call
4741 * @res: where to place the result
4742 *
4743 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4744 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004745int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4746 unsigned short port, struct rpc_cred *cred,
4747 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748{
4749 nfs4_verifier sc_verifier;
4750 struct nfs4_setclientid setclientid = {
4751 .sc_verifier = &sc_verifier,
4752 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004753 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754 };
4755 struct rpc_message msg = {
4756 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4757 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004758 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004759 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004761 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762
Chuck Leverde734832012-07-11 16:30:50 -04004763 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004764 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004765 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4766 setclientid.sc_name_len =
4767 nfs4_init_uniform_client_string(clp,
4768 setclientid.sc_name,
4769 sizeof(setclientid.sc_name));
4770 else
4771 setclientid.sc_name_len =
4772 nfs4_init_nonuniform_client_string(clp,
4773 setclientid.sc_name,
4774 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004775 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004776 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004777 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Djalal Harounife2d5392013-06-18 17:58:12 +01004778 sizeof(setclientid.sc_netid), "%s",
Chuck Leverd4d3c502007-12-10 14:57:09 -05004779 rpc_peeraddr2str(clp->cl_rpcclient,
4780 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004781 rcu_read_unlock();
4782 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004783 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 clp->cl_ipaddr, port >> 8, port & 255);
4785
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004786 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4787 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4788 setclientid.sc_name_len, setclientid.sc_name);
4789 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004790 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004791 dprintk("NFS reply setclientid: %d\n", status);
4792 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793}
4794
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004795/**
4796 * nfs4_proc_setclientid_confirm - Confirm client ID
4797 * @clp: state data structure
4798 * @res: result of a previous SETCLIENTID
4799 * @cred: RPC credential to use for this call
4800 *
4801 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4802 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004803int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004804 struct nfs4_setclientid_res *arg,
4805 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 struct rpc_message msg = {
4808 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004809 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004810 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812 int status;
4813
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004814 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4815 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4816 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004817 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004818 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004819 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820 return status;
4821}
4822
Trond Myklebustfe650402006-01-03 09:55:18 +01004823struct nfs4_delegreturndata {
4824 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004825 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004826 struct nfs_fh fh;
4827 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004828 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004829 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004830 int rpc_status;
4831};
4832
Trond Myklebustfe650402006-01-03 09:55:18 +01004833static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4834{
4835 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004836
Trond Myklebust14516c32010-07-31 14:29:06 -04004837 if (!nfs4_sequence_done(task, &data->res.seq_res))
4838 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004839
Trond Myklebustca8acf82013-08-13 10:36:56 -04004840 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004841 switch (task->tk_status) {
4842 case -NFS4ERR_STALE_STATEID:
4843 case -NFS4ERR_EXPIRED:
4844 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004845 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004846 break;
4847 default:
4848 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4849 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004850 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004851 return;
4852 }
4853 }
4854 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004855}
4856
4857static void nfs4_delegreturn_release(void *calldata)
4858{
Trond Myklebustfe650402006-01-03 09:55:18 +01004859 kfree(calldata);
4860}
4861
Andy Adamson938e1012009-04-01 09:22:28 -04004862#if defined(CONFIG_NFS_V4_1)
4863static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4864{
4865 struct nfs4_delegreturndata *d_data;
4866
4867 d_data = (struct nfs4_delegreturndata *)data;
4868
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004869 nfs4_setup_sequence(d_data->res.server,
4870 &d_data->args.seq_args,
4871 &d_data->res.seq_res,
4872 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004873}
4874#endif /* CONFIG_NFS_V4_1 */
4875
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004876static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004877#if defined(CONFIG_NFS_V4_1)
4878 .rpc_call_prepare = nfs4_delegreturn_prepare,
4879#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004880 .rpc_call_done = nfs4_delegreturn_done,
4881 .rpc_release = nfs4_delegreturn_release,
4882};
4883
Trond Myklebuste6f81072008-01-24 18:14:34 -05004884static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004885{
4886 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004887 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004888 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004889 struct rpc_message msg = {
4890 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4891 .rpc_cred = cred,
4892 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004893 struct rpc_task_setup task_setup_data = {
4894 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004895 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004896 .callback_ops = &nfs4_delegreturn_ops,
4897 .flags = RPC_TASK_ASYNC,
4898 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004899 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004900
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004901 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004902 if (data == NULL)
4903 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004904 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004905 data->args.fhandle = &data->fh;
4906 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004907 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004908 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004909 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004910 data->res.fattr = &data->fattr;
4911 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004912 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004913 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004914 data->rpc_status = 0;
4915
Trond Myklebustc970aa82007-07-14 15:39:59 -04004916 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004917 msg.rpc_argp = &data->args;
4918 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004919 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004920 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004921 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004922 if (!issync)
4923 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004924 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004925 if (status != 0)
4926 goto out;
4927 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004928 if (status == 0)
4929 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4930 else
4931 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004932out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004933 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004934 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935}
4936
Trond Myklebuste6f81072008-01-24 18:14:34 -05004937int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938{
4939 struct nfs_server *server = NFS_SERVER(inode);
4940 struct nfs4_exception exception = { };
4941 int err;
4942 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004943 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Trond Myklebustca8acf82013-08-13 10:36:56 -04004944 trace_nfs4_delegreturn(inode, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 switch (err) {
4946 case -NFS4ERR_STALE_STATEID:
4947 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 case 0:
4949 return 0;
4950 }
4951 err = nfs4_handle_exception(server, err, &exception);
4952 } while (exception.retry);
4953 return err;
4954}
4955
4956#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4957#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4958
4959/*
4960 * sleep, with exponential backoff, and retry the LOCK operation.
4961 */
4962static unsigned long
4963nfs4_set_lock_task_retry(unsigned long timeout)
4964{
Colin Cross416ad3c2013-05-06 23:50:06 +00004965 freezable_schedule_timeout_killable_unsafe(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 timeout <<= 1;
4967 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4968 return NFS4_LOCK_MAXTIMEOUT;
4969 return timeout;
4970}
4971
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4973{
4974 struct inode *inode = state->inode;
4975 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004976 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004977 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004979 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004981 struct nfs_lockt_res res = {
4982 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 };
4984 struct rpc_message msg = {
4985 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4986 .rpc_argp = &arg,
4987 .rpc_resp = &res,
4988 .rpc_cred = state->owner->so_cred,
4989 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990 struct nfs4_lock_state *lsp;
4991 int status;
4992
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004993 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004994 status = nfs4_set_lock_state(state, request);
4995 if (status != 0)
4996 goto out;
4997 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004998 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004999 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005000 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005001 switch (status) {
5002 case 0:
5003 request->fl_type = F_UNLCK;
5004 break;
5005 case -NFS4ERR_DENIED:
5006 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005008 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005009out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010 return status;
5011}
5012
5013static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5014{
5015 struct nfs4_exception exception = { };
5016 int err;
5017
5018 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005019 err = _nfs4_proc_getlk(state, cmd, request);
5020 trace_nfs4_get_lock(request, state, cmd, err);
5021 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 &exception);
5023 } while (exception.retry);
5024 return err;
5025}
5026
5027static int do_vfs_lock(struct file *file, struct file_lock *fl)
5028{
5029 int res = 0;
5030 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5031 case FL_POSIX:
5032 res = posix_lock_file_wait(file, fl);
5033 break;
5034 case FL_FLOCK:
5035 res = flock_lock_file_wait(file, fl);
5036 break;
5037 default:
5038 BUG();
5039 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040 return res;
5041}
5042
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005043struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005044 struct nfs_locku_args arg;
5045 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005046 struct nfs4_lock_state *lsp;
5047 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005048 struct file_lock fl;
5049 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005050 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005051};
5052
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005053static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5054 struct nfs_open_context *ctx,
5055 struct nfs4_lock_state *lsp,
5056 struct nfs_seqid *seqid)
5057{
5058 struct nfs4_unlockdata *p;
5059 struct inode *inode = lsp->ls_state->inode;
5060
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005061 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005062 if (p == NULL)
5063 return NULL;
5064 p->arg.fh = NFS_FH(inode);
5065 p->arg.fl = &p->fl;
5066 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005067 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005068 p->arg.stateid = &lsp->ls_stateid;
5069 p->lsp = lsp;
5070 atomic_inc(&lsp->ls_count);
5071 /* Ensure we don't close file until we're done freeing locks! */
5072 p->ctx = get_nfs_open_context(ctx);
5073 memcpy(&p->fl, fl, sizeof(p->fl));
5074 p->server = NFS_SERVER(inode);
5075 return p;
5076}
5077
Trond Myklebust06f814a2006-01-03 09:55:07 +01005078static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005079{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005080 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005081 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005082 nfs4_put_lock_state(calldata->lsp);
5083 put_nfs_open_context(calldata->ctx);
5084 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005085}
5086
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005087static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005088{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005089 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005090
Trond Myklebust14516c32010-07-31 14:29:06 -04005091 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5092 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005093 switch (task->tk_status) {
5094 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05005095 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
5096 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005097 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005098 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005099 case -NFS4ERR_BAD_STATEID:
5100 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005101 case -NFS4ERR_STALE_STATEID:
5102 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005103 break;
5104 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005105 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005106 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005107 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005108 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005109}
5110
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005111static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005112{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005113 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005115 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005116 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04005117 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005118 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005119 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005120 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005121 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005122 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005123 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005124 &calldata->res.seq_res,
5125 task) != 0)
5126 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005127 return;
5128out_no_action:
5129 task->tk_action = NULL;
5130out_wait:
5131 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132}
5133
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005134static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005135 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005136 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005137 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005138};
5139
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005140static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5141 struct nfs_open_context *ctx,
5142 struct nfs4_lock_state *lsp,
5143 struct nfs_seqid *seqid)
5144{
5145 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005146 struct rpc_message msg = {
5147 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5148 .rpc_cred = ctx->cred,
5149 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005150 struct rpc_task_setup task_setup_data = {
5151 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005152 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005153 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005154 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005155 .flags = RPC_TASK_ASYNC,
5156 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005157
Frank Filz137d6ac2007-07-09 15:32:29 -07005158 /* Ensure this is an unlock - when canceling a lock, the
5159 * canceled lock is passed in, and it won't be an unlock.
5160 */
5161 fl->fl_type = F_UNLCK;
5162
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005163 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5164 if (data == NULL) {
5165 nfs_free_seqid(seqid);
5166 return ERR_PTR(-ENOMEM);
5167 }
5168
Trond Myklebust9d12b212012-01-17 22:04:25 -05005169 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005170 msg.rpc_argp = &data->arg;
5171 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005172 task_setup_data.callback_data = data;
5173 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005174}
5175
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5177{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005178 struct inode *inode = state->inode;
5179 struct nfs4_state_owner *sp = state->owner;
5180 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005181 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005182 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005183 struct rpc_task *task;
5184 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005185 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186
Trond Myklebust9b073572006-06-29 16:38:34 -04005187 status = nfs4_set_lock_state(state, request);
5188 /* Unlock _before_ we do the RPC call */
5189 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005190 /* Exclude nfs_delegation_claim_locks() */
5191 mutex_lock(&sp->so_delegreturn_mutex);
5192 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005193 down_read(&nfsi->rwsem);
5194 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
5195 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005196 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005197 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005198 }
5199 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005200 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005201 if (status != 0)
5202 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005203 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005204 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005205 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5206 goto out;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005207 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005208 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005209 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04005210 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005211 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005212 status = PTR_ERR(task);
5213 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005214 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005215 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005216 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005217out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005218 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005219 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005220 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221}
5222
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005223struct nfs4_lockdata {
5224 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005225 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005226 struct nfs4_lock_state *lsp;
5227 struct nfs_open_context *ctx;
5228 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005229 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005230 int rpc_status;
5231 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005232 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005233};
5234
5235static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005236 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5237 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005238{
5239 struct nfs4_lockdata *p;
5240 struct inode *inode = lsp->ls_state->inode;
5241 struct nfs_server *server = NFS_SERVER(inode);
5242
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005243 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005244 if (p == NULL)
5245 return NULL;
5246
5247 p->arg.fh = NFS_FH(inode);
5248 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005249 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005250 if (p->arg.open_seqid == NULL)
5251 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005252 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005253 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005254 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005255 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04005256 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005257 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005258 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005259 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005260 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005261 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005262 atomic_inc(&lsp->ls_count);
5263 p->ctx = get_nfs_open_context(ctx);
5264 memcpy(&p->fl, fl, sizeof(p->fl));
5265 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005266out_free_seqid:
5267 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005268out_free:
5269 kfree(p);
5270 return NULL;
5271}
5272
5273static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5274{
5275 struct nfs4_lockdata *data = calldata;
5276 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277
Harvey Harrison3110ff82008-05-02 13:42:44 -07005278 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005279 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005280 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005281 /* Do we need to do an open_to_lock_owner? */
5282 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005283 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005284 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005285 }
Trond Myklebust8e472f32013-04-20 01:30:53 -04005286 data->arg.open_stateid = &state->open_stateid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005287 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005288 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005289 } else
5290 data->arg.new_lock_owner = 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04005291 if (!nfs4_valid_open_stateid(state)) {
5292 data->rpc_status = -EBADF;
5293 task->tk_action = NULL;
5294 goto out_release_open_seqid;
5295 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005296 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005297 if (nfs4_setup_sequence(data->server,
5298 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005299 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005300 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04005301 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04005302out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005303 nfs_release_seqid(data->arg.open_seqid);
5304out_release_lock_seqid:
5305 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005306out_wait:
5307 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005308 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005309}
5310
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005311static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5312{
5313 struct nfs4_lockdata *data = calldata;
5314
Harvey Harrison3110ff82008-05-02 13:42:44 -07005315 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005316
Trond Myklebust14516c32010-07-31 14:29:06 -04005317 if (!nfs4_sequence_done(task, &data->res.seq_res))
5318 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04005319
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005320 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005321 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005322 if (data->rpc_status == 0)
5323 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
5324 else
5325 goto out;
5326 }
5327 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05005328 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005329 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04005330 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005331 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005332out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07005333 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005334}
5335
5336static void nfs4_lock_release(void *calldata)
5337{
5338 struct nfs4_lockdata *data = calldata;
5339
Harvey Harrison3110ff82008-05-02 13:42:44 -07005340 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005341 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005342 if (data->cancelled != 0) {
5343 struct rpc_task *task;
5344 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5345 data->arg.lock_seqid);
5346 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08005347 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005348 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005349 } else
5350 nfs_free_seqid(data->arg.lock_seqid);
5351 nfs4_put_lock_state(data->lsp);
5352 put_nfs_open_context(data->ctx);
5353 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005354 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005355}
5356
5357static const struct rpc_call_ops nfs4_lock_ops = {
5358 .rpc_call_prepare = nfs4_lock_prepare,
5359 .rpc_call_done = nfs4_lock_done,
5360 .rpc_release = nfs4_lock_release,
5361};
5362
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005363static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5364{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005365 switch (error) {
5366 case -NFS4ERR_ADMIN_REVOKED:
5367 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05005368 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005369 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04005370 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05005371 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005372 break;
5373 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005374 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05005375 case -NFS4ERR_EXPIRED:
5376 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005377 };
5378}
5379
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005380static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005381{
5382 struct nfs4_lockdata *data;
5383 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005384 struct rpc_message msg = {
5385 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5386 .rpc_cred = state->owner->so_cred,
5387 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005388 struct rpc_task_setup task_setup_data = {
5389 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005390 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005391 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005392 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005393 .flags = RPC_TASK_ASYNC,
5394 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005395 int ret;
5396
Harvey Harrison3110ff82008-05-02 13:42:44 -07005397 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005398 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005399 fl->fl_u.nfs4_fl.owner,
5400 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005401 if (data == NULL)
5402 return -ENOMEM;
5403 if (IS_SETLKW(cmd))
5404 data->arg.block = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -05005405 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005406 msg.rpc_argp = &data->arg;
5407 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005408 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005409 if (recovery_type > NFS_LOCK_NEW) {
5410 if (recovery_type == NFS_LOCK_RECLAIM)
5411 data->arg.reclaim = NFS_LOCK_RECLAIM;
5412 nfs4_set_sequence_privileged(&data->arg.seq_args);
5413 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04005414 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005415 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005416 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005417 ret = nfs4_wait_for_completion_rpc_task(task);
5418 if (ret == 0) {
5419 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005420 if (ret)
5421 nfs4_handle_setlk_error(data->server, data->lsp,
5422 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005423 } else
5424 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005425 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005426 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005427 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428}
5429
5430static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5431{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005432 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005433 struct nfs4_exception exception = {
5434 .inode = state->inode,
5435 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005436 int err;
5437
5438 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005439 /* Cache the lock if possible... */
5440 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5441 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005442 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005443 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
Trond Myklebust168667c2010-10-19 19:47:49 -04005444 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00005445 break;
5446 nfs4_handle_exception(server, err, &exception);
5447 } while (exception.retry);
5448 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449}
5450
5451static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5452{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005453 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005454 struct nfs4_exception exception = {
5455 .inode = state->inode,
5456 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005457 int err;
5458
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005459 err = nfs4_set_lock_state(state, request);
5460 if (err != 0)
5461 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00005462 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005463 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5464 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005465 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005466 trace_nfs4_lock_expired(request, state, F_SETLK, err);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005467 switch (err) {
5468 default:
5469 goto out;
5470 case -NFS4ERR_GRACE:
5471 case -NFS4ERR_DELAY:
5472 nfs4_handle_exception(server, err, &exception);
5473 err = 0;
5474 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005475 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005476out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00005477 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478}
5479
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005480#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005481/**
5482 * nfs41_check_expired_locks - possibly free a lock stateid
5483 *
5484 * @state: NFSv4 state for an inode
5485 *
5486 * Returns NFS_OK if recovery for this stateid is now finished.
5487 * Otherwise a negative NFS4ERR value is returned.
5488 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005489static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005490{
Chuck Levereb64cf92012-07-11 16:30:05 -04005491 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005492 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005493 struct nfs_server *server = NFS_SERVER(state->inode);
5494
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005495 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04005496 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005497 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
5498
5499 status = nfs41_test_stateid(server,
5500 &lsp->ls_stateid,
5501 cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04005502 trace_nfs4_test_lock_stateid(state, lsp, status);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005503 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005504 /* Free the stateid unless the server
5505 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04005506 if (status != -NFS4ERR_BAD_STATEID)
5507 nfs41_free_stateid(server,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005508 &lsp->ls_stateid,
5509 cred);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005510 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005511 ret = status;
5512 }
5513 }
5514 };
5515
5516 return ret;
5517}
5518
5519static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5520{
5521 int status = NFS_OK;
5522
5523 if (test_bit(LK_STATE_IN_USE, &state->flags))
5524 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005525 if (status != NFS_OK)
5526 status = nfs4_lock_expired(state, request);
5527 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005528}
5529#endif
5530
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5532{
Trond Myklebust9a99af492013-02-04 20:17:49 -05005533 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005534 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005535 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005536 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005537 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005539 if ((fl_flags & FL_POSIX) &&
5540 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5541 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005542 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005543 status = nfs4_set_lock_state(state, request);
5544 if (status != 0)
5545 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005546 request->fl_flags |= FL_ACCESS;
5547 status = do_vfs_lock(request->fl_file, request);
5548 if (status < 0)
5549 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005550 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005551 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005552 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005553 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005554 request->fl_flags = fl_flags & ~FL_SLEEP;
5555 status = do_vfs_lock(request->fl_file, request);
5556 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005557 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05005558 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5559 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005560 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005561 if (status != 0)
Trond Myklebust9a99af492013-02-04 20:17:49 -05005562 goto out;
5563 down_read(&nfsi->rwsem);
5564 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5565 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005566 goto out_unlock;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005567 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005568 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005569 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005570 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005571 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5572 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005573out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005574 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005575out:
5576 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577 return status;
5578}
5579
5580static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5581{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005582 struct nfs4_exception exception = {
5583 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005584 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005585 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586 int err;
5587
5588 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005589 err = _nfs4_proc_setlk(state, cmd, request);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005590 trace_nfs4_set_lock(request, state, cmd, err);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005591 if (err == -NFS4ERR_DENIED)
5592 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005594 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595 } while (exception.retry);
5596 return err;
5597}
5598
5599static int
5600nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5601{
5602 struct nfs_open_context *ctx;
5603 struct nfs4_state *state;
5604 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5605 int status;
5606
5607 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005608 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609 state = ctx->state;
5610
5611 if (request->fl_start < 0 || request->fl_end < 0)
5612 return -EINVAL;
5613
Trond Myklebustd9531262009-07-21 19:22:38 -04005614 if (IS_GETLK(cmd)) {
5615 if (state != NULL)
5616 return nfs4_proc_getlk(state, F_GETLK, request);
5617 return 0;
5618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619
5620 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5621 return -EINVAL;
5622
Trond Myklebustd9531262009-07-21 19:22:38 -04005623 if (request->fl_type == F_UNLCK) {
5624 if (state != NULL)
5625 return nfs4_proc_unlck(state, cmd, request);
5626 return 0;
5627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628
Trond Myklebustd9531262009-07-21 19:22:38 -04005629 if (state == NULL)
5630 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005631 /*
5632 * Don't rely on the VFS having checked the file open mode,
5633 * since it won't do this for flock() locks.
5634 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005635 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005636 case F_RDLCK:
5637 if (!(filp->f_mode & FMODE_READ))
5638 return -EBADF;
5639 break;
5640 case F_WRLCK:
5641 if (!(filp->f_mode & FMODE_WRITE))
5642 return -EBADF;
5643 }
5644
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645 do {
5646 status = nfs4_proc_setlk(state, cmd, request);
5647 if ((status != -EAGAIN) || IS_SETLK(cmd))
5648 break;
5649 timeout = nfs4_set_lock_task_retry(timeout);
5650 status = -ERESTARTSYS;
5651 if (signalled())
5652 break;
5653 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654 return status;
5655}
5656
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005657int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05005658{
5659 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05005660 int err;
5661
5662 err = nfs4_set_lock_state(state, fl);
5663 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005664 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04005665 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005666 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05005667}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005668
Trond Myklebustcf470c32012-03-07 13:49:12 -05005669struct nfs_release_lockowner_data {
5670 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005671 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005672 struct nfs_release_lockowner_args args;
5673};
5674
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005675static void nfs4_release_lockowner_release(void *calldata)
5676{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005677 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005678 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005679 kfree(calldata);
5680}
5681
Trond Myklebust17280172012-03-11 13:11:00 -04005682static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005683 .rpc_release = nfs4_release_lockowner_release,
5684};
5685
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04005686static int nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005687{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005688 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005689 struct rpc_message msg = {
5690 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5691 };
5692
5693 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005694 return -EINVAL;
5695 data = kmalloc(sizeof(*data), GFP_NOFS);
5696 if (!data)
5697 return -ENOMEM;
5698 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005699 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005700 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5701 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5702 data->args.lock_owner.s_dev = server->s_dev;
5703 msg.rpc_argp = &data->args;
5704 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5705 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005706}
5707
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005708#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5709
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005710static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5711 const void *buf, size_t buflen,
5712 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005713{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005714 if (strcmp(key, "") != 0)
5715 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005716
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005717 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005718}
5719
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005720static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5721 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005722{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005723 if (strcmp(key, "") != 0)
5724 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005725
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005726 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005727}
5728
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005729static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5730 size_t list_len, const char *name,
5731 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005732{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005733 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005734
J. Bruce Fields096455a2006-03-20 23:23:42 -05005735 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5736 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005737
5738 if (list && len <= list_len)
5739 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005740 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005741}
5742
David Quigleyc9bccef2013-05-22 12:50:45 -04005743#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5744static inline int nfs4_server_supports_labels(struct nfs_server *server)
5745{
5746 return server->caps & NFS_CAP_SECURITY_LABEL;
5747}
5748
5749static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
5750 const void *buf, size_t buflen,
5751 int flags, int type)
5752{
5753 if (security_ismaclabel(key))
5754 return nfs4_set_security_label(dentry, buf, buflen);
5755
5756 return -EOPNOTSUPP;
5757}
5758
5759static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
5760 void *buf, size_t buflen, int type)
5761{
5762 if (security_ismaclabel(key))
5763 return nfs4_get_security_label(dentry->d_inode, buf, buflen);
5764 return -EOPNOTSUPP;
5765}
5766
5767static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
5768 size_t list_len, const char *name,
5769 size_t name_len, int type)
5770{
5771 size_t len = 0;
5772
5773 if (nfs_server_capable(dentry->d_inode, NFS_CAP_SECURITY_LABEL)) {
5774 len = security_inode_listsecurity(dentry->d_inode, NULL, 0);
5775 if (list && len <= list_len)
5776 security_inode_listsecurity(dentry->d_inode, list, len);
5777 }
5778 return len;
5779}
5780
5781static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
5782 .prefix = XATTR_SECURITY_PREFIX,
5783 .list = nfs4_xattr_list_nfs4_label,
5784 .get = nfs4_xattr_get_nfs4_label,
5785 .set = nfs4_xattr_set_nfs4_label,
5786};
5787#endif
5788
5789
Andy Adamson533eb462011-06-13 18:25:56 -04005790/*
5791 * nfs_fhget will use either the mounted_on_fileid or the fileid
5792 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005793static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5794{
Andy Adamson533eb462011-06-13 18:25:56 -04005795 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5796 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5797 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005798 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005799 return;
5800
5801 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005802 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005803 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5804 fattr->nlink = 2;
5805}
5806
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005807static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5808 const struct qstr *name,
5809 struct nfs4_fs_locations *fs_locations,
5810 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005811{
5812 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04005813 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005814 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005815 };
5816 struct nfs4_fs_locations_arg args = {
5817 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005818 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005819 .page = page,
5820 .bitmask = bitmask,
5821 };
Benny Halevy22958462009-04-01 09:22:02 -04005822 struct nfs4_fs_locations_res res = {
5823 .fs_locations = fs_locations,
5824 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005825 struct rpc_message msg = {
5826 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5827 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005828 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005829 };
5830 int status;
5831
Harvey Harrison3110ff82008-05-02 13:42:44 -07005832 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005833
5834 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5835 * is not supported */
5836 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5837 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5838 else
5839 bitmask[0] |= FATTR4_WORD0_FILEID;
5840
Trond Myklebustc228fd32007-01-13 02:28:11 -05005841 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005842 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005843 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005844 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005845 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005846 return status;
5847}
5848
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005849int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5850 const struct qstr *name,
5851 struct nfs4_fs_locations *fs_locations,
5852 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005853{
5854 struct nfs4_exception exception = { };
5855 int err;
5856 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04005857 err = _nfs4_proc_fs_locations(client, dir, name,
5858 fs_locations, page);
5859 trace_nfs4_get_fs_locations(dir, name, err);
5860 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005861 &exception);
5862 } while (exception.retry);
5863 return err;
5864}
5865
Andy Adamson5ec16a82013-08-08 10:57:55 -04005866/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04005867 * If 'use_integrity' is true and the state managment nfs_client
5868 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
5869 * and the machine credential as per RFC3530bis and RFC5661 Security
5870 * Considerations sections. Otherwise, just use the user cred with the
5871 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04005872 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04005873static 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 +00005874{
5875 int status;
5876 struct nfs4_secinfo_arg args = {
5877 .dir_fh = NFS_FH(dir),
5878 .name = name,
5879 };
5880 struct nfs4_secinfo_res res = {
5881 .flavors = flavors,
5882 };
5883 struct rpc_message msg = {
5884 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5885 .rpc_argp = &args,
5886 .rpc_resp = &res,
5887 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04005888 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
5889
5890 if (use_integrity) {
5891 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
5892 msg.rpc_cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
5893 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005894
5895 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04005896 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
5897 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005898 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04005899
5900 if (msg.rpc_cred)
5901 put_rpccred(msg.rpc_cred);
5902
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005903 return status;
5904}
5905
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005906int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5907 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005908{
5909 struct nfs4_exception exception = { };
5910 int err;
5911 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04005912 err = -NFS4ERR_WRONGSEC;
5913
5914 /* try to use integrity protection with machine cred */
5915 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
5916 err = _nfs4_proc_secinfo(dir, name, flavors, true);
5917
5918 /*
5919 * if unable to use integrity protection, or SECINFO with
5920 * integrity protection returns NFS4ERR_WRONGSEC (which is
5921 * disallowed by spec, but exists in deployed servers) use
5922 * the current filesystem's rpc_client and the user cred.
5923 */
5924 if (err == -NFS4ERR_WRONGSEC)
5925 err = _nfs4_proc_secinfo(dir, name, flavors, false);
5926
Trond Myklebust078ea3d2013-08-12 16:45:55 -04005927 trace_nfs4_secinfo(dir, name, err);
5928 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005929 &exception);
5930 } while (exception.retry);
5931 return err;
5932}
5933
Andy Adamson557134a2009-04-01 09:21:53 -04005934#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005935/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005936 * Check the exchange flags returned by the server for invalid flags, having
5937 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5938 * DS flags set.
5939 */
5940static int nfs4_check_cl_exchange_flags(u32 flags)
5941{
5942 if (flags & ~EXCHGID4_FLAG_MASK_R)
5943 goto out_inval;
5944 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5945 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5946 goto out_inval;
5947 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5948 goto out_inval;
5949 return NFS_OK;
5950out_inval:
5951 return -NFS4ERR_INVAL;
5952}
5953
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005954static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005955nfs41_same_server_scope(struct nfs41_server_scope *a,
5956 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005957{
5958 if (a->server_scope_sz == b->server_scope_sz &&
5959 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5960 return true;
5961
5962 return false;
5963}
5964
Andy Adamson357f54d2010-12-14 10:11:57 -05005965/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005966 * nfs4_proc_bind_conn_to_session()
5967 *
5968 * The 4.1 client currently uses the same TCP connection for the
5969 * fore and backchannel.
5970 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005971int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005972{
5973 int status;
5974 struct nfs41_bind_conn_to_session_res res;
5975 struct rpc_message msg = {
5976 .rpc_proc =
5977 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5978 .rpc_argp = clp,
5979 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005980 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005981 };
5982
5983 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005984
5985 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5986 if (unlikely(res.session == NULL)) {
5987 status = -ENOMEM;
5988 goto out;
5989 }
5990
5991 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005992 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005993 if (status == 0) {
5994 if (memcmp(res.session->sess_id.data,
5995 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5996 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5997 status = -EIO;
5998 goto out_session;
5999 }
6000 if (res.dir != NFS4_CDFS4_BOTH) {
6001 dprintk("NFS: %s: Unexpected direction from server\n",
6002 __func__);
6003 status = -EIO;
6004 goto out_session;
6005 }
6006 if (res.use_conn_in_rdma_mode) {
6007 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6008 __func__);
6009 status = -EIO;
6010 goto out_session;
6011 }
6012 }
6013out_session:
6014 kfree(res.session);
6015out:
6016 dprintk("<-- %s status= %d\n", __func__, status);
6017 return status;
6018}
6019
6020/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04006021 * nfs4_proc_exchange_id()
6022 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006023 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6024 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04006025 * Since the clientid has expired, all compounds using sessions
6026 * associated with the stale clientid will be returning
6027 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
6028 * be in some phase of session reset.
6029 */
Andy Adamson4d643d12009-12-04 15:52:24 -05006030int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04006031{
6032 nfs4_verifier verifier;
6033 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05006034 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04006035 .client = clp,
Trond Myklebust4f0b4292013-05-20 12:24:03 -04006036 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6037 EXCHGID4_FLAG_BIND_PRINC_STATEID,
Benny Halevy99fe60d2009-04-01 09:22:29 -04006038 };
6039 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04006040 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04006041 };
6042 int status;
6043 struct rpc_message msg = {
6044 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
6045 .rpc_argp = &args,
6046 .rpc_resp = &res,
6047 .rpc_cred = cred,
6048 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04006049
Chuck Leverf0920752012-05-21 22:45:41 -04006050 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04006051 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
6052 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006053 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
6054 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6055 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006056
Chuck Leveracdeb692012-05-21 22:46:16 -04006057 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006058 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04006059 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006060 status = -ENOMEM;
6061 goto out;
6062 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006063
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006064 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006065 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006066 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04006067 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04006068 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04006069 }
6070
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006071 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006072 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006073 status = -ENOMEM;
6074 goto out_server_scope;
6075 }
6076
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006077 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006078 trace_nfs4_exchange_id(clp, status);
Chuck Lever177313f2012-05-21 22:44:58 -04006079 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04006080 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006081
Chuck Lever177313f2012-05-21 22:44:58 -04006082 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04006083 clp->cl_clientid = res.clientid;
6084 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
6085 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
6086 clp->cl_seqid = res.seqid;
6087
Chuck Leveracdeb692012-05-21 22:46:16 -04006088 kfree(clp->cl_serverowner);
6089 clp->cl_serverowner = res.server_owner;
6090 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04006091
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006092 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04006093 kfree(clp->cl_implid);
6094 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006095
Chuck Lever177313f2012-05-21 22:44:58 -04006096 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006097 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006098 res.server_scope)) {
6099 dprintk("%s: server_scope mismatch detected\n",
6100 __func__);
6101 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006102 kfree(clp->cl_serverscope);
6103 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006104 }
6105
Chuck Lever177313f2012-05-21 22:44:58 -04006106 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006107 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006108 goto out;
6109 }
Trond Myklebust32b01312012-05-26 13:41:04 -04006110 } else
6111 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006112
Chuck Leveracdeb692012-05-21 22:46:16 -04006113out_server_owner:
6114 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006115out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006116 kfree(res.server_scope);
6117out:
Chuck Lever177313f2012-05-21 22:44:58 -04006118 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006119 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006120 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006121 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04006122 clp->cl_implid->date.seconds,
6123 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006124 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006125 return status;
6126}
6127
Trond Myklebust66245532012-05-25 17:18:09 -04006128static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
6129 struct rpc_cred *cred)
6130{
6131 struct rpc_message msg = {
6132 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
6133 .rpc_argp = clp,
6134 .rpc_cred = cred,
6135 };
6136 int status;
6137
6138 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006139 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04006140 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04006141 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04006142 "DESTROY_CLIENTID.", status, clp->cl_hostname);
6143 return status;
6144}
6145
6146static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
6147 struct rpc_cred *cred)
6148{
6149 unsigned int loop;
6150 int ret;
6151
6152 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
6153 ret = _nfs4_proc_destroy_clientid(clp, cred);
6154 switch (ret) {
6155 case -NFS4ERR_DELAY:
6156 case -NFS4ERR_CLIENTID_BUSY:
6157 ssleep(1);
6158 break;
6159 default:
6160 return ret;
6161 }
6162 }
6163 return 0;
6164}
6165
6166int nfs4_destroy_clientid(struct nfs_client *clp)
6167{
6168 struct rpc_cred *cred;
6169 int ret = 0;
6170
6171 if (clp->cl_mvops->minor_version < 1)
6172 goto out;
6173 if (clp->cl_exchange_flags == 0)
6174 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04006175 if (clp->cl_preserve_clid)
6176 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04006177 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04006178 ret = nfs4_proc_destroy_clientid(clp, cred);
6179 if (cred)
6180 put_rpccred(cred);
6181 switch (ret) {
6182 case 0:
6183 case -NFS4ERR_STALE_CLIENTID:
6184 clp->cl_exchange_flags = 0;
6185 }
6186out:
6187 return ret;
6188}
6189
Andy Adamson2050f0c2009-04-01 09:22:30 -04006190struct nfs4_get_lease_time_data {
6191 struct nfs4_get_lease_time_args *args;
6192 struct nfs4_get_lease_time_res *res;
6193 struct nfs_client *clp;
6194};
6195
6196static void nfs4_get_lease_time_prepare(struct rpc_task *task,
6197 void *calldata)
6198{
Andy Adamson2050f0c2009-04-01 09:22:30 -04006199 struct nfs4_get_lease_time_data *data =
6200 (struct nfs4_get_lease_time_data *)calldata;
6201
6202 dprintk("--> %s\n", __func__);
6203 /* just setup sequence, do not trigger session recovery
6204 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006205 nfs41_setup_sequence(data->clp->cl_session,
6206 &data->args->la_seq_args,
6207 &data->res->lr_seq_res,
6208 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006209 dprintk("<-- %s\n", __func__);
6210}
6211
6212/*
6213 * Called from nfs4_state_manager thread for session setup, so don't recover
6214 * from sequence operation or clientid errors.
6215 */
6216static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
6217{
6218 struct nfs4_get_lease_time_data *data =
6219 (struct nfs4_get_lease_time_data *)calldata;
6220
6221 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006222 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
6223 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04006224 switch (task->tk_status) {
6225 case -NFS4ERR_DELAY:
6226 case -NFS4ERR_GRACE:
6227 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
6228 rpc_delay(task, NFS4_POLL_RETRY_MIN);
6229 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006230 /* fall through */
6231 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006232 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006233 return;
6234 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04006235 dprintk("<-- %s\n", __func__);
6236}
6237
Trond Myklebust17280172012-03-11 13:11:00 -04006238static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04006239 .rpc_call_prepare = nfs4_get_lease_time_prepare,
6240 .rpc_call_done = nfs4_get_lease_time_done,
6241};
6242
6243int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
6244{
6245 struct rpc_task *task;
6246 struct nfs4_get_lease_time_args args;
6247 struct nfs4_get_lease_time_res res = {
6248 .lr_fsinfo = fsinfo,
6249 };
6250 struct nfs4_get_lease_time_data data = {
6251 .args = &args,
6252 .res = &res,
6253 .clp = clp,
6254 };
6255 struct rpc_message msg = {
6256 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
6257 .rpc_argp = &args,
6258 .rpc_resp = &res,
6259 };
6260 struct rpc_task_setup task_setup = {
6261 .rpc_client = clp->cl_rpcclient,
6262 .rpc_message = &msg,
6263 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006264 .callback_data = &data,
6265 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04006266 };
6267 int status;
6268
Trond Myklebust9d12b212012-01-17 22:04:25 -05006269 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006270 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006271 dprintk("--> %s\n", __func__);
6272 task = rpc_run_task(&task_setup);
6273
6274 if (IS_ERR(task))
6275 status = PTR_ERR(task);
6276 else {
6277 status = task->tk_status;
6278 rpc_put_task(task);
6279 }
6280 dprintk("<-- %s return %d\n", __func__, status);
6281
6282 return status;
6283}
6284
Andy Adamsonfc931582009-04-01 09:22:31 -04006285/*
6286 * Initialize the values to be used by the client in CREATE_SESSION
6287 * If nfs4_init_session set the fore channel request and response sizes,
6288 * use them.
6289 *
6290 * Set the back channel max_resp_sz_cached to zero to force the client to
6291 * always set csa_cachethis to FALSE because the current implementation
6292 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
6293 */
6294static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
6295{
Andy Adamson18aad3d2013-06-26 12:21:49 -04006296 unsigned int max_rqst_sz, max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006297
Andy Adamson18aad3d2013-06-26 12:21:49 -04006298 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
6299 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
6300
Andy Adamsonfc931582009-04-01 09:22:31 -04006301 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04006302 args->fc_attrs.max_rqst_sz = max_rqst_sz;
6303 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006304 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05006305 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04006306
6307 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05006308 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04006309 __func__,
6310 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05006311 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04006312
6313 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04006314 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
6315 args->bc_attrs.max_resp_sz = PAGE_SIZE;
6316 args->bc_attrs.max_resp_sz_cached = 0;
6317 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
6318 args->bc_attrs.max_reqs = 1;
6319
6320 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
6321 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
6322 __func__,
6323 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
6324 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
6325 args->bc_attrs.max_reqs);
6326}
6327
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006328static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04006329{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006330 struct nfs4_channel_attrs *sent = &args->fc_attrs;
6331 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
6332
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006333 if (rcvd->max_resp_sz > sent->max_resp_sz)
6334 return -EINVAL;
6335 /*
6336 * Our requested max_ops is the minimum we need; we're not
6337 * prepared to break up compounds into smaller pieces than that.
6338 * So, no point even trying to continue if the server won't
6339 * cooperate:
6340 */
6341 if (rcvd->max_ops < sent->max_ops)
6342 return -EINVAL;
6343 if (rcvd->max_reqs == 0)
6344 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006345 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
6346 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006347 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04006348}
6349
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006350static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
6351{
6352 struct nfs4_channel_attrs *sent = &args->bc_attrs;
6353 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04006354
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006355 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
6356 return -EINVAL;
6357 if (rcvd->max_resp_sz < sent->max_resp_sz)
6358 return -EINVAL;
6359 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
6360 return -EINVAL;
6361 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006362 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006363 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006364 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006365 return -EINVAL;
6366 return 0;
6367}
Andy Adamson8d353012009-04-01 09:22:32 -04006368
Andy Adamson8d353012009-04-01 09:22:32 -04006369static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
6370 struct nfs4_session *session)
6371{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006372 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04006373
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006374 ret = nfs4_verify_fore_channel_attrs(args, session);
6375 if (ret)
6376 return ret;
6377 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04006378}
6379
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006380static int _nfs4_proc_create_session(struct nfs_client *clp,
6381 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04006382{
6383 struct nfs4_session *session = clp->cl_session;
6384 struct nfs41_create_session_args args = {
6385 .client = clp,
6386 .cb_program = NFS4_CALLBACK,
6387 };
6388 struct nfs41_create_session_res res = {
6389 .client = clp,
6390 };
6391 struct rpc_message msg = {
6392 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
6393 .rpc_argp = &args,
6394 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006395 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04006396 };
6397 int status;
6398
6399 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04006400 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04006401
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006402 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006403 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04006404
Trond Myklebust43095d32012-11-20 11:13:12 -05006405 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04006406 /* Verify the session's negotiated channel_attrs values */
6407 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04006408 /* Increment the clientid slot sequence id */
6409 clp->cl_seqid++;
6410 }
6411
6412 return status;
6413}
6414
6415/*
6416 * Issues a CREATE_SESSION operation to the server.
6417 * It is the responsibility of the caller to verify the session is
6418 * expired before calling this routine.
6419 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006420int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04006421{
6422 int status;
6423 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04006424 struct nfs4_session *session = clp->cl_session;
6425
6426 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
6427
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006428 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04006429 if (status)
6430 goto out;
6431
Andy Adamsonaacd5532011-11-09 13:58:21 -05006432 /* Init or reset the session slot tables */
6433 status = nfs4_setup_session_slot_tables(session);
6434 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04006435 if (status)
6436 goto out;
6437
6438 ptr = (unsigned *)&session->sess_id.data[0];
6439 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
6440 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04006441out:
6442 dprintk("<-- %s\n", __func__);
6443 return status;
6444}
6445
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006446/*
6447 * Issue the over-the-wire RPC DESTROY_SESSION.
6448 * The caller must serialize access to this routine.
6449 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006450int nfs4_proc_destroy_session(struct nfs4_session *session,
6451 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006452{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006453 struct rpc_message msg = {
6454 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
6455 .rpc_argp = session,
6456 .rpc_cred = cred,
6457 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006458 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006459
6460 dprintk("--> nfs4_proc_destroy_session\n");
6461
6462 /* session is still being setup */
6463 if (session->clp->cl_cons_state != NFS_CS_READY)
6464 return status;
6465
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006466 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006467 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006468
6469 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04006470 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006471 "Session has been destroyed regardless...\n", status);
6472
6473 dprintk("<-- nfs4_proc_destroy_session\n");
6474 return status;
6475}
6476
Trond Myklebust7b38c362012-05-23 13:23:31 -04006477/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006478 * Renew the cl_session lease.
6479 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006480struct nfs4_sequence_data {
6481 struct nfs_client *clp;
6482 struct nfs4_sequence_args args;
6483 struct nfs4_sequence_res res;
6484};
6485
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006486static void nfs41_sequence_release(void *data)
6487{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006488 struct nfs4_sequence_data *calldata = data;
6489 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006490
Alexandros Batsakis71358402010-02-05 03:45:05 -08006491 if (atomic_read(&clp->cl_count) > 1)
6492 nfs4_schedule_state_renewal(clp);
6493 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006494 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006495}
6496
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006497static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6498{
6499 switch(task->tk_status) {
6500 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006501 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6502 return -EAGAIN;
6503 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006504 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006505 }
6506 return 0;
6507}
6508
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006509static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006510{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006511 struct nfs4_sequence_data *calldata = data;
6512 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006513
Trond Myklebust14516c32010-07-31 14:29:06 -04006514 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6515 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006516
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006517 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006518 if (task->tk_status < 0) {
6519 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006520 if (atomic_read(&clp->cl_count) == 1)
6521 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006522
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006523 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6524 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006525 return;
6526 }
6527 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006528 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006529out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006530 dprintk("<-- %s\n", __func__);
6531}
6532
6533static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6534{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006535 struct nfs4_sequence_data *calldata = data;
6536 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006537 struct nfs4_sequence_args *args;
6538 struct nfs4_sequence_res *res;
6539
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006540 args = task->tk_msg.rpc_argp;
6541 res = task->tk_msg.rpc_resp;
6542
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006543 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006544}
6545
6546static const struct rpc_call_ops nfs41_sequence_ops = {
6547 .rpc_call_done = nfs41_sequence_call_done,
6548 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006549 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006550};
6551
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006552static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
6553 struct rpc_cred *cred,
6554 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006555{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006556 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006557 struct rpc_message msg = {
6558 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6559 .rpc_cred = cred,
6560 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006561 struct rpc_task_setup task_setup_data = {
6562 .rpc_client = clp->cl_rpcclient,
6563 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006564 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04006565 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006566 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006567
Alexandros Batsakis71358402010-02-05 03:45:05 -08006568 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006569 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006570 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006571 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006572 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006573 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006574 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05006575 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006576 if (is_privileged)
6577 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006578 msg.rpc_argp = &calldata->args;
6579 msg.rpc_resp = &calldata->res;
6580 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006581 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006582
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006583 return rpc_run_task(&task_setup_data);
6584}
6585
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006586static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006587{
6588 struct rpc_task *task;
6589 int ret = 0;
6590
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006591 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6592 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006593 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006594 if (IS_ERR(task))
6595 ret = PTR_ERR(task);
6596 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006597 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006598 dprintk("<-- %s status=%d\n", __func__, ret);
6599 return ret;
6600}
6601
6602static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6603{
6604 struct rpc_task *task;
6605 int ret;
6606
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006607 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006608 if (IS_ERR(task)) {
6609 ret = PTR_ERR(task);
6610 goto out;
6611 }
6612 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006613 if (!ret) {
6614 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6615
6616 if (task->tk_status == 0)
6617 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006618 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006619 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006620 rpc_put_task(task);
6621out:
6622 dprintk("<-- %s status=%d\n", __func__, ret);
6623 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006624}
6625
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006626struct nfs4_reclaim_complete_data {
6627 struct nfs_client *clp;
6628 struct nfs41_reclaim_complete_args arg;
6629 struct nfs41_reclaim_complete_res res;
6630};
6631
6632static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6633{
6634 struct nfs4_reclaim_complete_data *calldata = data;
6635
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006636 nfs41_setup_sequence(calldata->clp->cl_session,
6637 &calldata->arg.seq_args,
6638 &calldata->res.seq_res,
6639 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006640}
6641
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006642static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6643{
6644 switch(task->tk_status) {
6645 case 0:
6646 case -NFS4ERR_COMPLETE_ALREADY:
6647 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6648 break;
6649 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006650 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006651 /* fall through */
6652 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006653 return -EAGAIN;
6654 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006655 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006656 }
6657 return 0;
6658}
6659
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006660static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6661{
6662 struct nfs4_reclaim_complete_data *calldata = data;
6663 struct nfs_client *clp = calldata->clp;
6664 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6665
6666 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006667 if (!nfs41_sequence_done(task, res))
6668 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006669
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006670 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006671 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6672 rpc_restart_call_prepare(task);
6673 return;
6674 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006675 dprintk("<-- %s\n", __func__);
6676}
6677
6678static void nfs4_free_reclaim_complete_data(void *data)
6679{
6680 struct nfs4_reclaim_complete_data *calldata = data;
6681
6682 kfree(calldata);
6683}
6684
6685static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6686 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6687 .rpc_call_done = nfs4_reclaim_complete_done,
6688 .rpc_release = nfs4_free_reclaim_complete_data,
6689};
6690
6691/*
6692 * Issue a global reclaim complete.
6693 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04006694static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
6695 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006696{
6697 struct nfs4_reclaim_complete_data *calldata;
6698 struct rpc_task *task;
6699 struct rpc_message msg = {
6700 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04006701 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006702 };
6703 struct rpc_task_setup task_setup_data = {
6704 .rpc_client = clp->cl_rpcclient,
6705 .rpc_message = &msg,
6706 .callback_ops = &nfs4_reclaim_complete_call_ops,
6707 .flags = RPC_TASK_ASYNC,
6708 };
6709 int status = -ENOMEM;
6710
6711 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006712 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006713 if (calldata == NULL)
6714 goto out;
6715 calldata->clp = clp;
6716 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006717
Trond Myklebust9d12b212012-01-17 22:04:25 -05006718 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006719 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006720 msg.rpc_argp = &calldata->arg;
6721 msg.rpc_resp = &calldata->res;
6722 task_setup_data.callback_data = calldata;
6723 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006724 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006725 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006726 goto out;
6727 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006728 status = nfs4_wait_for_completion_rpc_task(task);
6729 if (status == 0)
6730 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006731 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006732 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006733out:
6734 dprintk("<-- %s status=%d\n", __func__, status);
6735 return status;
6736}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006737
6738static void
6739nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6740{
6741 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006742 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006743 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006744
6745 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006746 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6747 * right now covering the LAYOUTGET we are about to send.
6748 * However, that is not so catastrophic, and there seems
6749 * to be no way to prevent it completely.
6750 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006751 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006752 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006753 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006754 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6755 NFS_I(lgp->args.inode)->layout,
6756 lgp->args.ctx->state)) {
6757 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00006758 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006759}
6760
6761static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6762{
6763 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006764 struct inode *inode = lgp->args.inode;
6765 struct nfs_server *server = NFS_SERVER(inode);
6766 struct pnfs_layout_hdr *lo;
6767 struct nfs4_state *state = NULL;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006768 unsigned long timeo, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006769
6770 dprintk("--> %s\n", __func__);
6771
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006772 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006773 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006774
6775 switch (task->tk_status) {
6776 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006777 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006778 case -NFS4ERR_LAYOUTTRYLATER:
6779 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05006780 timeo = rpc_get_timeout(task->tk_client);
6781 giveup = lgp->args.timestamp + timeo;
6782 if (time_after(giveup, jiffies))
6783 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006784 break;
6785 case -NFS4ERR_EXPIRED:
6786 case -NFS4ERR_BAD_STATEID:
6787 spin_lock(&inode->i_lock);
6788 lo = NFS_I(inode)->layout;
6789 if (!lo || list_empty(&lo->plh_segs)) {
6790 spin_unlock(&inode->i_lock);
6791 /* If the open stateid was bad, then recover it. */
6792 state = lgp->args.ctx->state;
6793 } else {
6794 LIST_HEAD(head);
6795
6796 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6797 spin_unlock(&inode->i_lock);
6798 /* Mark the bad layout state as invalid, then
6799 * retry using the open stateid. */
6800 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006801 }
6802 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006803 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6804 rpc_restart_call_prepare(task);
6805out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006806 dprintk("<-- %s\n", __func__);
6807}
6808
Idan Kedar85541162012-08-02 11:47:10 +03006809static size_t max_response_pages(struct nfs_server *server)
6810{
6811 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6812 return nfs_page_array_len(0, max_resp_sz);
6813}
6814
6815static void nfs4_free_pages(struct page **pages, size_t size)
6816{
6817 int i;
6818
6819 if (!pages)
6820 return;
6821
6822 for (i = 0; i < size; i++) {
6823 if (!pages[i])
6824 break;
6825 __free_page(pages[i]);
6826 }
6827 kfree(pages);
6828}
6829
6830static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6831{
6832 struct page **pages;
6833 int i;
6834
6835 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6836 if (!pages) {
6837 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6838 return NULL;
6839 }
6840
6841 for (i = 0; i < size; i++) {
6842 pages[i] = alloc_page(gfp_flags);
6843 if (!pages[i]) {
6844 dprintk("%s: failed to allocate page\n", __func__);
6845 nfs4_free_pages(pages, size);
6846 return NULL;
6847 }
6848 }
6849
6850 return pages;
6851}
6852
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006853static void nfs4_layoutget_release(void *calldata)
6854{
6855 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006856 struct inode *inode = lgp->args.inode;
6857 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006858 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006859
6860 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006861 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006862 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006863 put_nfs_open_context(lgp->args.ctx);
6864 kfree(calldata);
6865 dprintk("<-- %s\n", __func__);
6866}
6867
6868static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6869 .rpc_call_prepare = nfs4_layoutget_prepare,
6870 .rpc_call_done = nfs4_layoutget_done,
6871 .rpc_release = nfs4_layoutget_release,
6872};
6873
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006874struct pnfs_layout_segment *
6875nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006876{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006877 struct inode *inode = lgp->args.inode;
6878 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006879 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006880 struct rpc_task *task;
6881 struct rpc_message msg = {
6882 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6883 .rpc_argp = &lgp->args,
6884 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04006885 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006886 };
6887 struct rpc_task_setup task_setup_data = {
6888 .rpc_client = server->client,
6889 .rpc_message = &msg,
6890 .callback_ops = &nfs4_layoutget_call_ops,
6891 .callback_data = lgp,
6892 .flags = RPC_TASK_ASYNC,
6893 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006894 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006895 int status = 0;
6896
6897 dprintk("--> %s\n", __func__);
6898
Idan Kedar85541162012-08-02 11:47:10 +03006899 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6900 if (!lgp->args.layout.pages) {
6901 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006902 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006903 }
6904 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006905 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03006906
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006907 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006908 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006909 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006910
6911 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
6912 pnfs_get_layout_hdr(NFS_I(inode)->layout);
6913
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006914 task = rpc_run_task(&task_setup_data);
6915 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006916 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006917 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006918 if (status == 0)
6919 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04006920 trace_nfs4_layoutget(lgp->args.ctx,
6921 &lgp->args.range,
6922 &lgp->res.range,
6923 status);
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05006924 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
6925 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006926 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006927 rpc_put_task(task);
6928 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006929 if (status)
6930 return ERR_PTR(status);
6931 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006932}
6933
Benny Halevycbe82602011-05-22 19:52:37 +03006934static void
6935nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6936{
6937 struct nfs4_layoutreturn *lrp = calldata;
6938
6939 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006940 nfs41_setup_sequence(lrp->clp->cl_session,
6941 &lrp->args.seq_args,
6942 &lrp->res.seq_res,
6943 task);
Benny Halevycbe82602011-05-22 19:52:37 +03006944}
6945
6946static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6947{
6948 struct nfs4_layoutreturn *lrp = calldata;
6949 struct nfs_server *server;
6950
6951 dprintk("--> %s\n", __func__);
6952
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006953 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03006954 return;
6955
6956 server = NFS_SERVER(lrp->args.inode);
6957 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006958 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006959 return;
6960 }
Benny Halevycbe82602011-05-22 19:52:37 +03006961 dprintk("<-- %s\n", __func__);
6962}
6963
6964static void nfs4_layoutreturn_release(void *calldata)
6965{
6966 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006967 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006968
6969 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006970 spin_lock(&lo->plh_inode->i_lock);
6971 if (lrp->res.lrs_present)
6972 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6973 lo->plh_block_lgets--;
6974 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006975 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006976 kfree(calldata);
6977 dprintk("<-- %s\n", __func__);
6978}
6979
6980static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6981 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6982 .rpc_call_done = nfs4_layoutreturn_done,
6983 .rpc_release = nfs4_layoutreturn_release,
6984};
6985
6986int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6987{
6988 struct rpc_task *task;
6989 struct rpc_message msg = {
6990 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6991 .rpc_argp = &lrp->args,
6992 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04006993 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03006994 };
6995 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04006996 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03006997 .rpc_message = &msg,
6998 .callback_ops = &nfs4_layoutreturn_call_ops,
6999 .callback_data = lrp,
7000 };
7001 int status;
7002
7003 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05007004 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03007005 task = rpc_run_task(&task_setup_data);
7006 if (IS_ERR(task))
7007 return PTR_ERR(task);
7008 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007009 trace_nfs4_layoutreturn(lrp->args.inode, status);
Benny Halevycbe82602011-05-22 19:52:37 +03007010 dprintk("<-- %s status=%d\n", __func__, status);
7011 rpc_put_task(task);
7012 return status;
7013}
7014
Andy Adamson7f11d8d2011-07-30 20:52:35 -04007015/*
7016 * Retrieve the list of Data Server devices from the MDS.
7017 */
7018static int _nfs4_getdevicelist(struct nfs_server *server,
7019 const struct nfs_fh *fh,
7020 struct pnfs_devicelist *devlist)
7021{
7022 struct nfs4_getdevicelist_args args = {
7023 .fh = fh,
7024 .layoutclass = server->pnfs_curr_ld->id,
7025 };
7026 struct nfs4_getdevicelist_res res = {
7027 .devlist = devlist,
7028 };
7029 struct rpc_message msg = {
7030 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
7031 .rpc_argp = &args,
7032 .rpc_resp = &res,
7033 };
7034 int status;
7035
7036 dprintk("--> %s\n", __func__);
7037 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
7038 &res.seq_res, 0);
7039 dprintk("<-- %s status=%d\n", __func__, status);
7040 return status;
7041}
7042
7043int nfs4_proc_getdevicelist(struct nfs_server *server,
7044 const struct nfs_fh *fh,
7045 struct pnfs_devicelist *devlist)
7046{
7047 struct nfs4_exception exception = { };
7048 int err;
7049
7050 do {
7051 err = nfs4_handle_exception(server,
7052 _nfs4_getdevicelist(server, fh, devlist),
7053 &exception);
7054 } while (exception.retry);
7055
7056 dprintk("%s: err=%d, num_devs=%u\n", __func__,
7057 err, devlist->num_devs);
7058
7059 return err;
7060}
7061EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
7062
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007063static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007064_nfs4_proc_getdeviceinfo(struct nfs_server *server,
7065 struct pnfs_device *pdev,
7066 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007067{
7068 struct nfs4_getdeviceinfo_args args = {
7069 .pdev = pdev,
7070 };
7071 struct nfs4_getdeviceinfo_res res = {
7072 .pdev = pdev,
7073 };
7074 struct rpc_message msg = {
7075 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
7076 .rpc_argp = &args,
7077 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007078 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007079 };
7080 int status;
7081
7082 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00007083 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007084 dprintk("<-- %s status=%d\n", __func__, status);
7085
7086 return status;
7087}
7088
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007089int nfs4_proc_getdeviceinfo(struct nfs_server *server,
7090 struct pnfs_device *pdev,
7091 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007092{
7093 struct nfs4_exception exception = { };
7094 int err;
7095
7096 do {
7097 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007098 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007099 &exception);
7100 } while (exception.retry);
7101 return err;
7102}
7103EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
7104
Andy Adamson863a3c62011-03-23 13:27:54 +00007105static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
7106{
7107 struct nfs4_layoutcommit_data *data = calldata;
7108 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007109 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00007110
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007111 nfs41_setup_sequence(session,
7112 &data->args.seq_args,
7113 &data->res.seq_res,
7114 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00007115}
7116
7117static void
7118nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
7119{
7120 struct nfs4_layoutcommit_data *data = calldata;
7121 struct nfs_server *server = NFS_SERVER(data->args.inode);
7122
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007123 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00007124 return;
7125
7126 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007127 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
7128 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
7129 case -NFS4ERR_BADLAYOUT: /* no layout */
7130 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00007131 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007132 break;
7133 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00007134 nfs_post_op_update_inode_force_wcc(data->args.inode,
7135 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007136 break;
7137 default:
7138 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
7139 rpc_restart_call_prepare(task);
7140 return;
7141 }
7142 }
Andy Adamson863a3c62011-03-23 13:27:54 +00007143}
7144
7145static void nfs4_layoutcommit_release(void *calldata)
7146{
7147 struct nfs4_layoutcommit_data *data = calldata;
7148
Andy Adamsondb29c082011-07-30 20:52:38 -04007149 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00007150 put_rpccred(data->cred);
7151 kfree(data);
7152}
7153
7154static const struct rpc_call_ops nfs4_layoutcommit_ops = {
7155 .rpc_call_prepare = nfs4_layoutcommit_prepare,
7156 .rpc_call_done = nfs4_layoutcommit_done,
7157 .rpc_release = nfs4_layoutcommit_release,
7158};
7159
7160int
Andy Adamsonef311532011-03-12 02:58:10 -05007161nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00007162{
7163 struct rpc_message msg = {
7164 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
7165 .rpc_argp = &data->args,
7166 .rpc_resp = &data->res,
7167 .rpc_cred = data->cred,
7168 };
7169 struct rpc_task_setup task_setup_data = {
7170 .task = &data->task,
7171 .rpc_client = NFS_CLIENT(data->args.inode),
7172 .rpc_message = &msg,
7173 .callback_ops = &nfs4_layoutcommit_ops,
7174 .callback_data = data,
7175 .flags = RPC_TASK_ASYNC,
7176 };
7177 struct rpc_task *task;
7178 int status = 0;
7179
7180 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
7181 "lbw: %llu inode %lu\n",
7182 data->task.tk_pid, sync,
7183 data->args.lastbytewritten,
7184 data->args.inode->i_ino);
7185
Trond Myklebust9d12b212012-01-17 22:04:25 -05007186 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00007187 task = rpc_run_task(&task_setup_data);
7188 if (IS_ERR(task))
7189 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05007190 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00007191 goto out;
7192 status = nfs4_wait_for_completion_rpc_task(task);
7193 if (status != 0)
7194 goto out;
7195 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007196 trace_nfs4_layoutcommit(data->args.inode, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00007197out:
7198 dprintk("%s: status %d\n", __func__, status);
7199 rpc_put_task(task);
7200 return status;
7201}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007202
Andy Adamson97431202013-08-08 10:57:56 -04007203/**
7204 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
7205 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
7206 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007207static int
7208_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7209 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
7210{
7211 struct nfs41_secinfo_no_name_args args = {
7212 .style = SECINFO_STYLE_CURRENT_FH,
7213 };
7214 struct nfs4_secinfo_res res = {
7215 .flavors = flavors,
7216 };
7217 struct rpc_message msg = {
7218 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
7219 .rpc_argp = &args,
7220 .rpc_resp = &res,
7221 };
Andy Adamson97431202013-08-08 10:57:56 -04007222 return nfs4_call_sync(server->nfs_client->cl_rpcclient, server, &msg,
7223 &args.seq_args, &res.seq_res, 0);
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007224}
7225
7226static int
7227nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7228 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
7229{
7230 struct nfs4_exception exception = { };
7231 int err;
7232 do {
7233 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
7234 switch (err) {
7235 case 0:
7236 case -NFS4ERR_WRONGSEC:
7237 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007238 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007239 default:
7240 err = nfs4_handle_exception(server, err, &exception);
7241 }
7242 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007243out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007244 return err;
7245}
7246
7247static int
7248nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
7249 struct nfs_fsinfo *info)
7250{
7251 int err;
7252 struct page *page;
7253 rpc_authflavor_t flavor;
7254 struct nfs4_secinfo_flavors *flavors;
7255
7256 page = alloc_page(GFP_KERNEL);
7257 if (!page) {
7258 err = -ENOMEM;
7259 goto out;
7260 }
7261
7262 flavors = page_address(page);
7263 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
7264
7265 /*
7266 * Fall back on "guess and check" method if
7267 * the server doesn't support SECINFO_NO_NAME
7268 */
7269 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
7270 err = nfs4_find_root_sec(server, fhandle, info);
7271 goto out_freepage;
7272 }
7273 if (err)
7274 goto out_freepage;
7275
7276 flavor = nfs_find_best_sec(flavors);
7277 if (err == 0)
7278 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
7279
7280out_freepage:
7281 put_page(page);
7282 if (err == -EACCES)
7283 return -EPERM;
7284out:
7285 return err;
7286}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007287
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007288static int _nfs41_test_stateid(struct nfs_server *server,
7289 nfs4_stateid *stateid,
7290 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04007291{
7292 int status;
7293 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007294 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04007295 };
7296 struct nfs41_test_stateid_res res;
7297 struct rpc_message msg = {
7298 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
7299 .rpc_argp = &args,
7300 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007301 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04007302 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007303
Chuck Lever38527b12012-07-11 16:30:23 -04007304 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05007305 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007306 nfs4_set_sequence_privileged(&args.seq_args);
7307 status = nfs4_call_sync_sequence(server->client, server, &msg,
7308 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04007309 if (status != NFS_OK) {
7310 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04007311 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04007312 }
7313 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04007314 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04007315}
7316
Chuck Lever38527b12012-07-11 16:30:23 -04007317/**
7318 * nfs41_test_stateid - perform a TEST_STATEID operation
7319 *
7320 * @server: server / transport on which to perform the operation
7321 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007322 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04007323 *
7324 * Returns NFS_OK if the server recognizes that "stateid" is valid.
7325 * Otherwise a negative NFS4ERR value is returned if the operation
7326 * failed or the state ID is not currently valid.
7327 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007328static int nfs41_test_stateid(struct nfs_server *server,
7329 nfs4_stateid *stateid,
7330 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04007331{
7332 struct nfs4_exception exception = { };
7333 int err;
7334 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007335 err = _nfs41_test_stateid(server, stateid, cred);
Chuck Lever377e5072012-07-11 16:29:45 -04007336 if (err != -NFS4ERR_DELAY)
7337 break;
7338 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04007339 } while (exception.retry);
7340 return err;
7341}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007342
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007343struct nfs_free_stateid_data {
7344 struct nfs_server *server;
7345 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007346 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007347};
7348
7349static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
7350{
7351 struct nfs_free_stateid_data *data = calldata;
7352 nfs41_setup_sequence(nfs4_get_session(data->server),
7353 &data->args.seq_args,
7354 &data->res.seq_res,
7355 task);
7356}
7357
7358static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
7359{
7360 struct nfs_free_stateid_data *data = calldata;
7361
7362 nfs41_sequence_done(task, &data->res.seq_res);
7363
7364 switch (task->tk_status) {
7365 case -NFS4ERR_DELAY:
7366 if (nfs4_async_handle_error(task, data->server, NULL) == -EAGAIN)
7367 rpc_restart_call_prepare(task);
7368 }
7369}
7370
7371static void nfs41_free_stateid_release(void *calldata)
7372{
7373 kfree(calldata);
7374}
7375
Trond Myklebust17f26b12013-08-21 15:48:42 -04007376static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007377 .rpc_call_prepare = nfs41_free_stateid_prepare,
7378 .rpc_call_done = nfs41_free_stateid_done,
7379 .rpc_release = nfs41_free_stateid_release,
7380};
7381
7382static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
7383 nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007384 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007385 bool privileged)
7386{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007387 struct rpc_message msg = {
7388 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007389 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007390 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007391 struct rpc_task_setup task_setup = {
7392 .rpc_client = server->client,
7393 .rpc_message = &msg,
7394 .callback_ops = &nfs41_free_stateid_ops,
7395 .flags = RPC_TASK_ASYNC,
7396 };
7397 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007398
Chuck Lever38527b12012-07-11 16:30:23 -04007399 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007400 data = kmalloc(sizeof(*data), GFP_NOFS);
7401 if (!data)
7402 return ERR_PTR(-ENOMEM);
7403 data->server = server;
7404 nfs4_stateid_copy(&data->args.stateid, stateid);
7405
7406 task_setup.callback_data = data;
7407
7408 msg.rpc_argp = &data->args;
7409 msg.rpc_resp = &data->res;
7410 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
7411 if (privileged)
7412 nfs4_set_sequence_privileged(&data->args.seq_args);
7413
7414 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007415}
7416
Chuck Lever38527b12012-07-11 16:30:23 -04007417/**
7418 * nfs41_free_stateid - perform a FREE_STATEID operation
7419 *
7420 * @server: server / transport on which to perform the operation
7421 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007422 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04007423 *
7424 * Returns NFS_OK if the server freed "stateid". Otherwise a
7425 * negative NFS4ERR value is returned.
7426 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007427static int nfs41_free_stateid(struct nfs_server *server,
7428 nfs4_stateid *stateid,
7429 struct rpc_cred *cred)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007430{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007431 struct rpc_task *task;
7432 int ret;
7433
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007434 task = _nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007435 if (IS_ERR(task))
7436 return PTR_ERR(task);
7437 ret = rpc_wait_for_completion_task(task);
7438 if (!ret)
7439 ret = task->tk_status;
7440 rpc_put_task(task);
7441 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007442}
Trond Myklebust36281ca2012-03-04 18:13:56 -05007443
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007444static int nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
7445{
7446 struct rpc_task *task;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007447 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007448
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007449 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007450 nfs4_free_lock_state(server, lsp);
7451 if (IS_ERR(task))
7452 return PTR_ERR(task);
7453 rpc_put_task(task);
7454 return 0;
7455}
7456
Trond Myklebust36281ca2012-03-04 18:13:56 -05007457static bool nfs41_match_stateid(const nfs4_stateid *s1,
7458 const nfs4_stateid *s2)
7459{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007460 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007461 return false;
7462
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007463 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007464 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007465 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007466 return true;
7467
7468 return false;
7469}
7470
Andy Adamson557134a2009-04-01 09:21:53 -04007471#endif /* CONFIG_NFS_V4_1 */
7472
Trond Myklebust36281ca2012-03-04 18:13:56 -05007473static bool nfs4_match_stateid(const nfs4_stateid *s1,
7474 const nfs4_stateid *s2)
7475{
Trond Myklebustf597c532012-03-04 18:13:56 -05007476 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05007477}
7478
7479
Trond Myklebust17280172012-03-11 13:11:00 -04007480static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007481 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007482 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007483 .recover_open = nfs4_open_reclaim,
7484 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04007485 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04007486 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007487};
7488
Andy Adamson591d71c2009-04-01 09:22:47 -04007489#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007490static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007491 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
7492 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
7493 .recover_open = nfs4_open_reclaim,
7494 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05007495 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007496 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04007497 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04007498};
7499#endif /* CONFIG_NFS_V4_1 */
7500
Trond Myklebust17280172012-03-11 13:11:00 -04007501static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007502 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007503 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007504 .recover_open = nfs4_open_expired,
7505 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04007506 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007507};
7508
Andy Adamson591d71c2009-04-01 09:22:47 -04007509#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007510static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007511 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
7512 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007513 .recover_open = nfs41_open_expired,
7514 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05007515 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04007516};
7517#endif /* CONFIG_NFS_V4_1 */
7518
Trond Myklebust17280172012-03-11 13:11:00 -04007519static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007520 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04007521 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007522 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04007523};
7524
7525#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007526static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007527 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04007528 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007529 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04007530};
7531#endif
7532
Trond Myklebust97dc1352010-06-16 09:52:26 -04007533static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
7534 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04007535 .init_caps = NFS_CAP_READDIRPLUS
7536 | NFS_CAP_ATOMIC_OPEN
7537 | NFS_CAP_CHANGE_ATTR
7538 | NFS_CAP_POSIX_LOCK,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007539 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007540 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007541 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007542 .free_lock_state = nfs4_release_lockowner,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007543 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
7544 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
7545 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007546};
7547
7548#if defined(CONFIG_NFS_V4_1)
7549static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
7550 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04007551 .init_caps = NFS_CAP_READDIRPLUS
7552 | NFS_CAP_ATOMIC_OPEN
7553 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04007554 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04007555 | NFS_CAP_STATEID_NFSV41
7556 | NFS_CAP_ATOMIC_OPEN_V1,
Bryan Schumaker104287c2012-11-12 14:13:13 -05007557 .call_sync = nfs4_call_sync_sequence,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007558 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007559 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007560 .free_lock_state = nfs41_free_lock_state,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007561 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7562 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7563 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007564};
7565#endif
7566
Steve Dickson42c2c422013-05-22 12:50:38 -04007567#if defined(CONFIG_NFS_V4_2)
7568static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
7569 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04007570 .init_caps = NFS_CAP_READDIRPLUS
7571 | NFS_CAP_ATOMIC_OPEN
7572 | NFS_CAP_CHANGE_ATTR
7573 | NFS_CAP_POSIX_LOCK
7574 | NFS_CAP_STATEID_NFSV41
7575 | NFS_CAP_ATOMIC_OPEN_V1,
Steve Dickson42c2c422013-05-22 12:50:38 -04007576 .call_sync = nfs4_call_sync_sequence,
7577 .match_stateid = nfs41_match_stateid,
7578 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04007579 .free_lock_state = nfs41_free_lock_state,
Steve Dickson42c2c422013-05-22 12:50:38 -04007580 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7581 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7582 .state_renewal_ops = &nfs41_state_renewal_ops,
7583};
7584#endif
7585
Trond Myklebust97dc1352010-06-16 09:52:26 -04007586const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
7587 [0] = &nfs_v4_0_minor_ops,
7588#if defined(CONFIG_NFS_V4_1)
7589 [1] = &nfs_v4_1_minor_ops,
7590#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04007591#if defined(CONFIG_NFS_V4_2)
7592 [2] = &nfs_v4_2_minor_ops,
7593#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04007594};
7595
Trond Myklebust17f26b12013-08-21 15:48:42 -04007596static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04007597 .create = nfs_create,
7598 .lookup = nfs_lookup,
7599 .atomic_open = nfs_atomic_open,
7600 .link = nfs_link,
7601 .unlink = nfs_unlink,
7602 .symlink = nfs_symlink,
7603 .mkdir = nfs_mkdir,
7604 .rmdir = nfs_rmdir,
7605 .mknod = nfs_mknod,
7606 .rename = nfs_rename,
7607 .permission = nfs_permission,
7608 .getattr = nfs_getattr,
7609 .setattr = nfs_setattr,
7610 .getxattr = generic_getxattr,
7611 .setxattr = generic_setxattr,
7612 .listxattr = generic_listxattr,
7613 .removexattr = generic_removexattr,
7614};
7615
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08007616static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007617 .permission = nfs_permission,
7618 .getattr = nfs_getattr,
7619 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007620 .getxattr = generic_getxattr,
7621 .setxattr = generic_setxattr,
7622 .listxattr = generic_listxattr,
7623 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007624};
7625
David Howells509de812006-08-22 20:06:11 -04007626const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007627 .version = 4, /* protocol version */
7628 .dentry_ops = &nfs4_dentry_operations,
7629 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007630 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04007631 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007632 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04007633 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04007634 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007635 .getattr = nfs4_proc_getattr,
7636 .setattr = nfs4_proc_setattr,
7637 .lookup = nfs4_proc_lookup,
7638 .access = nfs4_proc_access,
7639 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007640 .create = nfs4_proc_create,
7641 .remove = nfs4_proc_remove,
7642 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04007643 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007644 .unlink_done = nfs4_proc_unlink_done,
7645 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04007646 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04007647 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04007648 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007649 .link = nfs4_proc_link,
7650 .symlink = nfs4_proc_symlink,
7651 .mkdir = nfs4_proc_mkdir,
7652 .rmdir = nfs4_proc_remove,
7653 .readdir = nfs4_proc_readdir,
7654 .mknod = nfs4_proc_mknod,
7655 .statfs = nfs4_proc_statfs,
7656 .fsinfo = nfs4_proc_fsinfo,
7657 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04007658 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007659 .decode_dirent = nfs4_decode_dirent,
7660 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04007661 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04007662 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05007663 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007664 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04007665 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04007666 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007667 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007668 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04007669 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007670 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007671 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00007672 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04007673 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04007674 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04007675 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04007676 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04007677 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00007678 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04007679 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04007680 .create_server = nfs4_create_server,
7681 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007682};
7683
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007684static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7685 .prefix = XATTR_NAME_NFSV4_ACL,
7686 .list = nfs4_xattr_list_nfs4_acl,
7687 .get = nfs4_xattr_get_nfs4_acl,
7688 .set = nfs4_xattr_set_nfs4_acl,
7689};
7690
7691const struct xattr_handler *nfs4_xattr_handlers[] = {
7692 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04007693#ifdef CONFIG_NFS_V4_SECURITY_LABEL
7694 &nfs4_xattr_nfs4_label_handler,
7695#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007696 NULL
7697};
7698
Linus Torvalds1da177e2005-04-16 15:20:36 -07007699/*
7700 * Local variables:
7701 * c-basic-offset: 8
7702 * End:
7703 */