blob: 7b702bc4941995eb64ba0a4b7455abe880c6d9b7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Trond Myklebust6926afd2012-01-07 13:22:46 -050054#include <linux/nfs_idmap.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000055#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050056#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010057#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050066#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000067#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Trond Myklebustc6d01c62013-08-09 11:51:26 -040069#include "nfs4trace.h"
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define NFSDBG_FACILITY NFSDBG_PROC
72
Trond Myklebust2066fe82006-09-15 08:30:46 -040073#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define NFS4_POLL_RETRY_MAX (15*HZ)
75
Trond Myklebustcdd4e682006-01-03 09:55:12 +010076struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010077static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080078static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050080static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Chuck Lever81934dd2012-03-01 17:01:57 -050081static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040082static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
83static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040084static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
85 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -040086 struct nfs4_state *state, struct nfs4_label *ilabel,
87 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040088#ifdef CONFIG_NFS_V4_1
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040089static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
90 struct rpc_cred *);
91static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *,
92 struct rpc_cred *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040093#endif
David Quigleyaa9c2662013-05-22 12:50:44 -040094
95#ifdef CONFIG_NFS_V4_SECURITY_LABEL
96static inline struct nfs4_label *
97nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
98 struct iattr *sattr, struct nfs4_label *label)
99{
100 int err;
101
102 if (label == NULL)
103 return NULL;
104
105 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
106 return NULL;
107
108 if (NFS_SERVER(dir)->nfs_client->cl_minorversion < 2)
109 return NULL;
110
111 err = security_dentry_init_security(dentry, sattr->ia_mode,
112 &dentry->d_name, (void **)&label->label, &label->len);
113 if (err == 0)
114 return label;
115
116 return NULL;
117}
118static inline void
119nfs4_label_release_security(struct nfs4_label *label)
120{
121 if (label)
122 security_release_secctx(label->label, label->len);
123}
124static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
125{
126 if (label)
127 return server->attr_bitmask;
128
129 return server->attr_bitmask_nl;
130}
131#else
132static inline struct nfs4_label *
133nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
134 struct iattr *sattr, struct nfs4_label *l)
135{ return NULL; }
136static inline void
137nfs4_label_release_security(struct nfs4_label *label)
138{ return; }
139static inline u32 *
140nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
141{ return server->attr_bitmask; }
142#endif
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500145static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Trond Myklebust52567b02009-10-23 14:46:42 -0400147 if (err >= -1000)
148 return err;
149 switch (err) {
150 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500151 case -NFS4ERR_LAYOUTTRYLATER:
152 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400153 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000154 case -NFS4ERR_WRONGSEC:
155 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800156 case -NFS4ERR_BADOWNER:
157 case -NFS4ERR_BADNAME:
158 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400159 case -NFS4ERR_SHARE_DENIED:
160 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400161 case -NFS4ERR_MINOR_VERS_MISMATCH:
162 return -EPROTONOSUPPORT;
Weston Andros Adamson6168f622012-09-10 14:00:46 -0400163 case -NFS4ERR_ACCESS:
164 return -EACCES;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400165 case -NFS4ERR_FILE_OPEN:
166 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400167 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700169 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400170 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400172 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
175/*
176 * This is our standard bitmap for GETATTR requests.
177 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400178const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 FATTR4_WORD0_TYPE
180 | FATTR4_WORD0_CHANGE
181 | FATTR4_WORD0_SIZE
182 | FATTR4_WORD0_FSID
183 | FATTR4_WORD0_FILEID,
184 FATTR4_WORD1_MODE
185 | FATTR4_WORD1_NUMLINKS
186 | FATTR4_WORD1_OWNER
187 | FATTR4_WORD1_OWNER_GROUP
188 | FATTR4_WORD1_RAWDEV
189 | FATTR4_WORD1_SPACE_USED
190 | FATTR4_WORD1_TIME_ACCESS
191 | FATTR4_WORD1_TIME_METADATA
David Quigleyaa9c2662013-05-22 12:50:44 -0400192 | FATTR4_WORD1_TIME_MODIFY,
193#ifdef CONFIG_NFS_V4_SECURITY_LABEL
194 FATTR4_WORD2_SECURITY_LABEL
195#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196};
197
Trond Myklebust1549210f2012-06-05 09:16:47 -0400198static const u32 nfs4_pnfs_open_bitmap[3] = {
199 FATTR4_WORD0_TYPE
200 | FATTR4_WORD0_CHANGE
201 | FATTR4_WORD0_SIZE
202 | FATTR4_WORD0_FSID
203 | FATTR4_WORD0_FILEID,
204 FATTR4_WORD1_MODE
205 | FATTR4_WORD1_NUMLINKS
206 | FATTR4_WORD1_OWNER
207 | FATTR4_WORD1_OWNER_GROUP
208 | FATTR4_WORD1_RAWDEV
209 | FATTR4_WORD1_SPACE_USED
210 | FATTR4_WORD1_TIME_ACCESS
211 | FATTR4_WORD1_TIME_METADATA
212 | FATTR4_WORD1_TIME_MODIFY,
213 FATTR4_WORD2_MDSTHRESHOLD
214};
215
Andy Adamsone23008e2012-10-02 21:07:32 -0400216static const u32 nfs4_open_noattr_bitmap[3] = {
217 FATTR4_WORD0_TYPE
218 | FATTR4_WORD0_CHANGE
219 | FATTR4_WORD0_FILEID,
220};
221
David Quigleya09df2c2013-05-22 12:50:41 -0400222const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 FATTR4_WORD0_FILES_AVAIL
224 | FATTR4_WORD0_FILES_FREE
225 | FATTR4_WORD0_FILES_TOTAL,
226 FATTR4_WORD1_SPACE_AVAIL
227 | FATTR4_WORD1_SPACE_FREE
228 | FATTR4_WORD1_SPACE_TOTAL
229};
230
David Quigleya09df2c2013-05-22 12:50:41 -0400231const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 FATTR4_WORD0_MAXLINK
233 | FATTR4_WORD0_MAXNAME,
234 0
235};
236
Fred Isamandae100c2011-07-30 20:52:37 -0400237const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 | FATTR4_WORD0_MAXREAD
239 | FATTR4_WORD0_MAXWRITE
240 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700241 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400242 | FATTR4_WORD1_FS_LAYOUT_TYPES,
243 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244};
245
David Quigleya09df2c2013-05-22 12:50:41 -0400246const u32 nfs4_fs_locations_bitmap[3] = {
Manoj Naik830b8e32006-06-09 09:34:25 -0400247 FATTR4_WORD0_TYPE
248 | FATTR4_WORD0_CHANGE
249 | FATTR4_WORD0_SIZE
250 | FATTR4_WORD0_FSID
251 | FATTR4_WORD0_FILEID
252 | FATTR4_WORD0_FS_LOCATIONS,
253 FATTR4_WORD1_MODE
254 | FATTR4_WORD1_NUMLINKS
255 | FATTR4_WORD1_OWNER
256 | FATTR4_WORD1_OWNER_GROUP
257 | FATTR4_WORD1_RAWDEV
258 | FATTR4_WORD1_SPACE_USED
259 | FATTR4_WORD1_TIME_ACCESS
260 | FATTR4_WORD1_TIME_METADATA
261 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400262 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400263};
264
Al Virobc4785c2006-10-19 23:28:51 -0700265static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 struct nfs4_readdir_arg *readdir)
267{
Al Viro0dbb4c62006-10-19 23:28:49 -0700268 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000271 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
273 return;
274 }
275
276 readdir->cookie = 0;
277 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
278 if (cookie == 2)
279 return;
280
281 /*
282 * NFSv4 servers do not return entries for '.' and '..'
283 * Therefore, we fake these entries here. We let '.'
284 * have cookie 0 and '..' have cookie 1. Note that
285 * when talking to the server, we always send cookie 0
286 * instead of 1 or 2.
287 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800288 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 if (cookie == 0) {
291 *p++ = xdr_one; /* next */
292 *p++ = xdr_zero; /* cookie, first word */
293 *p++ = xdr_one; /* cookie, second word */
294 *p++ = xdr_one; /* entry len */
295 memcpy(p, ".\0\0\0", 4); /* entry */
296 p++;
297 *p++ = xdr_one; /* bitmap length */
298 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
299 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400300 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302
303 *p++ = xdr_one; /* next */
304 *p++ = xdr_zero; /* cookie, first word */
305 *p++ = xdr_two; /* cookie, second word */
306 *p++ = xdr_two; /* entry len */
307 memcpy(p, "..\0\0", 4); /* entry */
308 p++;
309 *p++ = xdr_one; /* bitmap length */
310 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
311 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400312 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 readdir->pgbase = (char *)p - (char *)start;
315 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800316 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
Trond Myklebust65de8722008-12-23 15:21:44 -0500319static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
320{
321 int res = 0;
322
323 might_sleep();
324
325 if (*timeout <= 0)
326 *timeout = NFS4_POLL_RETRY_MIN;
327 if (*timeout > NFS4_POLL_RETRY_MAX)
328 *timeout = NFS4_POLL_RETRY_MAX;
Colin Cross416ad3c2013-05-06 23:50:06 +0000329 freezable_schedule_timeout_killable_unsafe(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500330 if (fatal_signal_pending(current))
331 res = -ERESTARTSYS;
332 *timeout <<= 1;
333 return res;
334}
335
336/* This is the error handling routine for processes that are allowed
337 * to sleep.
338 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800339static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500340{
341 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500342 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500343 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500344 int ret = errorcode;
345
346 exception->retry = 0;
347 switch(errorcode) {
348 case 0:
349 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500350 case -NFS4ERR_OPENMODE:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400351 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400352 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500353 exception->retry = 1;
354 return 0;
355 }
356 if (state == NULL)
357 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400358 ret = nfs4_schedule_stateid_recovery(server, state);
359 if (ret < 0)
360 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500361 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500362 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500363 case -NFS4ERR_ADMIN_REVOKED:
364 case -NFS4ERR_BAD_STATEID:
Trond Myklebustb570a972013-04-12 15:04:51 -0400365 if (inode != NULL && nfs4_have_delegation(inode, FMODE_READ)) {
366 nfs_remove_bad_delegation(inode);
367 exception->retry = 1;
368 break;
369 }
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500370 if (state == NULL)
371 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400372 ret = nfs4_schedule_stateid_recovery(server, state);
373 if (ret < 0)
374 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500375 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400376 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -0400377 if (state != NULL) {
378 ret = nfs4_schedule_stateid_recovery(server, state);
379 if (ret < 0)
380 break;
381 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500382 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500383 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500384 nfs4_schedule_lease_recovery(clp);
385 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500386#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400387 case -NFS4ERR_BADSESSION:
388 case -NFS4ERR_BADSLOT:
389 case -NFS4ERR_BAD_HIGH_SLOT:
390 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
391 case -NFS4ERR_DEADSESSION:
392 case -NFS4ERR_SEQ_FALSE_RETRY:
393 case -NFS4ERR_SEQ_MISORDERED:
394 dprintk("%s ERROR: %d Reset session\n", __func__,
395 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400396 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400397 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500398#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500399 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500400 if (exception->timeout > HZ) {
401 /* We have retried a decent amount, time to
402 * fail
403 */
404 ret = -EBUSY;
405 break;
406 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500407 case -NFS4ERR_GRACE:
408 case -NFS4ERR_DELAY:
409 ret = nfs4_delay(server->client, &exception->timeout);
410 if (ret != 0)
411 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400412 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500413 case -NFS4ERR_OLD_STATEID:
414 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800415 break;
416 case -NFS4ERR_BADOWNER:
417 /* The following works around a Linux server bug! */
418 case -NFS4ERR_BADNAME:
419 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
420 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
421 exception->retry = 1;
422 printk(KERN_WARNING "NFS: v4 server %s "
423 "does not accept raw "
424 "uid/gids. "
425 "Reenabling the idmapper.\n",
426 server->nfs_client->cl_hostname);
427 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500428 }
429 /* We failed to handle the error */
430 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500431wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500432 ret = nfs4_wait_clnt_recover(clp);
433 if (ret == 0)
434 exception->retry = 1;
435 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500436}
437
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400438/*
439 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
440 * or 'false' otherwise.
441 */
442static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
443{
444 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
445
446 if (flavor == RPC_AUTH_GSS_KRB5I ||
447 flavor == RPC_AUTH_GSS_KRB5P)
448 return true;
449
450 return false;
451}
Trond Myklebust65de8722008-12-23 15:21:44 -0500452
Trond Myklebust452e9352010-07-31 14:29:06 -0400453static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 spin_lock(&clp->cl_lock);
456 if (time_before(clp->cl_last_renewal,timestamp))
457 clp->cl_last_renewal = timestamp;
458 spin_unlock(&clp->cl_lock);
459}
460
Trond Myklebust452e9352010-07-31 14:29:06 -0400461static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
462{
463 do_renew_lease(server->nfs_client, timestamp);
464}
465
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400466struct nfs4_call_sync_data {
467 const struct nfs_server *seq_server;
468 struct nfs4_sequence_args *seq_args;
469 struct nfs4_sequence_res *seq_res;
470};
471
Chuck Levera9c92d62013-08-09 12:48:18 -0400472static void nfs4_init_sequence(struct nfs4_sequence_args *args,
473 struct nfs4_sequence_res *res, int cache_reply)
474{
475 args->sa_slot = NULL;
476 args->sa_cache_this = cache_reply;
477 args->sa_privileged = 0;
478
479 res->sr_slot = NULL;
480}
481
482static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
483{
484 args->sa_privileged = 1;
485}
486
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400487#if defined(CONFIG_NFS_V4_1)
488
Trond Myklebustd185a332010-06-16 09:52:25 -0400489static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400490{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500491 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400492 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500493 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400494
Benny Halevydfb4f3092010-09-24 09:17:01 -0400495 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400496 /* just wake up the next guy waiting since
497 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500498 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500499 return;
Andy Adamson13615872009-04-01 09:22:17 -0400500 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500501 tbl = res->sr_slot->table;
502 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500503
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500504 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500505 /* Be nice to the server: try to ensure that the last transmitted
506 * value for highest_user_slotid <= target_highest_slotid
507 */
508 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
509 send_new_highest_used_slotid = true;
510
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500511 if (nfs41_wake_and_assign_slot(tbl, res->sr_slot)) {
512 send_new_highest_used_slotid = false;
513 goto out_unlock;
514 }
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500515 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500516
517 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
518 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500519out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500520 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400521 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500522 if (send_new_highest_used_slotid)
523 nfs41_server_notify_highest_slotid_update(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400524}
525
Trond Myklebust14516c32010-07-31 14:29:06 -0400526static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400527{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500528 struct nfs4_session *session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500529 struct nfs4_slot *slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400530 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500531 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500532 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400533
Bryan Schumaker468f8612011-04-18 15:57:32 -0400534 /* don't increment the sequence number if the task wasn't sent */
535 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400536 goto out;
537
Trond Myklebust933602e2012-11-16 12:12:38 -0500538 slot = res->sr_slot;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500539 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500540
Trond Myklebustac20d162012-12-15 15:36:07 -0500541 if (slot->interrupted) {
542 slot->interrupted = 0;
543 interrupted = true;
544 }
545
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400546 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500547 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400548 switch (res->sr_status) {
549 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400550 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500551 ++slot->seq_nr;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500552 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500553 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500554 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500555 if (res->sr_status_flags != 0)
556 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500557 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400558 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500559 case 1:
560 /*
561 * sr_status remains 1 if an RPC level error occurred.
562 * The server may or may not have processed the sequence
563 * operation..
564 * Mark the slot as having hosted an interrupted RPC call.
565 */
566 slot->interrupted = 1;
567 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400568 case -NFS4ERR_DELAY:
569 /* The server detected a resend of the RPC call and
570 * returned NFS4ERR_DELAY as per Section 2.10.6.2
571 * of RFC5661.
572 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500573 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400574 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500575 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500576 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400577 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500578 case -NFS4ERR_BADSLOT:
579 /*
580 * The slot id we used was probably retired. Try again
581 * using a different slot id.
582 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500583 goto retry_nowait;
584 case -NFS4ERR_SEQ_MISORDERED:
585 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500586 * Was the last operation on this sequence interrupted?
587 * If so, retry after bumping the sequence number.
588 */
589 if (interrupted) {
590 ++slot->seq_nr;
591 goto retry_nowait;
592 }
593 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500594 * Could this slot have been previously retired?
595 * If so, then the server may be expecting seq_nr = 1!
596 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500597 if (slot->seq_nr != 1) {
598 slot->seq_nr = 1;
599 goto retry_nowait;
600 }
601 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500602 case -NFS4ERR_SEQ_FALSE_RETRY:
603 ++slot->seq_nr;
604 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400605 default:
606 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500607 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400608 }
609out:
610 /* The session may be reset by one of the error handlers. */
611 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400612 nfs41_sequence_free_slot(res);
Trond Myklebust85563072012-12-11 10:31:12 -0500613 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500614retry_nowait:
615 if (rpc_restart_call_prepare(task)) {
616 task->tk_status = 0;
617 ret = 0;
618 }
619 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400620out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400621 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400622 goto out;
623 rpc_delay(task, NFS4_POLL_RETRY_MAX);
624 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400625}
626
Trond Myklebust14516c32010-07-31 14:29:06 -0400627static int nfs4_sequence_done(struct rpc_task *task,
628 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400629{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500630 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400631 return 1;
632 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400633}
634
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000635int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400636 struct nfs4_sequence_args *args,
637 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400638 struct rpc_task *task)
639{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400640 struct nfs4_slot *slot;
641 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400642
643 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400644 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400645 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400646 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400647
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400648 tbl = &session->fc_slot_table;
649
Trond Myklebust69d206b2012-11-22 13:21:02 -0500650 task->tk_timeout = 0;
651
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400652 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400653 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400654 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500655 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500656 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400657 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400658 }
659
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500660 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500661 if (IS_ERR(slot)) {
662 /* If out of memory, try again in 1/4 second */
663 if (slot == ERR_PTR(-ENOMEM))
664 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400665 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400666 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400667 }
668 spin_unlock(&tbl->slot_tbl_lock);
669
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500670 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400671
Chuck Levere8d92382013-08-09 12:47:51 -0400672 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500673 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400674
Benny Halevydfb4f3092010-09-24 09:17:01 -0400675 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500676 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400677 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400678 /*
679 * sr_status is only set in decode_sequence, and so will remain
680 * set to 1 if an rpc level failure occurs.
681 */
682 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400683 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400684out_success:
685 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400686 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400687out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400688 /* Privileged tasks are queued with top priority */
689 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400690 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
691 NULL, RPC_PRIORITY_PRIVILEGED);
692 else
693 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400694 spin_unlock(&tbl->slot_tbl_lock);
695 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400696}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000697EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400698
Chuck Lever5a580e02013-08-09 12:48:09 -0400699static int nfs4_setup_sequence(const struct nfs_server *server,
700 struct nfs4_sequence_args *args,
701 struct nfs4_sequence_res *res,
702 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400703{
Trond Myklebust035168a2010-06-16 09:52:26 -0400704 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400705 int ret = 0;
706
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400707 if (session == NULL) {
708 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400709 goto out;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400710 }
Trond Myklebust035168a2010-06-16 09:52:26 -0400711
Chuck Levere8d92382013-08-09 12:47:51 -0400712 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400713 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400714 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168a2010-06-16 09:52:26 -0400715
Trond Myklebust9d12b212012-01-17 22:04:25 -0500716 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400717out:
718 dprintk("<-- %s status=%d\n", __func__, ret);
719 return ret;
720}
721
Andy Adamsonce5039c2009-04-01 09:22:13 -0400722static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
723{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400724 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400725 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400726
Trond Myklebust035168a2010-06-16 09:52:26 -0400727 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
728
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400729 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400730}
731
Andy Adamson69ab40c2009-04-01 09:22:19 -0400732static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
733{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400734 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400735
Trond Myklebust14516c32010-07-31 14:29:06 -0400736 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400737}
738
Trond Myklebust17280172012-03-11 13:11:00 -0400739static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400740 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400741 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400742};
743
Bryan Schumaker7c513052011-03-24 17:12:24 +0000744static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
745 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400746 struct rpc_message *msg,
747 struct nfs4_sequence_args *args,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400748 struct nfs4_sequence_res *res)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400749{
750 int ret;
751 struct rpc_task *task;
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400752 struct nfs4_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400753 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400754 .seq_args = args,
755 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400756 };
757 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000758 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400759 .rpc_message = msg,
760 .callback_ops = &nfs41_call_sync_ops,
761 .callback_data = &data
762 };
763
Andy Adamsonce5039c2009-04-01 09:22:13 -0400764 task = rpc_run_task(&task_setup);
765 if (IS_ERR(task))
766 ret = PTR_ERR(task);
767 else {
768 ret = task->tk_status;
769 rpc_put_task(task);
770 }
771 return ret;
772}
773
Trond Myklebustdf896452010-06-16 09:52:26 -0400774#else
Chuck Lever5a580e02013-08-09 12:48:09 -0400775static int nfs4_setup_sequence(const struct nfs_server *server,
776 struct nfs4_sequence_args *args,
777 struct nfs4_sequence_res *res,
778 struct rpc_task *task)
779{
780 rpc_call_start(task);
781 return 0;
782}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400783
Trond Myklebust14516c32010-07-31 14:29:06 -0400784static int nfs4_sequence_done(struct rpc_task *task,
785 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400786{
Trond Myklebust14516c32010-07-31 14:29:06 -0400787 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400788}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400789#endif /* CONFIG_NFS_V4_1 */
790
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400791static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000792int _nfs4_call_sync(struct rpc_clnt *clnt,
793 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400794 struct rpc_message *msg,
795 struct nfs4_sequence_args *args,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400796 struct nfs4_sequence_res *res)
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400797{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000798 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400799}
800
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400801static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000802int nfs4_call_sync(struct rpc_clnt *clnt,
803 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000804 struct rpc_message *msg,
805 struct nfs4_sequence_args *args,
806 struct nfs4_sequence_res *res,
807 int cache_reply)
808{
Chuck Levera9c92d62013-08-09 12:48:18 -0400809 nfs4_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000810 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400811 args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000812}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400813
Trond Myklebust38478b22006-05-25 01:40:57 -0400814static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
Trond Myklebust38478b22006-05-25 01:40:57 -0400816 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Trond Myklebust38478b22006-05-25 01:40:57 -0400818 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400819 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700820 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400821 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700822 dir->i_version = cinfo->after;
David Howellsde242c02012-12-20 21:52:38 +0000823 nfs_fscache_invalidate(dir);
Trond Myklebust38478b22006-05-25 01:40:57 -0400824 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825}
826
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100827struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400828 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100829 struct nfs_openargs o_arg;
830 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100831 struct nfs_open_confirmargs c_arg;
832 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500833 struct nfs4_string owner_name;
834 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100835 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400836 struct nfs4_label *f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100837 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400838 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100839 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400840 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100841 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100842 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400843 unsigned int rpc_done : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -0400844 unsigned int is_recover : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100845 int rpc_status;
846 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100847};
848
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400849static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
850 int err, struct nfs4_exception *exception)
851{
852 if (err != -EINVAL)
853 return false;
854 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
855 return false;
856 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
857 exception->retry = 1;
858 return true;
859}
860
861static enum open_claim_type4
862nfs4_map_atomic_open_claim(struct nfs_server *server,
863 enum open_claim_type4 claim)
864{
865 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
866 return claim;
867 switch (claim) {
868 default:
869 return claim;
870 case NFS4_OPEN_CLAIM_FH:
871 return NFS4_OPEN_CLAIM_NULL;
872 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
873 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
874 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
875 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
876 }
877}
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400878
879static void nfs4_init_opendata_res(struct nfs4_opendata *p)
880{
881 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400882 p->o_res.f_label = p->f_label;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400883 p->o_res.seqid = p->o_arg.seqid;
884 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400885 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400886 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400887 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500888 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400889}
890
Al Viro82a2c1b2011-06-22 18:30:55 -0400891static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500892 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400893 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -0400894 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400895 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400896 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100897{
Al Viro82a2c1b2011-06-22 18:30:55 -0400898 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100899 struct inode *dir = parent->d_inode;
900 struct nfs_server *server = NFS_SERVER(dir);
901 struct nfs4_opendata *p;
902
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400903 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100904 if (p == NULL)
905 goto err;
David Quigley14c43f72013-05-22 12:50:43 -0400906
907 p->f_label = nfs4_label_alloc(server, gfp_mask);
908 if (IS_ERR(p->f_label))
909 goto err_free_p;
910
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400911 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100912 if (p->o_arg.seqid == NULL)
David Quigley14c43f72013-05-22 12:50:43 -0400913 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -0400914 nfs_sb_active(dentry->d_sb);
915 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100916 p->dir = parent;
917 p->owner = sp;
918 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500919 p->o_arg.open_flags = flags;
920 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700921 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
922 * will return permission denied for all bits until close */
923 if (!(flags & O_EXCL)) {
924 /* ask server to check for all possible rights as results
925 * are cached */
926 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
927 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700928 }
David Howells7539bba2006-08-22 20:06:09 -0400929 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400930 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
931 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400932 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100933 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -0400934 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -0400935 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
David Quigley1775fd32013-05-22 12:50:42 -0400936 p->o_arg.label = label;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400937 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
938 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400939 case NFS4_OPEN_CLAIM_NULL:
940 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
941 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
942 p->o_arg.fh = NFS_FH(dir);
943 break;
944 case NFS4_OPEN_CLAIM_PREVIOUS:
945 case NFS4_OPEN_CLAIM_FH:
946 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
947 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
948 p->o_arg.fh = NFS_FH(dentry->d_inode);
949 }
Trond Myklebust536e43d2012-01-17 22:04:26 -0500950 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -0400951 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400952
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100953 p->o_arg.u.attrs = &p->attrs;
954 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500955
956 verf[0] = jiffies;
957 verf[1] = current->pid;
958 memcpy(p->o_arg.u.verifier.data, verf,
959 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100960 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100961 p->c_arg.fh = &p->o_res.fh;
962 p->c_arg.stateid = &p->o_res.stateid;
963 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400964 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400965 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100966 return p;
David Quigley14c43f72013-05-22 12:50:43 -0400967
968err_free_label:
969 nfs4_label_free(p->f_label);
970err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100971 kfree(p);
972err:
973 dput(parent);
974 return NULL;
975}
976
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400977static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100978{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400979 struct nfs4_opendata *p = container_of(kref,
980 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400981 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400982
983 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400984 if (p->state != NULL)
985 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400986 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -0400987
988 nfs4_label_free(p->f_label);
989
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400990 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400991 dput(p->dentry);
992 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500993 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400994 kfree(p);
995}
996
997static void nfs4_opendata_put(struct nfs4_opendata *p)
998{
999 if (p != NULL)
1000 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001001}
1002
Trond Myklebust06f814a2006-01-03 09:55:07 +01001003static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1004{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001005 int ret;
1006
Trond Myklebust06f814a2006-01-03 09:55:07 +01001007 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001008 return ret;
1009}
1010
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001011static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001012{
1013 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001014
Trond Myklebust536e43d2012-01-17 22:04:26 -05001015 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001016 goto out;
1017 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001018 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001019 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1020 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001021 break;
1022 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001023 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1024 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001025 break;
1026 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001027 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1028 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001029 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001030out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001031 return ret;
1032}
1033
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001034static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001035{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001036 if (delegation == NULL)
1037 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001038 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001039 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -05001040 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -04001041 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001042 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1043 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -05001044 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001045 return 1;
1046}
1047
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001048static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001049{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001050 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001051 case FMODE_WRITE:
1052 state->n_wronly++;
1053 break;
1054 case FMODE_READ:
1055 state->n_rdonly++;
1056 break;
1057 case FMODE_READ|FMODE_WRITE:
1058 state->n_rdwr++;
1059 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001060 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001061}
1062
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001063static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001064{
1065 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001066 nfs4_stateid_copy(&state->stateid, stateid);
1067 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust92b40e92013-04-20 01:25:45 -04001068 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001069 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001070 case FMODE_READ:
1071 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1072 break;
1073 case FMODE_WRITE:
1074 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1075 break;
1076 case FMODE_READ|FMODE_WRITE:
1077 set_bit(NFS_O_RDWR_STATE, &state->flags);
1078 }
1079}
1080
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001081static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001082{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001083 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001084 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001085 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001086}
1087
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001088static 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 -07001089{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001090 /*
1091 * Protect the call to nfs4_state_set_mode_locked and
1092 * serialise the stateid update
1093 */
1094 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001095 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001096 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001097 set_bit(NFS_DELEGATED_STATE, &state->flags);
1098 }
1099 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001100 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001101 write_sequnlock(&state->seqlock);
1102 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001103 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001104 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105}
1106
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001107static 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 -05001108{
1109 struct nfs_inode *nfsi = NFS_I(state->inode);
1110 struct nfs_delegation *deleg_cur;
1111 int ret = 0;
1112
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001113 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001114
1115 rcu_read_lock();
1116 deleg_cur = rcu_dereference(nfsi->delegation);
1117 if (deleg_cur == NULL)
1118 goto no_delegation;
1119
1120 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001121 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001122 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001123 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001124 goto no_delegation_unlock;
1125
1126 if (delegation == NULL)
1127 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001128 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001129 goto no_delegation_unlock;
1130
Trond Myklebustb7391f42008-12-23 15:21:52 -05001131 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001132 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001133 ret = 1;
1134no_delegation_unlock:
1135 spin_unlock(&deleg_cur->lock);
1136no_delegation:
1137 rcu_read_unlock();
1138
1139 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001140 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001141 ret = 1;
1142 }
1143
1144 return ret;
1145}
1146
1147
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001148static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001149{
1150 struct nfs_delegation *delegation;
1151
1152 rcu_read_lock();
1153 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001154 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001155 rcu_read_unlock();
1156 return;
1157 }
1158 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001159 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001160}
1161
Trond Myklebust6ee41262007-07-08 14:11:36 -04001162static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001163{
1164 struct nfs4_state *state = opendata->state;
1165 struct nfs_inode *nfsi = NFS_I(state->inode);
1166 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001167 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001168 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001169 nfs4_stateid stateid;
1170 int ret = -EAGAIN;
1171
Trond Myklebustaac00a82007-07-05 19:02:21 -04001172 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001173 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001174 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001175 if (can_open_cached(state, fmode, open_mode)) {
1176 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001177 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001178 goto out_return_state;
1179 }
1180 spin_unlock(&state->owner->so_lock);
1181 }
Trond Myklebust34310432008-12-23 15:21:38 -05001182 rcu_read_lock();
1183 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001184 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001185 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001186 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001187 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001188 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001189 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001190 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001191 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001192 if (!opendata->is_recover) {
1193 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1194 if (ret != 0)
1195 goto out;
1196 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001197 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001198
1199 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001200 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001201 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001202 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001203out:
1204 return ERR_PTR(ret);
1205out_return_state:
1206 atomic_inc(&state->count);
1207 return state;
1208}
1209
Andy Adamsone23008e2012-10-02 21:07:32 -04001210static void
1211nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1212{
1213 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1214 struct nfs_delegation *delegation;
1215 int delegation_flags = 0;
1216
1217 rcu_read_lock();
1218 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1219 if (delegation)
1220 delegation_flags = delegation->flags;
1221 rcu_read_unlock();
1222 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1223 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1224 "returning a delegation for "
1225 "OPEN(CLAIM_DELEGATE_CUR)\n",
1226 clp->cl_hostname);
1227 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1228 nfs_inode_set_delegation(state->inode,
1229 data->owner->so_cred,
1230 &data->o_res);
1231 else
1232 nfs_inode_reclaim_delegation(state->inode,
1233 data->owner->so_cred,
1234 &data->o_res);
1235}
1236
1237/*
1238 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1239 * and update the nfs4_state.
1240 */
1241static struct nfs4_state *
1242_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1243{
1244 struct inode *inode = data->state->inode;
1245 struct nfs4_state *state = data->state;
1246 int ret;
1247
1248 if (!data->rpc_done) {
1249 ret = data->rpc_status;
1250 goto err;
1251 }
1252
1253 ret = -ESTALE;
1254 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1255 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1256 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1257 goto err;
1258
1259 ret = -ENOMEM;
1260 state = nfs4_get_open_state(inode, data->owner);
1261 if (state == NULL)
1262 goto err;
1263
1264 ret = nfs_refresh_inode(inode, &data->f_attr);
1265 if (ret)
1266 goto err;
1267
David Quigleyaa9c2662013-05-22 12:50:44 -04001268 nfs_setsecurity(inode, &data->f_attr, data->f_label);
1269
Andy Adamsone23008e2012-10-02 21:07:32 -04001270 if (data->o_res.delegation_type != 0)
1271 nfs4_opendata_check_deleg(data, state);
1272 update_open_stateid(state, &data->o_res.stateid, NULL,
1273 data->o_arg.fmode);
1274
1275 return state;
1276err:
1277 return ERR_PTR(ret);
1278
1279}
1280
1281static struct nfs4_state *
1282_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001283{
1284 struct inode *inode;
1285 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001286 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001287
Trond Myklebustaac00a82007-07-05 19:02:21 -04001288 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001289 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001290 goto out;
1291 }
1292
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001293 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001294 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001295 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001296 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001297 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001298 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001299 goto err;
1300 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001301 state = nfs4_get_open_state(inode, data->owner);
1302 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001303 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001304 if (data->o_res.delegation_type != 0)
1305 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001306 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001307 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001308 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001309out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001310 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001311 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001312err_put_inode:
1313 iput(inode);
1314err:
1315 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001316}
1317
Andy Adamsone23008e2012-10-02 21:07:32 -04001318static struct nfs4_state *
1319nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1320{
1321 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1322 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1323 return _nfs4_opendata_to_nfs4_state(data);
1324}
1325
Trond Myklebust864472e2006-01-03 09:55:15 +01001326static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1327{
1328 struct nfs_inode *nfsi = NFS_I(state->inode);
1329 struct nfs_open_context *ctx;
1330
1331 spin_lock(&state->inode->i_lock);
1332 list_for_each_entry(ctx, &nfsi->open_files, list) {
1333 if (ctx->state != state)
1334 continue;
1335 get_nfs_open_context(ctx);
1336 spin_unlock(&state->inode->i_lock);
1337 return ctx;
1338 }
1339 spin_unlock(&state->inode->i_lock);
1340 return ERR_PTR(-ENOENT);
1341}
1342
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001343static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1344 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001345{
1346 struct nfs4_opendata *opendata;
1347
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001348 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001349 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001350 if (opendata == NULL)
1351 return ERR_PTR(-ENOMEM);
1352 opendata->state = state;
1353 atomic_inc(&state->count);
1354 return opendata;
1355}
1356
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001357static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001358{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001359 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001360 int ret;
1361
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001362 opendata->o_arg.open_flags = 0;
1363 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001364 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1365 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1366 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001367 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001368 if (ret != 0)
1369 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001370 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001371 if (IS_ERR(newstate))
1372 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001373 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001374 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001375 return 0;
1376}
1377
1378static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1379{
Trond Myklebust864472e2006-01-03 09:55:15 +01001380 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001381 int ret;
1382
1383 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001384 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001385 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001386 smp_rmb();
1387 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001388 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001389 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001390 if (ret != 0)
1391 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001392 if (newstate != state)
1393 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001394 }
1395 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001396 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001397 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001398 if (ret != 0)
1399 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001400 if (newstate != state)
1401 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001402 }
1403 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001404 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001405 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001406 if (ret != 0)
1407 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001408 if (newstate != state)
1409 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001410 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001411 /*
1412 * We may have performed cached opens for all three recoveries.
1413 * Check if we need to update the current stateid.
1414 */
1415 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001416 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001417 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001418 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001419 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001420 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001421 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001422 return 0;
1423}
1424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425/*
1426 * OPEN_RECLAIM:
1427 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001429static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001431 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001432 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001433 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 int status;
1435
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001436 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1437 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001438 if (IS_ERR(opendata))
1439 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001440 rcu_read_lock();
1441 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001442 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001443 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001444 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001445 opendata->o_arg.u.delegation_type = delegation_type;
1446 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001447 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 return status;
1449}
1450
Trond Myklebust539cd032007-06-05 11:46:42 -04001451static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452{
1453 struct nfs_server *server = NFS_SERVER(state->inode);
1454 struct nfs4_exception exception = { };
1455 int err;
1456 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001457 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001458 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001459 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1460 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001461 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001462 break;
1463 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 } while (exception.retry);
1465 return err;
1466}
1467
Trond Myklebust864472e2006-01-03 09:55:15 +01001468static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1469{
1470 struct nfs_open_context *ctx;
1471 int ret;
1472
1473 ctx = nfs4_state_find_open_context(state);
1474 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001475 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001476 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001477 put_nfs_open_context(ctx);
1478 return ret;
1479}
1480
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001481static 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 -07001482{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001483 switch (err) {
1484 default:
1485 printk(KERN_ERR "NFS: %s: unhandled error "
1486 "%d.\n", __func__, err);
1487 case 0:
1488 case -ENOENT:
1489 case -ESTALE:
1490 break;
1491 case -NFS4ERR_BADSESSION:
1492 case -NFS4ERR_BADSLOT:
1493 case -NFS4ERR_BAD_HIGH_SLOT:
1494 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1495 case -NFS4ERR_DEADSESSION:
1496 set_bit(NFS_DELEGATED_STATE, &state->flags);
1497 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1498 return -EAGAIN;
1499 case -NFS4ERR_STALE_CLIENTID:
1500 case -NFS4ERR_STALE_STATEID:
1501 set_bit(NFS_DELEGATED_STATE, &state->flags);
1502 case -NFS4ERR_EXPIRED:
1503 /* Don't recall a delegation if it was lost */
1504 nfs4_schedule_lease_recovery(server->nfs_client);
1505 return -EAGAIN;
1506 case -NFS4ERR_DELEG_REVOKED:
1507 case -NFS4ERR_ADMIN_REVOKED:
1508 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001509 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001510 nfs_inode_find_state_and_recover(state->inode,
1511 stateid);
1512 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001513 return 0;
1514 case -NFS4ERR_DELAY:
1515 case -NFS4ERR_GRACE:
1516 set_bit(NFS_DELEGATED_STATE, &state->flags);
1517 ssleep(1);
1518 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001519 case -ENOMEM:
1520 case -NFS4ERR_DENIED:
1521 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1522 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 return err;
1525}
1526
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001527int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1528{
1529 struct nfs_server *server = NFS_SERVER(state->inode);
1530 struct nfs4_opendata *opendata;
1531 int err;
1532
1533 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1534 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1535 if (IS_ERR(opendata))
1536 return PTR_ERR(opendata);
1537 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1538 err = nfs4_open_recover(opendata, state);
1539 nfs4_opendata_put(opendata);
1540 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1541}
1542
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001543static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1544{
1545 struct nfs4_opendata *data = calldata;
1546
1547 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001548 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001549 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001550 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001551 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001552 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001553 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001554}
1555
1556static void nfs4_open_confirm_release(void *calldata)
1557{
1558 struct nfs4_opendata *data = calldata;
1559 struct nfs4_state *state = NULL;
1560
1561 /* If this request hasn't been cancelled, do nothing */
1562 if (data->cancelled == 0)
1563 goto out_free;
1564 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001565 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001566 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001567 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001568 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001569 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001570out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001571 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001572}
1573
1574static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001575 .rpc_call_done = nfs4_open_confirm_done,
1576 .rpc_release = nfs4_open_confirm_release,
1577};
1578
1579/*
1580 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1581 */
1582static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1583{
1584 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1585 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001586 struct rpc_message msg = {
1587 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1588 .rpc_argp = &data->c_arg,
1589 .rpc_resp = &data->c_res,
1590 .rpc_cred = data->owner->so_cred,
1591 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001592 struct rpc_task_setup task_setup_data = {
1593 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001594 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001595 .callback_ops = &nfs4_open_confirm_ops,
1596 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001597 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001598 .flags = RPC_TASK_ASYNC,
1599 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 int status;
1601
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001602 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001603 data->rpc_done = 0;
1604 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001605 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001606 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001607 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001608 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001609 status = nfs4_wait_for_completion_rpc_task(task);
1610 if (status != 0) {
1611 data->cancelled = 1;
1612 smp_wmb();
1613 } else
1614 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001615 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 return status;
1617}
1618
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001619static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001621 struct nfs4_opendata *data = calldata;
1622 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001623 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001624
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001625 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001626 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001627 /*
1628 * Check if we still need to send an OPEN call, or if we can use
1629 * a delegation instead.
1630 */
1631 if (data->state != NULL) {
1632 struct nfs_delegation *delegation;
1633
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001634 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001635 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001636 rcu_read_lock();
1637 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001638 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
Trond Myklebustcd4c9be2013-04-23 14:46:25 -04001639 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
Trond Myklebust652f89f2011-12-09 19:05:58 -05001640 can_open_delegated(delegation, data->o_arg.fmode))
1641 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001642 rcu_read_unlock();
1643 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001644 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04001645 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust8188df12013-04-23 14:31:19 -04001646 switch (data->o_arg.claim) {
1647 case NFS4_OPEN_CLAIM_PREVIOUS:
1648 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1649 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001650 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04001651 case NFS4_OPEN_CLAIM_FH:
1652 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001653 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1654 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001655 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001656 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001657 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001658 &data->o_res.seq_res,
1659 task) != 0)
1660 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04001661
1662 /* Set the create mode (note dependency on the session type) */
1663 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1664 if (data->o_arg.open_flags & O_EXCL) {
1665 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1666 if (nfs4_has_persistent_session(clp))
1667 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1668 else if (clp->cl_mvops->minor_version > 0)
1669 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1670 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04001671 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001672unlock_no_action:
1673 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001674out_no_action:
1675 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001676out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001677 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001678}
1679
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001680static void nfs4_open_done(struct rpc_task *task, void *calldata)
1681{
1682 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001684 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001685
Trond Myklebust14516c32010-07-31 14:29:06 -04001686 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1687 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001688
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001689 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001690 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1691 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001692 case S_IFREG:
1693 break;
1694 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001695 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001696 break;
1697 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001698 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001699 break;
1700 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001701 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001702 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001703 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001704 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001705 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1706 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001707 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001708 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001709}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001710
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001711static void nfs4_open_release(void *calldata)
1712{
1713 struct nfs4_opendata *data = calldata;
1714 struct nfs4_state *state = NULL;
1715
1716 /* If this request hasn't been cancelled, do nothing */
1717 if (data->cancelled == 0)
1718 goto out_free;
1719 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001720 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001721 goto out_free;
1722 /* In case we need an open_confirm, no cleanup! */
1723 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1724 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001725 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001726 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001727 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001728out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001729 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001730}
1731
1732static const struct rpc_call_ops nfs4_open_ops = {
1733 .rpc_call_prepare = nfs4_open_prepare,
1734 .rpc_call_done = nfs4_open_done,
1735 .rpc_release = nfs4_open_release,
1736};
1737
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001738static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001739{
1740 struct inode *dir = data->dir->d_inode;
1741 struct nfs_server *server = NFS_SERVER(dir);
1742 struct nfs_openargs *o_arg = &data->o_arg;
1743 struct nfs_openres *o_res = &data->o_res;
1744 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001745 struct rpc_message msg = {
1746 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1747 .rpc_argp = o_arg,
1748 .rpc_resp = o_res,
1749 .rpc_cred = data->owner->so_cred,
1750 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001751 struct rpc_task_setup task_setup_data = {
1752 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001753 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001754 .callback_ops = &nfs4_open_ops,
1755 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001756 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001757 .flags = RPC_TASK_ASYNC,
1758 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001759 int status;
1760
Chuck Levera9c92d62013-08-09 12:48:18 -04001761 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001762 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001763 data->rpc_done = 0;
1764 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001765 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001766 data->is_recover = 0;
1767 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001768 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001769 data->is_recover = 1;
1770 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04001771 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001772 if (IS_ERR(task))
1773 return PTR_ERR(task);
1774 status = nfs4_wait_for_completion_rpc_task(task);
1775 if (status != 0) {
1776 data->cancelled = 1;
1777 smp_wmb();
1778 } else
1779 status = data->rpc_status;
1780 rpc_put_task(task);
1781
1782 return status;
1783}
1784
1785static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1786{
1787 struct inode *dir = data->dir->d_inode;
1788 struct nfs_openres *o_res = &data->o_res;
1789 int status;
1790
1791 status = nfs4_run_open_task(data, 1);
1792 if (status != 0 || !data->rpc_done)
1793 return status;
1794
Trond Myklebust6926afd2012-01-07 13:22:46 -05001795 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1796
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001797 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1798 status = _nfs4_proc_open_confirm(data);
1799 if (status != 0)
1800 return status;
1801 }
1802
1803 return status;
1804}
1805
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001806static int nfs4_opendata_access(struct rpc_cred *cred,
1807 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001808 struct nfs4_state *state, fmode_t fmode,
1809 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001810{
1811 struct nfs_access_entry cache;
1812 u32 mask;
1813
1814 /* access call failed or for some reason the server doesn't
1815 * support any access modes -- defer access call until later */
1816 if (opendata->o_res.access_supported == 0)
1817 return 0;
1818
1819 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001820 /* don't check MAY_WRITE - a newly created file may not have
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001821 * write mode bits, but POSIX allows the creating process to write.
1822 * use openflags to check for exec, because fmode won't
1823 * always have FMODE_EXEC set when file open for exec. */
1824 if (openflags & __FMODE_EXEC) {
1825 /* ONLY check for exec rights */
1826 mask = MAY_EXEC;
1827 } else if (fmode & FMODE_READ)
1828 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001829
1830 cache.cred = cred;
1831 cache.jiffies = jiffies;
1832 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1833 nfs_access_add_cache(state->inode, &cache);
1834
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001835 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001836 return 0;
1837
1838 /* even though OPEN succeeded, access is denied. Close the file */
1839 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001840 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001841}
1842
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001843/*
1844 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1845 */
1846static int _nfs4_proc_open(struct nfs4_opendata *data)
1847{
1848 struct inode *dir = data->dir->d_inode;
1849 struct nfs_server *server = NFS_SERVER(dir);
1850 struct nfs_openargs *o_arg = &data->o_arg;
1851 struct nfs_openres *o_res = &data->o_res;
1852 int status;
1853
1854 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001855 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001856 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001857 if (status != 0) {
1858 if (status == -NFS4ERR_BADNAME &&
1859 !(o_arg->open_flags & O_CREAT))
1860 return -ENOENT;
1861 return status;
1862 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001863
Trond Myklebust6926afd2012-01-07 13:22:46 -05001864 nfs_fattr_map_and_free_names(server, &data->f_attr);
1865
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001866 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001867 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001868 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1869 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001871 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001873 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 }
1875 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
David Quigley1775fd32013-05-22 12:50:42 -04001876 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001877 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878}
1879
Andy Adamsond83217c2011-03-01 01:34:17 +00001880static int nfs4_recover_expired_lease(struct nfs_server *server)
1881{
1882 return nfs4_client_recover_expired_lease(server->nfs_client);
1883}
1884
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885/*
1886 * OPEN_EXPIRED:
1887 * reclaim state on the server after a network partition.
1888 * Assumes caller holds the appropriate lock
1889 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001890static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001892 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001893 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001895 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001896 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001897 if (IS_ERR(opendata))
1898 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001899 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001900 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001901 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001902 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001903 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904}
1905
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001906static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001907{
Trond Myklebust539cd032007-06-05 11:46:42 -04001908 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001909 struct nfs4_exception exception = { };
1910 int err;
1911
1912 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001913 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001914 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001915 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1916 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001917 switch (err) {
1918 default:
1919 goto out;
1920 case -NFS4ERR_GRACE:
1921 case -NFS4ERR_DELAY:
1922 nfs4_handle_exception(server, err, &exception);
1923 err = 0;
1924 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001925 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001926out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001927 return err;
1928}
1929
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1931{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001933 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
Trond Myklebust864472e2006-01-03 09:55:15 +01001935 ctx = nfs4_state_find_open_context(state);
1936 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001937 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001938 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001939 put_nfs_open_context(ctx);
1940 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941}
1942
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001943#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001944static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001945{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001946 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001947 nfs4_stateid *stateid = &state->stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001948 struct nfs_delegation *delegation;
1949 struct rpc_cred *cred = NULL;
1950 int status = -NFS4ERR_BAD_STATEID;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001951
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001952 /* If a state reset has been done, test_stateid is unneeded */
1953 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1954 return;
1955
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001956 /* Get the delegation credential for use by test/free_stateid */
1957 rcu_read_lock();
1958 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1959 if (delegation != NULL &&
1960 nfs4_stateid_match(&delegation->stateid, stateid)) {
1961 cred = get_rpccred(delegation->cred);
1962 rcu_read_unlock();
1963 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04001964 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001965 } else
1966 rcu_read_unlock();
1967
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001968 if (status != NFS_OK) {
1969 /* Free the stateid unless the server explicitly
1970 * informs us the stateid is unrecognized. */
1971 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001972 nfs41_free_stateid(server, stateid, cred);
Bryan Schumaker69388672012-09-26 15:25:52 -04001973 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001974
Bryan Schumaker69388672012-09-26 15:25:52 -04001975 write_seqlock(&state->seqlock);
1976 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1977 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001978 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1979 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001980
1981 if (cred != NULL)
1982 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001983}
1984
1985/**
1986 * nfs41_check_open_stateid - possibly free an open stateid
1987 *
1988 * @state: NFSv4 state for an inode
1989 *
1990 * Returns NFS_OK if recovery for this stateid is now finished.
1991 * Otherwise a negative NFS4ERR value is returned.
1992 */
1993static int nfs41_check_open_stateid(struct nfs4_state *state)
1994{
1995 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001996 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001997 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001998 int status;
1999
2000 /* If a state reset has been done, test_stateid is unneeded */
2001 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2002 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2003 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2004 return -NFS4ERR_BAD_STATEID;
2005
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002006 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002007 trace_nfs4_test_open_stateid(state, NULL, status);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002008 if (status != NFS_OK) {
2009 /* Free the stateid unless the server explicitly
2010 * informs us the stateid is unrecognized. */
2011 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002012 nfs41_free_stateid(server, stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002013
2014 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2015 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2016 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002017 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002018 }
2019 return status;
2020}
2021
2022static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2023{
Chuck Levereb64cf92012-07-11 16:30:05 -04002024 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002025
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002026 nfs41_clear_delegation_stateid(state);
2027 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002028 if (status != NFS_OK)
2029 status = nfs4_open_expired(sp, state);
2030 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002031}
2032#endif
2033
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002035 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2036 * fields corresponding to attributes that were used to store the verifier.
2037 * Make sure we clobber those fields in the later setattr call
2038 */
2039static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2040{
2041 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2042 !(sattr->ia_valid & ATTR_ATIME_SET))
2043 sattr->ia_valid |= ATTR_ATIME;
2044
2045 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2046 !(sattr->ia_valid & ATTR_MTIME_SET))
2047 sattr->ia_valid |= ATTR_MTIME;
2048}
2049
Trond Myklebustc21443c2013-02-07 14:26:21 -05002050static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2051 fmode_t fmode,
2052 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002053 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002054{
2055 struct nfs4_state_owner *sp = opendata->owner;
2056 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002057 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002058 struct nfs4_state *state;
2059 unsigned int seq;
2060 int ret;
2061
2062 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2063
2064 ret = _nfs4_proc_open(opendata);
2065 if (ret != 0)
2066 goto out;
2067
2068 state = nfs4_opendata_to_nfs4_state(opendata);
2069 ret = PTR_ERR(state);
2070 if (IS_ERR(state))
2071 goto out;
2072 if (server->caps & NFS_CAP_POSIX_LOCK)
2073 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2074
Trond Myklebust275bb302013-05-29 13:11:28 -04002075 dentry = opendata->dentry;
2076 if (dentry->d_inode == NULL) {
2077 /* FIXME: Is this d_drop() ever needed? */
2078 d_drop(dentry);
2079 dentry = d_add_unique(dentry, igrab(state->inode));
2080 if (dentry == NULL) {
2081 dentry = opendata->dentry;
2082 } else if (dentry != ctx->dentry) {
2083 dput(ctx->dentry);
2084 ctx->dentry = dget(dentry);
2085 }
2086 nfs_set_verifier(dentry,
2087 nfs_save_change_attribute(opendata->dir->d_inode));
2088 }
2089
Trond Myklebustc21443c2013-02-07 14:26:21 -05002090 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2091 if (ret != 0)
2092 goto out;
2093
Trond Myklebust3efb9722013-05-29 13:17:04 -04002094 ctx->state = state;
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002095 if (dentry->d_inode == state->inode) {
2096 nfs_inode_attach_open_context(ctx);
2097 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2098 nfs4_schedule_stateid_recovery(server, state);
2099 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002100out:
2101 return ret;
2102}
2103
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002104/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002105 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 */
Andy Adamson82be4172012-05-23 05:02:35 -04002107static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002108 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002109 int flags,
2110 struct iattr *sattr,
Trond Myklebust959d9212013-06-28 16:29:51 -04002111 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112{
2113 struct nfs4_state_owner *sp;
2114 struct nfs4_state *state = NULL;
2115 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002116 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002117 struct dentry *dentry = ctx->dentry;
2118 struct rpc_cred *cred = ctx->cred;
2119 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2120 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002121 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002122 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002123 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
2125 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002127 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2128 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2130 goto out_err;
2131 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002132 status = nfs4_recover_expired_lease(server);
2133 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002134 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04002135 if (dentry->d_inode != NULL)
2136 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002137 status = -ENOMEM;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002138 if (dentry->d_inode)
2139 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002140 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002141 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002142 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002143 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
David Quigley14c43f72013-05-22 12:50:43 -04002145 if (label) {
2146 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2147 if (IS_ERR(olabel)) {
2148 status = PTR_ERR(olabel);
2149 goto err_opendata_put;
2150 }
2151 }
2152
Andy Adamson82be4172012-05-23 05:02:35 -04002153 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2154 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2155 if (!opendata->f_attr.mdsthreshold)
David Quigley14c43f72013-05-22 12:50:43 -04002156 goto err_free_label;
Trond Myklebust1549210f2012-06-05 09:16:47 -04002157 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002158 }
Al Viro82a2c1b2011-06-22 18:30:55 -04002159 if (dentry->d_inode != NULL)
2160 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002161
Trond Myklebust3efb9722013-05-29 13:17:04 -04002162 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002163 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002164 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002165 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002166
Trond Myklebust549b19c2013-04-16 18:42:34 -04002167 if ((opendata->o_arg.open_flags & O_EXCL) &&
2168 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002169 nfs4_exclusive_attrset(opendata, sattr);
2170
2171 nfs_fattr_init(opendata->o_res.f_attr);
2172 status = nfs4_do_setattr(state->inode, cred,
2173 opendata->o_res.f_attr, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002174 state, label, olabel);
2175 if (status == 0) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002176 nfs_setattr_update_inode(state->inode, sattr);
David Quigley1775fd32013-05-22 12:50:42 -04002177 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
David Quigleyaa9c2662013-05-22 12:50:44 -04002178 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
David Quigley1775fd32013-05-22 12:50:42 -04002179 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002180 }
Andy Adamson82be4172012-05-23 05:02:35 -04002181
2182 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2183 *ctx_th = opendata->f_attr.mdsthreshold;
2184 else
2185 kfree(opendata->f_attr.mdsthreshold);
2186 opendata->f_attr.mdsthreshold = NULL;
2187
David Quigley14c43f72013-05-22 12:50:43 -04002188 nfs4_label_free(olabel);
2189
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002190 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002193err_free_label:
2194 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002195err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002196 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002197 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002198err_put_state_owner:
2199 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 return status;
2202}
2203
2204
Andy Adamson82be4172012-05-23 05:02:35 -04002205static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002206 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002207 int flags,
2208 struct iattr *sattr,
Trond Myklebust959d9212013-06-28 16:29:51 -04002209 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002211 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 struct nfs4_exception exception = { };
2213 struct nfs4_state *res;
2214 int status;
2215
2216 do {
Trond Myklebust959d9212013-06-28 16:29:51 -04002217 status = _nfs4_do_open(dir, ctx, flags, sattr, label);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002218 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002219 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 if (status == 0)
2221 break;
2222 /* NOTE: BAD_SEQID means the server and client disagree about the
2223 * book-keeping w.r.t. state-changing operations
2224 * (OPEN/CLOSE/LOCK/LOCKU...)
2225 * It is actually a sign of a bug on the client or on the server.
2226 *
2227 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002228 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 * have unhashed the old state_owner for us, and that we can
2230 * therefore safely retry using a new one. We should still warn
2231 * the user though...
2232 */
2233 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002234 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002235 " returned a bad sequence-id error!\n",
2236 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 exception.retry = 1;
2238 continue;
2239 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002240 /*
2241 * BAD_STATEID on OPEN means that the server cancelled our
2242 * state before it received the OPEN_CONFIRM.
2243 * Recover by retrying the request as per the discussion
2244 * on Page 181 of RFC3530.
2245 */
2246 if (status == -NFS4ERR_BAD_STATEID) {
2247 exception.retry = 1;
2248 continue;
2249 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002250 if (status == -EAGAIN) {
2251 /* We must have found a delegation */
2252 exception.retry = 1;
2253 continue;
2254 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002255 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2256 continue;
2257 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 status, &exception));
2259 } while (exception.retry);
2260 return res;
2261}
2262
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002263static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2264 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002265 struct nfs4_state *state, struct nfs4_label *ilabel,
2266 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002268 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002270 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 .iap = sattr,
2272 .server = server,
2273 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002274 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 };
2276 struct nfs_setattrres res = {
2277 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002278 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 .server = server,
2280 };
2281 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002282 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2283 .rpc_argp = &arg,
2284 .rpc_resp = &res,
2285 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002287 unsigned long timestamp = jiffies;
Trond Myklebustee3ae842013-04-29 10:35:36 -04002288 fmode_t fmode;
2289 bool truncate;
Trond Myklebust65e43082005-08-16 11:49:44 -04002290 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
David Quigleyaa9c2662013-05-22 12:50:44 -04002292 arg.bitmask = nfs4_bitmask(server, ilabel);
2293 if (ilabel)
2294 arg.bitmask = nfs4_bitmask(server, olabel);
2295
Trond Myklebust0e574af2005-10-27 22:12:38 -04002296 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
Trond Myklebustee3ae842013-04-29 10:35:36 -04002298 /* Servers should only apply open mode checks for file size changes */
2299 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2300 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2301
2302 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2303 /* Use that stateid */
2304 } else if (truncate && state != NULL && nfs4_valid_open_stateid(state)) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002305 struct nfs_lockowner lockowner = {
2306 .l_owner = current->files,
2307 .l_pid = current->tgid,
2308 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002309 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002310 &lockowner);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002311 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002312 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
Bryan Schumaker7c513052011-03-24 17:12:24 +00002314 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002315 if (status == 0 && state != NULL)
2316 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002317 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318}
2319
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002320static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2321 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002322 struct nfs4_state *state, struct nfs4_label *ilabel,
2323 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002325 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002326 struct nfs4_exception exception = {
2327 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002328 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002329 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 int err;
2331 do {
David Quigley1775fd32013-05-22 12:50:42 -04002332 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
Trond Myklebustc1578b72013-08-12 16:58:42 -04002333 trace_nfs4_setattr(inode, err);
Trond Myklebust451146b2012-04-18 16:29:11 -04002334 switch (err) {
2335 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002336 if (!(sattr->ia_valid & ATTR_SIZE)) {
2337 pr_warn_once("NFSv4: server %s is incorrectly "
2338 "applying open mode checks to "
2339 "a SETATTR that is not "
2340 "changing file size.\n",
2341 server->nfs_client->cl_hostname);
2342 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002343 if (state && !(state->state & FMODE_WRITE)) {
2344 err = -EBADF;
2345 if (sattr->ia_valid & ATTR_OPEN)
2346 err = -EACCES;
2347 goto out;
2348 }
2349 }
2350 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002352out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 return err;
2354}
2355
2356struct nfs4_closedata {
2357 struct inode *inode;
2358 struct nfs4_state *state;
2359 struct nfs_closeargs arg;
2360 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002361 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002362 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002363 bool roc;
2364 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365};
2366
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002367static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002368{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002369 struct nfs4_closedata *calldata = data;
2370 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002371 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002372
Fred Isamanf7e89172011-01-06 11:36:32 +00002373 if (calldata->roc)
2374 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002375 nfs4_put_open_state(calldata->state);
2376 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002377 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002378 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002379 kfree(calldata);
2380}
2381
Trond Myklebust88069f72009-12-08 08:33:16 -05002382static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2383 fmode_t fmode)
2384{
2385 spin_lock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002386 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002387 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
2388 case FMODE_WRITE:
2389 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2390 break;
2391 case FMODE_READ:
2392 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2393 break;
2394 case 0:
2395 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2396 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2397 clear_bit(NFS_OPEN_STATE, &state->flags);
2398 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002399 spin_unlock(&state->owner->so_lock);
2400}
2401
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002402static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002404 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 struct nfs_server *server = NFS_SERVER(calldata->inode);
2407
Chuck Levera3ca5652012-03-01 17:00:40 -05002408 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002409 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2410 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002411 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 /* hmm. we are done with the inode, and in the process of freeing
2413 * the state_owner. we keep this around to process errors
2414 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 switch (task->tk_status) {
2416 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002417 if (calldata->roc)
2418 pnfs_roc_set_barrier(state->inode,
2419 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002420 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002421 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002422 nfs4_close_clear_stateid_flags(state,
2423 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 break;
2425 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002426 case -NFS4ERR_OLD_STATEID:
2427 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002429 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002430 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002432 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2433 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002435 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002436 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002437 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438}
2439
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002440static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002442 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002443 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002444 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002445 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002446
Chuck Levera3ca5652012-03-01 17:00:40 -05002447 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002448 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002449 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002450
Trond Myklebust88069f72009-12-08 08:33:16 -05002451 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2452 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002453 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002454 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002455 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002456 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002457 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2458 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2459 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002460 }
2461 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002462 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2463 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2464 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002465 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002466 }
Trond Myklebust5d422302013-03-14 16:57:48 -04002467 if (!nfs4_valid_open_stateid(state))
2468 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002469 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002470
2471 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002472 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002473 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002474 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002475
Fred Isamanf7e89172011-01-06 11:36:32 +00002476 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002477 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002478 if (calldata->roc &&
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002479 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2480 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002481 goto out_wait;
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002482 }
Fred Isamanf7e89172011-01-06 11:36:32 +00002483 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002484
Trond Myklebust516a6af2005-10-27 22:12:41 -04002485 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002486 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002487 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002488 &calldata->arg.seq_args,
2489 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002490 task) != 0)
2491 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002492 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002493 return;
2494out_no_action:
2495 task->tk_action = NULL;
2496out_wait:
2497 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498}
2499
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002500static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002501 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002502 .rpc_call_done = nfs4_close_done,
2503 .rpc_release = nfs4_free_closedata,
2504};
2505
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506/*
2507 * It is possible for data to be read/written from a mem-mapped file
2508 * after the sys_close call (which hits the vfs layer as a flush).
2509 * This means that we can't safely call nfsv4 close on a file until
2510 * the inode is cleared. This in turn means that we are not good
2511 * NFSv4 citizens - we do not indicate to the server to update the file's
2512 * share state even when we are done with one of the three share
2513 * stateid's in the inode.
2514 *
2515 * NOTE: Caller must be holding the sp->so_owner semaphore!
2516 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002517int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002519 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002521 struct nfs4_state_owner *sp = state->owner;
2522 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002523 struct rpc_message msg = {
2524 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2525 .rpc_cred = state->owner->so_cred,
2526 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002527 struct rpc_task_setup task_setup_data = {
2528 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002529 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002530 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002531 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002532 .flags = RPC_TASK_ASYNC,
2533 };
Trond Myklebust95121352005-10-18 14:20:12 -07002534 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002536 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002538 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04002539 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002540 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002542 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002543 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002545 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002546 if (calldata->arg.seqid == NULL)
2547 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002548 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002549 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002550 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002551 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002552 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002553 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002554 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002555
Trond Myklebust1174dd12010-12-21 10:52:24 -05002556 msg.rpc_argp = &calldata->arg;
2557 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002558 task_setup_data.callback_data = calldata;
2559 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002560 if (IS_ERR(task))
2561 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002562 status = 0;
2563 if (wait)
2564 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002565 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002566 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002567out_free_calldata:
2568 kfree(calldata);
2569out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002570 nfs4_put_open_state(state);
2571 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002572 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573}
2574
Trond Myklebust2b484292010-09-17 10:56:51 -04002575static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002576nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04002579 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2580
2581 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582
Trond Myklebust565277f2007-10-15 18:17:53 -04002583 /* Protect against concurrent sillydeletes */
Trond Myklebust959d9212013-06-28 16:29:51 -04002584 state = nfs4_do_open(dir, ctx, open_flags, attr, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04002585
2586 nfs4_label_release_security(label);
2587
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002588 if (IS_ERR(state))
2589 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04002590 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591}
2592
Trond Myklebust1185a552009-12-03 15:54:02 -05002593static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002594{
2595 if (ctx->state == NULL)
2596 return;
2597 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002598 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002599 else
Al Viro643168c2011-06-22 18:20:23 -04002600 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002601}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602
2603static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2604{
Benny Halevy43652ad2009-04-01 09:21:54 -04002605 struct nfs4_server_caps_arg args = {
2606 .fhandle = fhandle,
2607 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 struct nfs4_server_caps_res res = {};
2609 struct rpc_message msg = {
2610 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002611 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 .rpc_resp = &res,
2613 };
2614 int status;
2615
Bryan Schumaker7c513052011-03-24 17:12:24 +00002616 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 if (status == 0) {
2618 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002619 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2620 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2621 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2622 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2623 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2625 server->caps |= NFS_CAP_ACLS;
2626 if (res.has_links != 0)
2627 server->caps |= NFS_CAP_HARDLINKS;
2628 if (res.has_symlinks != 0)
2629 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002630 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2631 server->caps |= NFS_CAP_FILEID;
2632 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2633 server->caps |= NFS_CAP_MODE;
2634 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2635 server->caps |= NFS_CAP_NLINK;
2636 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2637 server->caps |= NFS_CAP_OWNER;
2638 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2639 server->caps |= NFS_CAP_OWNER_GROUP;
2640 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2641 server->caps |= NFS_CAP_ATIME;
2642 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2643 server->caps |= NFS_CAP_CTIME;
2644 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2645 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04002646#ifdef CONFIG_NFS_V4_SECURITY_LABEL
2647 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
2648 server->caps |= NFS_CAP_SECURITY_LABEL;
2649#endif
2650 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
2651 sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002652
David Quigleyaa9c2662013-05-22 12:50:44 -04002653 if (server->caps & NFS_CAP_SECURITY_LABEL) {
2654 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2655 res.attr_bitmask[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2656 }
Trond Myklebusta65318b2009-03-11 14:10:28 -04002657 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2658 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2659 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002661 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002663
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 return status;
2665}
2666
Trond Myklebust55a97592006-06-09 09:34:19 -04002667int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668{
2669 struct nfs4_exception exception = { };
2670 int err;
2671 do {
2672 err = nfs4_handle_exception(server,
2673 _nfs4_server_capabilities(server, fhandle),
2674 &exception);
2675 } while (exception.retry);
2676 return err;
2677}
2678
2679static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2680 struct nfs_fsinfo *info)
2681{
David Quigleyaa9c2662013-05-22 12:50:44 -04002682 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04002684 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 };
2686 struct nfs4_lookup_res res = {
2687 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002688 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 .fh = fhandle,
2690 };
2691 struct rpc_message msg = {
2692 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2693 .rpc_argp = &args,
2694 .rpc_resp = &res,
2695 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002696
David Quigleyaa9c2662013-05-22 12:50:44 -04002697 bitmask[0] = nfs4_fattr_bitmap[0];
2698 bitmask[1] = nfs4_fattr_bitmap[1];
2699 /*
2700 * Process the label in the upcoming getfattr
2701 */
2702 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
2703
Trond Myklebust0e574af2005-10-27 22:12:38 -04002704 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002705 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706}
2707
2708static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2709 struct nfs_fsinfo *info)
2710{
2711 struct nfs4_exception exception = { };
2712 int err;
2713 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002714 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04002715 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002716 switch (err) {
2717 case 0:
2718 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002719 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002720 default:
2721 err = nfs4_handle_exception(server, err, &exception);
2722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002724out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 return err;
2726}
2727
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002728static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2729 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2730{
Trond Myklebustc2190662013-08-26 19:23:04 -04002731 struct rpc_auth_create_args auth_args = {
2732 .pseudoflavor = flavor,
2733 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002734 struct rpc_auth *auth;
2735 int ret;
2736
Trond Myklebustc2190662013-08-26 19:23:04 -04002737 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002738 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04002739 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002740 goto out;
2741 }
2742 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002743out:
2744 return ret;
2745}
2746
Chuck Lever9a744ba2013-03-16 15:56:02 -04002747/*
2748 * Retry pseudoroot lookup with various security flavors. We do this when:
2749 *
2750 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2751 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2752 *
2753 * Returns zero on success, or a negative NFS4ERR value, or a
2754 * negative errno value.
2755 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002756static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002757 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758{
Chuck Lever9a744ba2013-03-16 15:56:02 -04002759 /* Per 3530bis 15.33.5 */
2760 static const rpc_authflavor_t flav_array[] = {
2761 RPC_AUTH_GSS_KRB5P,
2762 RPC_AUTH_GSS_KRB5I,
2763 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04002764 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04002765 RPC_AUTH_NULL,
2766 };
2767 int status = -EPERM;
2768 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769
Chuck Lever9a744ba2013-03-16 15:56:02 -04002770 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002771 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002772 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002773 continue;
2774 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002775 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04002776
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002777 /*
2778 * -EACCESS could mean that the user doesn't have correct permissions
2779 * to access the mount. It could also mean that we tried to mount
2780 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2781 * existing mount programs don't handle -EACCES very well so it should
2782 * be mapped to -EPERM instead.
2783 */
2784 if (status == -EACCES)
2785 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002786 return status;
2787}
2788
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002789static int nfs4_do_find_root_sec(struct nfs_server *server,
2790 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
2791{
2792 int mv = server->nfs_client->cl_minorversion;
2793 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
2794}
2795
2796/**
2797 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
2798 * @server: initialized nfs_server handle
2799 * @fhandle: we fill in the pseudo-fs root file handle
2800 * @info: we fill in an FSINFO struct
2801 *
2802 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002803 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002804int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2805 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002806{
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002807 int status;
2808
2809 status = nfs4_lookup_root(server, fhandle, info);
2810 if ((status == -NFS4ERR_WRONGSEC) &&
2811 !(server->flags & NFS_MOUNT_SECFLAVOUR))
2812 status = nfs4_do_find_root_sec(server, fhandle, info);
2813
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 if (status == 0)
2815 status = nfs4_server_capabilities(server, fhandle);
2816 if (status == 0)
2817 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002818
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002819 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820}
2821
Bryan Schumakerbae36242012-05-10 15:07:31 -04002822static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2823 struct nfs_fsinfo *info)
2824{
2825 int error;
2826 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04002827 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04002828
2829 error = nfs4_server_capabilities(server, mntfh);
2830 if (error < 0) {
2831 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2832 return error;
2833 }
2834
David Quigley14c43f72013-05-22 12:50:43 -04002835 label = nfs4_label_alloc(server, GFP_KERNEL);
2836 if (IS_ERR(label))
2837 return PTR_ERR(label);
2838
David Quigley1775fd32013-05-22 12:50:42 -04002839 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04002840 if (error < 0) {
2841 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04002842 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04002843 }
2844
2845 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2846 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2847 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2848
David Quigley14c43f72013-05-22 12:50:43 -04002849err_free_label:
2850 nfs4_label_free(label);
2851
Bryan Schumakerbae36242012-05-10 15:07:31 -04002852 return error;
2853}
2854
Manoj Naik6b97fd32006-06-09 09:34:29 -04002855/*
2856 * Get locations and (maybe) other attributes of a referral.
2857 * Note that we'll actually follow the referral later when
2858 * we detect fsid mismatch in inode revalidation
2859 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002860static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2861 const struct qstr *name, struct nfs_fattr *fattr,
2862 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002863{
2864 int status = -ENOMEM;
2865 struct page *page = NULL;
2866 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002867
2868 page = alloc_page(GFP_KERNEL);
2869 if (page == NULL)
2870 goto out;
2871 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2872 if (locations == NULL)
2873 goto out;
2874
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002875 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002876 if (status != 0)
2877 goto out;
2878 /* Make sure server returned a different fsid for the referral */
2879 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002880 dprintk("%s: server did not return a different fsid for"
2881 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002882 status = -EIO;
2883 goto out;
2884 }
Andy Adamson533eb462011-06-13 18:25:56 -04002885 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2886 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002887
Andy Adamson533eb462011-06-13 18:25:56 -04002888 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002889 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002890 memset(fhandle, 0, sizeof(struct nfs_fh));
2891out:
2892 if (page)
2893 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002894 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002895 return status;
2896}
2897
David Quigley1775fd32013-05-22 12:50:42 -04002898static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
2899 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900{
2901 struct nfs4_getattr_arg args = {
2902 .fh = fhandle,
2903 .bitmask = server->attr_bitmask,
2904 };
2905 struct nfs4_getattr_res res = {
2906 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002907 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 .server = server,
2909 };
2910 struct rpc_message msg = {
2911 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2912 .rpc_argp = &args,
2913 .rpc_resp = &res,
2914 };
David Quigleyaa9c2662013-05-22 12:50:44 -04002915
2916 args.bitmask = nfs4_bitmask(server, label);
2917
Trond Myklebust0e574af2005-10-27 22:12:38 -04002918 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002919 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920}
2921
David Quigley1775fd32013-05-22 12:50:42 -04002922static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
2923 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924{
2925 struct nfs4_exception exception = { };
2926 int err;
2927 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04002928 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
2929 trace_nfs4_getattr(server, fhandle, fattr, err);
2930 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 &exception);
2932 } while (exception.retry);
2933 return err;
2934}
2935
2936/*
2937 * The file is not closed if it is opened due to the a request to change
2938 * the size of the file. The open call will not be needed once the
2939 * VFS layer lookup-intents are implemented.
2940 *
2941 * Close is called when the inode is destroyed.
2942 * If we haven't opened the file for O_WRONLY, we
2943 * need to in the size_change case to obtain a stateid.
2944 *
2945 * Got race?
2946 * Because OPEN is always done by name in nfsv4, it is
2947 * possible that we opened a different file by the same
2948 * name. We can recognize this race condition, but we
2949 * can't do anything about it besides returning an error.
2950 *
2951 * This will be fixed with VFS changes (lookup-intent).
2952 */
2953static int
2954nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2955 struct iattr *sattr)
2956{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002957 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002958 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002959 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04002960 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 int status;
2962
Benny Halevy8a1636c2010-07-14 15:43:57 -04002963 if (pnfs_ld_layoutret_on_setattr(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04002964 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04002965
Trond Myklebust0e574af2005-10-27 22:12:38 -04002966 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967
Andy Adamson26699402012-05-30 16:12:24 -04002968 /* Deal with open(O_TRUNC) */
2969 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03002970 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04002971
2972 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03002973 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04002974 return 0;
2975
Trond Myklebustd5308382005-11-04 15:33:38 -05002976 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002977 if (sattr->ia_valid & ATTR_FILE) {
2978 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002979
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002980 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002981 if (ctx) {
2982 cred = ctx->cred;
2983 state = ctx->state;
2984 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002985 }
2986
David Quigley14c43f72013-05-22 12:50:43 -04002987 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
2988 if (IS_ERR(label))
2989 return PTR_ERR(label);
2990
2991 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04002992 if (status == 0) {
Trond Myklebust65e43082005-08-16 11:49:44 -04002993 nfs_setattr_update_inode(inode, sattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04002994 nfs_setsecurity(inode, fattr, label);
2995 }
David Quigley14c43f72013-05-22 12:50:43 -04002996 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 return status;
2998}
2999
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003000static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3001 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003002 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003003{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003004 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003005 int status;
3006 struct nfs4_lookup_arg args = {
3007 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003008 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003009 .name = name,
3010 };
3011 struct nfs4_lookup_res res = {
3012 .server = server,
3013 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003014 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003015 .fh = fhandle,
3016 };
3017 struct rpc_message msg = {
3018 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3019 .rpc_argp = &args,
3020 .rpc_resp = &res,
3021 };
3022
David Quigleyaa9c2662013-05-22 12:50:44 -04003023 args.bitmask = nfs4_bitmask(server, label);
3024
David Howells2b3de442006-08-22 20:06:09 -04003025 nfs_fattr_init(fattr);
3026
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003028 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 dprintk("NFS reply lookup: %d\n", status);
3030 return status;
3031}
3032
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003033static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003034{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003035 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003036 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003037 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3038 fattr->nlink = 2;
3039}
3040
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003041static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3042 struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003043 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003044{
3045 struct nfs4_exception exception = { };
3046 struct rpc_clnt *client = *clnt;
3047 int err;
3048 do {
David Quigley1775fd32013-05-22 12:50:42 -04003049 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003050 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003051 switch (err) {
3052 case -NFS4ERR_BADNAME:
3053 err = -ENOENT;
3054 goto out;
3055 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003056 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003057 goto out;
3058 case -NFS4ERR_WRONGSEC:
3059 err = -EPERM;
3060 if (client != *clnt)
3061 goto out;
3062
3063 client = nfs4_create_sec_client(client, dir, name);
3064 if (IS_ERR(client))
3065 return PTR_ERR(client);
3066
3067 exception.retry = 1;
3068 break;
3069 default:
3070 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3071 }
3072 } while (exception.retry);
3073
3074out:
3075 if (err == 0)
3076 *clnt = client;
3077 else if (client != *clnt)
3078 rpc_shutdown_client(client);
3079
3080 return err;
3081}
3082
Bryan Schumaker80a16b22012-04-27 13:27:46 -04003083static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003084 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3085 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003087 int status;
3088 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003089
David Quigley1775fd32013-05-22 12:50:42 -04003090 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003091 if (client != NFS_CLIENT(dir)) {
3092 rpc_shutdown_client(client);
3093 nfs_fixup_secinfo_attributes(fattr);
3094 }
3095 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096}
3097
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003098struct rpc_clnt *
3099nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3100 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3101{
3102 int status;
3103 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
3104
David Quigley1775fd32013-05-22 12:50:42 -04003105 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003106 if (status < 0) {
3107 rpc_shutdown_client(client);
3108 return ERR_PTR(status);
3109 }
3110 return client;
3111}
3112
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3114{
Trond Myklebust76b32992007-08-10 17:45:11 -04003115 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 struct nfs4_accessargs args = {
3117 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003118 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003120 struct nfs4_accessres res = {
3121 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003122 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 struct rpc_message msg = {
3124 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3125 .rpc_argp = &args,
3126 .rpc_resp = &res,
3127 .rpc_cred = entry->cred,
3128 };
3129 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003130 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131
3132 /*
3133 * Determine which access bits we want to ask for...
3134 */
3135 if (mode & MAY_READ)
3136 args.access |= NFS4_ACCESS_READ;
3137 if (S_ISDIR(inode->i_mode)) {
3138 if (mode & MAY_WRITE)
3139 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3140 if (mode & MAY_EXEC)
3141 args.access |= NFS4_ACCESS_LOOKUP;
3142 } else {
3143 if (mode & MAY_WRITE)
3144 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3145 if (mode & MAY_EXEC)
3146 args.access |= NFS4_ACCESS_EXECUTE;
3147 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003148
3149 res.fattr = nfs_alloc_fattr();
3150 if (res.fattr == NULL)
3151 return -ENOMEM;
3152
Bryan Schumaker7c513052011-03-24 17:12:24 +00003153 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003155 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003156 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003158 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 return status;
3160}
3161
3162static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3163{
3164 struct nfs4_exception exception = { };
3165 int err;
3166 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003167 err = _nfs4_proc_access(inode, entry);
3168 trace_nfs4_access(inode, err);
3169 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 &exception);
3171 } while (exception.retry);
3172 return err;
3173}
3174
3175/*
3176 * TODO: For the time being, we don't try to get any attributes
3177 * along with any of the zero-copy operations READ, READDIR,
3178 * READLINK, WRITE.
3179 *
3180 * In the case of the first three, we want to put the GETATTR
3181 * after the read-type operation -- this is because it is hard
3182 * to predict the length of a GETATTR response in v4, and thus
3183 * align the READ data correctly. This means that the GETATTR
3184 * may end up partially falling into the page cache, and we should
3185 * shift it into the 'tail' of the xdr_buf before processing.
3186 * To do this efficiently, we need to know the total length
3187 * of data received, which doesn't seem to be available outside
3188 * of the RPC layer.
3189 *
3190 * In the case of WRITE, we also want to put the GETATTR after
3191 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003192 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 *
3194 * Both of these changes to the XDR layer would in fact be quite
3195 * minor, but I decided to leave them for a subsequent patch.
3196 */
3197static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3198 unsigned int pgbase, unsigned int pglen)
3199{
3200 struct nfs4_readlink args = {
3201 .fh = NFS_FH(inode),
3202 .pgbase = pgbase,
3203 .pglen = pglen,
3204 .pages = &page,
3205 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003206 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 struct rpc_message msg = {
3208 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3209 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003210 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 };
3212
Bryan Schumaker7c513052011-03-24 17:12:24 +00003213 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 -07003214}
3215
3216static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3217 unsigned int pgbase, unsigned int pglen)
3218{
3219 struct nfs4_exception exception = { };
3220 int err;
3221 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003222 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3223 trace_nfs4_readlink(inode, err);
3224 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 &exception);
3226 } while (exception.retry);
3227 return err;
3228}
3229
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003231 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233static int
3234nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003235 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236{
David Quigleyaa9c2662013-05-22 12:50:44 -04003237 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003238 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 int status = 0;
3241
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003242 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3243 if (IS_ERR(ctx))
3244 return PTR_ERR(ctx);
3245
David Quigleyaa9c2662013-05-22 12:50:44 -04003246 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3247
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003248 sattr->ia_mode &= ~current_umask();
Trond Myklebust959d9212013-06-28 16:29:51 -04003249 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 if (IS_ERR(state)) {
3251 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003252 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003255 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003256 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 return status;
3258}
3259
3260static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3261{
Trond Myklebust16e42952005-10-27 22:12:44 -04003262 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003263 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003265 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003267 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003268 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003269 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003271 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3272 .rpc_argp = &args,
3273 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003275 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003276
Bryan Schumaker7c513052011-03-24 17:12:24 +00003277 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003278 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003279 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 return status;
3281}
3282
3283static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3284{
3285 struct nfs4_exception exception = { };
3286 int err;
3287 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003288 err = _nfs4_proc_remove(dir, name);
3289 trace_nfs4_remove(dir, name, err);
3290 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 &exception);
3292 } while (exception.retry);
3293 return err;
3294}
3295
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003296static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003298 struct nfs_server *server = NFS_SERVER(dir);
3299 struct nfs_removeargs *args = msg->rpc_argp;
3300 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003302 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04003304 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003305
3306 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307}
3308
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003309static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3310{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003311 nfs4_setup_sequence(NFS_SERVER(data->dir),
3312 &data->args.seq_args,
3313 &data->res.seq_res,
3314 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315}
3316
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003317static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003319 struct nfs_unlinkdata *data = task->tk_calldata;
3320 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003321
Trond Myklebust14516c32010-07-31 14:29:06 -04003322 if (!nfs4_sequence_done(task, &res->seq_res))
3323 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003324 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003325 return 0;
3326 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003327 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328}
3329
Jeff Laytond3d41522010-09-17 17:31:57 -04003330static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3331{
3332 struct nfs_server *server = NFS_SERVER(dir);
3333 struct nfs_renameargs *arg = msg->rpc_argp;
3334 struct nfs_renameres *res = msg->rpc_resp;
3335
3336 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003337 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04003338 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003339}
3340
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003341static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3342{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003343 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3344 &data->args.seq_args,
3345 &data->res.seq_res,
3346 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003347}
3348
3349static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3350 struct inode *new_dir)
3351{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003352 struct nfs_renamedata *data = task->tk_calldata;
3353 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04003354
3355 if (!nfs4_sequence_done(task, &res->seq_res))
3356 return 0;
3357 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3358 return 0;
3359
3360 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003361 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003362 return 1;
3363}
3364
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3366 struct inode *new_dir, struct qstr *new_name)
3367{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003368 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003369 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 .old_dir = NFS_FH(old_dir),
3371 .new_dir = NFS_FH(new_dir),
3372 .old_name = old_name,
3373 .new_name = new_name,
3374 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003375 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003376 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003377 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 struct rpc_message msg = {
3379 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3380 .rpc_argp = &arg,
3381 .rpc_resp = &res,
3382 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003383 int status = -ENOMEM;
David Quigley14c43f72013-05-22 12:50:43 -04003384
Bryan Schumaker7c513052011-03-24 17:12:24 +00003385 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 if (!status) {
3387 update_changeattr(old_dir, &res.old_cinfo);
3388 update_changeattr(new_dir, &res.new_cinfo);
3389 }
3390 return status;
3391}
3392
3393static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3394 struct inode *new_dir, struct qstr *new_name)
3395{
3396 struct nfs4_exception exception = { };
3397 int err;
3398 do {
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003399 err = _nfs4_proc_rename(old_dir, old_name,
3400 new_dir, new_name);
3401 trace_nfs4_rename(old_dir, old_name, new_dir, new_name, err);
3402 err = nfs4_handle_exception(NFS_SERVER(old_dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 &exception);
3404 } while (exception.retry);
3405 return err;
3406}
3407
3408static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3409{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003410 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 struct nfs4_link_arg arg = {
3412 .fh = NFS_FH(inode),
3413 .dir_fh = NFS_FH(dir),
3414 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003415 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003417 struct nfs4_link_res res = {
3418 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003419 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003420 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 struct rpc_message msg = {
3422 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3423 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003424 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003426 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427
Trond Myklebust136f2622010-04-16 16:22:49 -04003428 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003429 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003430 goto out;
3431
David Quigley14c43f72013-05-22 12:50:43 -04003432 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3433 if (IS_ERR(res.label)) {
3434 status = PTR_ERR(res.label);
3435 goto out;
3436 }
David Quigleyaa9c2662013-05-22 12:50:44 -04003437 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04003438
Bryan Schumaker7c513052011-03-24 17:12:24 +00003439 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003440 if (!status) {
3441 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04003442 status = nfs_post_op_update_inode(inode, res.fattr);
3443 if (!status)
3444 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003445 }
David Quigley14c43f72013-05-22 12:50:43 -04003446
3447
3448 nfs4_label_free(res.label);
3449
Trond Myklebust136f2622010-04-16 16:22:49 -04003450out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003451 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 return status;
3453}
3454
3455static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3456{
3457 struct nfs4_exception exception = { };
3458 int err;
3459 do {
3460 err = nfs4_handle_exception(NFS_SERVER(inode),
3461 _nfs4_proc_link(inode, dir, name),
3462 &exception);
3463 } while (exception.retry);
3464 return err;
3465}
3466
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003467struct nfs4_createdata {
3468 struct rpc_message msg;
3469 struct nfs4_create_arg arg;
3470 struct nfs4_create_res res;
3471 struct nfs_fh fh;
3472 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003473 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003474};
3475
3476static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3477 struct qstr *name, struct iattr *sattr, u32 ftype)
3478{
3479 struct nfs4_createdata *data;
3480
3481 data = kzalloc(sizeof(*data), GFP_KERNEL);
3482 if (data != NULL) {
3483 struct nfs_server *server = NFS_SERVER(dir);
3484
David Quigley14c43f72013-05-22 12:50:43 -04003485 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3486 if (IS_ERR(data->label))
3487 goto out_free;
3488
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003489 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3490 data->msg.rpc_argp = &data->arg;
3491 data->msg.rpc_resp = &data->res;
3492 data->arg.dir_fh = NFS_FH(dir);
3493 data->arg.server = server;
3494 data->arg.name = name;
3495 data->arg.attrs = sattr;
3496 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04003497 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003498 data->res.server = server;
3499 data->res.fh = &data->fh;
3500 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003501 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003502 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003503 }
3504 return data;
David Quigley14c43f72013-05-22 12:50:43 -04003505out_free:
3506 kfree(data);
3507 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003508}
3509
3510static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3511{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003512 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003513 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003514 if (status == 0) {
3515 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04003516 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003517 }
3518 return status;
3519}
3520
3521static void nfs4_free_createdata(struct nfs4_createdata *data)
3522{
David Quigley14c43f72013-05-22 12:50:43 -04003523 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003524 kfree(data);
3525}
3526
Chuck Lever4f390c12006-08-22 20:06:22 -04003527static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003528 struct page *page, unsigned int len, struct iattr *sattr,
3529 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003531 struct nfs4_createdata *data;
3532 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533
Chuck Lever94a6d752006-08-22 20:06:23 -04003534 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003535 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003536
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003537 status = -ENOMEM;
3538 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3539 if (data == NULL)
3540 goto out;
3541
3542 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3543 data->arg.u.symlink.pages = &page;
3544 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04003545 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003547 status = nfs4_do_create(dir, dentry, data);
3548
3549 nfs4_free_createdata(data);
3550out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 return status;
3552}
3553
Chuck Lever4f390c12006-08-22 20:06:22 -04003554static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003555 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556{
3557 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003558 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04003560
3561 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3562
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003564 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3565 trace_nfs4_symlink(dir, &dentry->d_name, err);
3566 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 &exception);
3568 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003569
3570 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 return err;
3572}
3573
3574static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003575 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003577 struct nfs4_createdata *data;
3578 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003580 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3581 if (data == NULL)
3582 goto out;
3583
David Quigley1775fd32013-05-22 12:50:42 -04003584 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003585 status = nfs4_do_create(dir, dentry, data);
3586
3587 nfs4_free_createdata(data);
3588out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 return status;
3590}
3591
3592static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3593 struct iattr *sattr)
3594{
3595 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003596 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003598
David Quigleyaa9c2662013-05-22 12:50:44 -04003599 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3600
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003601 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003603 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3604 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3605 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 &exception);
3607 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003608 nfs4_label_release_security(label);
3609
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 return err;
3611}
3612
3613static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003614 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615{
3616 struct inode *dir = dentry->d_inode;
3617 struct nfs4_readdir_arg args = {
3618 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003619 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 .pgbase = 0,
3621 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003622 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003623 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 };
3625 struct nfs4_readdir_res res;
3626 struct rpc_message msg = {
3627 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3628 .rpc_argp = &args,
3629 .rpc_resp = &res,
3630 .rpc_cred = cred,
3631 };
3632 int status;
3633
Harvey Harrison3110ff82008-05-02 13:42:44 -07003634 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003635 dentry->d_parent->d_name.name,
3636 dentry->d_name.name,
3637 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003638 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003640 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 -05003641 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003642 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003643 status += args.pgbase;
3644 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003645
3646 nfs_invalidate_atime(dir);
3647
Harvey Harrison3110ff82008-05-02 13:42:44 -07003648 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 return status;
3650}
3651
3652static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003653 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654{
3655 struct nfs4_exception exception = { };
3656 int err;
3657 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003658 err = _nfs4_proc_readdir(dentry, cred, cookie,
3659 pages, count, plus);
3660 trace_nfs4_readdir(dentry->d_inode, err);
3661 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 &exception);
3663 } while (exception.retry);
3664 return err;
3665}
3666
3667static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04003668 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003670 struct nfs4_createdata *data;
3671 int mode = sattr->ia_mode;
3672 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003674 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3675 if (data == NULL)
3676 goto out;
3677
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003679 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003681 data->arg.ftype = NF4BLK;
3682 data->arg.u.device.specdata1 = MAJOR(rdev);
3683 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 }
3685 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003686 data->arg.ftype = NF4CHR;
3687 data->arg.u.device.specdata1 = MAJOR(rdev);
3688 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003689 } else if (!S_ISSOCK(mode)) {
3690 status = -EINVAL;
3691 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 }
David Quigley1775fd32013-05-22 12:50:42 -04003693
David Quigleyaa9c2662013-05-22 12:50:44 -04003694 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003695 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003696out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003697 nfs4_free_createdata(data);
3698out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 return status;
3700}
3701
3702static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3703 struct iattr *sattr, dev_t rdev)
3704{
3705 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003706 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003708
David Quigleyaa9c2662013-05-22 12:50:44 -04003709 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3710
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003711 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003713 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
3714 trace_nfs4_mknod(dir, &dentry->d_name, err);
3715 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 &exception);
3717 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003718
3719 nfs4_label_release_security(label);
3720
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 return err;
3722}
3723
3724static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3725 struct nfs_fsstat *fsstat)
3726{
3727 struct nfs4_statfs_arg args = {
3728 .fh = fhandle,
3729 .bitmask = server->attr_bitmask,
3730 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003731 struct nfs4_statfs_res res = {
3732 .fsstat = fsstat,
3733 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 struct rpc_message msg = {
3735 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3736 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003737 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 };
3739
Trond Myklebust0e574af2005-10-27 22:12:38 -04003740 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003741 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742}
3743
3744static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3745{
3746 struct nfs4_exception exception = { };
3747 int err;
3748 do {
3749 err = nfs4_handle_exception(server,
3750 _nfs4_proc_statfs(server, fhandle, fsstat),
3751 &exception);
3752 } while (exception.retry);
3753 return err;
3754}
3755
3756static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3757 struct nfs_fsinfo *fsinfo)
3758{
3759 struct nfs4_fsinfo_arg args = {
3760 .fh = fhandle,
3761 .bitmask = server->attr_bitmask,
3762 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003763 struct nfs4_fsinfo_res res = {
3764 .fsinfo = fsinfo,
3765 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 struct rpc_message msg = {
3767 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3768 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003769 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 };
3771
Bryan Schumaker7c513052011-03-24 17:12:24 +00003772 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773}
3774
3775static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3776{
3777 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04003778 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 int err;
3780
3781 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04003782 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003783 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04003784 if (err == 0) {
3785 struct nfs_client *clp = server->nfs_client;
3786
3787 spin_lock(&clp->cl_lock);
3788 clp->cl_lease_time = fsinfo->lease_time * HZ;
3789 clp->cl_last_renewal = now;
3790 spin_unlock(&clp->cl_lock);
3791 break;
3792 }
3793 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 } while (exception.retry);
3795 return err;
3796}
3797
3798static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3799{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003800 int error;
3801
Trond Myklebust0e574af2005-10-27 22:12:38 -04003802 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003803 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003804 if (error == 0) {
3805 /* block layout checks this! */
3806 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003807 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003808 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003809
3810 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811}
3812
3813static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3814 struct nfs_pathconf *pathconf)
3815{
3816 struct nfs4_pathconf_arg args = {
3817 .fh = fhandle,
3818 .bitmask = server->attr_bitmask,
3819 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003820 struct nfs4_pathconf_res res = {
3821 .pathconf = pathconf,
3822 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 struct rpc_message msg = {
3824 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3825 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003826 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 };
3828
3829 /* None of the pathconf attributes are mandatory to implement */
3830 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3831 memset(pathconf, 0, sizeof(*pathconf));
3832 return 0;
3833 }
3834
Trond Myklebust0e574af2005-10-27 22:12:38 -04003835 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003836 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837}
3838
3839static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3840 struct nfs_pathconf *pathconf)
3841{
3842 struct nfs4_exception exception = { };
3843 int err;
3844
3845 do {
3846 err = nfs4_handle_exception(server,
3847 _nfs4_proc_pathconf(server, fhandle, pathconf),
3848 &exception);
3849 } while (exception.retry);
3850 return err;
3851}
3852
Trond Myklebust5521abf2013-03-16 20:54:34 -04003853int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04003854 const struct nfs_open_context *ctx,
3855 const struct nfs_lock_context *l_ctx,
3856 fmode_t fmode)
3857{
3858 const struct nfs_lockowner *lockowner = NULL;
3859
3860 if (l_ctx != NULL)
3861 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003862 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04003863}
3864EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
3865
Trond Myklebust5521abf2013-03-16 20:54:34 -04003866static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
3867 const struct nfs_open_context *ctx,
3868 const struct nfs_lock_context *l_ctx,
3869 fmode_t fmode)
3870{
3871 nfs4_stateid current_stateid;
3872
3873 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode))
3874 return false;
3875 return nfs4_stateid_match(stateid, &current_stateid);
3876}
3877
3878static bool nfs4_error_stateid_expired(int err)
3879{
3880 switch (err) {
3881 case -NFS4ERR_DELEG_REVOKED:
3882 case -NFS4ERR_ADMIN_REVOKED:
3883 case -NFS4ERR_BAD_STATEID:
3884 case -NFS4ERR_STALE_STATEID:
3885 case -NFS4ERR_OLD_STATEID:
3886 case -NFS4ERR_OPENMODE:
3887 case -NFS4ERR_EXPIRED:
3888 return true;
3889 }
3890 return false;
3891}
3892
Benny Halevyd20581a2011-05-22 19:52:03 +03003893void __nfs4_read_done_cb(struct nfs_read_data *data)
3894{
Fred Isamancd841602012-04-20 14:47:44 -04003895 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003896}
3897
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003898static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899{
Fred Isamancd841602012-04-20 14:47:44 -04003900 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901
Trond Myklebustcc668ab2013-08-14 15:31:28 -04003902 trace_nfs4_read(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003903 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003904 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003905 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003907
Benny Halevyd20581a2011-05-22 19:52:03 +03003908 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003910 renew_lease(server, data->timestamp);
3911 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912}
3913
Trond Myklebust5521abf2013-03-16 20:54:34 -04003914static bool nfs4_read_stateid_changed(struct rpc_task *task,
3915 struct nfs_readargs *args)
3916{
3917
3918 if (!nfs4_error_stateid_expired(task->tk_status) ||
3919 nfs4_stateid_is_current(&args->stateid,
3920 args->context,
3921 args->lock_context,
3922 FMODE_READ))
3923 return false;
3924 rpc_restart_call_prepare(task);
3925 return true;
3926}
3927
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003928static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3929{
3930
3931 dprintk("--> %s\n", __func__);
3932
3933 if (!nfs4_sequence_done(task, &data->res.seq_res))
3934 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003935 if (nfs4_read_stateid_changed(task, &data->args))
3936 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003937 return data->read_done_cb ? data->read_done_cb(task, data) :
3938 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003939}
3940
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003941static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003944 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003945 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Chuck Levera9c92d62013-08-09 12:48:18 -04003946 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947}
3948
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003949static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3950{
Trond Myklebust9b206142013-03-17 15:52:00 -04003951 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003952 &data->args.seq_args,
3953 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04003954 task))
3955 return;
3956 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3957 data->args.lock_context, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958}
3959
Fred Isamanb029bc92011-03-03 15:13:42 +00003960static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961{
Fred Isamancd841602012-04-20 14:47:44 -04003962 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963
Trond Myklebustcc668ab2013-08-14 15:31:28 -04003964 trace_nfs4_write(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003965 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003966 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003967 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003969 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003971 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003972 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003973 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974}
3975
Trond Myklebust5521abf2013-03-16 20:54:34 -04003976static bool nfs4_write_stateid_changed(struct rpc_task *task,
3977 struct nfs_writeargs *args)
3978{
3979
3980 if (!nfs4_error_stateid_expired(task->tk_status) ||
3981 nfs4_stateid_is_current(&args->stateid,
3982 args->context,
3983 args->lock_context,
3984 FMODE_WRITE))
3985 return false;
3986 rpc_restart_call_prepare(task);
3987 return true;
3988}
3989
Fred Isamanb029bc92011-03-03 15:13:42 +00003990static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3991{
3992 if (!nfs4_sequence_done(task, &data->res.seq_res))
3993 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003994 if (nfs4_write_stateid_changed(task, &data->args))
3995 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003996 return data->write_done_cb ? data->write_done_cb(task, data) :
3997 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003998}
3999
Trond Myklebust5a37f852012-04-28 14:55:16 -04004000static
4001bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00004002{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004003 const struct nfs_pgio_header *hdr = data->header;
4004
4005 /* Don't request attributes for pNFS or O_DIRECT writes */
4006 if (data->ds_clp != NULL || hdr->dreq != NULL)
4007 return false;
4008 /* Otherwise, request attributes if and only if we don't hold
4009 * a delegation
4010 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004011 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004012}
Fred Isamana69aef12011-03-03 15:13:47 +00004013
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004014static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015{
Fred Isamancd841602012-04-20 14:47:44 -04004016 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004017
Trond Myklebust5a37f852012-04-28 14:55:16 -04004018 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00004019 data->args.bitmask = NULL;
4020 data->res.fattr = NULL;
4021 } else
4022 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004023
Fred Isamanb029bc92011-03-03 15:13:42 +00004024 if (!data->write_done_cb)
4025 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004026 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 data->timestamp = jiffies;
4028
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004029 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004030 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031}
4032
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04004033static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
4034{
Trond Myklebust9b206142013-03-17 15:52:00 -04004035 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004036 &data->args.seq_args,
4037 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004038 task))
4039 return;
4040 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4041 data->args.lock_context, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042}
4043
Fred Isaman0b7c0152012-04-20 14:47:39 -04004044static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4045{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004046 nfs4_setup_sequence(NFS_SERVER(data->inode),
4047 &data->args.seq_args,
4048 &data->res.seq_res,
4049 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050}
4051
Fred Isaman0b7c0152012-04-20 14:47:39 -04004052static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004055
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004056 trace_nfs4_commit(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004057 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004058 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004059 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004061 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062}
4063
Fred Isaman0b7c0152012-04-20 14:47:39 -04004064static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004065{
4066 if (!nfs4_sequence_done(task, &data->res.seq_res))
4067 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004068 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004069}
4070
Fred Isaman0b7c0152012-04-20 14:47:39 -04004071static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004073 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004074
Fred Isaman0b7c0152012-04-20 14:47:39 -04004075 if (data->commit_done_cb == NULL)
4076 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004077 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004078 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004079 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080}
4081
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004082struct nfs4_renewdata {
4083 struct nfs_client *client;
4084 unsigned long timestamp;
4085};
4086
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087/*
4088 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4089 * standalone procedure for queueing an asynchronous RENEW.
4090 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004091static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004092{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004093 struct nfs4_renewdata *data = calldata;
4094 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004095
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004096 if (atomic_read(&clp->cl_count) > 1)
4097 nfs4_schedule_state_renewal(clp);
4098 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004099 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004100}
4101
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004102static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004104 struct nfs4_renewdata *data = calldata;
4105 struct nfs_client *clp = data->client;
4106 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004108 trace_nfs4_renew_async(clp, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04004110 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004111 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4112 return;
4113 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004114 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004115 return;
4116 }
4117 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004119 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120}
4121
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004122static const struct rpc_call_ops nfs4_renew_ops = {
4123 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004124 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004125};
4126
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004127static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128{
4129 struct rpc_message msg = {
4130 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4131 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004132 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004134 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004136 if (renew_flags == 0)
4137 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004138 if (!atomic_inc_not_zero(&clp->cl_count))
4139 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004140 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004141 if (data == NULL)
4142 return -ENOMEM;
4143 data->client = clp;
4144 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004145 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004146 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147}
4148
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004149static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150{
4151 struct rpc_message msg = {
4152 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4153 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004154 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 };
4156 unsigned long now = jiffies;
4157 int status;
4158
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004159 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 if (status < 0)
4161 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004162 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 return 0;
4164}
4165
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004166static inline int nfs4_server_supports_acls(struct nfs_server *server)
4167{
4168 return (server->caps & NFS_CAP_ACLS)
4169 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
4170 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
4171}
4172
Trond Myklebust21f498c2012-08-24 10:59:25 -04004173/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4174 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004175 * the stack.
4176 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004177#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004178
Neil Hormane9e3d722011-03-04 19:26:03 -05004179static int buf_to_pages_noslab(const void *buf, size_t buflen,
4180 struct page **pages, unsigned int *pgbase)
4181{
4182 struct page *newpage, **spages;
4183 int rc = 0;
4184 size_t len;
4185 spages = pages;
4186
4187 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004188 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004189 newpage = alloc_page(GFP_KERNEL);
4190
4191 if (newpage == NULL)
4192 goto unwind;
4193 memcpy(page_address(newpage), buf, len);
4194 buf += len;
4195 buflen -= len;
4196 *pages++ = newpage;
4197 rc++;
4198 } while (buflen != 0);
4199
4200 return rc;
4201
4202unwind:
4203 for(; rc > 0; rc--)
4204 __free_page(spages[rc-1]);
4205 return -ENOMEM;
4206}
4207
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004208struct nfs4_cached_acl {
4209 int cached;
4210 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004211 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004212};
4213
4214static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004215{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004216 struct nfs_inode *nfsi = NFS_I(inode);
4217
4218 spin_lock(&inode->i_lock);
4219 kfree(nfsi->nfs4_acl);
4220 nfsi->nfs4_acl = acl;
4221 spin_unlock(&inode->i_lock);
4222}
4223
4224static void nfs4_zap_acl_attr(struct inode *inode)
4225{
4226 nfs4_set_cached_acl(inode, NULL);
4227}
4228
4229static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4230{
4231 struct nfs_inode *nfsi = NFS_I(inode);
4232 struct nfs4_cached_acl *acl;
4233 int ret = -ENOENT;
4234
4235 spin_lock(&inode->i_lock);
4236 acl = nfsi->nfs4_acl;
4237 if (acl == NULL)
4238 goto out;
4239 if (buf == NULL) /* user is just asking for length */
4240 goto out_len;
4241 if (acl->cached == 0)
4242 goto out;
4243 ret = -ERANGE; /* see getxattr(2) man page */
4244 if (acl->len > buflen)
4245 goto out;
4246 memcpy(buf, acl->data, acl->len);
4247out_len:
4248 ret = acl->len;
4249out:
4250 spin_unlock(&inode->i_lock);
4251 return ret;
4252}
4253
Sachin Prabhu5794d212012-04-17 14:36:40 +01004254static 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 +00004255{
4256 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004257 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004258
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004259 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004260 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004261 if (acl == NULL)
4262 goto out;
4263 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004264 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004265 } else {
4266 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4267 if (acl == NULL)
4268 goto out;
4269 acl->cached = 0;
4270 }
4271 acl->len = acl_len;
4272out:
4273 nfs4_set_cached_acl(inode, acl);
4274}
4275
Andy Adamsonbf118a32011-12-07 11:55:27 -05004276/*
4277 * The getxattr API returns the required buffer length when called with a
4278 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4279 * the required buf. On a NULL buf, we send a page of data to the server
4280 * guessing that the ACL request can be serviced by a page. If so, we cache
4281 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4282 * the cache. If not so, we throw away the page, and cache the required
4283 * length. The next getxattr call will then produce another round trip to
4284 * the server, this time with the input buf of the required size.
4285 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004286static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004287{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004288 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004289 struct nfs_getaclargs args = {
4290 .fh = NFS_FH(inode),
4291 .acl_pages = pages,
4292 .acl_len = buflen,
4293 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004294 struct nfs_getaclres res = {
4295 .acl_len = buflen,
4296 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004297 struct rpc_message msg = {
4298 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4299 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004300 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004301 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004302 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4303 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004304
Andy Adamsonbf118a32011-12-07 11:55:27 -05004305 /* As long as we're doing a round trip to the server anyway,
4306 * let's be prepared for a page of acl data. */
4307 if (npages == 0)
4308 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004309 if (npages > ARRAY_SIZE(pages))
4310 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004311
Andy Adamsonbf118a32011-12-07 11:55:27 -05004312 for (i = 0; i < npages; i++) {
4313 pages[i] = alloc_page(GFP_KERNEL);
4314 if (!pages[i])
4315 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004316 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004317
4318 /* for decoding across pages */
4319 res.acl_scratch = alloc_page(GFP_KERNEL);
4320 if (!res.acl_scratch)
4321 goto out_free;
4322
Andy Adamsonbf118a32011-12-07 11:55:27 -05004323 args.acl_len = npages * PAGE_SIZE;
4324 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004325
Peng Taode040be2012-01-10 22:42:47 +08004326 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004327 __func__, buf, buflen, npages, args.acl_len);
4328 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4329 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004330 if (ret)
4331 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004332
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004333 /* Handle the case where the passed-in buffer is too short */
4334 if (res.acl_flags & NFS4_ACL_TRUNC) {
4335 /* Did the user only issue a request for the acl length? */
4336 if (buf == NULL)
4337 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004338 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004339 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004340 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004341 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004342 if (buf) {
4343 if (res.acl_len > buflen) {
4344 ret = -ERANGE;
4345 goto out_free;
4346 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004347 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004348 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004349out_ok:
4350 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004351out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004352 for (i = 0; i < npages; i++)
4353 if (pages[i])
4354 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004355 if (res.acl_scratch)
4356 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004357 return ret;
4358}
4359
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004360static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4361{
4362 struct nfs4_exception exception = { };
4363 ssize_t ret;
4364 do {
4365 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04004366 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004367 if (ret >= 0)
4368 break;
4369 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4370 } while (exception.retry);
4371 return ret;
4372}
4373
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004374static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4375{
4376 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004377 int ret;
4378
4379 if (!nfs4_server_supports_acls(server))
4380 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004381 ret = nfs_revalidate_inode(server, inode);
4382 if (ret < 0)
4383 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004384 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4385 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004386 ret = nfs4_read_cached_acl(inode, buf, buflen);
4387 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004388 /* -ENOENT is returned if there is no ACL or if there is an ACL
4389 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004390 return ret;
4391 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004392}
4393
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004394static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004395{
4396 struct nfs_server *server = NFS_SERVER(inode);
4397 struct page *pages[NFS4ACL_MAXPAGES];
4398 struct nfs_setaclargs arg = {
4399 .fh = NFS_FH(inode),
4400 .acl_pages = pages,
4401 .acl_len = buflen,
4402 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004403 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004404 struct rpc_message msg = {
4405 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4406 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004407 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004408 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004409 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004410 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004411
4412 if (!nfs4_server_supports_acls(server))
4413 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004414 if (npages > ARRAY_SIZE(pages))
4415 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004416 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4417 if (i < 0)
4418 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004419 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004420 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004421
4422 /*
4423 * Free each page after tx, so the only ref left is
4424 * held by the network stack
4425 */
4426 for (; i > 0; i--)
4427 put_page(pages[i-1]);
4428
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004429 /*
4430 * Acl update can result in inode attribute update.
4431 * so mark the attribute cache invalid.
4432 */
4433 spin_lock(&inode->i_lock);
4434 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4435 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004436 nfs_access_zap_cache(inode);
4437 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004438 return ret;
4439}
4440
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004441static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4442{
4443 struct nfs4_exception exception = { };
4444 int err;
4445 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004446 err = __nfs4_proc_set_acl(inode, buf, buflen);
4447 trace_nfs4_set_acl(inode, err);
4448 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004449 &exception);
4450 } while (exception.retry);
4451 return err;
4452}
4453
David Quigleyaa9c2662013-05-22 12:50:44 -04004454#ifdef CONFIG_NFS_V4_SECURITY_LABEL
4455static int _nfs4_get_security_label(struct inode *inode, void *buf,
4456 size_t buflen)
4457{
4458 struct nfs_server *server = NFS_SERVER(inode);
4459 struct nfs_fattr fattr;
4460 struct nfs4_label label = {0, 0, buflen, buf};
4461
4462 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4463 struct nfs4_getattr_arg args = {
4464 .fh = NFS_FH(inode),
4465 .bitmask = bitmask,
4466 };
4467 struct nfs4_getattr_res res = {
4468 .fattr = &fattr,
4469 .label = &label,
4470 .server = server,
4471 };
4472 struct rpc_message msg = {
4473 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4474 .rpc_argp = &args,
4475 .rpc_resp = &res,
4476 };
4477 int ret;
4478
4479 nfs_fattr_init(&fattr);
4480
4481 ret = rpc_call_sync(server->client, &msg, 0);
4482 if (ret)
4483 return ret;
4484 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4485 return -ENOENT;
4486 if (buflen < label.len)
4487 return -ERANGE;
4488 return 0;
4489}
4490
4491static int nfs4_get_security_label(struct inode *inode, void *buf,
4492 size_t buflen)
4493{
4494 struct nfs4_exception exception = { };
4495 int err;
4496
4497 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4498 return -EOPNOTSUPP;
4499
4500 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004501 err = _nfs4_get_security_label(inode, buf, buflen);
4502 trace_nfs4_get_security_label(inode, err);
4503 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004504 &exception);
4505 } while (exception.retry);
4506 return err;
4507}
4508
4509static int _nfs4_do_set_security_label(struct inode *inode,
4510 struct nfs4_label *ilabel,
4511 struct nfs_fattr *fattr,
4512 struct nfs4_label *olabel)
4513{
4514
4515 struct iattr sattr = {0};
4516 struct nfs_server *server = NFS_SERVER(inode);
4517 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4518 struct nfs_setattrargs args = {
4519 .fh = NFS_FH(inode),
4520 .iap = &sattr,
4521 .server = server,
4522 .bitmask = bitmask,
4523 .label = ilabel,
4524 };
4525 struct nfs_setattrres res = {
4526 .fattr = fattr,
4527 .label = olabel,
4528 .server = server,
4529 };
4530 struct rpc_message msg = {
4531 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
4532 .rpc_argp = &args,
4533 .rpc_resp = &res,
4534 };
4535 int status;
4536
4537 nfs4_stateid_copy(&args.stateid, &zero_stateid);
4538
4539 status = rpc_call_sync(server->client, &msg, 0);
4540 if (status)
4541 dprintk("%s failed: %d\n", __func__, status);
4542
4543 return status;
4544}
4545
4546static int nfs4_do_set_security_label(struct inode *inode,
4547 struct nfs4_label *ilabel,
4548 struct nfs_fattr *fattr,
4549 struct nfs4_label *olabel)
4550{
4551 struct nfs4_exception exception = { };
4552 int err;
4553
4554 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004555 err = _nfs4_do_set_security_label(inode, ilabel,
4556 fattr, olabel);
4557 trace_nfs4_set_security_label(inode, err);
4558 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004559 &exception);
4560 } while (exception.retry);
4561 return err;
4562}
4563
4564static int
4565nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4566{
4567 struct nfs4_label ilabel, *olabel = NULL;
4568 struct nfs_fattr fattr;
4569 struct rpc_cred *cred;
4570 struct inode *inode = dentry->d_inode;
4571 int status;
4572
4573 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4574 return -EOPNOTSUPP;
4575
4576 nfs_fattr_init(&fattr);
4577
4578 ilabel.pi = 0;
4579 ilabel.lfs = 0;
4580 ilabel.label = (char *)buf;
4581 ilabel.len = buflen;
4582
4583 cred = rpc_lookup_cred();
4584 if (IS_ERR(cred))
4585 return PTR_ERR(cred);
4586
4587 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4588 if (IS_ERR(olabel)) {
4589 status = -PTR_ERR(olabel);
4590 goto out;
4591 }
4592
4593 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4594 if (status == 0)
4595 nfs_setsecurity(inode, &fattr, olabel);
4596
4597 nfs4_label_free(olabel);
4598out:
4599 put_rpccred(cred);
4600 return status;
4601}
4602#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4603
4604
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004606nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004608 struct nfs_client *clp = server->nfs_client;
4609
4610 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 return 0;
4612 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004613 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004614 case -NFS4ERR_ADMIN_REVOKED:
4615 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004616 if (state == NULL)
4617 break;
4618 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004619 case -NFS4ERR_OPENMODE:
4620 if (state == NULL)
4621 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004622 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4623 goto stateid_invalid;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004624 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004625 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004626 if (state != NULL) {
4627 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4628 goto stateid_invalid;
4629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004631 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004632 nfs4_schedule_lease_recovery(clp);
4633 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004634#if defined(CONFIG_NFS_V4_1)
4635 case -NFS4ERR_BADSESSION:
4636 case -NFS4ERR_BADSLOT:
4637 case -NFS4ERR_BAD_HIGH_SLOT:
4638 case -NFS4ERR_DEADSESSION:
4639 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4640 case -NFS4ERR_SEQ_FALSE_RETRY:
4641 case -NFS4ERR_SEQ_MISORDERED:
4642 dprintk("%s ERROR %d, Reset session\n", __func__,
4643 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004644 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004645 task->tk_status = 0;
4646 return -EAGAIN;
4647#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004649 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004650 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4652 task->tk_status = 0;
4653 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004654 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 case -NFS4ERR_OLD_STATEID:
4656 task->tk_status = 0;
4657 return -EAGAIN;
4658 }
4659 task->tk_status = nfs4_map_errors(task->tk_status);
4660 return 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004661stateid_invalid:
4662 task->tk_status = -EIO;
4663 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004664wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004665 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004666 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4667 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4668 task->tk_status = 0;
4669 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670}
4671
Chuck Leverf0920752012-05-21 22:45:41 -04004672static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4673 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004674{
4675 __be32 verf[2];
4676
Chuck Lever2c820d92012-05-21 22:45:33 -04004677 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4678 /* An impossible timestamp guarantees this value
4679 * will never match a generated boot time. */
4680 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04004681 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04004682 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004683 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04004684 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
4685 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04004686 }
Chuck Levercd937102012-03-02 17:14:31 -05004687 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4688}
4689
Chuck Levere984a552012-09-14 17:24:21 -04004690static unsigned int
4691nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4692 char *buf, size_t len)
4693{
4694 unsigned int result;
4695
4696 rcu_read_lock();
4697 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4698 clp->cl_ipaddr,
4699 rpc_peeraddr2str(clp->cl_rpcclient,
4700 RPC_DISPLAY_ADDR),
4701 rpc_peeraddr2str(clp->cl_rpcclient,
4702 RPC_DISPLAY_PROTO));
4703 rcu_read_unlock();
4704 return result;
4705}
4706
4707static unsigned int
4708nfs4_init_uniform_client_string(const struct nfs_client *clp,
4709 char *buf, size_t len)
4710{
Trond Myklebust55b59292013-07-24 16:36:35 -04004711 const char *nodename = clp->cl_rpcclient->cl_nodename;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004712
4713 if (nfs4_client_id_uniquifier[0] != '\0')
Trond Myklebust55b59292013-07-24 16:36:35 -04004714 return scnprintf(buf, len, "Linux NFSv%u.%u %s/%s",
4715 clp->rpc_ops->version,
4716 clp->cl_minorversion,
4717 nfs4_client_id_uniquifier,
4718 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004719 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4720 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004721 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004722}
4723
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004724/**
4725 * nfs4_proc_setclientid - Negotiate client ID
4726 * @clp: state data structure
4727 * @program: RPC program for NFSv4 callback service
4728 * @port: IP port number for NFS4 callback service
4729 * @cred: RPC credential to use for this call
4730 * @res: where to place the result
4731 *
4732 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4733 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004734int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4735 unsigned short port, struct rpc_cred *cred,
4736 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737{
4738 nfs4_verifier sc_verifier;
4739 struct nfs4_setclientid setclientid = {
4740 .sc_verifier = &sc_verifier,
4741 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004742 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743 };
4744 struct rpc_message msg = {
4745 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4746 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004747 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004748 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004750 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751
Chuck Leverde734832012-07-11 16:30:50 -04004752 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004753 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004754 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4755 setclientid.sc_name_len =
4756 nfs4_init_uniform_client_string(clp,
4757 setclientid.sc_name,
4758 sizeof(setclientid.sc_name));
4759 else
4760 setclientid.sc_name_len =
4761 nfs4_init_nonuniform_client_string(clp,
4762 setclientid.sc_name,
4763 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004764 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004765 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004766 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Djalal Harounife2d5392013-06-18 17:58:12 +01004767 sizeof(setclientid.sc_netid), "%s",
Chuck Leverd4d3c502007-12-10 14:57:09 -05004768 rpc_peeraddr2str(clp->cl_rpcclient,
4769 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004770 rcu_read_unlock();
4771 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004772 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 clp->cl_ipaddr, port >> 8, port & 255);
4774
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004775 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4776 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4777 setclientid.sc_name_len, setclientid.sc_name);
4778 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004779 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004780 dprintk("NFS reply setclientid: %d\n", status);
4781 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782}
4783
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004784/**
4785 * nfs4_proc_setclientid_confirm - Confirm client ID
4786 * @clp: state data structure
4787 * @res: result of a previous SETCLIENTID
4788 * @cred: RPC credential to use for this call
4789 *
4790 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4791 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004792int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004793 struct nfs4_setclientid_res *arg,
4794 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 struct rpc_message msg = {
4797 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004798 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004799 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 int status;
4802
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004803 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4804 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4805 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004806 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004807 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004808 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809 return status;
4810}
4811
Trond Myklebustfe650402006-01-03 09:55:18 +01004812struct nfs4_delegreturndata {
4813 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004814 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004815 struct nfs_fh fh;
4816 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004817 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004818 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004819 int rpc_status;
4820};
4821
Trond Myklebustfe650402006-01-03 09:55:18 +01004822static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4823{
4824 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004825
Trond Myklebust14516c32010-07-31 14:29:06 -04004826 if (!nfs4_sequence_done(task, &data->res.seq_res))
4827 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004828
Trond Myklebustca8acf82013-08-13 10:36:56 -04004829 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004830 switch (task->tk_status) {
4831 case -NFS4ERR_STALE_STATEID:
4832 case -NFS4ERR_EXPIRED:
4833 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004834 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004835 break;
4836 default:
4837 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4838 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004839 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004840 return;
4841 }
4842 }
4843 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004844}
4845
4846static void nfs4_delegreturn_release(void *calldata)
4847{
Trond Myklebustfe650402006-01-03 09:55:18 +01004848 kfree(calldata);
4849}
4850
Andy Adamson938e1012009-04-01 09:22:28 -04004851#if defined(CONFIG_NFS_V4_1)
4852static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4853{
4854 struct nfs4_delegreturndata *d_data;
4855
4856 d_data = (struct nfs4_delegreturndata *)data;
4857
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004858 nfs4_setup_sequence(d_data->res.server,
4859 &d_data->args.seq_args,
4860 &d_data->res.seq_res,
4861 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004862}
4863#endif /* CONFIG_NFS_V4_1 */
4864
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004865static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004866#if defined(CONFIG_NFS_V4_1)
4867 .rpc_call_prepare = nfs4_delegreturn_prepare,
4868#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004869 .rpc_call_done = nfs4_delegreturn_done,
4870 .rpc_release = nfs4_delegreturn_release,
4871};
4872
Trond Myklebuste6f81072008-01-24 18:14:34 -05004873static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004874{
4875 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004876 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004877 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004878 struct rpc_message msg = {
4879 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4880 .rpc_cred = cred,
4881 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004882 struct rpc_task_setup task_setup_data = {
4883 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004884 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004885 .callback_ops = &nfs4_delegreturn_ops,
4886 .flags = RPC_TASK_ASYNC,
4887 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004888 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004889
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004890 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004891 if (data == NULL)
4892 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04004893 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004894 data->args.fhandle = &data->fh;
4895 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004896 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004897 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004898 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004899 data->res.fattr = &data->fattr;
4900 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004901 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004902 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004903 data->rpc_status = 0;
4904
Trond Myklebustc970aa82007-07-14 15:39:59 -04004905 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004906 msg.rpc_argp = &data->args;
4907 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004908 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004909 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004910 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004911 if (!issync)
4912 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004913 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004914 if (status != 0)
4915 goto out;
4916 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004917 if (status == 0)
4918 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4919 else
4920 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004921out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004922 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004923 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924}
4925
Trond Myklebuste6f81072008-01-24 18:14:34 -05004926int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927{
4928 struct nfs_server *server = NFS_SERVER(inode);
4929 struct nfs4_exception exception = { };
4930 int err;
4931 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004932 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Trond Myklebustca8acf82013-08-13 10:36:56 -04004933 trace_nfs4_delegreturn(inode, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 switch (err) {
4935 case -NFS4ERR_STALE_STATEID:
4936 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 case 0:
4938 return 0;
4939 }
4940 err = nfs4_handle_exception(server, err, &exception);
4941 } while (exception.retry);
4942 return err;
4943}
4944
4945#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4946#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4947
4948/*
4949 * sleep, with exponential backoff, and retry the LOCK operation.
4950 */
4951static unsigned long
4952nfs4_set_lock_task_retry(unsigned long timeout)
4953{
Colin Cross416ad3c2013-05-06 23:50:06 +00004954 freezable_schedule_timeout_killable_unsafe(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 timeout <<= 1;
4956 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4957 return NFS4_LOCK_MAXTIMEOUT;
4958 return timeout;
4959}
4960
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4962{
4963 struct inode *inode = state->inode;
4964 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004965 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004966 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004968 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004970 struct nfs_lockt_res res = {
4971 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972 };
4973 struct rpc_message msg = {
4974 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4975 .rpc_argp = &arg,
4976 .rpc_resp = &res,
4977 .rpc_cred = state->owner->so_cred,
4978 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 struct nfs4_lock_state *lsp;
4980 int status;
4981
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004982 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004983 status = nfs4_set_lock_state(state, request);
4984 if (status != 0)
4985 goto out;
4986 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004987 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004988 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004989 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004990 switch (status) {
4991 case 0:
4992 request->fl_type = F_UNLCK;
4993 break;
4994 case -NFS4ERR_DENIED:
4995 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004997 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004998out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 return status;
5000}
5001
5002static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5003{
5004 struct nfs4_exception exception = { };
5005 int err;
5006
5007 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005008 err = _nfs4_proc_getlk(state, cmd, request);
5009 trace_nfs4_get_lock(request, state, cmd, err);
5010 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011 &exception);
5012 } while (exception.retry);
5013 return err;
5014}
5015
5016static int do_vfs_lock(struct file *file, struct file_lock *fl)
5017{
5018 int res = 0;
5019 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5020 case FL_POSIX:
5021 res = posix_lock_file_wait(file, fl);
5022 break;
5023 case FL_FLOCK:
5024 res = flock_lock_file_wait(file, fl);
5025 break;
5026 default:
5027 BUG();
5028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029 return res;
5030}
5031
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005032struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005033 struct nfs_locku_args arg;
5034 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005035 struct nfs4_lock_state *lsp;
5036 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005037 struct file_lock fl;
5038 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005039 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005040};
5041
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005042static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5043 struct nfs_open_context *ctx,
5044 struct nfs4_lock_state *lsp,
5045 struct nfs_seqid *seqid)
5046{
5047 struct nfs4_unlockdata *p;
5048 struct inode *inode = lsp->ls_state->inode;
5049
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005050 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005051 if (p == NULL)
5052 return NULL;
5053 p->arg.fh = NFS_FH(inode);
5054 p->arg.fl = &p->fl;
5055 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005056 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005057 p->arg.stateid = &lsp->ls_stateid;
5058 p->lsp = lsp;
5059 atomic_inc(&lsp->ls_count);
5060 /* Ensure we don't close file until we're done freeing locks! */
5061 p->ctx = get_nfs_open_context(ctx);
5062 memcpy(&p->fl, fl, sizeof(p->fl));
5063 p->server = NFS_SERVER(inode);
5064 return p;
5065}
5066
Trond Myklebust06f814a2006-01-03 09:55:07 +01005067static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005068{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005069 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005070 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005071 nfs4_put_lock_state(calldata->lsp);
5072 put_nfs_open_context(calldata->ctx);
5073 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005074}
5075
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005076static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005077{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005078 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005079
Trond Myklebust14516c32010-07-31 14:29:06 -04005080 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5081 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005082 switch (task->tk_status) {
5083 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05005084 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
5085 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005086 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005087 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005088 case -NFS4ERR_BAD_STATEID:
5089 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005090 case -NFS4ERR_STALE_STATEID:
5091 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005092 break;
5093 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005094 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005095 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005096 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005097 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005098}
5099
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005100static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005101{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005102 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005104 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005105 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04005106 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005107 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005108 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005109 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005110 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005111 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005112 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005113 &calldata->res.seq_res,
5114 task) != 0)
5115 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005116 return;
5117out_no_action:
5118 task->tk_action = NULL;
5119out_wait:
5120 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121}
5122
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005123static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005124 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005125 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005126 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005127};
5128
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005129static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5130 struct nfs_open_context *ctx,
5131 struct nfs4_lock_state *lsp,
5132 struct nfs_seqid *seqid)
5133{
5134 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005135 struct rpc_message msg = {
5136 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5137 .rpc_cred = ctx->cred,
5138 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005139 struct rpc_task_setup task_setup_data = {
5140 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005141 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005142 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005143 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005144 .flags = RPC_TASK_ASYNC,
5145 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005146
Frank Filz137d6ac2007-07-09 15:32:29 -07005147 /* Ensure this is an unlock - when canceling a lock, the
5148 * canceled lock is passed in, and it won't be an unlock.
5149 */
5150 fl->fl_type = F_UNLCK;
5151
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005152 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5153 if (data == NULL) {
5154 nfs_free_seqid(seqid);
5155 return ERR_PTR(-ENOMEM);
5156 }
5157
Chuck Levera9c92d62013-08-09 12:48:18 -04005158 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005159 msg.rpc_argp = &data->arg;
5160 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005161 task_setup_data.callback_data = data;
5162 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005163}
5164
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5166{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005167 struct inode *inode = state->inode;
5168 struct nfs4_state_owner *sp = state->owner;
5169 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005170 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005171 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005172 struct rpc_task *task;
5173 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005174 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175
Trond Myklebust9b073572006-06-29 16:38:34 -04005176 status = nfs4_set_lock_state(state, request);
5177 /* Unlock _before_ we do the RPC call */
5178 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005179 /* Exclude nfs_delegation_claim_locks() */
5180 mutex_lock(&sp->so_delegreturn_mutex);
5181 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005182 down_read(&nfsi->rwsem);
5183 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
5184 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005185 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005186 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005187 }
5188 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005189 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005190 if (status != 0)
5191 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005192 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005193 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005194 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5195 goto out;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005196 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005197 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005198 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04005199 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005200 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005201 status = PTR_ERR(task);
5202 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005203 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005204 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005205 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005206out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005207 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005208 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005209 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210}
5211
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005212struct nfs4_lockdata {
5213 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005214 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005215 struct nfs4_lock_state *lsp;
5216 struct nfs_open_context *ctx;
5217 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005218 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005219 int rpc_status;
5220 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005221 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005222};
5223
5224static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005225 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5226 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005227{
5228 struct nfs4_lockdata *p;
5229 struct inode *inode = lsp->ls_state->inode;
5230 struct nfs_server *server = NFS_SERVER(inode);
5231
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005232 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005233 if (p == NULL)
5234 return NULL;
5235
5236 p->arg.fh = NFS_FH(inode);
5237 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005238 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005239 if (p->arg.open_seqid == NULL)
5240 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005241 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005242 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005243 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005244 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04005245 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005246 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005247 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005248 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005249 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005250 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005251 atomic_inc(&lsp->ls_count);
5252 p->ctx = get_nfs_open_context(ctx);
5253 memcpy(&p->fl, fl, sizeof(p->fl));
5254 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005255out_free_seqid:
5256 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005257out_free:
5258 kfree(p);
5259 return NULL;
5260}
5261
5262static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5263{
5264 struct nfs4_lockdata *data = calldata;
5265 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266
Harvey Harrison3110ff82008-05-02 13:42:44 -07005267 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005268 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005269 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005270 /* Do we need to do an open_to_lock_owner? */
5271 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005272 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005273 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005274 }
Trond Myklebust8e472f32013-04-20 01:30:53 -04005275 data->arg.open_stateid = &state->open_stateid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005276 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005277 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005278 } else
5279 data->arg.new_lock_owner = 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04005280 if (!nfs4_valid_open_stateid(state)) {
5281 data->rpc_status = -EBADF;
5282 task->tk_action = NULL;
5283 goto out_release_open_seqid;
5284 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005285 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005286 if (nfs4_setup_sequence(data->server,
5287 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005288 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005289 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04005290 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04005291out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005292 nfs_release_seqid(data->arg.open_seqid);
5293out_release_lock_seqid:
5294 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005295out_wait:
5296 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005297 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005298}
5299
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005300static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5301{
5302 struct nfs4_lockdata *data = calldata;
5303
Harvey Harrison3110ff82008-05-02 13:42:44 -07005304 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005305
Trond Myklebust14516c32010-07-31 14:29:06 -04005306 if (!nfs4_sequence_done(task, &data->res.seq_res))
5307 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04005308
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005309 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005310 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005311 if (data->rpc_status == 0)
5312 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
5313 else
5314 goto out;
5315 }
5316 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05005317 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005318 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04005319 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005320 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005321out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07005322 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005323}
5324
5325static void nfs4_lock_release(void *calldata)
5326{
5327 struct nfs4_lockdata *data = calldata;
5328
Harvey Harrison3110ff82008-05-02 13:42:44 -07005329 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005330 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005331 if (data->cancelled != 0) {
5332 struct rpc_task *task;
5333 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5334 data->arg.lock_seqid);
5335 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08005336 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005337 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005338 } else
5339 nfs_free_seqid(data->arg.lock_seqid);
5340 nfs4_put_lock_state(data->lsp);
5341 put_nfs_open_context(data->ctx);
5342 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005343 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005344}
5345
5346static const struct rpc_call_ops nfs4_lock_ops = {
5347 .rpc_call_prepare = nfs4_lock_prepare,
5348 .rpc_call_done = nfs4_lock_done,
5349 .rpc_release = nfs4_lock_release,
5350};
5351
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005352static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5353{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005354 switch (error) {
5355 case -NFS4ERR_ADMIN_REVOKED:
5356 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05005357 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005358 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04005359 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05005360 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005361 break;
5362 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005363 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05005364 case -NFS4ERR_EXPIRED:
5365 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005366 };
5367}
5368
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005369static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005370{
5371 struct nfs4_lockdata *data;
5372 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005373 struct rpc_message msg = {
5374 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5375 .rpc_cred = state->owner->so_cred,
5376 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005377 struct rpc_task_setup task_setup_data = {
5378 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005379 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005380 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005381 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005382 .flags = RPC_TASK_ASYNC,
5383 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005384 int ret;
5385
Harvey Harrison3110ff82008-05-02 13:42:44 -07005386 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005387 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005388 fl->fl_u.nfs4_fl.owner,
5389 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005390 if (data == NULL)
5391 return -ENOMEM;
5392 if (IS_SETLKW(cmd))
5393 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04005394 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005395 msg.rpc_argp = &data->arg;
5396 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005397 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005398 if (recovery_type > NFS_LOCK_NEW) {
5399 if (recovery_type == NFS_LOCK_RECLAIM)
5400 data->arg.reclaim = NFS_LOCK_RECLAIM;
5401 nfs4_set_sequence_privileged(&data->arg.seq_args);
5402 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04005403 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005404 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005405 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005406 ret = nfs4_wait_for_completion_rpc_task(task);
5407 if (ret == 0) {
5408 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005409 if (ret)
5410 nfs4_handle_setlk_error(data->server, data->lsp,
5411 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005412 } else
5413 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005414 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005415 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005416 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417}
5418
5419static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5420{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005421 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005422 struct nfs4_exception exception = {
5423 .inode = state->inode,
5424 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005425 int err;
5426
5427 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005428 /* Cache the lock if possible... */
5429 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5430 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005431 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005432 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
Trond Myklebust168667c2010-10-19 19:47:49 -04005433 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00005434 break;
5435 nfs4_handle_exception(server, err, &exception);
5436 } while (exception.retry);
5437 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438}
5439
5440static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5441{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005442 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005443 struct nfs4_exception exception = {
5444 .inode = state->inode,
5445 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005446 int err;
5447
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005448 err = nfs4_set_lock_state(state, request);
5449 if (err != 0)
5450 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00005451 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005452 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5453 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005454 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005455 trace_nfs4_lock_expired(request, state, F_SETLK, err);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005456 switch (err) {
5457 default:
5458 goto out;
5459 case -NFS4ERR_GRACE:
5460 case -NFS4ERR_DELAY:
5461 nfs4_handle_exception(server, err, &exception);
5462 err = 0;
5463 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005464 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005465out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00005466 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467}
5468
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005469#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005470/**
5471 * nfs41_check_expired_locks - possibly free a lock stateid
5472 *
5473 * @state: NFSv4 state for an inode
5474 *
5475 * Returns NFS_OK if recovery for this stateid is now finished.
5476 * Otherwise a negative NFS4ERR value is returned.
5477 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005478static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005479{
Chuck Levereb64cf92012-07-11 16:30:05 -04005480 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005481 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005482 struct nfs_server *server = NFS_SERVER(state->inode);
5483
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005484 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04005485 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005486 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
5487
5488 status = nfs41_test_stateid(server,
5489 &lsp->ls_stateid,
5490 cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04005491 trace_nfs4_test_lock_stateid(state, lsp, status);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005492 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005493 /* Free the stateid unless the server
5494 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04005495 if (status != -NFS4ERR_BAD_STATEID)
5496 nfs41_free_stateid(server,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005497 &lsp->ls_stateid,
5498 cred);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005499 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005500 ret = status;
5501 }
5502 }
5503 };
5504
5505 return ret;
5506}
5507
5508static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5509{
5510 int status = NFS_OK;
5511
5512 if (test_bit(LK_STATE_IN_USE, &state->flags))
5513 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005514 if (status != NFS_OK)
5515 status = nfs4_lock_expired(state, request);
5516 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005517}
5518#endif
5519
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5521{
Trond Myklebust9a99af492013-02-04 20:17:49 -05005522 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005523 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005524 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005525 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005526 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005528 if ((fl_flags & FL_POSIX) &&
5529 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5530 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005531 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005532 status = nfs4_set_lock_state(state, request);
5533 if (status != 0)
5534 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005535 request->fl_flags |= FL_ACCESS;
5536 status = do_vfs_lock(request->fl_file, request);
5537 if (status < 0)
5538 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005539 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005540 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005541 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005542 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005543 request->fl_flags = fl_flags & ~FL_SLEEP;
5544 status = do_vfs_lock(request->fl_file, request);
5545 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005546 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05005547 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5548 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005549 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005550 if (status != 0)
Trond Myklebust9a99af492013-02-04 20:17:49 -05005551 goto out;
5552 down_read(&nfsi->rwsem);
5553 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5554 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005555 goto out_unlock;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005556 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005557 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005558 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005559 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005560 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5561 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005562out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005563 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005564out:
5565 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566 return status;
5567}
5568
5569static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5570{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005571 struct nfs4_exception exception = {
5572 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005573 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005574 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575 int err;
5576
5577 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005578 err = _nfs4_proc_setlk(state, cmd, request);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005579 trace_nfs4_set_lock(request, state, cmd, err);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005580 if (err == -NFS4ERR_DENIED)
5581 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005583 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584 } while (exception.retry);
5585 return err;
5586}
5587
5588static int
5589nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5590{
5591 struct nfs_open_context *ctx;
5592 struct nfs4_state *state;
5593 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5594 int status;
5595
5596 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005597 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598 state = ctx->state;
5599
5600 if (request->fl_start < 0 || request->fl_end < 0)
5601 return -EINVAL;
5602
Trond Myklebustd9531262009-07-21 19:22:38 -04005603 if (IS_GETLK(cmd)) {
5604 if (state != NULL)
5605 return nfs4_proc_getlk(state, F_GETLK, request);
5606 return 0;
5607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608
5609 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5610 return -EINVAL;
5611
Trond Myklebustd9531262009-07-21 19:22:38 -04005612 if (request->fl_type == F_UNLCK) {
5613 if (state != NULL)
5614 return nfs4_proc_unlck(state, cmd, request);
5615 return 0;
5616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617
Trond Myklebustd9531262009-07-21 19:22:38 -04005618 if (state == NULL)
5619 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005620 /*
5621 * Don't rely on the VFS having checked the file open mode,
5622 * since it won't do this for flock() locks.
5623 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005624 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005625 case F_RDLCK:
5626 if (!(filp->f_mode & FMODE_READ))
5627 return -EBADF;
5628 break;
5629 case F_WRLCK:
5630 if (!(filp->f_mode & FMODE_WRITE))
5631 return -EBADF;
5632 }
5633
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634 do {
5635 status = nfs4_proc_setlk(state, cmd, request);
5636 if ((status != -EAGAIN) || IS_SETLK(cmd))
5637 break;
5638 timeout = nfs4_set_lock_task_retry(timeout);
5639 status = -ERESTARTSYS;
5640 if (signalled())
5641 break;
5642 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643 return status;
5644}
5645
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005646int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05005647{
5648 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05005649 int err;
5650
5651 err = nfs4_set_lock_state(state, fl);
5652 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005653 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04005654 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005655 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05005656}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005657
Trond Myklebustcf470c32012-03-07 13:49:12 -05005658struct nfs_release_lockowner_data {
5659 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005660 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005661 struct nfs_release_lockowner_args args;
5662};
5663
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005664static void nfs4_release_lockowner_release(void *calldata)
5665{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005666 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005667 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005668 kfree(calldata);
5669}
5670
Trond Myklebust17280172012-03-11 13:11:00 -04005671static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005672 .rpc_release = nfs4_release_lockowner_release,
5673};
5674
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04005675static int nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005676{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005677 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005678 struct rpc_message msg = {
5679 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5680 };
5681
5682 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005683 return -EINVAL;
5684 data = kmalloc(sizeof(*data), GFP_NOFS);
5685 if (!data)
5686 return -ENOMEM;
5687 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005688 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005689 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5690 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5691 data->args.lock_owner.s_dev = server->s_dev;
5692 msg.rpc_argp = &data->args;
5693 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5694 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005695}
5696
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005697#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5698
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005699static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5700 const void *buf, size_t buflen,
5701 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005702{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005703 if (strcmp(key, "") != 0)
5704 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005705
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005706 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005707}
5708
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005709static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5710 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005711{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005712 if (strcmp(key, "") != 0)
5713 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005714
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005715 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005716}
5717
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005718static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5719 size_t list_len, const char *name,
5720 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005721{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005722 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005723
J. Bruce Fields096455a2006-03-20 23:23:42 -05005724 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5725 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005726
5727 if (list && len <= list_len)
5728 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005729 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005730}
5731
David Quigleyc9bccef2013-05-22 12:50:45 -04005732#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5733static inline int nfs4_server_supports_labels(struct nfs_server *server)
5734{
5735 return server->caps & NFS_CAP_SECURITY_LABEL;
5736}
5737
5738static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
5739 const void *buf, size_t buflen,
5740 int flags, int type)
5741{
5742 if (security_ismaclabel(key))
5743 return nfs4_set_security_label(dentry, buf, buflen);
5744
5745 return -EOPNOTSUPP;
5746}
5747
5748static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
5749 void *buf, size_t buflen, int type)
5750{
5751 if (security_ismaclabel(key))
5752 return nfs4_get_security_label(dentry->d_inode, buf, buflen);
5753 return -EOPNOTSUPP;
5754}
5755
5756static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
5757 size_t list_len, const char *name,
5758 size_t name_len, int type)
5759{
5760 size_t len = 0;
5761
5762 if (nfs_server_capable(dentry->d_inode, NFS_CAP_SECURITY_LABEL)) {
5763 len = security_inode_listsecurity(dentry->d_inode, NULL, 0);
5764 if (list && len <= list_len)
5765 security_inode_listsecurity(dentry->d_inode, list, len);
5766 }
5767 return len;
5768}
5769
5770static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
5771 .prefix = XATTR_SECURITY_PREFIX,
5772 .list = nfs4_xattr_list_nfs4_label,
5773 .get = nfs4_xattr_get_nfs4_label,
5774 .set = nfs4_xattr_set_nfs4_label,
5775};
5776#endif
5777
5778
Andy Adamson533eb462011-06-13 18:25:56 -04005779/*
5780 * nfs_fhget will use either the mounted_on_fileid or the fileid
5781 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005782static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5783{
Andy Adamson533eb462011-06-13 18:25:56 -04005784 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5785 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5786 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005787 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005788 return;
5789
5790 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005791 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005792 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5793 fattr->nlink = 2;
5794}
5795
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005796static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5797 const struct qstr *name,
5798 struct nfs4_fs_locations *fs_locations,
5799 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005800{
5801 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04005802 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005803 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005804 };
5805 struct nfs4_fs_locations_arg args = {
5806 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005807 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005808 .page = page,
5809 .bitmask = bitmask,
5810 };
Benny Halevy22958462009-04-01 09:22:02 -04005811 struct nfs4_fs_locations_res res = {
5812 .fs_locations = fs_locations,
5813 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005814 struct rpc_message msg = {
5815 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5816 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005817 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005818 };
5819 int status;
5820
Harvey Harrison3110ff82008-05-02 13:42:44 -07005821 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005822
5823 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5824 * is not supported */
5825 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5826 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5827 else
5828 bitmask[0] |= FATTR4_WORD0_FILEID;
5829
Trond Myklebustc228fd32007-01-13 02:28:11 -05005830 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005831 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005832 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005833 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005834 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005835 return status;
5836}
5837
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005838int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5839 const struct qstr *name,
5840 struct nfs4_fs_locations *fs_locations,
5841 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005842{
5843 struct nfs4_exception exception = { };
5844 int err;
5845 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04005846 err = _nfs4_proc_fs_locations(client, dir, name,
5847 fs_locations, page);
5848 trace_nfs4_get_fs_locations(dir, name, err);
5849 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005850 &exception);
5851 } while (exception.retry);
5852 return err;
5853}
5854
Andy Adamson5ec16a82013-08-08 10:57:55 -04005855/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04005856 * If 'use_integrity' is true and the state managment nfs_client
5857 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
5858 * and the machine credential as per RFC3530bis and RFC5661 Security
5859 * Considerations sections. Otherwise, just use the user cred with the
5860 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04005861 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04005862static 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 +00005863{
5864 int status;
5865 struct nfs4_secinfo_arg args = {
5866 .dir_fh = NFS_FH(dir),
5867 .name = name,
5868 };
5869 struct nfs4_secinfo_res res = {
5870 .flavors = flavors,
5871 };
5872 struct rpc_message msg = {
5873 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5874 .rpc_argp = &args,
5875 .rpc_resp = &res,
5876 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04005877 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
5878
5879 if (use_integrity) {
5880 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
5881 msg.rpc_cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
5882 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005883
5884 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04005885 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
5886 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005887 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04005888
5889 if (msg.rpc_cred)
5890 put_rpccred(msg.rpc_cred);
5891
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005892 return status;
5893}
5894
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005895int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5896 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005897{
5898 struct nfs4_exception exception = { };
5899 int err;
5900 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04005901 err = -NFS4ERR_WRONGSEC;
5902
5903 /* try to use integrity protection with machine cred */
5904 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
5905 err = _nfs4_proc_secinfo(dir, name, flavors, true);
5906
5907 /*
5908 * if unable to use integrity protection, or SECINFO with
5909 * integrity protection returns NFS4ERR_WRONGSEC (which is
5910 * disallowed by spec, but exists in deployed servers) use
5911 * the current filesystem's rpc_client and the user cred.
5912 */
5913 if (err == -NFS4ERR_WRONGSEC)
5914 err = _nfs4_proc_secinfo(dir, name, flavors, false);
5915
Trond Myklebust078ea3d2013-08-12 16:45:55 -04005916 trace_nfs4_secinfo(dir, name, err);
5917 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005918 &exception);
5919 } while (exception.retry);
5920 return err;
5921}
5922
Andy Adamson557134a2009-04-01 09:21:53 -04005923#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005924/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005925 * Check the exchange flags returned by the server for invalid flags, having
5926 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5927 * DS flags set.
5928 */
5929static int nfs4_check_cl_exchange_flags(u32 flags)
5930{
5931 if (flags & ~EXCHGID4_FLAG_MASK_R)
5932 goto out_inval;
5933 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5934 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5935 goto out_inval;
5936 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5937 goto out_inval;
5938 return NFS_OK;
5939out_inval:
5940 return -NFS4ERR_INVAL;
5941}
5942
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005943static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005944nfs41_same_server_scope(struct nfs41_server_scope *a,
5945 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005946{
5947 if (a->server_scope_sz == b->server_scope_sz &&
5948 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5949 return true;
5950
5951 return false;
5952}
5953
Andy Adamson357f54d2010-12-14 10:11:57 -05005954/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005955 * nfs4_proc_bind_conn_to_session()
5956 *
5957 * The 4.1 client currently uses the same TCP connection for the
5958 * fore and backchannel.
5959 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005960int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005961{
5962 int status;
5963 struct nfs41_bind_conn_to_session_res res;
5964 struct rpc_message msg = {
5965 .rpc_proc =
5966 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5967 .rpc_argp = clp,
5968 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005969 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005970 };
5971
5972 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005973
5974 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5975 if (unlikely(res.session == NULL)) {
5976 status = -ENOMEM;
5977 goto out;
5978 }
5979
5980 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005981 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005982 if (status == 0) {
5983 if (memcmp(res.session->sess_id.data,
5984 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5985 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5986 status = -EIO;
5987 goto out_session;
5988 }
5989 if (res.dir != NFS4_CDFS4_BOTH) {
5990 dprintk("NFS: %s: Unexpected direction from server\n",
5991 __func__);
5992 status = -EIO;
5993 goto out_session;
5994 }
5995 if (res.use_conn_in_rdma_mode) {
5996 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5997 __func__);
5998 status = -EIO;
5999 goto out_session;
6000 }
6001 }
6002out_session:
6003 kfree(res.session);
6004out:
6005 dprintk("<-- %s status= %d\n", __func__, status);
6006 return status;
6007}
6008
6009/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04006010 * nfs4_proc_exchange_id()
6011 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006012 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6013 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04006014 * Since the clientid has expired, all compounds using sessions
6015 * associated with the stale clientid will be returning
6016 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
6017 * be in some phase of session reset.
6018 */
Andy Adamson4d643d12009-12-04 15:52:24 -05006019int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04006020{
6021 nfs4_verifier verifier;
6022 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05006023 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04006024 .client = clp,
Trond Myklebust4f0b4292013-05-20 12:24:03 -04006025 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6026 EXCHGID4_FLAG_BIND_PRINC_STATEID,
Benny Halevy99fe60d2009-04-01 09:22:29 -04006027 };
6028 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04006029 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04006030 };
6031 int status;
6032 struct rpc_message msg = {
6033 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
6034 .rpc_argp = &args,
6035 .rpc_resp = &res,
6036 .rpc_cred = cred,
6037 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04006038
Chuck Leverf0920752012-05-21 22:45:41 -04006039 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04006040 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
6041 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006042 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
6043 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6044 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006045
Chuck Leveracdeb692012-05-21 22:46:16 -04006046 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006047 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04006048 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006049 status = -ENOMEM;
6050 goto out;
6051 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006052
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006053 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006054 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006055 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04006056 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04006057 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04006058 }
6059
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006060 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006061 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006062 status = -ENOMEM;
6063 goto out_server_scope;
6064 }
6065
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006066 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006067 trace_nfs4_exchange_id(clp, status);
Chuck Lever177313f2012-05-21 22:44:58 -04006068 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04006069 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006070
Chuck Lever177313f2012-05-21 22:44:58 -04006071 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04006072 clp->cl_clientid = res.clientid;
6073 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
6074 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
6075 clp->cl_seqid = res.seqid;
6076
Chuck Leveracdeb692012-05-21 22:46:16 -04006077 kfree(clp->cl_serverowner);
6078 clp->cl_serverowner = res.server_owner;
6079 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04006080
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006081 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04006082 kfree(clp->cl_implid);
6083 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006084
Chuck Lever177313f2012-05-21 22:44:58 -04006085 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006086 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006087 res.server_scope)) {
6088 dprintk("%s: server_scope mismatch detected\n",
6089 __func__);
6090 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006091 kfree(clp->cl_serverscope);
6092 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006093 }
6094
Chuck Lever177313f2012-05-21 22:44:58 -04006095 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006096 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006097 goto out;
6098 }
Trond Myklebust32b01312012-05-26 13:41:04 -04006099 } else
6100 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006101
Chuck Leveracdeb692012-05-21 22:46:16 -04006102out_server_owner:
6103 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006104out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006105 kfree(res.server_scope);
6106out:
Chuck Lever177313f2012-05-21 22:44:58 -04006107 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006108 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006109 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006110 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04006111 clp->cl_implid->date.seconds,
6112 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006113 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006114 return status;
6115}
6116
Trond Myklebust66245532012-05-25 17:18:09 -04006117static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
6118 struct rpc_cred *cred)
6119{
6120 struct rpc_message msg = {
6121 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
6122 .rpc_argp = clp,
6123 .rpc_cred = cred,
6124 };
6125 int status;
6126
6127 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006128 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04006129 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04006130 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04006131 "DESTROY_CLIENTID.", status, clp->cl_hostname);
6132 return status;
6133}
6134
6135static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
6136 struct rpc_cred *cred)
6137{
6138 unsigned int loop;
6139 int ret;
6140
6141 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
6142 ret = _nfs4_proc_destroy_clientid(clp, cred);
6143 switch (ret) {
6144 case -NFS4ERR_DELAY:
6145 case -NFS4ERR_CLIENTID_BUSY:
6146 ssleep(1);
6147 break;
6148 default:
6149 return ret;
6150 }
6151 }
6152 return 0;
6153}
6154
6155int nfs4_destroy_clientid(struct nfs_client *clp)
6156{
6157 struct rpc_cred *cred;
6158 int ret = 0;
6159
6160 if (clp->cl_mvops->minor_version < 1)
6161 goto out;
6162 if (clp->cl_exchange_flags == 0)
6163 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04006164 if (clp->cl_preserve_clid)
6165 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04006166 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04006167 ret = nfs4_proc_destroy_clientid(clp, cred);
6168 if (cred)
6169 put_rpccred(cred);
6170 switch (ret) {
6171 case 0:
6172 case -NFS4ERR_STALE_CLIENTID:
6173 clp->cl_exchange_flags = 0;
6174 }
6175out:
6176 return ret;
6177}
6178
Andy Adamson2050f0c2009-04-01 09:22:30 -04006179struct nfs4_get_lease_time_data {
6180 struct nfs4_get_lease_time_args *args;
6181 struct nfs4_get_lease_time_res *res;
6182 struct nfs_client *clp;
6183};
6184
6185static void nfs4_get_lease_time_prepare(struct rpc_task *task,
6186 void *calldata)
6187{
Andy Adamson2050f0c2009-04-01 09:22:30 -04006188 struct nfs4_get_lease_time_data *data =
6189 (struct nfs4_get_lease_time_data *)calldata;
6190
6191 dprintk("--> %s\n", __func__);
6192 /* just setup sequence, do not trigger session recovery
6193 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006194 nfs41_setup_sequence(data->clp->cl_session,
6195 &data->args->la_seq_args,
6196 &data->res->lr_seq_res,
6197 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006198 dprintk("<-- %s\n", __func__);
6199}
6200
6201/*
6202 * Called from nfs4_state_manager thread for session setup, so don't recover
6203 * from sequence operation or clientid errors.
6204 */
6205static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
6206{
6207 struct nfs4_get_lease_time_data *data =
6208 (struct nfs4_get_lease_time_data *)calldata;
6209
6210 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006211 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
6212 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04006213 switch (task->tk_status) {
6214 case -NFS4ERR_DELAY:
6215 case -NFS4ERR_GRACE:
6216 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
6217 rpc_delay(task, NFS4_POLL_RETRY_MIN);
6218 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006219 /* fall through */
6220 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006221 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006222 return;
6223 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04006224 dprintk("<-- %s\n", __func__);
6225}
6226
Trond Myklebust17280172012-03-11 13:11:00 -04006227static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04006228 .rpc_call_prepare = nfs4_get_lease_time_prepare,
6229 .rpc_call_done = nfs4_get_lease_time_done,
6230};
6231
6232int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
6233{
6234 struct rpc_task *task;
6235 struct nfs4_get_lease_time_args args;
6236 struct nfs4_get_lease_time_res res = {
6237 .lr_fsinfo = fsinfo,
6238 };
6239 struct nfs4_get_lease_time_data data = {
6240 .args = &args,
6241 .res = &res,
6242 .clp = clp,
6243 };
6244 struct rpc_message msg = {
6245 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
6246 .rpc_argp = &args,
6247 .rpc_resp = &res,
6248 };
6249 struct rpc_task_setup task_setup = {
6250 .rpc_client = clp->cl_rpcclient,
6251 .rpc_message = &msg,
6252 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006253 .callback_data = &data,
6254 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04006255 };
6256 int status;
6257
Chuck Levera9c92d62013-08-09 12:48:18 -04006258 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006259 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006260 dprintk("--> %s\n", __func__);
6261 task = rpc_run_task(&task_setup);
6262
6263 if (IS_ERR(task))
6264 status = PTR_ERR(task);
6265 else {
6266 status = task->tk_status;
6267 rpc_put_task(task);
6268 }
6269 dprintk("<-- %s return %d\n", __func__, status);
6270
6271 return status;
6272}
6273
Andy Adamsonfc931582009-04-01 09:22:31 -04006274/*
6275 * Initialize the values to be used by the client in CREATE_SESSION
6276 * If nfs4_init_session set the fore channel request and response sizes,
6277 * use them.
6278 *
6279 * Set the back channel max_resp_sz_cached to zero to force the client to
6280 * always set csa_cachethis to FALSE because the current implementation
6281 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
6282 */
6283static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
6284{
Andy Adamson18aad3d2013-06-26 12:21:49 -04006285 unsigned int max_rqst_sz, max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006286
Andy Adamson18aad3d2013-06-26 12:21:49 -04006287 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
6288 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
6289
Andy Adamsonfc931582009-04-01 09:22:31 -04006290 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04006291 args->fc_attrs.max_rqst_sz = max_rqst_sz;
6292 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006293 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05006294 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04006295
6296 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05006297 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04006298 __func__,
6299 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05006300 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04006301
6302 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04006303 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
6304 args->bc_attrs.max_resp_sz = PAGE_SIZE;
6305 args->bc_attrs.max_resp_sz_cached = 0;
6306 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
6307 args->bc_attrs.max_reqs = 1;
6308
6309 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
6310 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
6311 __func__,
6312 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
6313 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
6314 args->bc_attrs.max_reqs);
6315}
6316
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006317static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04006318{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006319 struct nfs4_channel_attrs *sent = &args->fc_attrs;
6320 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
6321
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006322 if (rcvd->max_resp_sz > sent->max_resp_sz)
6323 return -EINVAL;
6324 /*
6325 * Our requested max_ops is the minimum we need; we're not
6326 * prepared to break up compounds into smaller pieces than that.
6327 * So, no point even trying to continue if the server won't
6328 * cooperate:
6329 */
6330 if (rcvd->max_ops < sent->max_ops)
6331 return -EINVAL;
6332 if (rcvd->max_reqs == 0)
6333 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006334 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
6335 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006336 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04006337}
6338
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006339static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
6340{
6341 struct nfs4_channel_attrs *sent = &args->bc_attrs;
6342 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04006343
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006344 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
6345 return -EINVAL;
6346 if (rcvd->max_resp_sz < sent->max_resp_sz)
6347 return -EINVAL;
6348 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
6349 return -EINVAL;
6350 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006351 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006352 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006353 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006354 return -EINVAL;
6355 return 0;
6356}
Andy Adamson8d353012009-04-01 09:22:32 -04006357
Andy Adamson8d353012009-04-01 09:22:32 -04006358static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
6359 struct nfs4_session *session)
6360{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006361 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04006362
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006363 ret = nfs4_verify_fore_channel_attrs(args, session);
6364 if (ret)
6365 return ret;
6366 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04006367}
6368
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006369static int _nfs4_proc_create_session(struct nfs_client *clp,
6370 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04006371{
6372 struct nfs4_session *session = clp->cl_session;
6373 struct nfs41_create_session_args args = {
6374 .client = clp,
6375 .cb_program = NFS4_CALLBACK,
6376 };
6377 struct nfs41_create_session_res res = {
6378 .client = clp,
6379 };
6380 struct rpc_message msg = {
6381 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
6382 .rpc_argp = &args,
6383 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006384 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04006385 };
6386 int status;
6387
6388 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04006389 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04006390
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006391 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006392 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04006393
Trond Myklebust43095d32012-11-20 11:13:12 -05006394 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04006395 /* Verify the session's negotiated channel_attrs values */
6396 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04006397 /* Increment the clientid slot sequence id */
6398 clp->cl_seqid++;
6399 }
6400
6401 return status;
6402}
6403
6404/*
6405 * Issues a CREATE_SESSION operation to the server.
6406 * It is the responsibility of the caller to verify the session is
6407 * expired before calling this routine.
6408 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006409int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04006410{
6411 int status;
6412 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04006413 struct nfs4_session *session = clp->cl_session;
6414
6415 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
6416
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006417 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04006418 if (status)
6419 goto out;
6420
Andy Adamsonaacd5532011-11-09 13:58:21 -05006421 /* Init or reset the session slot tables */
6422 status = nfs4_setup_session_slot_tables(session);
6423 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04006424 if (status)
6425 goto out;
6426
6427 ptr = (unsigned *)&session->sess_id.data[0];
6428 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
6429 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04006430out:
6431 dprintk("<-- %s\n", __func__);
6432 return status;
6433}
6434
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006435/*
6436 * Issue the over-the-wire RPC DESTROY_SESSION.
6437 * The caller must serialize access to this routine.
6438 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006439int nfs4_proc_destroy_session(struct nfs4_session *session,
6440 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006441{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006442 struct rpc_message msg = {
6443 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
6444 .rpc_argp = session,
6445 .rpc_cred = cred,
6446 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006447 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006448
6449 dprintk("--> nfs4_proc_destroy_session\n");
6450
6451 /* session is still being setup */
6452 if (session->clp->cl_cons_state != NFS_CS_READY)
6453 return status;
6454
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006455 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006456 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006457
6458 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04006459 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006460 "Session has been destroyed regardless...\n", status);
6461
6462 dprintk("<-- nfs4_proc_destroy_session\n");
6463 return status;
6464}
6465
Trond Myklebust7b38c362012-05-23 13:23:31 -04006466/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006467 * Renew the cl_session lease.
6468 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006469struct nfs4_sequence_data {
6470 struct nfs_client *clp;
6471 struct nfs4_sequence_args args;
6472 struct nfs4_sequence_res res;
6473};
6474
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006475static void nfs41_sequence_release(void *data)
6476{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006477 struct nfs4_sequence_data *calldata = data;
6478 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006479
Alexandros Batsakis71358402010-02-05 03:45:05 -08006480 if (atomic_read(&clp->cl_count) > 1)
6481 nfs4_schedule_state_renewal(clp);
6482 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006483 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006484}
6485
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006486static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6487{
6488 switch(task->tk_status) {
6489 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006490 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6491 return -EAGAIN;
6492 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006493 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006494 }
6495 return 0;
6496}
6497
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006498static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006499{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006500 struct nfs4_sequence_data *calldata = data;
6501 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006502
Trond Myklebust14516c32010-07-31 14:29:06 -04006503 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6504 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006505
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006506 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006507 if (task->tk_status < 0) {
6508 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006509 if (atomic_read(&clp->cl_count) == 1)
6510 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006511
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006512 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6513 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006514 return;
6515 }
6516 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006517 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006518out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006519 dprintk("<-- %s\n", __func__);
6520}
6521
6522static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6523{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006524 struct nfs4_sequence_data *calldata = data;
6525 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006526 struct nfs4_sequence_args *args;
6527 struct nfs4_sequence_res *res;
6528
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006529 args = task->tk_msg.rpc_argp;
6530 res = task->tk_msg.rpc_resp;
6531
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006532 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006533}
6534
6535static const struct rpc_call_ops nfs41_sequence_ops = {
6536 .rpc_call_done = nfs41_sequence_call_done,
6537 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006538 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006539};
6540
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006541static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
6542 struct rpc_cred *cred,
6543 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006544{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006545 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006546 struct rpc_message msg = {
6547 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6548 .rpc_cred = cred,
6549 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006550 struct rpc_task_setup task_setup_data = {
6551 .rpc_client = clp->cl_rpcclient,
6552 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006553 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04006554 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006555 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006556
Alexandros Batsakis71358402010-02-05 03:45:05 -08006557 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006558 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006559 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006560 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006561 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006562 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006563 }
Chuck Levera9c92d62013-08-09 12:48:18 -04006564 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006565 if (is_privileged)
6566 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006567 msg.rpc_argp = &calldata->args;
6568 msg.rpc_resp = &calldata->res;
6569 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006570 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006571
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006572 return rpc_run_task(&task_setup_data);
6573}
6574
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006575static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006576{
6577 struct rpc_task *task;
6578 int ret = 0;
6579
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006580 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6581 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006582 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006583 if (IS_ERR(task))
6584 ret = PTR_ERR(task);
6585 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006586 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006587 dprintk("<-- %s status=%d\n", __func__, ret);
6588 return ret;
6589}
6590
6591static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6592{
6593 struct rpc_task *task;
6594 int ret;
6595
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006596 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006597 if (IS_ERR(task)) {
6598 ret = PTR_ERR(task);
6599 goto out;
6600 }
6601 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006602 if (!ret) {
6603 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6604
6605 if (task->tk_status == 0)
6606 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006607 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006608 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006609 rpc_put_task(task);
6610out:
6611 dprintk("<-- %s status=%d\n", __func__, ret);
6612 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006613}
6614
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006615struct nfs4_reclaim_complete_data {
6616 struct nfs_client *clp;
6617 struct nfs41_reclaim_complete_args arg;
6618 struct nfs41_reclaim_complete_res res;
6619};
6620
6621static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6622{
6623 struct nfs4_reclaim_complete_data *calldata = data;
6624
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006625 nfs41_setup_sequence(calldata->clp->cl_session,
6626 &calldata->arg.seq_args,
6627 &calldata->res.seq_res,
6628 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006629}
6630
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006631static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6632{
6633 switch(task->tk_status) {
6634 case 0:
6635 case -NFS4ERR_COMPLETE_ALREADY:
6636 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6637 break;
6638 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006639 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006640 /* fall through */
6641 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006642 return -EAGAIN;
6643 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006644 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006645 }
6646 return 0;
6647}
6648
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006649static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6650{
6651 struct nfs4_reclaim_complete_data *calldata = data;
6652 struct nfs_client *clp = calldata->clp;
6653 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6654
6655 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006656 if (!nfs41_sequence_done(task, res))
6657 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006658
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006659 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006660 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6661 rpc_restart_call_prepare(task);
6662 return;
6663 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006664 dprintk("<-- %s\n", __func__);
6665}
6666
6667static void nfs4_free_reclaim_complete_data(void *data)
6668{
6669 struct nfs4_reclaim_complete_data *calldata = data;
6670
6671 kfree(calldata);
6672}
6673
6674static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6675 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6676 .rpc_call_done = nfs4_reclaim_complete_done,
6677 .rpc_release = nfs4_free_reclaim_complete_data,
6678};
6679
6680/*
6681 * Issue a global reclaim complete.
6682 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04006683static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
6684 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006685{
6686 struct nfs4_reclaim_complete_data *calldata;
6687 struct rpc_task *task;
6688 struct rpc_message msg = {
6689 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04006690 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006691 };
6692 struct rpc_task_setup task_setup_data = {
6693 .rpc_client = clp->cl_rpcclient,
6694 .rpc_message = &msg,
6695 .callback_ops = &nfs4_reclaim_complete_call_ops,
6696 .flags = RPC_TASK_ASYNC,
6697 };
6698 int status = -ENOMEM;
6699
6700 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006701 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006702 if (calldata == NULL)
6703 goto out;
6704 calldata->clp = clp;
6705 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006706
Chuck Levera9c92d62013-08-09 12:48:18 -04006707 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006708 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006709 msg.rpc_argp = &calldata->arg;
6710 msg.rpc_resp = &calldata->res;
6711 task_setup_data.callback_data = calldata;
6712 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006713 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006714 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006715 goto out;
6716 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006717 status = nfs4_wait_for_completion_rpc_task(task);
6718 if (status == 0)
6719 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006720 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006721 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006722out:
6723 dprintk("<-- %s status=%d\n", __func__, status);
6724 return status;
6725}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006726
6727static void
6728nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6729{
6730 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006731 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006732 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006733
6734 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006735 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6736 * right now covering the LAYOUTGET we are about to send.
6737 * However, that is not so catastrophic, and there seems
6738 * to be no way to prevent it completely.
6739 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006740 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006741 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006742 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006743 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6744 NFS_I(lgp->args.inode)->layout,
6745 lgp->args.ctx->state)) {
6746 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00006747 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006748}
6749
6750static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6751{
6752 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006753 struct inode *inode = lgp->args.inode;
6754 struct nfs_server *server = NFS_SERVER(inode);
6755 struct pnfs_layout_hdr *lo;
6756 struct nfs4_state *state = NULL;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006757 unsigned long timeo, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006758
6759 dprintk("--> %s\n", __func__);
6760
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006761 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006762 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006763
6764 switch (task->tk_status) {
6765 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006766 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006767 case -NFS4ERR_LAYOUTTRYLATER:
6768 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05006769 timeo = rpc_get_timeout(task->tk_client);
6770 giveup = lgp->args.timestamp + timeo;
6771 if (time_after(giveup, jiffies))
6772 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006773 break;
6774 case -NFS4ERR_EXPIRED:
6775 case -NFS4ERR_BAD_STATEID:
6776 spin_lock(&inode->i_lock);
6777 lo = NFS_I(inode)->layout;
6778 if (!lo || list_empty(&lo->plh_segs)) {
6779 spin_unlock(&inode->i_lock);
6780 /* If the open stateid was bad, then recover it. */
6781 state = lgp->args.ctx->state;
6782 } else {
6783 LIST_HEAD(head);
6784
6785 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6786 spin_unlock(&inode->i_lock);
6787 /* Mark the bad layout state as invalid, then
6788 * retry using the open stateid. */
6789 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006790 }
6791 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006792 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6793 rpc_restart_call_prepare(task);
6794out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006795 dprintk("<-- %s\n", __func__);
6796}
6797
Idan Kedar85541162012-08-02 11:47:10 +03006798static size_t max_response_pages(struct nfs_server *server)
6799{
6800 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6801 return nfs_page_array_len(0, max_resp_sz);
6802}
6803
6804static void nfs4_free_pages(struct page **pages, size_t size)
6805{
6806 int i;
6807
6808 if (!pages)
6809 return;
6810
6811 for (i = 0; i < size; i++) {
6812 if (!pages[i])
6813 break;
6814 __free_page(pages[i]);
6815 }
6816 kfree(pages);
6817}
6818
6819static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6820{
6821 struct page **pages;
6822 int i;
6823
6824 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6825 if (!pages) {
6826 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6827 return NULL;
6828 }
6829
6830 for (i = 0; i < size; i++) {
6831 pages[i] = alloc_page(gfp_flags);
6832 if (!pages[i]) {
6833 dprintk("%s: failed to allocate page\n", __func__);
6834 nfs4_free_pages(pages, size);
6835 return NULL;
6836 }
6837 }
6838
6839 return pages;
6840}
6841
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006842static void nfs4_layoutget_release(void *calldata)
6843{
6844 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006845 struct inode *inode = lgp->args.inode;
6846 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006847 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006848
6849 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006850 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006851 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006852 put_nfs_open_context(lgp->args.ctx);
6853 kfree(calldata);
6854 dprintk("<-- %s\n", __func__);
6855}
6856
6857static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6858 .rpc_call_prepare = nfs4_layoutget_prepare,
6859 .rpc_call_done = nfs4_layoutget_done,
6860 .rpc_release = nfs4_layoutget_release,
6861};
6862
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006863struct pnfs_layout_segment *
6864nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006865{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006866 struct inode *inode = lgp->args.inode;
6867 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006868 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006869 struct rpc_task *task;
6870 struct rpc_message msg = {
6871 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6872 .rpc_argp = &lgp->args,
6873 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04006874 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006875 };
6876 struct rpc_task_setup task_setup_data = {
6877 .rpc_client = server->client,
6878 .rpc_message = &msg,
6879 .callback_ops = &nfs4_layoutget_call_ops,
6880 .callback_data = lgp,
6881 .flags = RPC_TASK_ASYNC,
6882 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006883 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006884 int status = 0;
6885
6886 dprintk("--> %s\n", __func__);
6887
Idan Kedar85541162012-08-02 11:47:10 +03006888 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6889 if (!lgp->args.layout.pages) {
6890 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006891 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006892 }
6893 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006894 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03006895
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006896 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006897 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04006898 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006899
6900 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
6901 pnfs_get_layout_hdr(NFS_I(inode)->layout);
6902
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006903 task = rpc_run_task(&task_setup_data);
6904 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006905 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006906 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006907 if (status == 0)
6908 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04006909 trace_nfs4_layoutget(lgp->args.ctx,
6910 &lgp->args.range,
6911 &lgp->res.range,
6912 status);
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05006913 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
6914 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006915 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006916 rpc_put_task(task);
6917 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006918 if (status)
6919 return ERR_PTR(status);
6920 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006921}
6922
Benny Halevycbe82602011-05-22 19:52:37 +03006923static void
6924nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6925{
6926 struct nfs4_layoutreturn *lrp = calldata;
6927
6928 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006929 nfs41_setup_sequence(lrp->clp->cl_session,
6930 &lrp->args.seq_args,
6931 &lrp->res.seq_res,
6932 task);
Benny Halevycbe82602011-05-22 19:52:37 +03006933}
6934
6935static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6936{
6937 struct nfs4_layoutreturn *lrp = calldata;
6938 struct nfs_server *server;
6939
6940 dprintk("--> %s\n", __func__);
6941
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006942 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03006943 return;
6944
6945 server = NFS_SERVER(lrp->args.inode);
6946 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006947 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006948 return;
6949 }
Benny Halevycbe82602011-05-22 19:52:37 +03006950 dprintk("<-- %s\n", __func__);
6951}
6952
6953static void nfs4_layoutreturn_release(void *calldata)
6954{
6955 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006956 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006957
6958 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006959 spin_lock(&lo->plh_inode->i_lock);
6960 if (lrp->res.lrs_present)
6961 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6962 lo->plh_block_lgets--;
6963 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006964 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006965 kfree(calldata);
6966 dprintk("<-- %s\n", __func__);
6967}
6968
6969static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6970 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6971 .rpc_call_done = nfs4_layoutreturn_done,
6972 .rpc_release = nfs4_layoutreturn_release,
6973};
6974
6975int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6976{
6977 struct rpc_task *task;
6978 struct rpc_message msg = {
6979 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6980 .rpc_argp = &lrp->args,
6981 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04006982 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03006983 };
6984 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04006985 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03006986 .rpc_message = &msg,
6987 .callback_ops = &nfs4_layoutreturn_call_ops,
6988 .callback_data = lrp,
6989 };
6990 int status;
6991
6992 dprintk("--> %s\n", __func__);
Chuck Levera9c92d62013-08-09 12:48:18 -04006993 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006994 task = rpc_run_task(&task_setup_data);
6995 if (IS_ERR(task))
6996 return PTR_ERR(task);
6997 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04006998 trace_nfs4_layoutreturn(lrp->args.inode, status);
Benny Halevycbe82602011-05-22 19:52:37 +03006999 dprintk("<-- %s status=%d\n", __func__, status);
7000 rpc_put_task(task);
7001 return status;
7002}
7003
Andy Adamson7f11d8d2011-07-30 20:52:35 -04007004/*
7005 * Retrieve the list of Data Server devices from the MDS.
7006 */
7007static int _nfs4_getdevicelist(struct nfs_server *server,
7008 const struct nfs_fh *fh,
7009 struct pnfs_devicelist *devlist)
7010{
7011 struct nfs4_getdevicelist_args args = {
7012 .fh = fh,
7013 .layoutclass = server->pnfs_curr_ld->id,
7014 };
7015 struct nfs4_getdevicelist_res res = {
7016 .devlist = devlist,
7017 };
7018 struct rpc_message msg = {
7019 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
7020 .rpc_argp = &args,
7021 .rpc_resp = &res,
7022 };
7023 int status;
7024
7025 dprintk("--> %s\n", __func__);
7026 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
7027 &res.seq_res, 0);
7028 dprintk("<-- %s status=%d\n", __func__, status);
7029 return status;
7030}
7031
7032int nfs4_proc_getdevicelist(struct nfs_server *server,
7033 const struct nfs_fh *fh,
7034 struct pnfs_devicelist *devlist)
7035{
7036 struct nfs4_exception exception = { };
7037 int err;
7038
7039 do {
7040 err = nfs4_handle_exception(server,
7041 _nfs4_getdevicelist(server, fh, devlist),
7042 &exception);
7043 } while (exception.retry);
7044
7045 dprintk("%s: err=%d, num_devs=%u\n", __func__,
7046 err, devlist->num_devs);
7047
7048 return err;
7049}
7050EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
7051
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007052static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007053_nfs4_proc_getdeviceinfo(struct nfs_server *server,
7054 struct pnfs_device *pdev,
7055 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007056{
7057 struct nfs4_getdeviceinfo_args args = {
7058 .pdev = pdev,
7059 };
7060 struct nfs4_getdeviceinfo_res res = {
7061 .pdev = pdev,
7062 };
7063 struct rpc_message msg = {
7064 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
7065 .rpc_argp = &args,
7066 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007067 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007068 };
7069 int status;
7070
7071 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00007072 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007073 dprintk("<-- %s status=%d\n", __func__, status);
7074
7075 return status;
7076}
7077
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007078int nfs4_proc_getdeviceinfo(struct nfs_server *server,
7079 struct pnfs_device *pdev,
7080 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007081{
7082 struct nfs4_exception exception = { };
7083 int err;
7084
7085 do {
7086 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007087 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007088 &exception);
7089 } while (exception.retry);
7090 return err;
7091}
7092EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
7093
Andy Adamson863a3c62011-03-23 13:27:54 +00007094static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
7095{
7096 struct nfs4_layoutcommit_data *data = calldata;
7097 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007098 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00007099
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007100 nfs41_setup_sequence(session,
7101 &data->args.seq_args,
7102 &data->res.seq_res,
7103 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00007104}
7105
7106static void
7107nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
7108{
7109 struct nfs4_layoutcommit_data *data = calldata;
7110 struct nfs_server *server = NFS_SERVER(data->args.inode);
7111
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007112 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00007113 return;
7114
7115 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007116 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
7117 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
7118 case -NFS4ERR_BADLAYOUT: /* no layout */
7119 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00007120 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007121 break;
7122 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00007123 nfs_post_op_update_inode_force_wcc(data->args.inode,
7124 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007125 break;
7126 default:
7127 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
7128 rpc_restart_call_prepare(task);
7129 return;
7130 }
7131 }
Andy Adamson863a3c62011-03-23 13:27:54 +00007132}
7133
7134static void nfs4_layoutcommit_release(void *calldata)
7135{
7136 struct nfs4_layoutcommit_data *data = calldata;
7137
Andy Adamsondb29c082011-07-30 20:52:38 -04007138 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00007139 put_rpccred(data->cred);
7140 kfree(data);
7141}
7142
7143static const struct rpc_call_ops nfs4_layoutcommit_ops = {
7144 .rpc_call_prepare = nfs4_layoutcommit_prepare,
7145 .rpc_call_done = nfs4_layoutcommit_done,
7146 .rpc_release = nfs4_layoutcommit_release,
7147};
7148
7149int
Andy Adamsonef311532011-03-12 02:58:10 -05007150nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00007151{
7152 struct rpc_message msg = {
7153 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
7154 .rpc_argp = &data->args,
7155 .rpc_resp = &data->res,
7156 .rpc_cred = data->cred,
7157 };
7158 struct rpc_task_setup task_setup_data = {
7159 .task = &data->task,
7160 .rpc_client = NFS_CLIENT(data->args.inode),
7161 .rpc_message = &msg,
7162 .callback_ops = &nfs4_layoutcommit_ops,
7163 .callback_data = data,
7164 .flags = RPC_TASK_ASYNC,
7165 };
7166 struct rpc_task *task;
7167 int status = 0;
7168
7169 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
7170 "lbw: %llu inode %lu\n",
7171 data->task.tk_pid, sync,
7172 data->args.lastbytewritten,
7173 data->args.inode->i_ino);
7174
Chuck Levera9c92d62013-08-09 12:48:18 -04007175 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00007176 task = rpc_run_task(&task_setup_data);
7177 if (IS_ERR(task))
7178 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05007179 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00007180 goto out;
7181 status = nfs4_wait_for_completion_rpc_task(task);
7182 if (status != 0)
7183 goto out;
7184 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007185 trace_nfs4_layoutcommit(data->args.inode, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00007186out:
7187 dprintk("%s: status %d\n", __func__, status);
7188 rpc_put_task(task);
7189 return status;
7190}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007191
Andy Adamson97431202013-08-08 10:57:56 -04007192/**
7193 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
7194 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
7195 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007196static int
7197_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7198 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
7199{
7200 struct nfs41_secinfo_no_name_args args = {
7201 .style = SECINFO_STYLE_CURRENT_FH,
7202 };
7203 struct nfs4_secinfo_res res = {
7204 .flavors = flavors,
7205 };
7206 struct rpc_message msg = {
7207 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
7208 .rpc_argp = &args,
7209 .rpc_resp = &res,
7210 };
Andy Adamson97431202013-08-08 10:57:56 -04007211 return nfs4_call_sync(server->nfs_client->cl_rpcclient, server, &msg,
7212 &args.seq_args, &res.seq_res, 0);
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007213}
7214
7215static int
7216nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7217 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
7218{
7219 struct nfs4_exception exception = { };
7220 int err;
7221 do {
7222 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
7223 switch (err) {
7224 case 0:
7225 case -NFS4ERR_WRONGSEC:
7226 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007227 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007228 default:
7229 err = nfs4_handle_exception(server, err, &exception);
7230 }
7231 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007232out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007233 return err;
7234}
7235
7236static int
7237nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
7238 struct nfs_fsinfo *info)
7239{
7240 int err;
7241 struct page *page;
7242 rpc_authflavor_t flavor;
7243 struct nfs4_secinfo_flavors *flavors;
7244
7245 page = alloc_page(GFP_KERNEL);
7246 if (!page) {
7247 err = -ENOMEM;
7248 goto out;
7249 }
7250
7251 flavors = page_address(page);
7252 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
7253
7254 /*
7255 * Fall back on "guess and check" method if
7256 * the server doesn't support SECINFO_NO_NAME
7257 */
7258 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
7259 err = nfs4_find_root_sec(server, fhandle, info);
7260 goto out_freepage;
7261 }
7262 if (err)
7263 goto out_freepage;
7264
7265 flavor = nfs_find_best_sec(flavors);
7266 if (err == 0)
7267 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
7268
7269out_freepage:
7270 put_page(page);
7271 if (err == -EACCES)
7272 return -EPERM;
7273out:
7274 return err;
7275}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007276
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007277static int _nfs41_test_stateid(struct nfs_server *server,
7278 nfs4_stateid *stateid,
7279 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04007280{
7281 int status;
7282 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007283 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04007284 };
7285 struct nfs41_test_stateid_res res;
7286 struct rpc_message msg = {
7287 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
7288 .rpc_argp = &args,
7289 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007290 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04007291 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007292
Chuck Lever38527b12012-07-11 16:30:23 -04007293 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04007294 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007295 nfs4_set_sequence_privileged(&args.seq_args);
7296 status = nfs4_call_sync_sequence(server->client, server, &msg,
7297 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04007298 if (status != NFS_OK) {
7299 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04007300 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04007301 }
7302 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04007303 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04007304}
7305
Chuck Lever38527b12012-07-11 16:30:23 -04007306/**
7307 * nfs41_test_stateid - perform a TEST_STATEID operation
7308 *
7309 * @server: server / transport on which to perform the operation
7310 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007311 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04007312 *
7313 * Returns NFS_OK if the server recognizes that "stateid" is valid.
7314 * Otherwise a negative NFS4ERR value is returned if the operation
7315 * failed or the state ID is not currently valid.
7316 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007317static int nfs41_test_stateid(struct nfs_server *server,
7318 nfs4_stateid *stateid,
7319 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04007320{
7321 struct nfs4_exception exception = { };
7322 int err;
7323 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007324 err = _nfs41_test_stateid(server, stateid, cred);
Chuck Lever377e5072012-07-11 16:29:45 -04007325 if (err != -NFS4ERR_DELAY)
7326 break;
7327 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04007328 } while (exception.retry);
7329 return err;
7330}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007331
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007332struct nfs_free_stateid_data {
7333 struct nfs_server *server;
7334 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007335 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007336};
7337
7338static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
7339{
7340 struct nfs_free_stateid_data *data = calldata;
7341 nfs41_setup_sequence(nfs4_get_session(data->server),
7342 &data->args.seq_args,
7343 &data->res.seq_res,
7344 task);
7345}
7346
7347static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
7348{
7349 struct nfs_free_stateid_data *data = calldata;
7350
7351 nfs41_sequence_done(task, &data->res.seq_res);
7352
7353 switch (task->tk_status) {
7354 case -NFS4ERR_DELAY:
7355 if (nfs4_async_handle_error(task, data->server, NULL) == -EAGAIN)
7356 rpc_restart_call_prepare(task);
7357 }
7358}
7359
7360static void nfs41_free_stateid_release(void *calldata)
7361{
7362 kfree(calldata);
7363}
7364
Trond Myklebust17f26b12013-08-21 15:48:42 -04007365static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007366 .rpc_call_prepare = nfs41_free_stateid_prepare,
7367 .rpc_call_done = nfs41_free_stateid_done,
7368 .rpc_release = nfs41_free_stateid_release,
7369};
7370
7371static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
7372 nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007373 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007374 bool privileged)
7375{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007376 struct rpc_message msg = {
7377 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007378 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007379 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007380 struct rpc_task_setup task_setup = {
7381 .rpc_client = server->client,
7382 .rpc_message = &msg,
7383 .callback_ops = &nfs41_free_stateid_ops,
7384 .flags = RPC_TASK_ASYNC,
7385 };
7386 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007387
Chuck Lever38527b12012-07-11 16:30:23 -04007388 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007389 data = kmalloc(sizeof(*data), GFP_NOFS);
7390 if (!data)
7391 return ERR_PTR(-ENOMEM);
7392 data->server = server;
7393 nfs4_stateid_copy(&data->args.stateid, stateid);
7394
7395 task_setup.callback_data = data;
7396
7397 msg.rpc_argp = &data->args;
7398 msg.rpc_resp = &data->res;
Chuck Levera9c92d62013-08-09 12:48:18 -04007399 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007400 if (privileged)
7401 nfs4_set_sequence_privileged(&data->args.seq_args);
7402
7403 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007404}
7405
Chuck Lever38527b12012-07-11 16:30:23 -04007406/**
7407 * nfs41_free_stateid - perform a FREE_STATEID operation
7408 *
7409 * @server: server / transport on which to perform the operation
7410 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007411 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04007412 *
7413 * Returns NFS_OK if the server freed "stateid". Otherwise a
7414 * negative NFS4ERR value is returned.
7415 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007416static int nfs41_free_stateid(struct nfs_server *server,
7417 nfs4_stateid *stateid,
7418 struct rpc_cred *cred)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007419{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007420 struct rpc_task *task;
7421 int ret;
7422
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007423 task = _nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007424 if (IS_ERR(task))
7425 return PTR_ERR(task);
7426 ret = rpc_wait_for_completion_task(task);
7427 if (!ret)
7428 ret = task->tk_status;
7429 rpc_put_task(task);
7430 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007431}
Trond Myklebust36281ca2012-03-04 18:13:56 -05007432
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007433static int nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
7434{
7435 struct rpc_task *task;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007436 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007437
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007438 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007439 nfs4_free_lock_state(server, lsp);
7440 if (IS_ERR(task))
7441 return PTR_ERR(task);
7442 rpc_put_task(task);
7443 return 0;
7444}
7445
Trond Myklebust36281ca2012-03-04 18:13:56 -05007446static bool nfs41_match_stateid(const nfs4_stateid *s1,
7447 const nfs4_stateid *s2)
7448{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007449 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007450 return false;
7451
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007452 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007453 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007454 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007455 return true;
7456
7457 return false;
7458}
7459
Andy Adamson557134a2009-04-01 09:21:53 -04007460#endif /* CONFIG_NFS_V4_1 */
7461
Trond Myklebust36281ca2012-03-04 18:13:56 -05007462static bool nfs4_match_stateid(const nfs4_stateid *s1,
7463 const nfs4_stateid *s2)
7464{
Trond Myklebustf597c532012-03-04 18:13:56 -05007465 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05007466}
7467
7468
Trond Myklebust17280172012-03-11 13:11:00 -04007469static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007470 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007471 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007472 .recover_open = nfs4_open_reclaim,
7473 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04007474 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04007475 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007476};
7477
Andy Adamson591d71c2009-04-01 09:22:47 -04007478#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007479static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007480 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
7481 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
7482 .recover_open = nfs4_open_reclaim,
7483 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05007484 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007485 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04007486 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04007487};
7488#endif /* CONFIG_NFS_V4_1 */
7489
Trond Myklebust17280172012-03-11 13:11:00 -04007490static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007491 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007492 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007493 .recover_open = nfs4_open_expired,
7494 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04007495 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007496};
7497
Andy Adamson591d71c2009-04-01 09:22:47 -04007498#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007499static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007500 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
7501 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007502 .recover_open = nfs41_open_expired,
7503 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05007504 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04007505};
7506#endif /* CONFIG_NFS_V4_1 */
7507
Trond Myklebust17280172012-03-11 13:11:00 -04007508static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007509 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04007510 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007511 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04007512};
7513
7514#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007515static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007516 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04007517 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007518 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04007519};
7520#endif
7521
Trond Myklebust97dc1352010-06-16 09:52:26 -04007522static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
7523 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04007524 .init_caps = NFS_CAP_READDIRPLUS
7525 | NFS_CAP_ATOMIC_OPEN
7526 | NFS_CAP_CHANGE_ATTR
7527 | NFS_CAP_POSIX_LOCK,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007528 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007529 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007530 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007531 .free_lock_state = nfs4_release_lockowner,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007532 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
7533 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
7534 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007535};
7536
7537#if defined(CONFIG_NFS_V4_1)
7538static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
7539 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04007540 .init_caps = NFS_CAP_READDIRPLUS
7541 | NFS_CAP_ATOMIC_OPEN
7542 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04007543 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04007544 | NFS_CAP_STATEID_NFSV41
7545 | NFS_CAP_ATOMIC_OPEN_V1,
Bryan Schumaker104287c2012-11-12 14:13:13 -05007546 .call_sync = nfs4_call_sync_sequence,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007547 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007548 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007549 .free_lock_state = nfs41_free_lock_state,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007550 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7551 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7552 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007553};
7554#endif
7555
Steve Dickson42c2c422013-05-22 12:50:38 -04007556#if defined(CONFIG_NFS_V4_2)
7557static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
7558 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04007559 .init_caps = NFS_CAP_READDIRPLUS
7560 | NFS_CAP_ATOMIC_OPEN
7561 | NFS_CAP_CHANGE_ATTR
7562 | NFS_CAP_POSIX_LOCK
7563 | NFS_CAP_STATEID_NFSV41
7564 | NFS_CAP_ATOMIC_OPEN_V1,
Steve Dickson42c2c422013-05-22 12:50:38 -04007565 .call_sync = nfs4_call_sync_sequence,
7566 .match_stateid = nfs41_match_stateid,
7567 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04007568 .free_lock_state = nfs41_free_lock_state,
Steve Dickson42c2c422013-05-22 12:50:38 -04007569 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7570 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7571 .state_renewal_ops = &nfs41_state_renewal_ops,
7572};
7573#endif
7574
Trond Myklebust97dc1352010-06-16 09:52:26 -04007575const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
7576 [0] = &nfs_v4_0_minor_ops,
7577#if defined(CONFIG_NFS_V4_1)
7578 [1] = &nfs_v4_1_minor_ops,
7579#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04007580#if defined(CONFIG_NFS_V4_2)
7581 [2] = &nfs_v4_2_minor_ops,
7582#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04007583};
7584
Trond Myklebust17f26b12013-08-21 15:48:42 -04007585static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04007586 .create = nfs_create,
7587 .lookup = nfs_lookup,
7588 .atomic_open = nfs_atomic_open,
7589 .link = nfs_link,
7590 .unlink = nfs_unlink,
7591 .symlink = nfs_symlink,
7592 .mkdir = nfs_mkdir,
7593 .rmdir = nfs_rmdir,
7594 .mknod = nfs_mknod,
7595 .rename = nfs_rename,
7596 .permission = nfs_permission,
7597 .getattr = nfs_getattr,
7598 .setattr = nfs_setattr,
7599 .getxattr = generic_getxattr,
7600 .setxattr = generic_setxattr,
7601 .listxattr = generic_listxattr,
7602 .removexattr = generic_removexattr,
7603};
7604
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08007605static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007606 .permission = nfs_permission,
7607 .getattr = nfs_getattr,
7608 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007609 .getxattr = generic_getxattr,
7610 .setxattr = generic_setxattr,
7611 .listxattr = generic_listxattr,
7612 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007613};
7614
David Howells509de812006-08-22 20:06:11 -04007615const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007616 .version = 4, /* protocol version */
7617 .dentry_ops = &nfs4_dentry_operations,
7618 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007619 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04007620 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007621 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04007622 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04007623 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007624 .getattr = nfs4_proc_getattr,
7625 .setattr = nfs4_proc_setattr,
7626 .lookup = nfs4_proc_lookup,
7627 .access = nfs4_proc_access,
7628 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007629 .create = nfs4_proc_create,
7630 .remove = nfs4_proc_remove,
7631 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04007632 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007633 .unlink_done = nfs4_proc_unlink_done,
7634 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04007635 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04007636 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04007637 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007638 .link = nfs4_proc_link,
7639 .symlink = nfs4_proc_symlink,
7640 .mkdir = nfs4_proc_mkdir,
7641 .rmdir = nfs4_proc_remove,
7642 .readdir = nfs4_proc_readdir,
7643 .mknod = nfs4_proc_mknod,
7644 .statfs = nfs4_proc_statfs,
7645 .fsinfo = nfs4_proc_fsinfo,
7646 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04007647 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007648 .decode_dirent = nfs4_decode_dirent,
7649 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04007650 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04007651 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05007652 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007653 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04007654 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04007655 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007656 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007657 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04007658 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007659 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007660 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00007661 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04007662 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04007663 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04007664 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04007665 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04007666 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00007667 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04007668 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04007669 .create_server = nfs4_create_server,
7670 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007671};
7672
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007673static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7674 .prefix = XATTR_NAME_NFSV4_ACL,
7675 .list = nfs4_xattr_list_nfs4_acl,
7676 .get = nfs4_xattr_get_nfs4_acl,
7677 .set = nfs4_xattr_set_nfs4_acl,
7678};
7679
7680const struct xattr_handler *nfs4_xattr_handlers[] = {
7681 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04007682#ifdef CONFIG_NFS_V4_SECURITY_LABEL
7683 &nfs4_xattr_nfs4_label_handler,
7684#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007685 NULL
7686};
7687
Linus Torvalds1da177e2005-04-16 15:20:36 -07007688/*
7689 * Local variables:
7690 * c-basic-offset: 8
7691 * End:
7692 */