blob: 05742b81a2a878d42ff7e22c133d4c653d86dc5c [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
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400466#if defined(CONFIG_NFS_V4_1)
467
Trond Myklebustd185a332010-06-16 09:52:25 -0400468static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400469{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500470 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400471 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500472 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400473
Benny Halevydfb4f3092010-09-24 09:17:01 -0400474 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400475 /* just wake up the next guy waiting since
476 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500477 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500478 return;
Andy Adamson13615872009-04-01 09:22:17 -0400479 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500480 tbl = res->sr_slot->table;
481 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500482
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500483 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500484 /* Be nice to the server: try to ensure that the last transmitted
485 * value for highest_user_slotid <= target_highest_slotid
486 */
487 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
488 send_new_highest_used_slotid = true;
489
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500490 if (nfs41_wake_and_assign_slot(tbl, res->sr_slot)) {
491 send_new_highest_used_slotid = false;
492 goto out_unlock;
493 }
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500494 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500495
496 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
497 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500498out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500499 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400500 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500501 if (send_new_highest_used_slotid)
502 nfs41_server_notify_highest_slotid_update(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400503}
504
Trond Myklebust14516c32010-07-31 14:29:06 -0400505static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400506{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500507 struct nfs4_session *session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500508 struct nfs4_slot *slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400509 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500510 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500511 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400512
Bryan Schumaker468f8612011-04-18 15:57:32 -0400513 /* don't increment the sequence number if the task wasn't sent */
514 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400515 goto out;
516
Trond Myklebust933602e2012-11-16 12:12:38 -0500517 slot = res->sr_slot;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500518 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500519
Trond Myklebustac20d162012-12-15 15:36:07 -0500520 if (slot->interrupted) {
521 slot->interrupted = 0;
522 interrupted = true;
523 }
524
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400525 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500526 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400527 switch (res->sr_status) {
528 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400529 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500530 ++slot->seq_nr;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500531 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500532 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500533 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500534 if (res->sr_status_flags != 0)
535 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500536 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400537 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500538 case 1:
539 /*
540 * sr_status remains 1 if an RPC level error occurred.
541 * The server may or may not have processed the sequence
542 * operation..
543 * Mark the slot as having hosted an interrupted RPC call.
544 */
545 slot->interrupted = 1;
546 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400547 case -NFS4ERR_DELAY:
548 /* The server detected a resend of the RPC call and
549 * returned NFS4ERR_DELAY as per Section 2.10.6.2
550 * of RFC5661.
551 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500552 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400553 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500554 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500555 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400556 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500557 case -NFS4ERR_BADSLOT:
558 /*
559 * The slot id we used was probably retired. Try again
560 * using a different slot id.
561 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500562 goto retry_nowait;
563 case -NFS4ERR_SEQ_MISORDERED:
564 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500565 * Was the last operation on this sequence interrupted?
566 * If so, retry after bumping the sequence number.
567 */
568 if (interrupted) {
569 ++slot->seq_nr;
570 goto retry_nowait;
571 }
572 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500573 * Could this slot have been previously retired?
574 * If so, then the server may be expecting seq_nr = 1!
575 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500576 if (slot->seq_nr != 1) {
577 slot->seq_nr = 1;
578 goto retry_nowait;
579 }
580 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500581 case -NFS4ERR_SEQ_FALSE_RETRY:
582 ++slot->seq_nr;
583 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400584 default:
585 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500586 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400587 }
588out:
589 /* The session may be reset by one of the error handlers. */
590 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400591 nfs41_sequence_free_slot(res);
Trond Myklebust85563072012-12-11 10:31:12 -0500592 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500593retry_nowait:
594 if (rpc_restart_call_prepare(task)) {
595 task->tk_status = 0;
596 ret = 0;
597 }
598 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400599out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400600 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400601 goto out;
602 rpc_delay(task, NFS4_POLL_RETRY_MAX);
603 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400604}
605
Trond Myklebust14516c32010-07-31 14:29:06 -0400606static int nfs4_sequence_done(struct rpc_task *task,
607 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400608{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500609 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400610 return 1;
611 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400612}
613
Trond Myklebust9d12b212012-01-17 22:04:25 -0500614static void nfs41_init_sequence(struct nfs4_sequence_args *args,
615 struct nfs4_sequence_res *res, int cache_reply)
616{
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500617 args->sa_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500618 args->sa_cache_this = 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400619 args->sa_privileged = 0;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500620 if (cache_reply)
621 args->sa_cache_this = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500622 res->sr_slot = NULL;
623}
624
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400625static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
626{
627 args->sa_privileged = 1;
628}
629
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000630int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400631 struct nfs4_sequence_args *args,
632 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400633 struct rpc_task *task)
634{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400635 struct nfs4_slot *slot;
636 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400637
638 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400639 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400640 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400641 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400642
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400643 tbl = &session->fc_slot_table;
644
Trond Myklebust69d206b2012-11-22 13:21:02 -0500645 task->tk_timeout = 0;
646
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400647 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400648 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400649 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500650 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500651 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400652 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400653 }
654
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500655 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500656 if (IS_ERR(slot)) {
657 /* If out of memory, try again in 1/4 second */
658 if (slot == ERR_PTR(-ENOMEM))
659 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400660 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400661 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400662 }
663 spin_unlock(&tbl->slot_tbl_lock);
664
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500665 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400666
Chuck Levere8d92382013-08-09 12:47:51 -0400667 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500668 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400669
Benny Halevydfb4f3092010-09-24 09:17:01 -0400670 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500671 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400672 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400673 /*
674 * sr_status is only set in decode_sequence, and so will remain
675 * set to 1 if an rpc level failure occurs.
676 */
677 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400678 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400679out_success:
680 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400681 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400682out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400683 /* Privileged tasks are queued with top priority */
684 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400685 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
686 NULL, RPC_PRIORITY_PRIVILEGED);
687 else
688 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400689 spin_unlock(&tbl->slot_tbl_lock);
690 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400691}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000692EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400693
Trond Myklebust035168a2010-06-16 09:52:26 -0400694int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400695 struct nfs4_sequence_args *args,
696 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400697 struct rpc_task *task)
698{
Trond Myklebust035168a2010-06-16 09:52:26 -0400699 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400700 int ret = 0;
701
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400702 if (session == NULL) {
703 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400704 goto out;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400705 }
Trond Myklebust035168a2010-06-16 09:52:26 -0400706
Chuck Levere8d92382013-08-09 12:47:51 -0400707 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400708 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400709 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168a2010-06-16 09:52:26 -0400710
Trond Myklebust9d12b212012-01-17 22:04:25 -0500711 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400712out:
713 dprintk("<-- %s status=%d\n", __func__, ret);
714 return ret;
715}
716
717struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400718 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400719 struct nfs4_sequence_args *seq_args;
720 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400721};
722
723static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
724{
725 struct nfs41_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400726 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400727
Trond Myklebust035168a2010-06-16 09:52:26 -0400728 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
729
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400730 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400731}
732
Andy Adamson69ab40c2009-04-01 09:22:19 -0400733static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
734{
735 struct nfs41_call_sync_data *data = calldata;
736
Trond Myklebust14516c32010-07-31 14:29:06 -0400737 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400738}
739
Trond Myklebust17280172012-03-11 13:11:00 -0400740static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400741 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400742 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400743};
744
Bryan Schumaker7c513052011-03-24 17:12:24 +0000745static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
746 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400747 struct rpc_message *msg,
748 struct nfs4_sequence_args *args,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400749 struct nfs4_sequence_res *res)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400750{
751 int ret;
752 struct rpc_task *task;
753 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400754 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400755 .seq_args = args,
756 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400757 };
758 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000759 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400760 .rpc_message = msg,
761 .callback_ops = &nfs41_call_sync_ops,
762 .callback_data = &data
763 };
764
Andy Adamsonce5039c2009-04-01 09:22:13 -0400765 task = rpc_run_task(&task_setup);
766 if (IS_ERR(task))
767 ret = PTR_ERR(task);
768 else {
769 ret = task->tk_status;
770 rpc_put_task(task);
771 }
772 return ret;
773}
774
Trond Myklebustdf896452010-06-16 09:52:26 -0400775#else
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400776static
Trond Myklebust9d12b212012-01-17 22:04:25 -0500777void nfs41_init_sequence(struct nfs4_sequence_args *args,
778 struct nfs4_sequence_res *res, int cache_reply)
779{
780}
781
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400782static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
783{
784}
785
786
Trond Myklebust14516c32010-07-31 14:29:06 -0400787static int nfs4_sequence_done(struct rpc_task *task,
788 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400789{
Trond Myklebust14516c32010-07-31 14:29:06 -0400790 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400791}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400792#endif /* CONFIG_NFS_V4_1 */
793
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400794static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000795int _nfs4_call_sync(struct rpc_clnt *clnt,
796 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400797 struct rpc_message *msg,
798 struct nfs4_sequence_args *args,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400799 struct nfs4_sequence_res *res)
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400800{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000801 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400802}
803
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400804static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000805int nfs4_call_sync(struct rpc_clnt *clnt,
806 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000807 struct rpc_message *msg,
808 struct nfs4_sequence_args *args,
809 struct nfs4_sequence_res *res,
810 int cache_reply)
811{
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400812 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000813 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400814 args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000815}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400816
Trond Myklebust38478b22006-05-25 01:40:57 -0400817static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
Trond Myklebust38478b22006-05-25 01:40:57 -0400819 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Trond Myklebust38478b22006-05-25 01:40:57 -0400821 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400822 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700823 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400824 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700825 dir->i_version = cinfo->after;
David Howellsde242c02012-12-20 21:52:38 +0000826 nfs_fscache_invalidate(dir);
Trond Myklebust38478b22006-05-25 01:40:57 -0400827 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828}
829
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100830struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400831 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100832 struct nfs_openargs o_arg;
833 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100834 struct nfs_open_confirmargs c_arg;
835 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500836 struct nfs4_string owner_name;
837 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100838 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400839 struct nfs4_label *f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100840 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400841 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100842 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400843 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100844 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100845 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400846 unsigned int rpc_done : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -0400847 unsigned int is_recover : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100848 int rpc_status;
849 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100850};
851
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400852static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
853 int err, struct nfs4_exception *exception)
854{
855 if (err != -EINVAL)
856 return false;
857 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
858 return false;
859 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
860 exception->retry = 1;
861 return true;
862}
863
864static enum open_claim_type4
865nfs4_map_atomic_open_claim(struct nfs_server *server,
866 enum open_claim_type4 claim)
867{
868 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
869 return claim;
870 switch (claim) {
871 default:
872 return claim;
873 case NFS4_OPEN_CLAIM_FH:
874 return NFS4_OPEN_CLAIM_NULL;
875 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
876 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
877 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
878 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
879 }
880}
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400881
882static void nfs4_init_opendata_res(struct nfs4_opendata *p)
883{
884 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400885 p->o_res.f_label = p->f_label;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400886 p->o_res.seqid = p->o_arg.seqid;
887 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400888 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400889 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400890 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500891 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400892}
893
Al Viro82a2c1b2011-06-22 18:30:55 -0400894static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500895 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400896 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -0400897 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400898 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400899 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100900{
Al Viro82a2c1b2011-06-22 18:30:55 -0400901 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100902 struct inode *dir = parent->d_inode;
903 struct nfs_server *server = NFS_SERVER(dir);
904 struct nfs4_opendata *p;
905
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400906 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100907 if (p == NULL)
908 goto err;
David Quigley14c43f72013-05-22 12:50:43 -0400909
910 p->f_label = nfs4_label_alloc(server, gfp_mask);
911 if (IS_ERR(p->f_label))
912 goto err_free_p;
913
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400914 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100915 if (p->o_arg.seqid == NULL)
David Quigley14c43f72013-05-22 12:50:43 -0400916 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -0400917 nfs_sb_active(dentry->d_sb);
918 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100919 p->dir = parent;
920 p->owner = sp;
921 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500922 p->o_arg.open_flags = flags;
923 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700924 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
925 * will return permission denied for all bits until close */
926 if (!(flags & O_EXCL)) {
927 /* ask server to check for all possible rights as results
928 * are cached */
929 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
930 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700931 }
David Howells7539bba2006-08-22 20:06:09 -0400932 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400933 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
934 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400935 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100936 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -0400937 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -0400938 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
David Quigley1775fd32013-05-22 12:50:42 -0400939 p->o_arg.label = label;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400940 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
941 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400942 case NFS4_OPEN_CLAIM_NULL:
943 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
944 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
945 p->o_arg.fh = NFS_FH(dir);
946 break;
947 case NFS4_OPEN_CLAIM_PREVIOUS:
948 case NFS4_OPEN_CLAIM_FH:
949 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
950 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
951 p->o_arg.fh = NFS_FH(dentry->d_inode);
952 }
Trond Myklebust536e43d2012-01-17 22:04:26 -0500953 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -0400954 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400955
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100956 p->o_arg.u.attrs = &p->attrs;
957 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500958
959 verf[0] = jiffies;
960 verf[1] = current->pid;
961 memcpy(p->o_arg.u.verifier.data, verf,
962 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100963 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100964 p->c_arg.fh = &p->o_res.fh;
965 p->c_arg.stateid = &p->o_res.stateid;
966 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400967 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400968 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100969 return p;
David Quigley14c43f72013-05-22 12:50:43 -0400970
971err_free_label:
972 nfs4_label_free(p->f_label);
973err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100974 kfree(p);
975err:
976 dput(parent);
977 return NULL;
978}
979
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400980static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100981{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400982 struct nfs4_opendata *p = container_of(kref,
983 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400984 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400985
986 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400987 if (p->state != NULL)
988 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400989 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -0400990
991 nfs4_label_free(p->f_label);
992
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400993 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400994 dput(p->dentry);
995 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500996 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400997 kfree(p);
998}
999
1000static void nfs4_opendata_put(struct nfs4_opendata *p)
1001{
1002 if (p != NULL)
1003 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001004}
1005
Trond Myklebust06f814a2006-01-03 09:55:07 +01001006static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1007{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001008 int ret;
1009
Trond Myklebust06f814a2006-01-03 09:55:07 +01001010 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001011 return ret;
1012}
1013
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001014static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001015{
1016 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001017
Trond Myklebust536e43d2012-01-17 22:04:26 -05001018 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001019 goto out;
1020 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001021 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001022 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1023 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001024 break;
1025 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001026 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1027 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001028 break;
1029 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001030 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1031 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001032 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001033out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001034 return ret;
1035}
1036
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001037static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001038{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001039 if (delegation == NULL)
1040 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001041 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001042 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -05001043 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -04001044 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001045 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1046 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -05001047 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001048 return 1;
1049}
1050
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001051static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001052{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001053 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001054 case FMODE_WRITE:
1055 state->n_wronly++;
1056 break;
1057 case FMODE_READ:
1058 state->n_rdonly++;
1059 break;
1060 case FMODE_READ|FMODE_WRITE:
1061 state->n_rdwr++;
1062 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001063 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001064}
1065
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001066static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001067{
1068 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001069 nfs4_stateid_copy(&state->stateid, stateid);
1070 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust92b40e92013-04-20 01:25:45 -04001071 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001072 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001073 case FMODE_READ:
1074 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1075 break;
1076 case FMODE_WRITE:
1077 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1078 break;
1079 case FMODE_READ|FMODE_WRITE:
1080 set_bit(NFS_O_RDWR_STATE, &state->flags);
1081 }
1082}
1083
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001084static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001085{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001086 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001087 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001088 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001089}
1090
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001091static 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 -07001092{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001093 /*
1094 * Protect the call to nfs4_state_set_mode_locked and
1095 * serialise the stateid update
1096 */
1097 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001098 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001099 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001100 set_bit(NFS_DELEGATED_STATE, &state->flags);
1101 }
1102 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001103 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001104 write_sequnlock(&state->seqlock);
1105 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001106 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001107 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108}
1109
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001110static 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 -05001111{
1112 struct nfs_inode *nfsi = NFS_I(state->inode);
1113 struct nfs_delegation *deleg_cur;
1114 int ret = 0;
1115
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001116 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001117
1118 rcu_read_lock();
1119 deleg_cur = rcu_dereference(nfsi->delegation);
1120 if (deleg_cur == NULL)
1121 goto no_delegation;
1122
1123 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001124 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001125 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001126 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001127 goto no_delegation_unlock;
1128
1129 if (delegation == NULL)
1130 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001131 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001132 goto no_delegation_unlock;
1133
Trond Myklebustb7391f42008-12-23 15:21:52 -05001134 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001135 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001136 ret = 1;
1137no_delegation_unlock:
1138 spin_unlock(&deleg_cur->lock);
1139no_delegation:
1140 rcu_read_unlock();
1141
1142 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001143 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001144 ret = 1;
1145 }
1146
1147 return ret;
1148}
1149
1150
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001151static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001152{
1153 struct nfs_delegation *delegation;
1154
1155 rcu_read_lock();
1156 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001157 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001158 rcu_read_unlock();
1159 return;
1160 }
1161 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001162 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001163}
1164
Trond Myklebust6ee41262007-07-08 14:11:36 -04001165static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001166{
1167 struct nfs4_state *state = opendata->state;
1168 struct nfs_inode *nfsi = NFS_I(state->inode);
1169 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001170 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001171 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001172 nfs4_stateid stateid;
1173 int ret = -EAGAIN;
1174
Trond Myklebustaac00a82007-07-05 19:02:21 -04001175 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001176 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001177 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001178 if (can_open_cached(state, fmode, open_mode)) {
1179 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001180 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001181 goto out_return_state;
1182 }
1183 spin_unlock(&state->owner->so_lock);
1184 }
Trond Myklebust34310432008-12-23 15:21:38 -05001185 rcu_read_lock();
1186 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001187 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001188 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001189 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001190 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001191 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001192 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001193 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001194 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001195 if (!opendata->is_recover) {
1196 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1197 if (ret != 0)
1198 goto out;
1199 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001200 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001201
1202 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001203 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001204 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001205 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001206out:
1207 return ERR_PTR(ret);
1208out_return_state:
1209 atomic_inc(&state->count);
1210 return state;
1211}
1212
Andy Adamsone23008e2012-10-02 21:07:32 -04001213static void
1214nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1215{
1216 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1217 struct nfs_delegation *delegation;
1218 int delegation_flags = 0;
1219
1220 rcu_read_lock();
1221 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1222 if (delegation)
1223 delegation_flags = delegation->flags;
1224 rcu_read_unlock();
1225 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1226 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1227 "returning a delegation for "
1228 "OPEN(CLAIM_DELEGATE_CUR)\n",
1229 clp->cl_hostname);
1230 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1231 nfs_inode_set_delegation(state->inode,
1232 data->owner->so_cred,
1233 &data->o_res);
1234 else
1235 nfs_inode_reclaim_delegation(state->inode,
1236 data->owner->so_cred,
1237 &data->o_res);
1238}
1239
1240/*
1241 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1242 * and update the nfs4_state.
1243 */
1244static struct nfs4_state *
1245_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1246{
1247 struct inode *inode = data->state->inode;
1248 struct nfs4_state *state = data->state;
1249 int ret;
1250
1251 if (!data->rpc_done) {
1252 ret = data->rpc_status;
1253 goto err;
1254 }
1255
1256 ret = -ESTALE;
1257 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1258 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1259 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1260 goto err;
1261
1262 ret = -ENOMEM;
1263 state = nfs4_get_open_state(inode, data->owner);
1264 if (state == NULL)
1265 goto err;
1266
1267 ret = nfs_refresh_inode(inode, &data->f_attr);
1268 if (ret)
1269 goto err;
1270
David Quigleyaa9c2662013-05-22 12:50:44 -04001271 nfs_setsecurity(inode, &data->f_attr, data->f_label);
1272
Andy Adamsone23008e2012-10-02 21:07:32 -04001273 if (data->o_res.delegation_type != 0)
1274 nfs4_opendata_check_deleg(data, state);
1275 update_open_stateid(state, &data->o_res.stateid, NULL,
1276 data->o_arg.fmode);
1277
1278 return state;
1279err:
1280 return ERR_PTR(ret);
1281
1282}
1283
1284static struct nfs4_state *
1285_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001286{
1287 struct inode *inode;
1288 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001289 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001290
Trond Myklebustaac00a82007-07-05 19:02:21 -04001291 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001292 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001293 goto out;
1294 }
1295
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001296 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001297 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001298 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001299 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001300 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001301 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001302 goto err;
1303 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001304 state = nfs4_get_open_state(inode, data->owner);
1305 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001306 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001307 if (data->o_res.delegation_type != 0)
1308 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001309 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001310 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001311 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001312out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001313 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001314 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001315err_put_inode:
1316 iput(inode);
1317err:
1318 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001319}
1320
Andy Adamsone23008e2012-10-02 21:07:32 -04001321static struct nfs4_state *
1322nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1323{
1324 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1325 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1326 return _nfs4_opendata_to_nfs4_state(data);
1327}
1328
Trond Myklebust864472e2006-01-03 09:55:15 +01001329static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1330{
1331 struct nfs_inode *nfsi = NFS_I(state->inode);
1332 struct nfs_open_context *ctx;
1333
1334 spin_lock(&state->inode->i_lock);
1335 list_for_each_entry(ctx, &nfsi->open_files, list) {
1336 if (ctx->state != state)
1337 continue;
1338 get_nfs_open_context(ctx);
1339 spin_unlock(&state->inode->i_lock);
1340 return ctx;
1341 }
1342 spin_unlock(&state->inode->i_lock);
1343 return ERR_PTR(-ENOENT);
1344}
1345
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001346static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1347 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001348{
1349 struct nfs4_opendata *opendata;
1350
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001351 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001352 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001353 if (opendata == NULL)
1354 return ERR_PTR(-ENOMEM);
1355 opendata->state = state;
1356 atomic_inc(&state->count);
1357 return opendata;
1358}
1359
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001360static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001361{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001362 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001363 int ret;
1364
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001365 opendata->o_arg.open_flags = 0;
1366 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001367 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1368 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1369 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001370 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001371 if (ret != 0)
1372 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001373 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001374 if (IS_ERR(newstate))
1375 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001376 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001377 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001378 return 0;
1379}
1380
1381static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1382{
Trond Myklebust864472e2006-01-03 09:55:15 +01001383 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001384 int ret;
1385
1386 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001387 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001388 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001389 smp_rmb();
1390 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001391 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001392 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001393 if (ret != 0)
1394 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001395 if (newstate != state)
1396 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001397 }
1398 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001399 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001400 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001401 if (ret != 0)
1402 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001403 if (newstate != state)
1404 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001405 }
1406 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001407 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001408 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001409 if (ret != 0)
1410 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001411 if (newstate != state)
1412 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001413 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001414 /*
1415 * We may have performed cached opens for all three recoveries.
1416 * Check if we need to update the current stateid.
1417 */
1418 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001419 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001420 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001421 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001422 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001423 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001424 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001425 return 0;
1426}
1427
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428/*
1429 * OPEN_RECLAIM:
1430 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001432static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001434 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001435 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001436 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 int status;
1438
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001439 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1440 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001441 if (IS_ERR(opendata))
1442 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001443 rcu_read_lock();
1444 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001445 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001446 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001447 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001448 opendata->o_arg.u.delegation_type = delegation_type;
1449 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001450 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 return status;
1452}
1453
Trond Myklebust539cd032007-06-05 11:46:42 -04001454static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455{
1456 struct nfs_server *server = NFS_SERVER(state->inode);
1457 struct nfs4_exception exception = { };
1458 int err;
1459 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001460 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001461 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001462 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1463 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001464 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001465 break;
1466 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 } while (exception.retry);
1468 return err;
1469}
1470
Trond Myklebust864472e2006-01-03 09:55:15 +01001471static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1472{
1473 struct nfs_open_context *ctx;
1474 int ret;
1475
1476 ctx = nfs4_state_find_open_context(state);
1477 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001478 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001479 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001480 put_nfs_open_context(ctx);
1481 return ret;
1482}
1483
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001484static 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 -07001485{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001486 switch (err) {
1487 default:
1488 printk(KERN_ERR "NFS: %s: unhandled error "
1489 "%d.\n", __func__, err);
1490 case 0:
1491 case -ENOENT:
1492 case -ESTALE:
1493 break;
1494 case -NFS4ERR_BADSESSION:
1495 case -NFS4ERR_BADSLOT:
1496 case -NFS4ERR_BAD_HIGH_SLOT:
1497 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1498 case -NFS4ERR_DEADSESSION:
1499 set_bit(NFS_DELEGATED_STATE, &state->flags);
1500 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1501 return -EAGAIN;
1502 case -NFS4ERR_STALE_CLIENTID:
1503 case -NFS4ERR_STALE_STATEID:
1504 set_bit(NFS_DELEGATED_STATE, &state->flags);
1505 case -NFS4ERR_EXPIRED:
1506 /* Don't recall a delegation if it was lost */
1507 nfs4_schedule_lease_recovery(server->nfs_client);
1508 return -EAGAIN;
1509 case -NFS4ERR_DELEG_REVOKED:
1510 case -NFS4ERR_ADMIN_REVOKED:
1511 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001512 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001513 nfs_inode_find_state_and_recover(state->inode,
1514 stateid);
1515 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001516 return 0;
1517 case -NFS4ERR_DELAY:
1518 case -NFS4ERR_GRACE:
1519 set_bit(NFS_DELEGATED_STATE, &state->flags);
1520 ssleep(1);
1521 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001522 case -ENOMEM:
1523 case -NFS4ERR_DENIED:
1524 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1525 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 return err;
1528}
1529
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001530int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1531{
1532 struct nfs_server *server = NFS_SERVER(state->inode);
1533 struct nfs4_opendata *opendata;
1534 int err;
1535
1536 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1537 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1538 if (IS_ERR(opendata))
1539 return PTR_ERR(opendata);
1540 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1541 err = nfs4_open_recover(opendata, state);
1542 nfs4_opendata_put(opendata);
1543 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1544}
1545
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001546static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1547{
1548 struct nfs4_opendata *data = calldata;
1549
1550 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001551 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001552 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001553 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001554 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001555 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001556 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001557}
1558
1559static void nfs4_open_confirm_release(void *calldata)
1560{
1561 struct nfs4_opendata *data = calldata;
1562 struct nfs4_state *state = NULL;
1563
1564 /* If this request hasn't been cancelled, do nothing */
1565 if (data->cancelled == 0)
1566 goto out_free;
1567 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001568 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001569 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001570 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001571 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001572 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001573out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001574 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001575}
1576
1577static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001578 .rpc_call_done = nfs4_open_confirm_done,
1579 .rpc_release = nfs4_open_confirm_release,
1580};
1581
1582/*
1583 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1584 */
1585static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1586{
1587 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1588 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001589 struct rpc_message msg = {
1590 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1591 .rpc_argp = &data->c_arg,
1592 .rpc_resp = &data->c_res,
1593 .rpc_cred = data->owner->so_cred,
1594 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001595 struct rpc_task_setup task_setup_data = {
1596 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001597 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001598 .callback_ops = &nfs4_open_confirm_ops,
1599 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001600 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001601 .flags = RPC_TASK_ASYNC,
1602 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 int status;
1604
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001605 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001606 data->rpc_done = 0;
1607 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001608 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001609 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001610 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001611 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001612 status = nfs4_wait_for_completion_rpc_task(task);
1613 if (status != 0) {
1614 data->cancelled = 1;
1615 smp_wmb();
1616 } else
1617 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001618 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 return status;
1620}
1621
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001622static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001624 struct nfs4_opendata *data = calldata;
1625 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001626 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001627
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001628 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001629 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001630 /*
1631 * Check if we still need to send an OPEN call, or if we can use
1632 * a delegation instead.
1633 */
1634 if (data->state != NULL) {
1635 struct nfs_delegation *delegation;
1636
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001637 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001638 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001639 rcu_read_lock();
1640 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001641 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
Trond Myklebustcd4c9be2013-04-23 14:46:25 -04001642 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
Trond Myklebust652f89f2011-12-09 19:05:58 -05001643 can_open_delegated(delegation, data->o_arg.fmode))
1644 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001645 rcu_read_unlock();
1646 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001647 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04001648 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust8188df12013-04-23 14:31:19 -04001649 switch (data->o_arg.claim) {
1650 case NFS4_OPEN_CLAIM_PREVIOUS:
1651 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1652 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001653 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04001654 case NFS4_OPEN_CLAIM_FH:
1655 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001656 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1657 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001658 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001659 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001660 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001661 &data->o_res.seq_res,
1662 task) != 0)
1663 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04001664
1665 /* Set the create mode (note dependency on the session type) */
1666 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1667 if (data->o_arg.open_flags & O_EXCL) {
1668 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1669 if (nfs4_has_persistent_session(clp))
1670 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1671 else if (clp->cl_mvops->minor_version > 0)
1672 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1673 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04001674 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001675unlock_no_action:
1676 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001677out_no_action:
1678 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001679out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001680 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001681}
1682
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001683static void nfs4_open_done(struct rpc_task *task, void *calldata)
1684{
1685 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001687 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001688
Trond Myklebust14516c32010-07-31 14:29:06 -04001689 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1690 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001691
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001692 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001693 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1694 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001695 case S_IFREG:
1696 break;
1697 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001698 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001699 break;
1700 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001701 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001702 break;
1703 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001704 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001705 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001706 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001707 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001708 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1709 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001710 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001711 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001712}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001713
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001714static void nfs4_open_release(void *calldata)
1715{
1716 struct nfs4_opendata *data = calldata;
1717 struct nfs4_state *state = NULL;
1718
1719 /* If this request hasn't been cancelled, do nothing */
1720 if (data->cancelled == 0)
1721 goto out_free;
1722 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001723 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001724 goto out_free;
1725 /* In case we need an open_confirm, no cleanup! */
1726 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1727 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001728 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001729 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001730 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001731out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001732 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001733}
1734
1735static const struct rpc_call_ops nfs4_open_ops = {
1736 .rpc_call_prepare = nfs4_open_prepare,
1737 .rpc_call_done = nfs4_open_done,
1738 .rpc_release = nfs4_open_release,
1739};
1740
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001741static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001742{
1743 struct inode *dir = data->dir->d_inode;
1744 struct nfs_server *server = NFS_SERVER(dir);
1745 struct nfs_openargs *o_arg = &data->o_arg;
1746 struct nfs_openres *o_res = &data->o_res;
1747 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001748 struct rpc_message msg = {
1749 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1750 .rpc_argp = o_arg,
1751 .rpc_resp = o_res,
1752 .rpc_cred = data->owner->so_cred,
1753 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001754 struct rpc_task_setup task_setup_data = {
1755 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001756 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001757 .callback_ops = &nfs4_open_ops,
1758 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001759 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001760 .flags = RPC_TASK_ASYNC,
1761 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001762 int status;
1763
Trond Myklebust9d12b212012-01-17 22:04:25 -05001764 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001765 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001766 data->rpc_done = 0;
1767 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001768 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001769 data->is_recover = 0;
1770 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001771 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001772 data->is_recover = 1;
1773 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04001774 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001775 if (IS_ERR(task))
1776 return PTR_ERR(task);
1777 status = nfs4_wait_for_completion_rpc_task(task);
1778 if (status != 0) {
1779 data->cancelled = 1;
1780 smp_wmb();
1781 } else
1782 status = data->rpc_status;
1783 rpc_put_task(task);
1784
1785 return status;
1786}
1787
1788static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1789{
1790 struct inode *dir = data->dir->d_inode;
1791 struct nfs_openres *o_res = &data->o_res;
1792 int status;
1793
1794 status = nfs4_run_open_task(data, 1);
1795 if (status != 0 || !data->rpc_done)
1796 return status;
1797
Trond Myklebust6926afd2012-01-07 13:22:46 -05001798 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1799
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001800 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1801 status = _nfs4_proc_open_confirm(data);
1802 if (status != 0)
1803 return status;
1804 }
1805
1806 return status;
1807}
1808
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001809static int nfs4_opendata_access(struct rpc_cred *cred,
1810 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001811 struct nfs4_state *state, fmode_t fmode,
1812 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001813{
1814 struct nfs_access_entry cache;
1815 u32 mask;
1816
1817 /* access call failed or for some reason the server doesn't
1818 * support any access modes -- defer access call until later */
1819 if (opendata->o_res.access_supported == 0)
1820 return 0;
1821
1822 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001823 /* don't check MAY_WRITE - a newly created file may not have
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001824 * write mode bits, but POSIX allows the creating process to write.
1825 * use openflags to check for exec, because fmode won't
1826 * always have FMODE_EXEC set when file open for exec. */
1827 if (openflags & __FMODE_EXEC) {
1828 /* ONLY check for exec rights */
1829 mask = MAY_EXEC;
1830 } else if (fmode & FMODE_READ)
1831 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001832
1833 cache.cred = cred;
1834 cache.jiffies = jiffies;
1835 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1836 nfs_access_add_cache(state->inode, &cache);
1837
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001838 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001839 return 0;
1840
1841 /* even though OPEN succeeded, access is denied. Close the file */
1842 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001843 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001844}
1845
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001846/*
1847 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1848 */
1849static int _nfs4_proc_open(struct nfs4_opendata *data)
1850{
1851 struct inode *dir = data->dir->d_inode;
1852 struct nfs_server *server = NFS_SERVER(dir);
1853 struct nfs_openargs *o_arg = &data->o_arg;
1854 struct nfs_openres *o_res = &data->o_res;
1855 int status;
1856
1857 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001858 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001859 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001860 if (status != 0) {
1861 if (status == -NFS4ERR_BADNAME &&
1862 !(o_arg->open_flags & O_CREAT))
1863 return -ENOENT;
1864 return status;
1865 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001866
Trond Myklebust6926afd2012-01-07 13:22:46 -05001867 nfs_fattr_map_and_free_names(server, &data->f_attr);
1868
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001869 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001870 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001871 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1872 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001874 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001876 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 }
1878 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
David Quigley1775fd32013-05-22 12:50:42 -04001879 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001880 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881}
1882
Andy Adamsond83217c2011-03-01 01:34:17 +00001883static int nfs4_recover_expired_lease(struct nfs_server *server)
1884{
1885 return nfs4_client_recover_expired_lease(server->nfs_client);
1886}
1887
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888/*
1889 * OPEN_EXPIRED:
1890 * reclaim state on the server after a network partition.
1891 * Assumes caller holds the appropriate lock
1892 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001893static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001895 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001896 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001898 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001899 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001900 if (IS_ERR(opendata))
1901 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001902 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001903 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001904 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001905 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001906 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907}
1908
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001909static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001910{
Trond Myklebust539cd032007-06-05 11:46:42 -04001911 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001912 struct nfs4_exception exception = { };
1913 int err;
1914
1915 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001916 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001917 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001918 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1919 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001920 switch (err) {
1921 default:
1922 goto out;
1923 case -NFS4ERR_GRACE:
1924 case -NFS4ERR_DELAY:
1925 nfs4_handle_exception(server, err, &exception);
1926 err = 0;
1927 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001928 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001929out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001930 return err;
1931}
1932
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1934{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001936 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
Trond Myklebust864472e2006-01-03 09:55:15 +01001938 ctx = nfs4_state_find_open_context(state);
1939 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001940 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001941 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001942 put_nfs_open_context(ctx);
1943 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944}
1945
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001946#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001947static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001948{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001949 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001950 nfs4_stateid *stateid = &state->stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001951 struct nfs_delegation *delegation;
1952 struct rpc_cred *cred = NULL;
1953 int status = -NFS4ERR_BAD_STATEID;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001954
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001955 /* If a state reset has been done, test_stateid is unneeded */
1956 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1957 return;
1958
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001959 /* Get the delegation credential for use by test/free_stateid */
1960 rcu_read_lock();
1961 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1962 if (delegation != NULL &&
1963 nfs4_stateid_match(&delegation->stateid, stateid)) {
1964 cred = get_rpccred(delegation->cred);
1965 rcu_read_unlock();
1966 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04001967 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001968 } else
1969 rcu_read_unlock();
1970
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001971 if (status != NFS_OK) {
1972 /* Free the stateid unless the server explicitly
1973 * informs us the stateid is unrecognized. */
1974 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001975 nfs41_free_stateid(server, stateid, cred);
Bryan Schumaker69388672012-09-26 15:25:52 -04001976 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001977
Bryan Schumaker69388672012-09-26 15:25:52 -04001978 write_seqlock(&state->seqlock);
1979 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1980 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001981 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1982 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001983
1984 if (cred != NULL)
1985 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001986}
1987
1988/**
1989 * nfs41_check_open_stateid - possibly free an open stateid
1990 *
1991 * @state: NFSv4 state for an inode
1992 *
1993 * Returns NFS_OK if recovery for this stateid is now finished.
1994 * Otherwise a negative NFS4ERR value is returned.
1995 */
1996static int nfs41_check_open_stateid(struct nfs4_state *state)
1997{
1998 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001999 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002000 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002001 int status;
2002
2003 /* If a state reset has been done, test_stateid is unneeded */
2004 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2005 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2006 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2007 return -NFS4ERR_BAD_STATEID;
2008
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002009 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002010 trace_nfs4_test_open_stateid(state, NULL, status);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002011 if (status != NFS_OK) {
2012 /* Free the stateid unless the server explicitly
2013 * informs us the stateid is unrecognized. */
2014 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002015 nfs41_free_stateid(server, stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002016
2017 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2018 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2019 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002020 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002021 }
2022 return status;
2023}
2024
2025static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2026{
Chuck Levereb64cf92012-07-11 16:30:05 -04002027 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002028
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002029 nfs41_clear_delegation_stateid(state);
2030 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002031 if (status != NFS_OK)
2032 status = nfs4_open_expired(sp, state);
2033 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002034}
2035#endif
2036
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002038 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2039 * fields corresponding to attributes that were used to store the verifier.
2040 * Make sure we clobber those fields in the later setattr call
2041 */
2042static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2043{
2044 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2045 !(sattr->ia_valid & ATTR_ATIME_SET))
2046 sattr->ia_valid |= ATTR_ATIME;
2047
2048 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2049 !(sattr->ia_valid & ATTR_MTIME_SET))
2050 sattr->ia_valid |= ATTR_MTIME;
2051}
2052
Trond Myklebustc21443c2013-02-07 14:26:21 -05002053static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2054 fmode_t fmode,
2055 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002056 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002057{
2058 struct nfs4_state_owner *sp = opendata->owner;
2059 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002060 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002061 struct nfs4_state *state;
2062 unsigned int seq;
2063 int ret;
2064
2065 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2066
2067 ret = _nfs4_proc_open(opendata);
2068 if (ret != 0)
2069 goto out;
2070
2071 state = nfs4_opendata_to_nfs4_state(opendata);
2072 ret = PTR_ERR(state);
2073 if (IS_ERR(state))
2074 goto out;
2075 if (server->caps & NFS_CAP_POSIX_LOCK)
2076 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2077
Trond Myklebust275bb302013-05-29 13:11:28 -04002078 dentry = opendata->dentry;
2079 if (dentry->d_inode == NULL) {
2080 /* FIXME: Is this d_drop() ever needed? */
2081 d_drop(dentry);
2082 dentry = d_add_unique(dentry, igrab(state->inode));
2083 if (dentry == NULL) {
2084 dentry = opendata->dentry;
2085 } else if (dentry != ctx->dentry) {
2086 dput(ctx->dentry);
2087 ctx->dentry = dget(dentry);
2088 }
2089 nfs_set_verifier(dentry,
2090 nfs_save_change_attribute(opendata->dir->d_inode));
2091 }
2092
Trond Myklebustc21443c2013-02-07 14:26:21 -05002093 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2094 if (ret != 0)
2095 goto out;
2096
Trond Myklebust3efb9722013-05-29 13:17:04 -04002097 ctx->state = state;
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002098 if (dentry->d_inode == state->inode) {
2099 nfs_inode_attach_open_context(ctx);
2100 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2101 nfs4_schedule_stateid_recovery(server, state);
2102 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002103out:
2104 return ret;
2105}
2106
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002107/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002108 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 */
Andy Adamson82be4172012-05-23 05:02:35 -04002110static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002111 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002112 int flags,
2113 struct iattr *sattr,
Trond Myklebust959d9212013-06-28 16:29:51 -04002114 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115{
2116 struct nfs4_state_owner *sp;
2117 struct nfs4_state *state = NULL;
2118 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002119 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002120 struct dentry *dentry = ctx->dentry;
2121 struct rpc_cred *cred = ctx->cred;
2122 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2123 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002124 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002125 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002126 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
2128 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002130 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2131 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2133 goto out_err;
2134 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002135 status = nfs4_recover_expired_lease(server);
2136 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002137 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04002138 if (dentry->d_inode != NULL)
2139 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002140 status = -ENOMEM;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002141 if (dentry->d_inode)
2142 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002143 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002144 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002145 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002146 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
David Quigley14c43f72013-05-22 12:50:43 -04002148 if (label) {
2149 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2150 if (IS_ERR(olabel)) {
2151 status = PTR_ERR(olabel);
2152 goto err_opendata_put;
2153 }
2154 }
2155
Andy Adamson82be4172012-05-23 05:02:35 -04002156 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2157 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2158 if (!opendata->f_attr.mdsthreshold)
David Quigley14c43f72013-05-22 12:50:43 -04002159 goto err_free_label;
Trond Myklebust1549210f2012-06-05 09:16:47 -04002160 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002161 }
Al Viro82a2c1b2011-06-22 18:30:55 -04002162 if (dentry->d_inode != NULL)
2163 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002164
Trond Myklebust3efb9722013-05-29 13:17:04 -04002165 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002166 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002167 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002168 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002169
Trond Myklebust549b19c2013-04-16 18:42:34 -04002170 if ((opendata->o_arg.open_flags & O_EXCL) &&
2171 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002172 nfs4_exclusive_attrset(opendata, sattr);
2173
2174 nfs_fattr_init(opendata->o_res.f_attr);
2175 status = nfs4_do_setattr(state->inode, cred,
2176 opendata->o_res.f_attr, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002177 state, label, olabel);
2178 if (status == 0) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002179 nfs_setattr_update_inode(state->inode, sattr);
David Quigley1775fd32013-05-22 12:50:42 -04002180 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
David Quigleyaa9c2662013-05-22 12:50:44 -04002181 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
David Quigley1775fd32013-05-22 12:50:42 -04002182 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002183 }
Andy Adamson82be4172012-05-23 05:02:35 -04002184
2185 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2186 *ctx_th = opendata->f_attr.mdsthreshold;
2187 else
2188 kfree(opendata->f_attr.mdsthreshold);
2189 opendata->f_attr.mdsthreshold = NULL;
2190
David Quigley14c43f72013-05-22 12:50:43 -04002191 nfs4_label_free(olabel);
2192
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002193 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002196err_free_label:
2197 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002198err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002199 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002200 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002201err_put_state_owner:
2202 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 return status;
2205}
2206
2207
Andy Adamson82be4172012-05-23 05:02:35 -04002208static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002209 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002210 int flags,
2211 struct iattr *sattr,
Trond Myklebust959d9212013-06-28 16:29:51 -04002212 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002214 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 struct nfs4_exception exception = { };
2216 struct nfs4_state *res;
2217 int status;
2218
2219 do {
Trond Myklebust959d9212013-06-28 16:29:51 -04002220 status = _nfs4_do_open(dir, ctx, flags, sattr, label);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002221 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002222 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 if (status == 0)
2224 break;
2225 /* NOTE: BAD_SEQID means the server and client disagree about the
2226 * book-keeping w.r.t. state-changing operations
2227 * (OPEN/CLOSE/LOCK/LOCKU...)
2228 * It is actually a sign of a bug on the client or on the server.
2229 *
2230 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002231 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 * have unhashed the old state_owner for us, and that we can
2233 * therefore safely retry using a new one. We should still warn
2234 * the user though...
2235 */
2236 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002237 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002238 " returned a bad sequence-id error!\n",
2239 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 exception.retry = 1;
2241 continue;
2242 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002243 /*
2244 * BAD_STATEID on OPEN means that the server cancelled our
2245 * state before it received the OPEN_CONFIRM.
2246 * Recover by retrying the request as per the discussion
2247 * on Page 181 of RFC3530.
2248 */
2249 if (status == -NFS4ERR_BAD_STATEID) {
2250 exception.retry = 1;
2251 continue;
2252 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002253 if (status == -EAGAIN) {
2254 /* We must have found a delegation */
2255 exception.retry = 1;
2256 continue;
2257 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002258 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2259 continue;
2260 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 status, &exception));
2262 } while (exception.retry);
2263 return res;
2264}
2265
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002266static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2267 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002268 struct nfs4_state *state, struct nfs4_label *ilabel,
2269 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002271 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002273 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 .iap = sattr,
2275 .server = server,
2276 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002277 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 };
2279 struct nfs_setattrres res = {
2280 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002281 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 .server = server,
2283 };
2284 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002285 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2286 .rpc_argp = &arg,
2287 .rpc_resp = &res,
2288 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002290 unsigned long timestamp = jiffies;
Trond Myklebustee3ae842013-04-29 10:35:36 -04002291 fmode_t fmode;
2292 bool truncate;
Trond Myklebust65e43082005-08-16 11:49:44 -04002293 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
David Quigleyaa9c2662013-05-22 12:50:44 -04002295 arg.bitmask = nfs4_bitmask(server, ilabel);
2296 if (ilabel)
2297 arg.bitmask = nfs4_bitmask(server, olabel);
2298
Trond Myklebust0e574af2005-10-27 22:12:38 -04002299 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
Trond Myklebustee3ae842013-04-29 10:35:36 -04002301 /* Servers should only apply open mode checks for file size changes */
2302 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2303 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2304
2305 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2306 /* Use that stateid */
2307 } else if (truncate && state != NULL && nfs4_valid_open_stateid(state)) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002308 struct nfs_lockowner lockowner = {
2309 .l_owner = current->files,
2310 .l_pid = current->tgid,
2311 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002312 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002313 &lockowner);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002314 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002315 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
Bryan Schumaker7c513052011-03-24 17:12:24 +00002317 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002318 if (status == 0 && state != NULL)
2319 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002320 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321}
2322
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002323static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2324 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002325 struct nfs4_state *state, struct nfs4_label *ilabel,
2326 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002328 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002329 struct nfs4_exception exception = {
2330 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002331 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002332 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 int err;
2334 do {
David Quigley1775fd32013-05-22 12:50:42 -04002335 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
Trond Myklebustc1578b72013-08-12 16:58:42 -04002336 trace_nfs4_setattr(inode, err);
Trond Myklebust451146b2012-04-18 16:29:11 -04002337 switch (err) {
2338 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002339 if (!(sattr->ia_valid & ATTR_SIZE)) {
2340 pr_warn_once("NFSv4: server %s is incorrectly "
2341 "applying open mode checks to "
2342 "a SETATTR that is not "
2343 "changing file size.\n",
2344 server->nfs_client->cl_hostname);
2345 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002346 if (state && !(state->state & FMODE_WRITE)) {
2347 err = -EBADF;
2348 if (sattr->ia_valid & ATTR_OPEN)
2349 err = -EACCES;
2350 goto out;
2351 }
2352 }
2353 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002355out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 return err;
2357}
2358
2359struct nfs4_closedata {
2360 struct inode *inode;
2361 struct nfs4_state *state;
2362 struct nfs_closeargs arg;
2363 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002364 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002365 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002366 bool roc;
2367 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368};
2369
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002370static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002371{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002372 struct nfs4_closedata *calldata = data;
2373 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002374 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002375
Fred Isamanf7e89172011-01-06 11:36:32 +00002376 if (calldata->roc)
2377 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002378 nfs4_put_open_state(calldata->state);
2379 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002380 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002381 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002382 kfree(calldata);
2383}
2384
Trond Myklebust88069f72009-12-08 08:33:16 -05002385static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2386 fmode_t fmode)
2387{
2388 spin_lock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002389 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002390 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
2391 case FMODE_WRITE:
2392 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2393 break;
2394 case FMODE_READ:
2395 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2396 break;
2397 case 0:
2398 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2399 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2400 clear_bit(NFS_OPEN_STATE, &state->flags);
2401 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002402 spin_unlock(&state->owner->so_lock);
2403}
2404
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002405static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002407 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 struct nfs_server *server = NFS_SERVER(calldata->inode);
2410
Chuck Levera3ca5652012-03-01 17:00:40 -05002411 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002412 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2413 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002414 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 /* hmm. we are done with the inode, and in the process of freeing
2416 * the state_owner. we keep this around to process errors
2417 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 switch (task->tk_status) {
2419 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002420 if (calldata->roc)
2421 pnfs_roc_set_barrier(state->inode,
2422 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002423 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002424 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002425 nfs4_close_clear_stateid_flags(state,
2426 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 break;
2428 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002429 case -NFS4ERR_OLD_STATEID:
2430 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002432 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002433 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002435 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2436 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002438 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002439 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002440 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441}
2442
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002443static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002445 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002446 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002447 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002448 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002449
Chuck Levera3ca5652012-03-01 17:00:40 -05002450 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002451 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002452 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002453
Trond Myklebust88069f72009-12-08 08:33:16 -05002454 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2455 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002456 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002457 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002458 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002459 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002460 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2461 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2462 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002463 }
2464 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002465 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2466 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2467 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002468 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002469 }
Trond Myklebust5d422302013-03-14 16:57:48 -04002470 if (!nfs4_valid_open_stateid(state))
2471 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002472 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002473
2474 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002475 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002476 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002477 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002478
Fred Isamanf7e89172011-01-06 11:36:32 +00002479 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002480 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002481 if (calldata->roc &&
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002482 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2483 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002484 goto out_wait;
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002485 }
Fred Isamanf7e89172011-01-06 11:36:32 +00002486 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002487
Trond Myklebust516a6af2005-10-27 22:12:41 -04002488 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002489 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002490 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002491 &calldata->arg.seq_args,
2492 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002493 task) != 0)
2494 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002495 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002496 return;
2497out_no_action:
2498 task->tk_action = NULL;
2499out_wait:
2500 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501}
2502
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002503static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002504 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002505 .rpc_call_done = nfs4_close_done,
2506 .rpc_release = nfs4_free_closedata,
2507};
2508
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509/*
2510 * It is possible for data to be read/written from a mem-mapped file
2511 * after the sys_close call (which hits the vfs layer as a flush).
2512 * This means that we can't safely call nfsv4 close on a file until
2513 * the inode is cleared. This in turn means that we are not good
2514 * NFSv4 citizens - we do not indicate to the server to update the file's
2515 * share state even when we are done with one of the three share
2516 * stateid's in the inode.
2517 *
2518 * NOTE: Caller must be holding the sp->so_owner semaphore!
2519 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002520int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002522 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002524 struct nfs4_state_owner *sp = state->owner;
2525 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002526 struct rpc_message msg = {
2527 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2528 .rpc_cred = state->owner->so_cred,
2529 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002530 struct rpc_task_setup task_setup_data = {
2531 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002532 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002533 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002534 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002535 .flags = RPC_TASK_ASYNC,
2536 };
Trond Myklebust95121352005-10-18 14:20:12 -07002537 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002539 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002541 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002542 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002543 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002545 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002546 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002548 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002549 if (calldata->arg.seqid == NULL)
2550 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002551 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002552 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002553 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002554 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002555 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002556 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002557 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002558
Trond Myklebust1174dd12010-12-21 10:52:24 -05002559 msg.rpc_argp = &calldata->arg;
2560 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002561 task_setup_data.callback_data = calldata;
2562 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002563 if (IS_ERR(task))
2564 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002565 status = 0;
2566 if (wait)
2567 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002568 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002569 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002570out_free_calldata:
2571 kfree(calldata);
2572out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002573 nfs4_put_open_state(state);
2574 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002575 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576}
2577
Trond Myklebust2b484292010-09-17 10:56:51 -04002578static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002579nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04002582 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2583
2584 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585
Trond Myklebust565277f2007-10-15 18:17:53 -04002586 /* Protect against concurrent sillydeletes */
Trond Myklebust959d9212013-06-28 16:29:51 -04002587 state = nfs4_do_open(dir, ctx, open_flags, attr, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04002588
2589 nfs4_label_release_security(label);
2590
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002591 if (IS_ERR(state))
2592 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04002593 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594}
2595
Trond Myklebust1185a552009-12-03 15:54:02 -05002596static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002597{
2598 if (ctx->state == NULL)
2599 return;
2600 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002601 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002602 else
Al Viro643168c2011-06-22 18:20:23 -04002603 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002604}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605
2606static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2607{
Benny Halevy43652ad2009-04-01 09:21:54 -04002608 struct nfs4_server_caps_arg args = {
2609 .fhandle = fhandle,
2610 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 struct nfs4_server_caps_res res = {};
2612 struct rpc_message msg = {
2613 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002614 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 .rpc_resp = &res,
2616 };
2617 int status;
2618
Bryan Schumaker7c513052011-03-24 17:12:24 +00002619 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 if (status == 0) {
2621 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002622 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2623 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2624 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2625 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2626 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2628 server->caps |= NFS_CAP_ACLS;
2629 if (res.has_links != 0)
2630 server->caps |= NFS_CAP_HARDLINKS;
2631 if (res.has_symlinks != 0)
2632 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002633 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2634 server->caps |= NFS_CAP_FILEID;
2635 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2636 server->caps |= NFS_CAP_MODE;
2637 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2638 server->caps |= NFS_CAP_NLINK;
2639 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2640 server->caps |= NFS_CAP_OWNER;
2641 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2642 server->caps |= NFS_CAP_OWNER_GROUP;
2643 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2644 server->caps |= NFS_CAP_ATIME;
2645 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2646 server->caps |= NFS_CAP_CTIME;
2647 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2648 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04002649#ifdef CONFIG_NFS_V4_SECURITY_LABEL
2650 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
2651 server->caps |= NFS_CAP_SECURITY_LABEL;
2652#endif
2653 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
2654 sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002655
David Quigleyaa9c2662013-05-22 12:50:44 -04002656 if (server->caps & NFS_CAP_SECURITY_LABEL) {
2657 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2658 res.attr_bitmask[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2659 }
Trond Myklebusta65318b2009-03-11 14:10:28 -04002660 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2661 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2662 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002664 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002666
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 return status;
2668}
2669
Trond Myklebust55a97592006-06-09 09:34:19 -04002670int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671{
2672 struct nfs4_exception exception = { };
2673 int err;
2674 do {
2675 err = nfs4_handle_exception(server,
2676 _nfs4_server_capabilities(server, fhandle),
2677 &exception);
2678 } while (exception.retry);
2679 return err;
2680}
2681
2682static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2683 struct nfs_fsinfo *info)
2684{
David Quigleyaa9c2662013-05-22 12:50:44 -04002685 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04002687 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 };
2689 struct nfs4_lookup_res res = {
2690 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002691 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 .fh = fhandle,
2693 };
2694 struct rpc_message msg = {
2695 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2696 .rpc_argp = &args,
2697 .rpc_resp = &res,
2698 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002699
David Quigleyaa9c2662013-05-22 12:50:44 -04002700 bitmask[0] = nfs4_fattr_bitmap[0];
2701 bitmask[1] = nfs4_fattr_bitmap[1];
2702 /*
2703 * Process the label in the upcoming getfattr
2704 */
2705 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
2706
Trond Myklebust0e574af2005-10-27 22:12:38 -04002707 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002708 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709}
2710
2711static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2712 struct nfs_fsinfo *info)
2713{
2714 struct nfs4_exception exception = { };
2715 int err;
2716 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002717 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04002718 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002719 switch (err) {
2720 case 0:
2721 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002722 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002723 default:
2724 err = nfs4_handle_exception(server, err, &exception);
2725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002727out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 return err;
2729}
2730
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002731static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2732 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2733{
Trond Myklebustc2190662013-08-26 19:23:04 -04002734 struct rpc_auth_create_args auth_args = {
2735 .pseudoflavor = flavor,
2736 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002737 struct rpc_auth *auth;
2738 int ret;
2739
Trond Myklebustc2190662013-08-26 19:23:04 -04002740 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002741 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04002742 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002743 goto out;
2744 }
2745 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002746out:
2747 return ret;
2748}
2749
Chuck Lever9a744ba2013-03-16 15:56:02 -04002750/*
2751 * Retry pseudoroot lookup with various security flavors. We do this when:
2752 *
2753 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2754 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2755 *
2756 * Returns zero on success, or a negative NFS4ERR value, or a
2757 * negative errno value.
2758 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002759static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002760 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761{
Chuck Lever9a744ba2013-03-16 15:56:02 -04002762 /* Per 3530bis 15.33.5 */
2763 static const rpc_authflavor_t flav_array[] = {
2764 RPC_AUTH_GSS_KRB5P,
2765 RPC_AUTH_GSS_KRB5I,
2766 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04002767 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04002768 RPC_AUTH_NULL,
2769 };
2770 int status = -EPERM;
2771 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772
Chuck Lever9a744ba2013-03-16 15:56:02 -04002773 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002774 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002775 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002776 continue;
2777 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002778 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04002779
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002780 /*
2781 * -EACCESS could mean that the user doesn't have correct permissions
2782 * to access the mount. It could also mean that we tried to mount
2783 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2784 * existing mount programs don't handle -EACCES very well so it should
2785 * be mapped to -EPERM instead.
2786 */
2787 if (status == -EACCES)
2788 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002789 return status;
2790}
2791
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002792static int nfs4_do_find_root_sec(struct nfs_server *server,
2793 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
2794{
2795 int mv = server->nfs_client->cl_minorversion;
2796 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
2797}
2798
2799/**
2800 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
2801 * @server: initialized nfs_server handle
2802 * @fhandle: we fill in the pseudo-fs root file handle
2803 * @info: we fill in an FSINFO struct
2804 *
2805 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002806 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002807int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2808 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002809{
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002810 int status;
2811
2812 status = nfs4_lookup_root(server, fhandle, info);
2813 if ((status == -NFS4ERR_WRONGSEC) &&
2814 !(server->flags & NFS_MOUNT_SECFLAVOUR))
2815 status = nfs4_do_find_root_sec(server, fhandle, info);
2816
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 if (status == 0)
2818 status = nfs4_server_capabilities(server, fhandle);
2819 if (status == 0)
2820 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002821
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002822 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823}
2824
Bryan Schumakerbae36242012-05-10 15:07:31 -04002825static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2826 struct nfs_fsinfo *info)
2827{
2828 int error;
2829 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04002830 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04002831
2832 error = nfs4_server_capabilities(server, mntfh);
2833 if (error < 0) {
2834 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2835 return error;
2836 }
2837
David Quigley14c43f72013-05-22 12:50:43 -04002838 label = nfs4_label_alloc(server, GFP_KERNEL);
2839 if (IS_ERR(label))
2840 return PTR_ERR(label);
2841
David Quigley1775fd32013-05-22 12:50:42 -04002842 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04002843 if (error < 0) {
2844 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04002845 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04002846 }
2847
2848 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2849 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2850 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2851
David Quigley14c43f72013-05-22 12:50:43 -04002852err_free_label:
2853 nfs4_label_free(label);
2854
Bryan Schumakerbae36242012-05-10 15:07:31 -04002855 return error;
2856}
2857
Manoj Naik6b97fd32006-06-09 09:34:29 -04002858/*
2859 * Get locations and (maybe) other attributes of a referral.
2860 * Note that we'll actually follow the referral later when
2861 * we detect fsid mismatch in inode revalidation
2862 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002863static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2864 const struct qstr *name, struct nfs_fattr *fattr,
2865 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002866{
2867 int status = -ENOMEM;
2868 struct page *page = NULL;
2869 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002870
2871 page = alloc_page(GFP_KERNEL);
2872 if (page == NULL)
2873 goto out;
2874 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2875 if (locations == NULL)
2876 goto out;
2877
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002878 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002879 if (status != 0)
2880 goto out;
2881 /* Make sure server returned a different fsid for the referral */
2882 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002883 dprintk("%s: server did not return a different fsid for"
2884 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002885 status = -EIO;
2886 goto out;
2887 }
Andy Adamson533eb462011-06-13 18:25:56 -04002888 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2889 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002890
Andy Adamson533eb462011-06-13 18:25:56 -04002891 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002892 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002893 memset(fhandle, 0, sizeof(struct nfs_fh));
2894out:
2895 if (page)
2896 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002897 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002898 return status;
2899}
2900
David Quigley1775fd32013-05-22 12:50:42 -04002901static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
2902 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903{
2904 struct nfs4_getattr_arg args = {
2905 .fh = fhandle,
2906 .bitmask = server->attr_bitmask,
2907 };
2908 struct nfs4_getattr_res res = {
2909 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002910 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 .server = server,
2912 };
2913 struct rpc_message msg = {
2914 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2915 .rpc_argp = &args,
2916 .rpc_resp = &res,
2917 };
David Quigleyaa9c2662013-05-22 12:50:44 -04002918
2919 args.bitmask = nfs4_bitmask(server, label);
2920
Trond Myklebust0e574af2005-10-27 22:12:38 -04002921 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002922 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923}
2924
David Quigley1775fd32013-05-22 12:50:42 -04002925static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
2926 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927{
2928 struct nfs4_exception exception = { };
2929 int err;
2930 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04002931 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
2932 trace_nfs4_getattr(server, fhandle, fattr, err);
2933 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 &exception);
2935 } while (exception.retry);
2936 return err;
2937}
2938
2939/*
2940 * The file is not closed if it is opened due to the a request to change
2941 * the size of the file. The open call will not be needed once the
2942 * VFS layer lookup-intents are implemented.
2943 *
2944 * Close is called when the inode is destroyed.
2945 * If we haven't opened the file for O_WRONLY, we
2946 * need to in the size_change case to obtain a stateid.
2947 *
2948 * Got race?
2949 * Because OPEN is always done by name in nfsv4, it is
2950 * possible that we opened a different file by the same
2951 * name. We can recognize this race condition, but we
2952 * can't do anything about it besides returning an error.
2953 *
2954 * This will be fixed with VFS changes (lookup-intent).
2955 */
2956static int
2957nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2958 struct iattr *sattr)
2959{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002960 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002961 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002962 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04002963 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 int status;
2965
Benny Halevy8a1636c2010-07-14 15:43:57 -04002966 if (pnfs_ld_layoutret_on_setattr(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04002967 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04002968
Trond Myklebust0e574af2005-10-27 22:12:38 -04002969 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970
Andy Adamson26699402012-05-30 16:12:24 -04002971 /* Deal with open(O_TRUNC) */
2972 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03002973 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04002974
2975 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03002976 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04002977 return 0;
2978
Trond Myklebustd5308382005-11-04 15:33:38 -05002979 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002980 if (sattr->ia_valid & ATTR_FILE) {
2981 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002982
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002983 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002984 if (ctx) {
2985 cred = ctx->cred;
2986 state = ctx->state;
2987 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002988 }
2989
David Quigley14c43f72013-05-22 12:50:43 -04002990 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
2991 if (IS_ERR(label))
2992 return PTR_ERR(label);
2993
2994 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04002995 if (status == 0) {
Trond Myklebust65e43082005-08-16 11:49:44 -04002996 nfs_setattr_update_inode(inode, sattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04002997 nfs_setsecurity(inode, fattr, label);
2998 }
David Quigley14c43f72013-05-22 12:50:43 -04002999 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 return status;
3001}
3002
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003003static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3004 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003005 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003006{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003007 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003008 int status;
3009 struct nfs4_lookup_arg args = {
3010 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003011 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003012 .name = name,
3013 };
3014 struct nfs4_lookup_res res = {
3015 .server = server,
3016 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003017 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003018 .fh = fhandle,
3019 };
3020 struct rpc_message msg = {
3021 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3022 .rpc_argp = &args,
3023 .rpc_resp = &res,
3024 };
3025
David Quigleyaa9c2662013-05-22 12:50:44 -04003026 args.bitmask = nfs4_bitmask(server, label);
3027
David Howells2b3de442006-08-22 20:06:09 -04003028 nfs_fattr_init(fattr);
3029
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003031 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 dprintk("NFS reply lookup: %d\n", status);
3033 return status;
3034}
3035
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003036static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003037{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003038 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003039 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003040 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3041 fattr->nlink = 2;
3042}
3043
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003044static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3045 struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003046 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003047{
3048 struct nfs4_exception exception = { };
3049 struct rpc_clnt *client = *clnt;
3050 int err;
3051 do {
David Quigley1775fd32013-05-22 12:50:42 -04003052 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003053 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003054 switch (err) {
3055 case -NFS4ERR_BADNAME:
3056 err = -ENOENT;
3057 goto out;
3058 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003059 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003060 goto out;
3061 case -NFS4ERR_WRONGSEC:
3062 err = -EPERM;
3063 if (client != *clnt)
3064 goto out;
3065
3066 client = nfs4_create_sec_client(client, dir, name);
3067 if (IS_ERR(client))
3068 return PTR_ERR(client);
3069
3070 exception.retry = 1;
3071 break;
3072 default:
3073 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3074 }
3075 } while (exception.retry);
3076
3077out:
3078 if (err == 0)
3079 *clnt = client;
3080 else if (client != *clnt)
3081 rpc_shutdown_client(client);
3082
3083 return err;
3084}
3085
Bryan Schumaker80a16b22012-04-27 13:27:46 -04003086static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003087 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3088 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003090 int status;
3091 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003092
David Quigley1775fd32013-05-22 12:50:42 -04003093 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003094 if (client != NFS_CLIENT(dir)) {
3095 rpc_shutdown_client(client);
3096 nfs_fixup_secinfo_attributes(fattr);
3097 }
3098 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099}
3100
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003101struct rpc_clnt *
3102nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3103 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3104{
3105 int status;
3106 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
3107
David Quigley1775fd32013-05-22 12:50:42 -04003108 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003109 if (status < 0) {
3110 rpc_shutdown_client(client);
3111 return ERR_PTR(status);
3112 }
3113 return client;
3114}
3115
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3117{
Trond Myklebust76b32992007-08-10 17:45:11 -04003118 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 struct nfs4_accessargs args = {
3120 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003121 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003123 struct nfs4_accessres res = {
3124 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003125 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 struct rpc_message msg = {
3127 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3128 .rpc_argp = &args,
3129 .rpc_resp = &res,
3130 .rpc_cred = entry->cred,
3131 };
3132 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003133 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134
3135 /*
3136 * Determine which access bits we want to ask for...
3137 */
3138 if (mode & MAY_READ)
3139 args.access |= NFS4_ACCESS_READ;
3140 if (S_ISDIR(inode->i_mode)) {
3141 if (mode & MAY_WRITE)
3142 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3143 if (mode & MAY_EXEC)
3144 args.access |= NFS4_ACCESS_LOOKUP;
3145 } else {
3146 if (mode & MAY_WRITE)
3147 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3148 if (mode & MAY_EXEC)
3149 args.access |= NFS4_ACCESS_EXECUTE;
3150 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003151
3152 res.fattr = nfs_alloc_fattr();
3153 if (res.fattr == NULL)
3154 return -ENOMEM;
3155
Bryan Schumaker7c513052011-03-24 17:12:24 +00003156 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003158 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003159 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003161 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 return status;
3163}
3164
3165static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3166{
3167 struct nfs4_exception exception = { };
3168 int err;
3169 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003170 err = _nfs4_proc_access(inode, entry);
3171 trace_nfs4_access(inode, err);
3172 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 &exception);
3174 } while (exception.retry);
3175 return err;
3176}
3177
3178/*
3179 * TODO: For the time being, we don't try to get any attributes
3180 * along with any of the zero-copy operations READ, READDIR,
3181 * READLINK, WRITE.
3182 *
3183 * In the case of the first three, we want to put the GETATTR
3184 * after the read-type operation -- this is because it is hard
3185 * to predict the length of a GETATTR response in v4, and thus
3186 * align the READ data correctly. This means that the GETATTR
3187 * may end up partially falling into the page cache, and we should
3188 * shift it into the 'tail' of the xdr_buf before processing.
3189 * To do this efficiently, we need to know the total length
3190 * of data received, which doesn't seem to be available outside
3191 * of the RPC layer.
3192 *
3193 * In the case of WRITE, we also want to put the GETATTR after
3194 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003195 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 *
3197 * Both of these changes to the XDR layer would in fact be quite
3198 * minor, but I decided to leave them for a subsequent patch.
3199 */
3200static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3201 unsigned int pgbase, unsigned int pglen)
3202{
3203 struct nfs4_readlink args = {
3204 .fh = NFS_FH(inode),
3205 .pgbase = pgbase,
3206 .pglen = pglen,
3207 .pages = &page,
3208 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003209 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 struct rpc_message msg = {
3211 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3212 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003213 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 };
3215
Bryan Schumaker7c513052011-03-24 17:12:24 +00003216 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 -07003217}
3218
3219static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3220 unsigned int pgbase, unsigned int pglen)
3221{
3222 struct nfs4_exception exception = { };
3223 int err;
3224 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003225 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3226 trace_nfs4_readlink(inode, err);
3227 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 &exception);
3229 } while (exception.retry);
3230 return err;
3231}
3232
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003234 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236static int
3237nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003238 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239{
David Quigleyaa9c2662013-05-22 12:50:44 -04003240 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003241 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 int status = 0;
3244
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003245 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3246 if (IS_ERR(ctx))
3247 return PTR_ERR(ctx);
3248
David Quigleyaa9c2662013-05-22 12:50:44 -04003249 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3250
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003251 sattr->ia_mode &= ~current_umask();
Trond Myklebust959d9212013-06-28 16:29:51 -04003252 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 if (IS_ERR(state)) {
3254 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003255 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003258 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003259 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 return status;
3261}
3262
3263static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3264{
Trond Myklebust16e42952005-10-27 22:12:44 -04003265 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003266 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003268 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003270 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003271 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003272 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003274 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3275 .rpc_argp = &args,
3276 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003278 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003279
Bryan Schumaker7c513052011-03-24 17:12:24 +00003280 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003281 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003282 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 return status;
3284}
3285
3286static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3287{
3288 struct nfs4_exception exception = { };
3289 int err;
3290 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003291 err = _nfs4_proc_remove(dir, name);
3292 trace_nfs4_remove(dir, name, err);
3293 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 &exception);
3295 } while (exception.retry);
3296 return err;
3297}
3298
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003299static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003301 struct nfs_server *server = NFS_SERVER(dir);
3302 struct nfs_removeargs *args = msg->rpc_argp;
3303 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003305 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003307 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003308
3309 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310}
3311
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003312static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3313{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003314 nfs4_setup_sequence(NFS_SERVER(data->dir),
3315 &data->args.seq_args,
3316 &data->res.seq_res,
3317 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318}
3319
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003320static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003322 struct nfs_unlinkdata *data = task->tk_calldata;
3323 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003324
Trond Myklebust14516c32010-07-31 14:29:06 -04003325 if (!nfs4_sequence_done(task, &res->seq_res))
3326 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003327 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003328 return 0;
3329 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003330 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331}
3332
Jeff Laytond3d41522010-09-17 17:31:57 -04003333static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3334{
3335 struct nfs_server *server = NFS_SERVER(dir);
3336 struct nfs_renameargs *arg = msg->rpc_argp;
3337 struct nfs_renameres *res = msg->rpc_resp;
3338
3339 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003340 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003341 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003342}
3343
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003344static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3345{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003346 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3347 &data->args.seq_args,
3348 &data->res.seq_res,
3349 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003350}
3351
3352static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3353 struct inode *new_dir)
3354{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003355 struct nfs_renamedata *data = task->tk_calldata;
3356 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04003357
3358 if (!nfs4_sequence_done(task, &res->seq_res))
3359 return 0;
3360 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3361 return 0;
3362
3363 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003364 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003365 return 1;
3366}
3367
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3369 struct inode *new_dir, struct qstr *new_name)
3370{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003371 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003372 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 .old_dir = NFS_FH(old_dir),
3374 .new_dir = NFS_FH(new_dir),
3375 .old_name = old_name,
3376 .new_name = new_name,
3377 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003378 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003379 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003380 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 struct rpc_message msg = {
3382 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3383 .rpc_argp = &arg,
3384 .rpc_resp = &res,
3385 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003386 int status = -ENOMEM;
David Quigley14c43f72013-05-22 12:50:43 -04003387
Bryan Schumaker7c513052011-03-24 17:12:24 +00003388 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 if (!status) {
3390 update_changeattr(old_dir, &res.old_cinfo);
3391 update_changeattr(new_dir, &res.new_cinfo);
3392 }
3393 return status;
3394}
3395
3396static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3397 struct inode *new_dir, struct qstr *new_name)
3398{
3399 struct nfs4_exception exception = { };
3400 int err;
3401 do {
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003402 err = _nfs4_proc_rename(old_dir, old_name,
3403 new_dir, new_name);
3404 trace_nfs4_rename(old_dir, old_name, new_dir, new_name, err);
3405 err = nfs4_handle_exception(NFS_SERVER(old_dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 &exception);
3407 } while (exception.retry);
3408 return err;
3409}
3410
3411static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3412{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003413 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 struct nfs4_link_arg arg = {
3415 .fh = NFS_FH(inode),
3416 .dir_fh = NFS_FH(dir),
3417 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003418 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003420 struct nfs4_link_res res = {
3421 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003422 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003423 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 struct rpc_message msg = {
3425 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3426 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003427 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003429 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430
Trond Myklebust136f2622010-04-16 16:22:49 -04003431 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003432 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003433 goto out;
3434
David Quigley14c43f72013-05-22 12:50:43 -04003435 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3436 if (IS_ERR(res.label)) {
3437 status = PTR_ERR(res.label);
3438 goto out;
3439 }
David Quigleyaa9c2662013-05-22 12:50:44 -04003440 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04003441
Bryan Schumaker7c513052011-03-24 17:12:24 +00003442 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003443 if (!status) {
3444 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04003445 status = nfs_post_op_update_inode(inode, res.fattr);
3446 if (!status)
3447 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003448 }
David Quigley14c43f72013-05-22 12:50:43 -04003449
3450
3451 nfs4_label_free(res.label);
3452
Trond Myklebust136f2622010-04-16 16:22:49 -04003453out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003454 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 return status;
3456}
3457
3458static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3459{
3460 struct nfs4_exception exception = { };
3461 int err;
3462 do {
3463 err = nfs4_handle_exception(NFS_SERVER(inode),
3464 _nfs4_proc_link(inode, dir, name),
3465 &exception);
3466 } while (exception.retry);
3467 return err;
3468}
3469
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003470struct nfs4_createdata {
3471 struct rpc_message msg;
3472 struct nfs4_create_arg arg;
3473 struct nfs4_create_res res;
3474 struct nfs_fh fh;
3475 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003476 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003477};
3478
3479static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3480 struct qstr *name, struct iattr *sattr, u32 ftype)
3481{
3482 struct nfs4_createdata *data;
3483
3484 data = kzalloc(sizeof(*data), GFP_KERNEL);
3485 if (data != NULL) {
3486 struct nfs_server *server = NFS_SERVER(dir);
3487
David Quigley14c43f72013-05-22 12:50:43 -04003488 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3489 if (IS_ERR(data->label))
3490 goto out_free;
3491
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003492 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3493 data->msg.rpc_argp = &data->arg;
3494 data->msg.rpc_resp = &data->res;
3495 data->arg.dir_fh = NFS_FH(dir);
3496 data->arg.server = server;
3497 data->arg.name = name;
3498 data->arg.attrs = sattr;
3499 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04003500 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003501 data->res.server = server;
3502 data->res.fh = &data->fh;
3503 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003504 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003505 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003506 }
3507 return data;
David Quigley14c43f72013-05-22 12:50:43 -04003508out_free:
3509 kfree(data);
3510 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003511}
3512
3513static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3514{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003515 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003516 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003517 if (status == 0) {
3518 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04003519 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003520 }
3521 return status;
3522}
3523
3524static void nfs4_free_createdata(struct nfs4_createdata *data)
3525{
David Quigley14c43f72013-05-22 12:50:43 -04003526 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003527 kfree(data);
3528}
3529
Chuck Lever4f390c12006-08-22 20:06:22 -04003530static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003531 struct page *page, unsigned int len, struct iattr *sattr,
3532 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003534 struct nfs4_createdata *data;
3535 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536
Chuck Lever94a6d752006-08-22 20:06:23 -04003537 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003538 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003539
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003540 status = -ENOMEM;
3541 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3542 if (data == NULL)
3543 goto out;
3544
3545 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3546 data->arg.u.symlink.pages = &page;
3547 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04003548 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003550 status = nfs4_do_create(dir, dentry, data);
3551
3552 nfs4_free_createdata(data);
3553out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 return status;
3555}
3556
Chuck Lever4f390c12006-08-22 20:06:22 -04003557static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003558 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559{
3560 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003561 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04003563
3564 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3565
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003567 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3568 trace_nfs4_symlink(dir, &dentry->d_name, err);
3569 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 &exception);
3571 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003572
3573 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 return err;
3575}
3576
3577static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003578 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003580 struct nfs4_createdata *data;
3581 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003583 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3584 if (data == NULL)
3585 goto out;
3586
David Quigley1775fd32013-05-22 12:50:42 -04003587 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003588 status = nfs4_do_create(dir, dentry, data);
3589
3590 nfs4_free_createdata(data);
3591out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 return status;
3593}
3594
3595static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3596 struct iattr *sattr)
3597{
3598 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003599 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003601
David Quigleyaa9c2662013-05-22 12:50:44 -04003602 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3603
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003604 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003606 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3607 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3608 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 &exception);
3610 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003611 nfs4_label_release_security(label);
3612
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 return err;
3614}
3615
3616static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003617 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618{
3619 struct inode *dir = dentry->d_inode;
3620 struct nfs4_readdir_arg args = {
3621 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003622 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 .pgbase = 0,
3624 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003625 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003626 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 };
3628 struct nfs4_readdir_res res;
3629 struct rpc_message msg = {
3630 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3631 .rpc_argp = &args,
3632 .rpc_resp = &res,
3633 .rpc_cred = cred,
3634 };
3635 int status;
3636
Harvey Harrison3110ff82008-05-02 13:42:44 -07003637 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003638 dentry->d_parent->d_name.name,
3639 dentry->d_name.name,
3640 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003641 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003643 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 -05003644 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003645 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003646 status += args.pgbase;
3647 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003648
3649 nfs_invalidate_atime(dir);
3650
Harvey Harrison3110ff82008-05-02 13:42:44 -07003651 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 return status;
3653}
3654
3655static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003656 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657{
3658 struct nfs4_exception exception = { };
3659 int err;
3660 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003661 err = _nfs4_proc_readdir(dentry, cred, cookie,
3662 pages, count, plus);
3663 trace_nfs4_readdir(dentry->d_inode, err);
3664 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 &exception);
3666 } while (exception.retry);
3667 return err;
3668}
3669
3670static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04003671 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003673 struct nfs4_createdata *data;
3674 int mode = sattr->ia_mode;
3675 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003677 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3678 if (data == NULL)
3679 goto out;
3680
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003682 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003684 data->arg.ftype = NF4BLK;
3685 data->arg.u.device.specdata1 = MAJOR(rdev);
3686 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 }
3688 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003689 data->arg.ftype = NF4CHR;
3690 data->arg.u.device.specdata1 = MAJOR(rdev);
3691 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003692 } else if (!S_ISSOCK(mode)) {
3693 status = -EINVAL;
3694 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 }
David Quigley1775fd32013-05-22 12:50:42 -04003696
David Quigleyaa9c2662013-05-22 12:50:44 -04003697 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003698 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003699out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003700 nfs4_free_createdata(data);
3701out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 return status;
3703}
3704
3705static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3706 struct iattr *sattr, dev_t rdev)
3707{
3708 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003709 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003711
David Quigleyaa9c2662013-05-22 12:50:44 -04003712 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3713
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003714 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003716 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
3717 trace_nfs4_mknod(dir, &dentry->d_name, err);
3718 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 &exception);
3720 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003721
3722 nfs4_label_release_security(label);
3723
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 return err;
3725}
3726
3727static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3728 struct nfs_fsstat *fsstat)
3729{
3730 struct nfs4_statfs_arg args = {
3731 .fh = fhandle,
3732 .bitmask = server->attr_bitmask,
3733 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003734 struct nfs4_statfs_res res = {
3735 .fsstat = fsstat,
3736 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737 struct rpc_message msg = {
3738 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3739 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003740 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 };
3742
Trond Myklebust0e574af2005-10-27 22:12:38 -04003743 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003744 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745}
3746
3747static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3748{
3749 struct nfs4_exception exception = { };
3750 int err;
3751 do {
3752 err = nfs4_handle_exception(server,
3753 _nfs4_proc_statfs(server, fhandle, fsstat),
3754 &exception);
3755 } while (exception.retry);
3756 return err;
3757}
3758
3759static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3760 struct nfs_fsinfo *fsinfo)
3761{
3762 struct nfs4_fsinfo_arg args = {
3763 .fh = fhandle,
3764 .bitmask = server->attr_bitmask,
3765 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003766 struct nfs4_fsinfo_res res = {
3767 .fsinfo = fsinfo,
3768 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 struct rpc_message msg = {
3770 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3771 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003772 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 };
3774
Bryan Schumaker7c513052011-03-24 17:12:24 +00003775 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776}
3777
3778static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3779{
3780 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04003781 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 int err;
3783
3784 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04003785 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003786 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04003787 if (err == 0) {
3788 struct nfs_client *clp = server->nfs_client;
3789
3790 spin_lock(&clp->cl_lock);
3791 clp->cl_lease_time = fsinfo->lease_time * HZ;
3792 clp->cl_last_renewal = now;
3793 spin_unlock(&clp->cl_lock);
3794 break;
3795 }
3796 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 } while (exception.retry);
3798 return err;
3799}
3800
3801static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3802{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003803 int error;
3804
Trond Myklebust0e574af2005-10-27 22:12:38 -04003805 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003806 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003807 if (error == 0) {
3808 /* block layout checks this! */
3809 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003810 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003811 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003812
3813 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814}
3815
3816static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3817 struct nfs_pathconf *pathconf)
3818{
3819 struct nfs4_pathconf_arg args = {
3820 .fh = fhandle,
3821 .bitmask = server->attr_bitmask,
3822 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003823 struct nfs4_pathconf_res res = {
3824 .pathconf = pathconf,
3825 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 struct rpc_message msg = {
3827 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3828 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003829 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 };
3831
3832 /* None of the pathconf attributes are mandatory to implement */
3833 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3834 memset(pathconf, 0, sizeof(*pathconf));
3835 return 0;
3836 }
3837
Trond Myklebust0e574af2005-10-27 22:12:38 -04003838 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003839 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840}
3841
3842static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3843 struct nfs_pathconf *pathconf)
3844{
3845 struct nfs4_exception exception = { };
3846 int err;
3847
3848 do {
3849 err = nfs4_handle_exception(server,
3850 _nfs4_proc_pathconf(server, fhandle, pathconf),
3851 &exception);
3852 } while (exception.retry);
3853 return err;
3854}
3855
Trond Myklebust5521abf2013-03-16 20:54:34 -04003856int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04003857 const struct nfs_open_context *ctx,
3858 const struct nfs_lock_context *l_ctx,
3859 fmode_t fmode)
3860{
3861 const struct nfs_lockowner *lockowner = NULL;
3862
3863 if (l_ctx != NULL)
3864 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003865 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04003866}
3867EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
3868
Trond Myklebust5521abf2013-03-16 20:54:34 -04003869static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
3870 const struct nfs_open_context *ctx,
3871 const struct nfs_lock_context *l_ctx,
3872 fmode_t fmode)
3873{
3874 nfs4_stateid current_stateid;
3875
3876 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode))
3877 return false;
3878 return nfs4_stateid_match(stateid, &current_stateid);
3879}
3880
3881static bool nfs4_error_stateid_expired(int err)
3882{
3883 switch (err) {
3884 case -NFS4ERR_DELEG_REVOKED:
3885 case -NFS4ERR_ADMIN_REVOKED:
3886 case -NFS4ERR_BAD_STATEID:
3887 case -NFS4ERR_STALE_STATEID:
3888 case -NFS4ERR_OLD_STATEID:
3889 case -NFS4ERR_OPENMODE:
3890 case -NFS4ERR_EXPIRED:
3891 return true;
3892 }
3893 return false;
3894}
3895
Benny Halevyd20581a2011-05-22 19:52:03 +03003896void __nfs4_read_done_cb(struct nfs_read_data *data)
3897{
Fred Isamancd841602012-04-20 14:47:44 -04003898 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003899}
3900
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003901static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902{
Fred Isamancd841602012-04-20 14:47:44 -04003903 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904
Trond Myklebustcc668ab2013-08-14 15:31:28 -04003905 trace_nfs4_read(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003906 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003907 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003908 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003910
Benny Halevyd20581a2011-05-22 19:52:03 +03003911 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003913 renew_lease(server, data->timestamp);
3914 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915}
3916
Trond Myklebust5521abf2013-03-16 20:54:34 -04003917static bool nfs4_read_stateid_changed(struct rpc_task *task,
3918 struct nfs_readargs *args)
3919{
3920
3921 if (!nfs4_error_stateid_expired(task->tk_status) ||
3922 nfs4_stateid_is_current(&args->stateid,
3923 args->context,
3924 args->lock_context,
3925 FMODE_READ))
3926 return false;
3927 rpc_restart_call_prepare(task);
3928 return true;
3929}
3930
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003931static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3932{
3933
3934 dprintk("--> %s\n", __func__);
3935
3936 if (!nfs4_sequence_done(task, &data->res.seq_res))
3937 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003938 if (nfs4_read_stateid_changed(task, &data->args))
3939 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003940 return data->read_done_cb ? data->read_done_cb(task, data) :
3941 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003942}
3943
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003944static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003947 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003948 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003949 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950}
3951
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003952static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3953{
Trond Myklebust9b206142013-03-17 15:52:00 -04003954 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003955 &data->args.seq_args,
3956 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04003957 task))
3958 return;
3959 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3960 data->args.lock_context, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961}
3962
Fred Isamanb029bc92011-03-03 15:13:42 +00003963static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964{
Fred Isamancd841602012-04-20 14:47:44 -04003965 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966
Trond Myklebustcc668ab2013-08-14 15:31:28 -04003967 trace_nfs4_write(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003968 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003969 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003970 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003972 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003974 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003975 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003976 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977}
3978
Trond Myklebust5521abf2013-03-16 20:54:34 -04003979static bool nfs4_write_stateid_changed(struct rpc_task *task,
3980 struct nfs_writeargs *args)
3981{
3982
3983 if (!nfs4_error_stateid_expired(task->tk_status) ||
3984 nfs4_stateid_is_current(&args->stateid,
3985 args->context,
3986 args->lock_context,
3987 FMODE_WRITE))
3988 return false;
3989 rpc_restart_call_prepare(task);
3990 return true;
3991}
3992
Fred Isamanb029bc92011-03-03 15:13:42 +00003993static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3994{
3995 if (!nfs4_sequence_done(task, &data->res.seq_res))
3996 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003997 if (nfs4_write_stateid_changed(task, &data->args))
3998 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003999 return data->write_done_cb ? data->write_done_cb(task, data) :
4000 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00004001}
4002
Trond Myklebust5a37f852012-04-28 14:55:16 -04004003static
4004bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00004005{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004006 const struct nfs_pgio_header *hdr = data->header;
4007
4008 /* Don't request attributes for pNFS or O_DIRECT writes */
4009 if (data->ds_clp != NULL || hdr->dreq != NULL)
4010 return false;
4011 /* Otherwise, request attributes if and only if we don't hold
4012 * a delegation
4013 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004014 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004015}
Fred Isamana69aef12011-03-03 15:13:47 +00004016
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004017static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018{
Fred Isamancd841602012-04-20 14:47:44 -04004019 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004020
Trond Myklebust5a37f852012-04-28 14:55:16 -04004021 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00004022 data->args.bitmask = NULL;
4023 data->res.fattr = NULL;
4024 } else
4025 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004026
Fred Isamanb029bc92011-03-03 15:13:42 +00004027 if (!data->write_done_cb)
4028 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004029 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 data->timestamp = jiffies;
4031
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004032 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05004033 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034}
4035
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04004036static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
4037{
Trond Myklebust9b206142013-03-17 15:52:00 -04004038 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004039 &data->args.seq_args,
4040 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004041 task))
4042 return;
4043 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4044 data->args.lock_context, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045}
4046
Fred Isaman0b7c0152012-04-20 14:47:39 -04004047static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4048{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004049 nfs4_setup_sequence(NFS_SERVER(data->inode),
4050 &data->args.seq_args,
4051 &data->res.seq_res,
4052 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053}
4054
Fred Isaman0b7c0152012-04-20 14:47:39 -04004055static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004058
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004059 trace_nfs4_commit(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004060 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004061 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004062 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004064 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065}
4066
Fred Isaman0b7c0152012-04-20 14:47:39 -04004067static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004068{
4069 if (!nfs4_sequence_done(task, &data->res.seq_res))
4070 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004071 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004072}
4073
Fred Isaman0b7c0152012-04-20 14:47:39 -04004074static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004076 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004077
Fred Isaman0b7c0152012-04-20 14:47:39 -04004078 if (data->commit_done_cb == NULL)
4079 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004080 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004081 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05004082 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083}
4084
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004085struct nfs4_renewdata {
4086 struct nfs_client *client;
4087 unsigned long timestamp;
4088};
4089
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090/*
4091 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4092 * standalone procedure for queueing an asynchronous RENEW.
4093 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004094static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004095{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004096 struct nfs4_renewdata *data = calldata;
4097 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004098
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004099 if (atomic_read(&clp->cl_count) > 1)
4100 nfs4_schedule_state_renewal(clp);
4101 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004102 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004103}
4104
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004105static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004107 struct nfs4_renewdata *data = calldata;
4108 struct nfs_client *clp = data->client;
4109 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004111 trace_nfs4_renew_async(clp, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04004113 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004114 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4115 return;
4116 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004117 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004118 return;
4119 }
4120 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004122 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123}
4124
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004125static const struct rpc_call_ops nfs4_renew_ops = {
4126 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004127 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004128};
4129
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004130static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131{
4132 struct rpc_message msg = {
4133 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4134 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004135 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004137 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004139 if (renew_flags == 0)
4140 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004141 if (!atomic_inc_not_zero(&clp->cl_count))
4142 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004143 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004144 if (data == NULL)
4145 return -ENOMEM;
4146 data->client = clp;
4147 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004148 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004149 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150}
4151
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004152static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153{
4154 struct rpc_message msg = {
4155 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4156 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004157 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 };
4159 unsigned long now = jiffies;
4160 int status;
4161
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004162 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 if (status < 0)
4164 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004165 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 return 0;
4167}
4168
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004169static inline int nfs4_server_supports_acls(struct nfs_server *server)
4170{
4171 return (server->caps & NFS_CAP_ACLS)
4172 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
4173 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
4174}
4175
Trond Myklebust21f498c2012-08-24 10:59:25 -04004176/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4177 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004178 * the stack.
4179 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004180#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004181
Neil Hormane9e3d722011-03-04 19:26:03 -05004182static int buf_to_pages_noslab(const void *buf, size_t buflen,
4183 struct page **pages, unsigned int *pgbase)
4184{
4185 struct page *newpage, **spages;
4186 int rc = 0;
4187 size_t len;
4188 spages = pages;
4189
4190 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004191 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004192 newpage = alloc_page(GFP_KERNEL);
4193
4194 if (newpage == NULL)
4195 goto unwind;
4196 memcpy(page_address(newpage), buf, len);
4197 buf += len;
4198 buflen -= len;
4199 *pages++ = newpage;
4200 rc++;
4201 } while (buflen != 0);
4202
4203 return rc;
4204
4205unwind:
4206 for(; rc > 0; rc--)
4207 __free_page(spages[rc-1]);
4208 return -ENOMEM;
4209}
4210
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004211struct nfs4_cached_acl {
4212 int cached;
4213 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004214 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004215};
4216
4217static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004218{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004219 struct nfs_inode *nfsi = NFS_I(inode);
4220
4221 spin_lock(&inode->i_lock);
4222 kfree(nfsi->nfs4_acl);
4223 nfsi->nfs4_acl = acl;
4224 spin_unlock(&inode->i_lock);
4225}
4226
4227static void nfs4_zap_acl_attr(struct inode *inode)
4228{
4229 nfs4_set_cached_acl(inode, NULL);
4230}
4231
4232static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4233{
4234 struct nfs_inode *nfsi = NFS_I(inode);
4235 struct nfs4_cached_acl *acl;
4236 int ret = -ENOENT;
4237
4238 spin_lock(&inode->i_lock);
4239 acl = nfsi->nfs4_acl;
4240 if (acl == NULL)
4241 goto out;
4242 if (buf == NULL) /* user is just asking for length */
4243 goto out_len;
4244 if (acl->cached == 0)
4245 goto out;
4246 ret = -ERANGE; /* see getxattr(2) man page */
4247 if (acl->len > buflen)
4248 goto out;
4249 memcpy(buf, acl->data, acl->len);
4250out_len:
4251 ret = acl->len;
4252out:
4253 spin_unlock(&inode->i_lock);
4254 return ret;
4255}
4256
Sachin Prabhu5794d212012-04-17 14:36:40 +01004257static 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 +00004258{
4259 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004260 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004261
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004262 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004263 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004264 if (acl == NULL)
4265 goto out;
4266 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004267 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004268 } else {
4269 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4270 if (acl == NULL)
4271 goto out;
4272 acl->cached = 0;
4273 }
4274 acl->len = acl_len;
4275out:
4276 nfs4_set_cached_acl(inode, acl);
4277}
4278
Andy Adamsonbf118a32011-12-07 11:55:27 -05004279/*
4280 * The getxattr API returns the required buffer length when called with a
4281 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4282 * the required buf. On a NULL buf, we send a page of data to the server
4283 * guessing that the ACL request can be serviced by a page. If so, we cache
4284 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4285 * the cache. If not so, we throw away the page, and cache the required
4286 * length. The next getxattr call will then produce another round trip to
4287 * the server, this time with the input buf of the required size.
4288 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004289static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004290{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004291 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004292 struct nfs_getaclargs args = {
4293 .fh = NFS_FH(inode),
4294 .acl_pages = pages,
4295 .acl_len = buflen,
4296 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004297 struct nfs_getaclres res = {
4298 .acl_len = buflen,
4299 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004300 struct rpc_message msg = {
4301 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4302 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004303 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004304 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004305 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4306 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004307
Andy Adamsonbf118a32011-12-07 11:55:27 -05004308 /* As long as we're doing a round trip to the server anyway,
4309 * let's be prepared for a page of acl data. */
4310 if (npages == 0)
4311 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004312 if (npages > ARRAY_SIZE(pages))
4313 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004314
Andy Adamsonbf118a32011-12-07 11:55:27 -05004315 for (i = 0; i < npages; i++) {
4316 pages[i] = alloc_page(GFP_KERNEL);
4317 if (!pages[i])
4318 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004319 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004320
4321 /* for decoding across pages */
4322 res.acl_scratch = alloc_page(GFP_KERNEL);
4323 if (!res.acl_scratch)
4324 goto out_free;
4325
Andy Adamsonbf118a32011-12-07 11:55:27 -05004326 args.acl_len = npages * PAGE_SIZE;
4327 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004328
Peng Taode040be2012-01-10 22:42:47 +08004329 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004330 __func__, buf, buflen, npages, args.acl_len);
4331 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4332 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004333 if (ret)
4334 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004335
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004336 /* Handle the case where the passed-in buffer is too short */
4337 if (res.acl_flags & NFS4_ACL_TRUNC) {
4338 /* Did the user only issue a request for the acl length? */
4339 if (buf == NULL)
4340 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004341 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004342 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004343 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004344 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004345 if (buf) {
4346 if (res.acl_len > buflen) {
4347 ret = -ERANGE;
4348 goto out_free;
4349 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004350 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004351 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004352out_ok:
4353 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004354out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004355 for (i = 0; i < npages; i++)
4356 if (pages[i])
4357 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004358 if (res.acl_scratch)
4359 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004360 return ret;
4361}
4362
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004363static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4364{
4365 struct nfs4_exception exception = { };
4366 ssize_t ret;
4367 do {
4368 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04004369 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004370 if (ret >= 0)
4371 break;
4372 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4373 } while (exception.retry);
4374 return ret;
4375}
4376
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004377static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4378{
4379 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004380 int ret;
4381
4382 if (!nfs4_server_supports_acls(server))
4383 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004384 ret = nfs_revalidate_inode(server, inode);
4385 if (ret < 0)
4386 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004387 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4388 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004389 ret = nfs4_read_cached_acl(inode, buf, buflen);
4390 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004391 /* -ENOENT is returned if there is no ACL or if there is an ACL
4392 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004393 return ret;
4394 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004395}
4396
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004397static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004398{
4399 struct nfs_server *server = NFS_SERVER(inode);
4400 struct page *pages[NFS4ACL_MAXPAGES];
4401 struct nfs_setaclargs arg = {
4402 .fh = NFS_FH(inode),
4403 .acl_pages = pages,
4404 .acl_len = buflen,
4405 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004406 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004407 struct rpc_message msg = {
4408 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4409 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004410 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004411 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004412 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004413 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004414
4415 if (!nfs4_server_supports_acls(server))
4416 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004417 if (npages > ARRAY_SIZE(pages))
4418 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004419 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4420 if (i < 0)
4421 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004422 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004423 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004424
4425 /*
4426 * Free each page after tx, so the only ref left is
4427 * held by the network stack
4428 */
4429 for (; i > 0; i--)
4430 put_page(pages[i-1]);
4431
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004432 /*
4433 * Acl update can result in inode attribute update.
4434 * so mark the attribute cache invalid.
4435 */
4436 spin_lock(&inode->i_lock);
4437 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4438 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004439 nfs_access_zap_cache(inode);
4440 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004441 return ret;
4442}
4443
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004444static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4445{
4446 struct nfs4_exception exception = { };
4447 int err;
4448 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004449 err = __nfs4_proc_set_acl(inode, buf, buflen);
4450 trace_nfs4_set_acl(inode, err);
4451 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004452 &exception);
4453 } while (exception.retry);
4454 return err;
4455}
4456
David Quigleyaa9c2662013-05-22 12:50:44 -04004457#ifdef CONFIG_NFS_V4_SECURITY_LABEL
4458static int _nfs4_get_security_label(struct inode *inode, void *buf,
4459 size_t buflen)
4460{
4461 struct nfs_server *server = NFS_SERVER(inode);
4462 struct nfs_fattr fattr;
4463 struct nfs4_label label = {0, 0, buflen, buf};
4464
4465 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4466 struct nfs4_getattr_arg args = {
4467 .fh = NFS_FH(inode),
4468 .bitmask = bitmask,
4469 };
4470 struct nfs4_getattr_res res = {
4471 .fattr = &fattr,
4472 .label = &label,
4473 .server = server,
4474 };
4475 struct rpc_message msg = {
4476 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4477 .rpc_argp = &args,
4478 .rpc_resp = &res,
4479 };
4480 int ret;
4481
4482 nfs_fattr_init(&fattr);
4483
4484 ret = rpc_call_sync(server->client, &msg, 0);
4485 if (ret)
4486 return ret;
4487 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4488 return -ENOENT;
4489 if (buflen < label.len)
4490 return -ERANGE;
4491 return 0;
4492}
4493
4494static int nfs4_get_security_label(struct inode *inode, void *buf,
4495 size_t buflen)
4496{
4497 struct nfs4_exception exception = { };
4498 int err;
4499
4500 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4501 return -EOPNOTSUPP;
4502
4503 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004504 err = _nfs4_get_security_label(inode, buf, buflen);
4505 trace_nfs4_get_security_label(inode, err);
4506 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004507 &exception);
4508 } while (exception.retry);
4509 return err;
4510}
4511
4512static int _nfs4_do_set_security_label(struct inode *inode,
4513 struct nfs4_label *ilabel,
4514 struct nfs_fattr *fattr,
4515 struct nfs4_label *olabel)
4516{
4517
4518 struct iattr sattr = {0};
4519 struct nfs_server *server = NFS_SERVER(inode);
4520 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4521 struct nfs_setattrargs args = {
4522 .fh = NFS_FH(inode),
4523 .iap = &sattr,
4524 .server = server,
4525 .bitmask = bitmask,
4526 .label = ilabel,
4527 };
4528 struct nfs_setattrres res = {
4529 .fattr = fattr,
4530 .label = olabel,
4531 .server = server,
4532 };
4533 struct rpc_message msg = {
4534 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
4535 .rpc_argp = &args,
4536 .rpc_resp = &res,
4537 };
4538 int status;
4539
4540 nfs4_stateid_copy(&args.stateid, &zero_stateid);
4541
4542 status = rpc_call_sync(server->client, &msg, 0);
4543 if (status)
4544 dprintk("%s failed: %d\n", __func__, status);
4545
4546 return status;
4547}
4548
4549static int nfs4_do_set_security_label(struct inode *inode,
4550 struct nfs4_label *ilabel,
4551 struct nfs_fattr *fattr,
4552 struct nfs4_label *olabel)
4553{
4554 struct nfs4_exception exception = { };
4555 int err;
4556
4557 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004558 err = _nfs4_do_set_security_label(inode, ilabel,
4559 fattr, olabel);
4560 trace_nfs4_set_security_label(inode, err);
4561 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004562 &exception);
4563 } while (exception.retry);
4564 return err;
4565}
4566
4567static int
4568nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4569{
4570 struct nfs4_label ilabel, *olabel = NULL;
4571 struct nfs_fattr fattr;
4572 struct rpc_cred *cred;
4573 struct inode *inode = dentry->d_inode;
4574 int status;
4575
4576 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4577 return -EOPNOTSUPP;
4578
4579 nfs_fattr_init(&fattr);
4580
4581 ilabel.pi = 0;
4582 ilabel.lfs = 0;
4583 ilabel.label = (char *)buf;
4584 ilabel.len = buflen;
4585
4586 cred = rpc_lookup_cred();
4587 if (IS_ERR(cred))
4588 return PTR_ERR(cred);
4589
4590 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4591 if (IS_ERR(olabel)) {
4592 status = -PTR_ERR(olabel);
4593 goto out;
4594 }
4595
4596 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4597 if (status == 0)
4598 nfs_setsecurity(inode, &fattr, olabel);
4599
4600 nfs4_label_free(olabel);
4601out:
4602 put_rpccred(cred);
4603 return status;
4604}
4605#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4606
4607
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004609nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004611 struct nfs_client *clp = server->nfs_client;
4612
4613 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 return 0;
4615 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004616 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004617 case -NFS4ERR_ADMIN_REVOKED:
4618 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004619 if (state == NULL)
4620 break;
4621 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004622 case -NFS4ERR_OPENMODE:
4623 if (state == NULL)
4624 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004625 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4626 goto stateid_invalid;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004627 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004628 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004629 if (state != NULL) {
4630 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4631 goto stateid_invalid;
4632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004634 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004635 nfs4_schedule_lease_recovery(clp);
4636 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004637#if defined(CONFIG_NFS_V4_1)
4638 case -NFS4ERR_BADSESSION:
4639 case -NFS4ERR_BADSLOT:
4640 case -NFS4ERR_BAD_HIGH_SLOT:
4641 case -NFS4ERR_DEADSESSION:
4642 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4643 case -NFS4ERR_SEQ_FALSE_RETRY:
4644 case -NFS4ERR_SEQ_MISORDERED:
4645 dprintk("%s ERROR %d, Reset session\n", __func__,
4646 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004647 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004648 task->tk_status = 0;
4649 return -EAGAIN;
4650#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004652 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004653 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4655 task->tk_status = 0;
4656 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004657 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 case -NFS4ERR_OLD_STATEID:
4659 task->tk_status = 0;
4660 return -EAGAIN;
4661 }
4662 task->tk_status = nfs4_map_errors(task->tk_status);
4663 return 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004664stateid_invalid:
4665 task->tk_status = -EIO;
4666 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004667wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004668 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004669 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4670 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4671 task->tk_status = 0;
4672 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673}
4674
Chuck Leverf0920752012-05-21 22:45:41 -04004675static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4676 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004677{
4678 __be32 verf[2];
4679
Chuck Lever2c820d92012-05-21 22:45:33 -04004680 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4681 /* An impossible timestamp guarantees this value
4682 * will never match a generated boot time. */
4683 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04004684 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04004685 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004686 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04004687 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
4688 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04004689 }
Chuck Levercd937102012-03-02 17:14:31 -05004690 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4691}
4692
Chuck Levere984a552012-09-14 17:24:21 -04004693static unsigned int
4694nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4695 char *buf, size_t len)
4696{
4697 unsigned int result;
4698
4699 rcu_read_lock();
4700 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4701 clp->cl_ipaddr,
4702 rpc_peeraddr2str(clp->cl_rpcclient,
4703 RPC_DISPLAY_ADDR),
4704 rpc_peeraddr2str(clp->cl_rpcclient,
4705 RPC_DISPLAY_PROTO));
4706 rcu_read_unlock();
4707 return result;
4708}
4709
4710static unsigned int
4711nfs4_init_uniform_client_string(const struct nfs_client *clp,
4712 char *buf, size_t len)
4713{
Trond Myklebust55b59292013-07-24 16:36:35 -04004714 const char *nodename = clp->cl_rpcclient->cl_nodename;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004715
4716 if (nfs4_client_id_uniquifier[0] != '\0')
Trond Myklebust55b59292013-07-24 16:36:35 -04004717 return scnprintf(buf, len, "Linux NFSv%u.%u %s/%s",
4718 clp->rpc_ops->version,
4719 clp->cl_minorversion,
4720 nfs4_client_id_uniquifier,
4721 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004722 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4723 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004724 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004725}
4726
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004727/**
4728 * nfs4_proc_setclientid - Negotiate client ID
4729 * @clp: state data structure
4730 * @program: RPC program for NFSv4 callback service
4731 * @port: IP port number for NFS4 callback service
4732 * @cred: RPC credential to use for this call
4733 * @res: where to place the result
4734 *
4735 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4736 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004737int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4738 unsigned short port, struct rpc_cred *cred,
4739 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740{
4741 nfs4_verifier sc_verifier;
4742 struct nfs4_setclientid setclientid = {
4743 .sc_verifier = &sc_verifier,
4744 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004745 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 };
4747 struct rpc_message msg = {
4748 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4749 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004750 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004751 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004753 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754
Chuck Leverde734832012-07-11 16:30:50 -04004755 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004756 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004757 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4758 setclientid.sc_name_len =
4759 nfs4_init_uniform_client_string(clp,
4760 setclientid.sc_name,
4761 sizeof(setclientid.sc_name));
4762 else
4763 setclientid.sc_name_len =
4764 nfs4_init_nonuniform_client_string(clp,
4765 setclientid.sc_name,
4766 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004767 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004768 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004769 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Djalal Harounife2d5392013-06-18 17:58:12 +01004770 sizeof(setclientid.sc_netid), "%s",
Chuck Leverd4d3c502007-12-10 14:57:09 -05004771 rpc_peeraddr2str(clp->cl_rpcclient,
4772 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004773 rcu_read_unlock();
4774 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004775 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776 clp->cl_ipaddr, port >> 8, port & 255);
4777
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004778 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4779 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4780 setclientid.sc_name_len, setclientid.sc_name);
4781 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004782 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004783 dprintk("NFS reply setclientid: %d\n", status);
4784 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785}
4786
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004787/**
4788 * nfs4_proc_setclientid_confirm - Confirm client ID
4789 * @clp: state data structure
4790 * @res: result of a previous SETCLIENTID
4791 * @cred: RPC credential to use for this call
4792 *
4793 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4794 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004795int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004796 struct nfs4_setclientid_res *arg,
4797 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 struct rpc_message msg = {
4800 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004801 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004802 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 int status;
4805
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004806 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4807 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4808 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004809 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004810 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004811 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812 return status;
4813}
4814
Trond Myklebustfe650402006-01-03 09:55:18 +01004815struct nfs4_delegreturndata {
4816 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004817 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004818 struct nfs_fh fh;
4819 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004820 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004821 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004822 int rpc_status;
4823};
4824
Trond Myklebustfe650402006-01-03 09:55:18 +01004825static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4826{
4827 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004828
Trond Myklebust14516c32010-07-31 14:29:06 -04004829 if (!nfs4_sequence_done(task, &data->res.seq_res))
4830 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004831
Trond Myklebustca8acf82013-08-13 10:36:56 -04004832 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004833 switch (task->tk_status) {
4834 case -NFS4ERR_STALE_STATEID:
4835 case -NFS4ERR_EXPIRED:
4836 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004837 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004838 break;
4839 default:
4840 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4841 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004842 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004843 return;
4844 }
4845 }
4846 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004847}
4848
4849static void nfs4_delegreturn_release(void *calldata)
4850{
Trond Myklebustfe650402006-01-03 09:55:18 +01004851 kfree(calldata);
4852}
4853
Andy Adamson938e1012009-04-01 09:22:28 -04004854#if defined(CONFIG_NFS_V4_1)
4855static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4856{
4857 struct nfs4_delegreturndata *d_data;
4858
4859 d_data = (struct nfs4_delegreturndata *)data;
4860
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004861 nfs4_setup_sequence(d_data->res.server,
4862 &d_data->args.seq_args,
4863 &d_data->res.seq_res,
4864 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004865}
4866#endif /* CONFIG_NFS_V4_1 */
4867
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004868static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004869#if defined(CONFIG_NFS_V4_1)
4870 .rpc_call_prepare = nfs4_delegreturn_prepare,
4871#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004872 .rpc_call_done = nfs4_delegreturn_done,
4873 .rpc_release = nfs4_delegreturn_release,
4874};
4875
Trond Myklebuste6f81072008-01-24 18:14:34 -05004876static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004877{
4878 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004879 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004880 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004881 struct rpc_message msg = {
4882 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4883 .rpc_cred = cred,
4884 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004885 struct rpc_task_setup task_setup_data = {
4886 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004887 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004888 .callback_ops = &nfs4_delegreturn_ops,
4889 .flags = RPC_TASK_ASYNC,
4890 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004891 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004892
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004893 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004894 if (data == NULL)
4895 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004896 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004897 data->args.fhandle = &data->fh;
4898 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004899 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004900 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004901 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004902 data->res.fattr = &data->fattr;
4903 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004904 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004905 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004906 data->rpc_status = 0;
4907
Trond Myklebustc970aa82007-07-14 15:39:59 -04004908 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004909 msg.rpc_argp = &data->args;
4910 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004911 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004912 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004913 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004914 if (!issync)
4915 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004916 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004917 if (status != 0)
4918 goto out;
4919 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004920 if (status == 0)
4921 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4922 else
4923 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004924out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004925 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004926 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927}
4928
Trond Myklebuste6f81072008-01-24 18:14:34 -05004929int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930{
4931 struct nfs_server *server = NFS_SERVER(inode);
4932 struct nfs4_exception exception = { };
4933 int err;
4934 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004935 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Trond Myklebustca8acf82013-08-13 10:36:56 -04004936 trace_nfs4_delegreturn(inode, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 switch (err) {
4938 case -NFS4ERR_STALE_STATEID:
4939 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 case 0:
4941 return 0;
4942 }
4943 err = nfs4_handle_exception(server, err, &exception);
4944 } while (exception.retry);
4945 return err;
4946}
4947
4948#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4949#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4950
4951/*
4952 * sleep, with exponential backoff, and retry the LOCK operation.
4953 */
4954static unsigned long
4955nfs4_set_lock_task_retry(unsigned long timeout)
4956{
Colin Cross416ad3c2013-05-06 23:50:06 +00004957 freezable_schedule_timeout_killable_unsafe(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958 timeout <<= 1;
4959 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4960 return NFS4_LOCK_MAXTIMEOUT;
4961 return timeout;
4962}
4963
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4965{
4966 struct inode *inode = state->inode;
4967 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004968 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004969 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004971 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004973 struct nfs_lockt_res res = {
4974 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975 };
4976 struct rpc_message msg = {
4977 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4978 .rpc_argp = &arg,
4979 .rpc_resp = &res,
4980 .rpc_cred = state->owner->so_cred,
4981 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982 struct nfs4_lock_state *lsp;
4983 int status;
4984
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004985 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004986 status = nfs4_set_lock_state(state, request);
4987 if (status != 0)
4988 goto out;
4989 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004990 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004991 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004992 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004993 switch (status) {
4994 case 0:
4995 request->fl_type = F_UNLCK;
4996 break;
4997 case -NFS4ERR_DENIED:
4998 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005000 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005001out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002 return status;
5003}
5004
5005static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5006{
5007 struct nfs4_exception exception = { };
5008 int err;
5009
5010 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005011 err = _nfs4_proc_getlk(state, cmd, request);
5012 trace_nfs4_get_lock(request, state, cmd, err);
5013 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 &exception);
5015 } while (exception.retry);
5016 return err;
5017}
5018
5019static int do_vfs_lock(struct file *file, struct file_lock *fl)
5020{
5021 int res = 0;
5022 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5023 case FL_POSIX:
5024 res = posix_lock_file_wait(file, fl);
5025 break;
5026 case FL_FLOCK:
5027 res = flock_lock_file_wait(file, fl);
5028 break;
5029 default:
5030 BUG();
5031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 return res;
5033}
5034
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005035struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005036 struct nfs_locku_args arg;
5037 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005038 struct nfs4_lock_state *lsp;
5039 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005040 struct file_lock fl;
5041 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005042 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005043};
5044
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005045static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5046 struct nfs_open_context *ctx,
5047 struct nfs4_lock_state *lsp,
5048 struct nfs_seqid *seqid)
5049{
5050 struct nfs4_unlockdata *p;
5051 struct inode *inode = lsp->ls_state->inode;
5052
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005053 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005054 if (p == NULL)
5055 return NULL;
5056 p->arg.fh = NFS_FH(inode);
5057 p->arg.fl = &p->fl;
5058 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005059 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005060 p->arg.stateid = &lsp->ls_stateid;
5061 p->lsp = lsp;
5062 atomic_inc(&lsp->ls_count);
5063 /* Ensure we don't close file until we're done freeing locks! */
5064 p->ctx = get_nfs_open_context(ctx);
5065 memcpy(&p->fl, fl, sizeof(p->fl));
5066 p->server = NFS_SERVER(inode);
5067 return p;
5068}
5069
Trond Myklebust06f814a2006-01-03 09:55:07 +01005070static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005071{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005072 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005073 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005074 nfs4_put_lock_state(calldata->lsp);
5075 put_nfs_open_context(calldata->ctx);
5076 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005077}
5078
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005079static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005080{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005081 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005082
Trond Myklebust14516c32010-07-31 14:29:06 -04005083 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5084 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005085 switch (task->tk_status) {
5086 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05005087 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
5088 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005089 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005090 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005091 case -NFS4ERR_BAD_STATEID:
5092 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005093 case -NFS4ERR_STALE_STATEID:
5094 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005095 break;
5096 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005097 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005098 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005099 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005100 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005101}
5102
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005103static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005104{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005105 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005107 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005108 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04005109 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005110 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005111 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005112 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005113 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005114 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005115 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005116 &calldata->res.seq_res,
5117 task) != 0)
5118 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005119 return;
5120out_no_action:
5121 task->tk_action = NULL;
5122out_wait:
5123 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124}
5125
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005126static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005127 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005128 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005129 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005130};
5131
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005132static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5133 struct nfs_open_context *ctx,
5134 struct nfs4_lock_state *lsp,
5135 struct nfs_seqid *seqid)
5136{
5137 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005138 struct rpc_message msg = {
5139 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5140 .rpc_cred = ctx->cred,
5141 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005142 struct rpc_task_setup task_setup_data = {
5143 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005144 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005145 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005146 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005147 .flags = RPC_TASK_ASYNC,
5148 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005149
Frank Filz137d6ac2007-07-09 15:32:29 -07005150 /* Ensure this is an unlock - when canceling a lock, the
5151 * canceled lock is passed in, and it won't be an unlock.
5152 */
5153 fl->fl_type = F_UNLCK;
5154
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005155 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5156 if (data == NULL) {
5157 nfs_free_seqid(seqid);
5158 return ERR_PTR(-ENOMEM);
5159 }
5160
Trond Myklebust9d12b212012-01-17 22:04:25 -05005161 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005162 msg.rpc_argp = &data->arg;
5163 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005164 task_setup_data.callback_data = data;
5165 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005166}
5167
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5169{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005170 struct inode *inode = state->inode;
5171 struct nfs4_state_owner *sp = state->owner;
5172 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005173 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005174 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005175 struct rpc_task *task;
5176 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005177 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178
Trond Myklebust9b073572006-06-29 16:38:34 -04005179 status = nfs4_set_lock_state(state, request);
5180 /* Unlock _before_ we do the RPC call */
5181 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005182 /* Exclude nfs_delegation_claim_locks() */
5183 mutex_lock(&sp->so_delegreturn_mutex);
5184 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005185 down_read(&nfsi->rwsem);
5186 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
5187 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005188 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005189 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005190 }
5191 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005192 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005193 if (status != 0)
5194 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005195 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005196 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005197 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5198 goto out;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005199 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005200 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005201 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04005202 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005203 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005204 status = PTR_ERR(task);
5205 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005206 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005207 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005208 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005209out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005210 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005211 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005212 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213}
5214
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005215struct nfs4_lockdata {
5216 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005217 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005218 struct nfs4_lock_state *lsp;
5219 struct nfs_open_context *ctx;
5220 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005221 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005222 int rpc_status;
5223 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005224 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005225};
5226
5227static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005228 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5229 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005230{
5231 struct nfs4_lockdata *p;
5232 struct inode *inode = lsp->ls_state->inode;
5233 struct nfs_server *server = NFS_SERVER(inode);
5234
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005235 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005236 if (p == NULL)
5237 return NULL;
5238
5239 p->arg.fh = NFS_FH(inode);
5240 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005241 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005242 if (p->arg.open_seqid == NULL)
5243 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005244 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005245 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005246 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005247 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04005248 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005249 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005250 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005251 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005252 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005253 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005254 atomic_inc(&lsp->ls_count);
5255 p->ctx = get_nfs_open_context(ctx);
5256 memcpy(&p->fl, fl, sizeof(p->fl));
5257 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005258out_free_seqid:
5259 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005260out_free:
5261 kfree(p);
5262 return NULL;
5263}
5264
5265static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5266{
5267 struct nfs4_lockdata *data = calldata;
5268 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269
Harvey Harrison3110ff82008-05-02 13:42:44 -07005270 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005271 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005272 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005273 /* Do we need to do an open_to_lock_owner? */
5274 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005275 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005276 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005277 }
Trond Myklebust8e472f32013-04-20 01:30:53 -04005278 data->arg.open_stateid = &state->open_stateid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005279 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005280 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005281 } else
5282 data->arg.new_lock_owner = 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04005283 if (!nfs4_valid_open_stateid(state)) {
5284 data->rpc_status = -EBADF;
5285 task->tk_action = NULL;
5286 goto out_release_open_seqid;
5287 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005288 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005289 if (nfs4_setup_sequence(data->server,
5290 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005291 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005292 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04005293 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04005294out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005295 nfs_release_seqid(data->arg.open_seqid);
5296out_release_lock_seqid:
5297 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005298out_wait:
5299 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005300 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005301}
5302
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005303static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5304{
5305 struct nfs4_lockdata *data = calldata;
5306
Harvey Harrison3110ff82008-05-02 13:42:44 -07005307 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005308
Trond Myklebust14516c32010-07-31 14:29:06 -04005309 if (!nfs4_sequence_done(task, &data->res.seq_res))
5310 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04005311
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005312 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005313 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005314 if (data->rpc_status == 0)
5315 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
5316 else
5317 goto out;
5318 }
5319 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05005320 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005321 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04005322 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005323 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005324out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07005325 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005326}
5327
5328static void nfs4_lock_release(void *calldata)
5329{
5330 struct nfs4_lockdata *data = calldata;
5331
Harvey Harrison3110ff82008-05-02 13:42:44 -07005332 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005333 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005334 if (data->cancelled != 0) {
5335 struct rpc_task *task;
5336 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5337 data->arg.lock_seqid);
5338 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08005339 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005340 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005341 } else
5342 nfs_free_seqid(data->arg.lock_seqid);
5343 nfs4_put_lock_state(data->lsp);
5344 put_nfs_open_context(data->ctx);
5345 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005346 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005347}
5348
5349static const struct rpc_call_ops nfs4_lock_ops = {
5350 .rpc_call_prepare = nfs4_lock_prepare,
5351 .rpc_call_done = nfs4_lock_done,
5352 .rpc_release = nfs4_lock_release,
5353};
5354
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005355static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5356{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005357 switch (error) {
5358 case -NFS4ERR_ADMIN_REVOKED:
5359 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05005360 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005361 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04005362 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05005363 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005364 break;
5365 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005366 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05005367 case -NFS4ERR_EXPIRED:
5368 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005369 };
5370}
5371
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005372static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005373{
5374 struct nfs4_lockdata *data;
5375 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005376 struct rpc_message msg = {
5377 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5378 .rpc_cred = state->owner->so_cred,
5379 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005380 struct rpc_task_setup task_setup_data = {
5381 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005382 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005383 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005384 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005385 .flags = RPC_TASK_ASYNC,
5386 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005387 int ret;
5388
Harvey Harrison3110ff82008-05-02 13:42:44 -07005389 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005390 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005391 fl->fl_u.nfs4_fl.owner,
5392 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005393 if (data == NULL)
5394 return -ENOMEM;
5395 if (IS_SETLKW(cmd))
5396 data->arg.block = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -05005397 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005398 msg.rpc_argp = &data->arg;
5399 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005400 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005401 if (recovery_type > NFS_LOCK_NEW) {
5402 if (recovery_type == NFS_LOCK_RECLAIM)
5403 data->arg.reclaim = NFS_LOCK_RECLAIM;
5404 nfs4_set_sequence_privileged(&data->arg.seq_args);
5405 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04005406 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005407 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005408 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005409 ret = nfs4_wait_for_completion_rpc_task(task);
5410 if (ret == 0) {
5411 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005412 if (ret)
5413 nfs4_handle_setlk_error(data->server, data->lsp,
5414 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005415 } else
5416 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005417 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005418 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005419 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420}
5421
5422static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5423{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005424 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005425 struct nfs4_exception exception = {
5426 .inode = state->inode,
5427 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005428 int err;
5429
5430 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005431 /* Cache the lock if possible... */
5432 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5433 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005434 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005435 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
Trond Myklebust168667c2010-10-19 19:47:49 -04005436 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00005437 break;
5438 nfs4_handle_exception(server, err, &exception);
5439 } while (exception.retry);
5440 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441}
5442
5443static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5444{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005445 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005446 struct nfs4_exception exception = {
5447 .inode = state->inode,
5448 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005449 int err;
5450
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005451 err = nfs4_set_lock_state(state, request);
5452 if (err != 0)
5453 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00005454 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005455 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5456 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005457 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005458 trace_nfs4_lock_expired(request, state, F_SETLK, err);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005459 switch (err) {
5460 default:
5461 goto out;
5462 case -NFS4ERR_GRACE:
5463 case -NFS4ERR_DELAY:
5464 nfs4_handle_exception(server, err, &exception);
5465 err = 0;
5466 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005467 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005468out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00005469 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470}
5471
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005472#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005473/**
5474 * nfs41_check_expired_locks - possibly free a lock stateid
5475 *
5476 * @state: NFSv4 state for an inode
5477 *
5478 * Returns NFS_OK if recovery for this stateid is now finished.
5479 * Otherwise a negative NFS4ERR value is returned.
5480 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005481static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005482{
Chuck Levereb64cf92012-07-11 16:30:05 -04005483 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005484 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005485 struct nfs_server *server = NFS_SERVER(state->inode);
5486
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005487 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04005488 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005489 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
5490
5491 status = nfs41_test_stateid(server,
5492 &lsp->ls_stateid,
5493 cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04005494 trace_nfs4_test_lock_stateid(state, lsp, status);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005495 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005496 /* Free the stateid unless the server
5497 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04005498 if (status != -NFS4ERR_BAD_STATEID)
5499 nfs41_free_stateid(server,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005500 &lsp->ls_stateid,
5501 cred);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005502 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005503 ret = status;
5504 }
5505 }
5506 };
5507
5508 return ret;
5509}
5510
5511static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5512{
5513 int status = NFS_OK;
5514
5515 if (test_bit(LK_STATE_IN_USE, &state->flags))
5516 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005517 if (status != NFS_OK)
5518 status = nfs4_lock_expired(state, request);
5519 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005520}
5521#endif
5522
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5524{
Trond Myklebust9a99af492013-02-04 20:17:49 -05005525 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005526 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005527 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005528 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005529 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005531 if ((fl_flags & FL_POSIX) &&
5532 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5533 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005534 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005535 status = nfs4_set_lock_state(state, request);
5536 if (status != 0)
5537 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005538 request->fl_flags |= FL_ACCESS;
5539 status = do_vfs_lock(request->fl_file, request);
5540 if (status < 0)
5541 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005542 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005543 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005544 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005545 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005546 request->fl_flags = fl_flags & ~FL_SLEEP;
5547 status = do_vfs_lock(request->fl_file, request);
5548 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005549 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05005550 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5551 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005552 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005553 if (status != 0)
Trond Myklebust9a99af492013-02-04 20:17:49 -05005554 goto out;
5555 down_read(&nfsi->rwsem);
5556 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5557 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005558 goto out_unlock;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005559 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005560 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005561 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005562 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005563 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5564 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005565out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005566 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005567out:
5568 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569 return status;
5570}
5571
5572static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5573{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005574 struct nfs4_exception exception = {
5575 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005576 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005577 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578 int err;
5579
5580 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005581 err = _nfs4_proc_setlk(state, cmd, request);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005582 trace_nfs4_set_lock(request, state, cmd, err);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005583 if (err == -NFS4ERR_DENIED)
5584 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005586 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587 } while (exception.retry);
5588 return err;
5589}
5590
5591static int
5592nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5593{
5594 struct nfs_open_context *ctx;
5595 struct nfs4_state *state;
5596 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5597 int status;
5598
5599 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005600 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601 state = ctx->state;
5602
5603 if (request->fl_start < 0 || request->fl_end < 0)
5604 return -EINVAL;
5605
Trond Myklebustd9531262009-07-21 19:22:38 -04005606 if (IS_GETLK(cmd)) {
5607 if (state != NULL)
5608 return nfs4_proc_getlk(state, F_GETLK, request);
5609 return 0;
5610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611
5612 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5613 return -EINVAL;
5614
Trond Myklebustd9531262009-07-21 19:22:38 -04005615 if (request->fl_type == F_UNLCK) {
5616 if (state != NULL)
5617 return nfs4_proc_unlck(state, cmd, request);
5618 return 0;
5619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620
Trond Myklebustd9531262009-07-21 19:22:38 -04005621 if (state == NULL)
5622 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005623 /*
5624 * Don't rely on the VFS having checked the file open mode,
5625 * since it won't do this for flock() locks.
5626 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005627 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005628 case F_RDLCK:
5629 if (!(filp->f_mode & FMODE_READ))
5630 return -EBADF;
5631 break;
5632 case F_WRLCK:
5633 if (!(filp->f_mode & FMODE_WRITE))
5634 return -EBADF;
5635 }
5636
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637 do {
5638 status = nfs4_proc_setlk(state, cmd, request);
5639 if ((status != -EAGAIN) || IS_SETLK(cmd))
5640 break;
5641 timeout = nfs4_set_lock_task_retry(timeout);
5642 status = -ERESTARTSYS;
5643 if (signalled())
5644 break;
5645 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 return status;
5647}
5648
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005649int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05005650{
5651 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05005652 int err;
5653
5654 err = nfs4_set_lock_state(state, fl);
5655 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005656 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04005657 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005658 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05005659}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005660
Trond Myklebustcf470c32012-03-07 13:49:12 -05005661struct nfs_release_lockowner_data {
5662 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005663 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005664 struct nfs_release_lockowner_args args;
5665};
5666
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005667static void nfs4_release_lockowner_release(void *calldata)
5668{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005669 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005670 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005671 kfree(calldata);
5672}
5673
Trond Myklebust17280172012-03-11 13:11:00 -04005674static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005675 .rpc_release = nfs4_release_lockowner_release,
5676};
5677
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04005678static int nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005679{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005680 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005681 struct rpc_message msg = {
5682 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5683 };
5684
5685 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005686 return -EINVAL;
5687 data = kmalloc(sizeof(*data), GFP_NOFS);
5688 if (!data)
5689 return -ENOMEM;
5690 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005691 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005692 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5693 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5694 data->args.lock_owner.s_dev = server->s_dev;
5695 msg.rpc_argp = &data->args;
5696 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5697 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005698}
5699
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005700#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5701
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005702static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5703 const void *buf, size_t buflen,
5704 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005705{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005706 if (strcmp(key, "") != 0)
5707 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005708
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005709 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005710}
5711
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005712static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5713 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005714{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005715 if (strcmp(key, "") != 0)
5716 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005717
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005718 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005719}
5720
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005721static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5722 size_t list_len, const char *name,
5723 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005724{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005725 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005726
J. Bruce Fields096455a2006-03-20 23:23:42 -05005727 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5728 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005729
5730 if (list && len <= list_len)
5731 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005732 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005733}
5734
David Quigleyc9bccef2013-05-22 12:50:45 -04005735#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5736static inline int nfs4_server_supports_labels(struct nfs_server *server)
5737{
5738 return server->caps & NFS_CAP_SECURITY_LABEL;
5739}
5740
5741static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
5742 const void *buf, size_t buflen,
5743 int flags, int type)
5744{
5745 if (security_ismaclabel(key))
5746 return nfs4_set_security_label(dentry, buf, buflen);
5747
5748 return -EOPNOTSUPP;
5749}
5750
5751static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
5752 void *buf, size_t buflen, int type)
5753{
5754 if (security_ismaclabel(key))
5755 return nfs4_get_security_label(dentry->d_inode, buf, buflen);
5756 return -EOPNOTSUPP;
5757}
5758
5759static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
5760 size_t list_len, const char *name,
5761 size_t name_len, int type)
5762{
5763 size_t len = 0;
5764
5765 if (nfs_server_capable(dentry->d_inode, NFS_CAP_SECURITY_LABEL)) {
5766 len = security_inode_listsecurity(dentry->d_inode, NULL, 0);
5767 if (list && len <= list_len)
5768 security_inode_listsecurity(dentry->d_inode, list, len);
5769 }
5770 return len;
5771}
5772
5773static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
5774 .prefix = XATTR_SECURITY_PREFIX,
5775 .list = nfs4_xattr_list_nfs4_label,
5776 .get = nfs4_xattr_get_nfs4_label,
5777 .set = nfs4_xattr_set_nfs4_label,
5778};
5779#endif
5780
5781
Andy Adamson533eb462011-06-13 18:25:56 -04005782/*
5783 * nfs_fhget will use either the mounted_on_fileid or the fileid
5784 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005785static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5786{
Andy Adamson533eb462011-06-13 18:25:56 -04005787 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5788 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5789 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005790 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005791 return;
5792
5793 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005794 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005795 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5796 fattr->nlink = 2;
5797}
5798
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005799static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5800 const struct qstr *name,
5801 struct nfs4_fs_locations *fs_locations,
5802 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005803{
5804 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04005805 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005806 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005807 };
5808 struct nfs4_fs_locations_arg args = {
5809 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005810 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005811 .page = page,
5812 .bitmask = bitmask,
5813 };
Benny Halevy22958462009-04-01 09:22:02 -04005814 struct nfs4_fs_locations_res res = {
5815 .fs_locations = fs_locations,
5816 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005817 struct rpc_message msg = {
5818 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5819 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005820 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005821 };
5822 int status;
5823
Harvey Harrison3110ff82008-05-02 13:42:44 -07005824 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005825
5826 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5827 * is not supported */
5828 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5829 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5830 else
5831 bitmask[0] |= FATTR4_WORD0_FILEID;
5832
Trond Myklebustc228fd32007-01-13 02:28:11 -05005833 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005834 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005835 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005836 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005837 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005838 return status;
5839}
5840
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005841int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5842 const struct qstr *name,
5843 struct nfs4_fs_locations *fs_locations,
5844 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005845{
5846 struct nfs4_exception exception = { };
5847 int err;
5848 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04005849 err = _nfs4_proc_fs_locations(client, dir, name,
5850 fs_locations, page);
5851 trace_nfs4_get_fs_locations(dir, name, err);
5852 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005853 &exception);
5854 } while (exception.retry);
5855 return err;
5856}
5857
Andy Adamson5ec16a82013-08-08 10:57:55 -04005858/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04005859 * If 'use_integrity' is true and the state managment nfs_client
5860 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
5861 * and the machine credential as per RFC3530bis and RFC5661 Security
5862 * Considerations sections. Otherwise, just use the user cred with the
5863 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04005864 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04005865static 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 +00005866{
5867 int status;
5868 struct nfs4_secinfo_arg args = {
5869 .dir_fh = NFS_FH(dir),
5870 .name = name,
5871 };
5872 struct nfs4_secinfo_res res = {
5873 .flavors = flavors,
5874 };
5875 struct rpc_message msg = {
5876 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5877 .rpc_argp = &args,
5878 .rpc_resp = &res,
5879 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04005880 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
5881
5882 if (use_integrity) {
5883 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
5884 msg.rpc_cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
5885 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005886
5887 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04005888 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
5889 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005890 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04005891
5892 if (msg.rpc_cred)
5893 put_rpccred(msg.rpc_cred);
5894
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005895 return status;
5896}
5897
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005898int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5899 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005900{
5901 struct nfs4_exception exception = { };
5902 int err;
5903 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04005904 err = -NFS4ERR_WRONGSEC;
5905
5906 /* try to use integrity protection with machine cred */
5907 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
5908 err = _nfs4_proc_secinfo(dir, name, flavors, true);
5909
5910 /*
5911 * if unable to use integrity protection, or SECINFO with
5912 * integrity protection returns NFS4ERR_WRONGSEC (which is
5913 * disallowed by spec, but exists in deployed servers) use
5914 * the current filesystem's rpc_client and the user cred.
5915 */
5916 if (err == -NFS4ERR_WRONGSEC)
5917 err = _nfs4_proc_secinfo(dir, name, flavors, false);
5918
Trond Myklebust078ea3d2013-08-12 16:45:55 -04005919 trace_nfs4_secinfo(dir, name, err);
5920 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005921 &exception);
5922 } while (exception.retry);
5923 return err;
5924}
5925
Andy Adamson557134a2009-04-01 09:21:53 -04005926#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005927/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005928 * Check the exchange flags returned by the server for invalid flags, having
5929 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5930 * DS flags set.
5931 */
5932static int nfs4_check_cl_exchange_flags(u32 flags)
5933{
5934 if (flags & ~EXCHGID4_FLAG_MASK_R)
5935 goto out_inval;
5936 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5937 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5938 goto out_inval;
5939 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5940 goto out_inval;
5941 return NFS_OK;
5942out_inval:
5943 return -NFS4ERR_INVAL;
5944}
5945
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005946static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005947nfs41_same_server_scope(struct nfs41_server_scope *a,
5948 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005949{
5950 if (a->server_scope_sz == b->server_scope_sz &&
5951 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5952 return true;
5953
5954 return false;
5955}
5956
Andy Adamson357f54d2010-12-14 10:11:57 -05005957/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005958 * nfs4_proc_bind_conn_to_session()
5959 *
5960 * The 4.1 client currently uses the same TCP connection for the
5961 * fore and backchannel.
5962 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005963int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005964{
5965 int status;
5966 struct nfs41_bind_conn_to_session_res res;
5967 struct rpc_message msg = {
5968 .rpc_proc =
5969 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5970 .rpc_argp = clp,
5971 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005972 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005973 };
5974
5975 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005976
5977 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5978 if (unlikely(res.session == NULL)) {
5979 status = -ENOMEM;
5980 goto out;
5981 }
5982
5983 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005984 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005985 if (status == 0) {
5986 if (memcmp(res.session->sess_id.data,
5987 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5988 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5989 status = -EIO;
5990 goto out_session;
5991 }
5992 if (res.dir != NFS4_CDFS4_BOTH) {
5993 dprintk("NFS: %s: Unexpected direction from server\n",
5994 __func__);
5995 status = -EIO;
5996 goto out_session;
5997 }
5998 if (res.use_conn_in_rdma_mode) {
5999 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6000 __func__);
6001 status = -EIO;
6002 goto out_session;
6003 }
6004 }
6005out_session:
6006 kfree(res.session);
6007out:
6008 dprintk("<-- %s status= %d\n", __func__, status);
6009 return status;
6010}
6011
6012/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04006013 * nfs4_proc_exchange_id()
6014 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006015 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6016 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04006017 * Since the clientid has expired, all compounds using sessions
6018 * associated with the stale clientid will be returning
6019 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
6020 * be in some phase of session reset.
6021 */
Andy Adamson4d643d12009-12-04 15:52:24 -05006022int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04006023{
6024 nfs4_verifier verifier;
6025 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05006026 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04006027 .client = clp,
Trond Myklebust4f0b4292013-05-20 12:24:03 -04006028 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6029 EXCHGID4_FLAG_BIND_PRINC_STATEID,
Benny Halevy99fe60d2009-04-01 09:22:29 -04006030 };
6031 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04006032 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04006033 };
6034 int status;
6035 struct rpc_message msg = {
6036 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
6037 .rpc_argp = &args,
6038 .rpc_resp = &res,
6039 .rpc_cred = cred,
6040 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04006041
Chuck Leverf0920752012-05-21 22:45:41 -04006042 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04006043 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
6044 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006045 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
6046 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6047 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006048
Chuck Leveracdeb692012-05-21 22:46:16 -04006049 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006050 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04006051 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006052 status = -ENOMEM;
6053 goto out;
6054 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006055
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006056 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006057 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006058 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04006059 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04006060 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04006061 }
6062
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006063 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006064 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006065 status = -ENOMEM;
6066 goto out_server_scope;
6067 }
6068
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006069 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006070 trace_nfs4_exchange_id(clp, status);
Chuck Lever177313f2012-05-21 22:44:58 -04006071 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04006072 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006073
Chuck Lever177313f2012-05-21 22:44:58 -04006074 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04006075 clp->cl_clientid = res.clientid;
6076 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
6077 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
6078 clp->cl_seqid = res.seqid;
6079
Chuck Leveracdeb692012-05-21 22:46:16 -04006080 kfree(clp->cl_serverowner);
6081 clp->cl_serverowner = res.server_owner;
6082 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04006083
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006084 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04006085 kfree(clp->cl_implid);
6086 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006087
Chuck Lever177313f2012-05-21 22:44:58 -04006088 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006089 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006090 res.server_scope)) {
6091 dprintk("%s: server_scope mismatch detected\n",
6092 __func__);
6093 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006094 kfree(clp->cl_serverscope);
6095 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006096 }
6097
Chuck Lever177313f2012-05-21 22:44:58 -04006098 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006099 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006100 goto out;
6101 }
Trond Myklebust32b01312012-05-26 13:41:04 -04006102 } else
6103 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006104
Chuck Leveracdeb692012-05-21 22:46:16 -04006105out_server_owner:
6106 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006107out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006108 kfree(res.server_scope);
6109out:
Chuck Lever177313f2012-05-21 22:44:58 -04006110 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006111 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006112 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006113 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04006114 clp->cl_implid->date.seconds,
6115 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006116 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006117 return status;
6118}
6119
Trond Myklebust66245532012-05-25 17:18:09 -04006120static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
6121 struct rpc_cred *cred)
6122{
6123 struct rpc_message msg = {
6124 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
6125 .rpc_argp = clp,
6126 .rpc_cred = cred,
6127 };
6128 int status;
6129
6130 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006131 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04006132 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04006133 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04006134 "DESTROY_CLIENTID.", status, clp->cl_hostname);
6135 return status;
6136}
6137
6138static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
6139 struct rpc_cred *cred)
6140{
6141 unsigned int loop;
6142 int ret;
6143
6144 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
6145 ret = _nfs4_proc_destroy_clientid(clp, cred);
6146 switch (ret) {
6147 case -NFS4ERR_DELAY:
6148 case -NFS4ERR_CLIENTID_BUSY:
6149 ssleep(1);
6150 break;
6151 default:
6152 return ret;
6153 }
6154 }
6155 return 0;
6156}
6157
6158int nfs4_destroy_clientid(struct nfs_client *clp)
6159{
6160 struct rpc_cred *cred;
6161 int ret = 0;
6162
6163 if (clp->cl_mvops->minor_version < 1)
6164 goto out;
6165 if (clp->cl_exchange_flags == 0)
6166 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04006167 if (clp->cl_preserve_clid)
6168 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04006169 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04006170 ret = nfs4_proc_destroy_clientid(clp, cred);
6171 if (cred)
6172 put_rpccred(cred);
6173 switch (ret) {
6174 case 0:
6175 case -NFS4ERR_STALE_CLIENTID:
6176 clp->cl_exchange_flags = 0;
6177 }
6178out:
6179 return ret;
6180}
6181
Andy Adamson2050f0c2009-04-01 09:22:30 -04006182struct nfs4_get_lease_time_data {
6183 struct nfs4_get_lease_time_args *args;
6184 struct nfs4_get_lease_time_res *res;
6185 struct nfs_client *clp;
6186};
6187
6188static void nfs4_get_lease_time_prepare(struct rpc_task *task,
6189 void *calldata)
6190{
Andy Adamson2050f0c2009-04-01 09:22:30 -04006191 struct nfs4_get_lease_time_data *data =
6192 (struct nfs4_get_lease_time_data *)calldata;
6193
6194 dprintk("--> %s\n", __func__);
6195 /* just setup sequence, do not trigger session recovery
6196 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006197 nfs41_setup_sequence(data->clp->cl_session,
6198 &data->args->la_seq_args,
6199 &data->res->lr_seq_res,
6200 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006201 dprintk("<-- %s\n", __func__);
6202}
6203
6204/*
6205 * Called from nfs4_state_manager thread for session setup, so don't recover
6206 * from sequence operation or clientid errors.
6207 */
6208static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
6209{
6210 struct nfs4_get_lease_time_data *data =
6211 (struct nfs4_get_lease_time_data *)calldata;
6212
6213 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006214 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
6215 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04006216 switch (task->tk_status) {
6217 case -NFS4ERR_DELAY:
6218 case -NFS4ERR_GRACE:
6219 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
6220 rpc_delay(task, NFS4_POLL_RETRY_MIN);
6221 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006222 /* fall through */
6223 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006224 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006225 return;
6226 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04006227 dprintk("<-- %s\n", __func__);
6228}
6229
Trond Myklebust17280172012-03-11 13:11:00 -04006230static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04006231 .rpc_call_prepare = nfs4_get_lease_time_prepare,
6232 .rpc_call_done = nfs4_get_lease_time_done,
6233};
6234
6235int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
6236{
6237 struct rpc_task *task;
6238 struct nfs4_get_lease_time_args args;
6239 struct nfs4_get_lease_time_res res = {
6240 .lr_fsinfo = fsinfo,
6241 };
6242 struct nfs4_get_lease_time_data data = {
6243 .args = &args,
6244 .res = &res,
6245 .clp = clp,
6246 };
6247 struct rpc_message msg = {
6248 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
6249 .rpc_argp = &args,
6250 .rpc_resp = &res,
6251 };
6252 struct rpc_task_setup task_setup = {
6253 .rpc_client = clp->cl_rpcclient,
6254 .rpc_message = &msg,
6255 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006256 .callback_data = &data,
6257 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04006258 };
6259 int status;
6260
Trond Myklebust9d12b212012-01-17 22:04:25 -05006261 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006262 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006263 dprintk("--> %s\n", __func__);
6264 task = rpc_run_task(&task_setup);
6265
6266 if (IS_ERR(task))
6267 status = PTR_ERR(task);
6268 else {
6269 status = task->tk_status;
6270 rpc_put_task(task);
6271 }
6272 dprintk("<-- %s return %d\n", __func__, status);
6273
6274 return status;
6275}
6276
Andy Adamsonfc931582009-04-01 09:22:31 -04006277/*
6278 * Initialize the values to be used by the client in CREATE_SESSION
6279 * If nfs4_init_session set the fore channel request and response sizes,
6280 * use them.
6281 *
6282 * Set the back channel max_resp_sz_cached to zero to force the client to
6283 * always set csa_cachethis to FALSE because the current implementation
6284 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
6285 */
6286static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
6287{
Andy Adamson18aad3d2013-06-26 12:21:49 -04006288 unsigned int max_rqst_sz, max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006289
Andy Adamson18aad3d2013-06-26 12:21:49 -04006290 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
6291 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
6292
Andy Adamsonfc931582009-04-01 09:22:31 -04006293 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04006294 args->fc_attrs.max_rqst_sz = max_rqst_sz;
6295 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006296 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05006297 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04006298
6299 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05006300 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04006301 __func__,
6302 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05006303 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04006304
6305 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04006306 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
6307 args->bc_attrs.max_resp_sz = PAGE_SIZE;
6308 args->bc_attrs.max_resp_sz_cached = 0;
6309 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
6310 args->bc_attrs.max_reqs = 1;
6311
6312 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
6313 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
6314 __func__,
6315 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
6316 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
6317 args->bc_attrs.max_reqs);
6318}
6319
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006320static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04006321{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006322 struct nfs4_channel_attrs *sent = &args->fc_attrs;
6323 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
6324
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006325 if (rcvd->max_resp_sz > sent->max_resp_sz)
6326 return -EINVAL;
6327 /*
6328 * Our requested max_ops is the minimum we need; we're not
6329 * prepared to break up compounds into smaller pieces than that.
6330 * So, no point even trying to continue if the server won't
6331 * cooperate:
6332 */
6333 if (rcvd->max_ops < sent->max_ops)
6334 return -EINVAL;
6335 if (rcvd->max_reqs == 0)
6336 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006337 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
6338 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006339 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04006340}
6341
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006342static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
6343{
6344 struct nfs4_channel_attrs *sent = &args->bc_attrs;
6345 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04006346
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006347 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
6348 return -EINVAL;
6349 if (rcvd->max_resp_sz < sent->max_resp_sz)
6350 return -EINVAL;
6351 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
6352 return -EINVAL;
6353 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006354 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006355 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006356 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006357 return -EINVAL;
6358 return 0;
6359}
Andy Adamson8d353012009-04-01 09:22:32 -04006360
Andy Adamson8d353012009-04-01 09:22:32 -04006361static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
6362 struct nfs4_session *session)
6363{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006364 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04006365
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006366 ret = nfs4_verify_fore_channel_attrs(args, session);
6367 if (ret)
6368 return ret;
6369 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04006370}
6371
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006372static int _nfs4_proc_create_session(struct nfs_client *clp,
6373 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04006374{
6375 struct nfs4_session *session = clp->cl_session;
6376 struct nfs41_create_session_args args = {
6377 .client = clp,
6378 .cb_program = NFS4_CALLBACK,
6379 };
6380 struct nfs41_create_session_res res = {
6381 .client = clp,
6382 };
6383 struct rpc_message msg = {
6384 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
6385 .rpc_argp = &args,
6386 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006387 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04006388 };
6389 int status;
6390
6391 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04006392 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04006393
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006394 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006395 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04006396
Trond Myklebust43095d32012-11-20 11:13:12 -05006397 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04006398 /* Verify the session's negotiated channel_attrs values */
6399 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04006400 /* Increment the clientid slot sequence id */
6401 clp->cl_seqid++;
6402 }
6403
6404 return status;
6405}
6406
6407/*
6408 * Issues a CREATE_SESSION operation to the server.
6409 * It is the responsibility of the caller to verify the session is
6410 * expired before calling this routine.
6411 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006412int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04006413{
6414 int status;
6415 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04006416 struct nfs4_session *session = clp->cl_session;
6417
6418 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
6419
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006420 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04006421 if (status)
6422 goto out;
6423
Andy Adamsonaacd5532011-11-09 13:58:21 -05006424 /* Init or reset the session slot tables */
6425 status = nfs4_setup_session_slot_tables(session);
6426 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04006427 if (status)
6428 goto out;
6429
6430 ptr = (unsigned *)&session->sess_id.data[0];
6431 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
6432 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04006433out:
6434 dprintk("<-- %s\n", __func__);
6435 return status;
6436}
6437
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006438/*
6439 * Issue the over-the-wire RPC DESTROY_SESSION.
6440 * The caller must serialize access to this routine.
6441 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006442int nfs4_proc_destroy_session(struct nfs4_session *session,
6443 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006444{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006445 struct rpc_message msg = {
6446 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
6447 .rpc_argp = session,
6448 .rpc_cred = cred,
6449 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006450 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006451
6452 dprintk("--> nfs4_proc_destroy_session\n");
6453
6454 /* session is still being setup */
6455 if (session->clp->cl_cons_state != NFS_CS_READY)
6456 return status;
6457
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006458 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006459 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006460
6461 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04006462 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006463 "Session has been destroyed regardless...\n", status);
6464
6465 dprintk("<-- nfs4_proc_destroy_session\n");
6466 return status;
6467}
6468
Trond Myklebust7b38c362012-05-23 13:23:31 -04006469/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006470 * Renew the cl_session lease.
6471 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006472struct nfs4_sequence_data {
6473 struct nfs_client *clp;
6474 struct nfs4_sequence_args args;
6475 struct nfs4_sequence_res res;
6476};
6477
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006478static void nfs41_sequence_release(void *data)
6479{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006480 struct nfs4_sequence_data *calldata = data;
6481 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006482
Alexandros Batsakis71358402010-02-05 03:45:05 -08006483 if (atomic_read(&clp->cl_count) > 1)
6484 nfs4_schedule_state_renewal(clp);
6485 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006486 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006487}
6488
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006489static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6490{
6491 switch(task->tk_status) {
6492 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006493 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6494 return -EAGAIN;
6495 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006496 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006497 }
6498 return 0;
6499}
6500
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006501static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006502{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006503 struct nfs4_sequence_data *calldata = data;
6504 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006505
Trond Myklebust14516c32010-07-31 14:29:06 -04006506 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6507 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006508
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006509 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006510 if (task->tk_status < 0) {
6511 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006512 if (atomic_read(&clp->cl_count) == 1)
6513 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006514
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006515 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6516 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006517 return;
6518 }
6519 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006520 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006521out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006522 dprintk("<-- %s\n", __func__);
6523}
6524
6525static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6526{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006527 struct nfs4_sequence_data *calldata = data;
6528 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006529 struct nfs4_sequence_args *args;
6530 struct nfs4_sequence_res *res;
6531
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006532 args = task->tk_msg.rpc_argp;
6533 res = task->tk_msg.rpc_resp;
6534
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006535 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006536}
6537
6538static const struct rpc_call_ops nfs41_sequence_ops = {
6539 .rpc_call_done = nfs41_sequence_call_done,
6540 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006541 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006542};
6543
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006544static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
6545 struct rpc_cred *cred,
6546 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006547{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006548 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006549 struct rpc_message msg = {
6550 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6551 .rpc_cred = cred,
6552 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006553 struct rpc_task_setup task_setup_data = {
6554 .rpc_client = clp->cl_rpcclient,
6555 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006556 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04006557 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006558 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006559
Alexandros Batsakis71358402010-02-05 03:45:05 -08006560 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006561 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006562 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006563 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006564 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006565 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006566 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05006567 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006568 if (is_privileged)
6569 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006570 msg.rpc_argp = &calldata->args;
6571 msg.rpc_resp = &calldata->res;
6572 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006573 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006574
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006575 return rpc_run_task(&task_setup_data);
6576}
6577
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006578static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006579{
6580 struct rpc_task *task;
6581 int ret = 0;
6582
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006583 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6584 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006585 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006586 if (IS_ERR(task))
6587 ret = PTR_ERR(task);
6588 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006589 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006590 dprintk("<-- %s status=%d\n", __func__, ret);
6591 return ret;
6592}
6593
6594static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6595{
6596 struct rpc_task *task;
6597 int ret;
6598
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006599 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006600 if (IS_ERR(task)) {
6601 ret = PTR_ERR(task);
6602 goto out;
6603 }
6604 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006605 if (!ret) {
6606 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6607
6608 if (task->tk_status == 0)
6609 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006610 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006611 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006612 rpc_put_task(task);
6613out:
6614 dprintk("<-- %s status=%d\n", __func__, ret);
6615 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006616}
6617
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006618struct nfs4_reclaim_complete_data {
6619 struct nfs_client *clp;
6620 struct nfs41_reclaim_complete_args arg;
6621 struct nfs41_reclaim_complete_res res;
6622};
6623
6624static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6625{
6626 struct nfs4_reclaim_complete_data *calldata = data;
6627
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006628 nfs41_setup_sequence(calldata->clp->cl_session,
6629 &calldata->arg.seq_args,
6630 &calldata->res.seq_res,
6631 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006632}
6633
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006634static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6635{
6636 switch(task->tk_status) {
6637 case 0:
6638 case -NFS4ERR_COMPLETE_ALREADY:
6639 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6640 break;
6641 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006642 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006643 /* fall through */
6644 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006645 return -EAGAIN;
6646 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006647 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006648 }
6649 return 0;
6650}
6651
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006652static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6653{
6654 struct nfs4_reclaim_complete_data *calldata = data;
6655 struct nfs_client *clp = calldata->clp;
6656 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6657
6658 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006659 if (!nfs41_sequence_done(task, res))
6660 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006661
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006662 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006663 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6664 rpc_restart_call_prepare(task);
6665 return;
6666 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006667 dprintk("<-- %s\n", __func__);
6668}
6669
6670static void nfs4_free_reclaim_complete_data(void *data)
6671{
6672 struct nfs4_reclaim_complete_data *calldata = data;
6673
6674 kfree(calldata);
6675}
6676
6677static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6678 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6679 .rpc_call_done = nfs4_reclaim_complete_done,
6680 .rpc_release = nfs4_free_reclaim_complete_data,
6681};
6682
6683/*
6684 * Issue a global reclaim complete.
6685 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04006686static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
6687 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006688{
6689 struct nfs4_reclaim_complete_data *calldata;
6690 struct rpc_task *task;
6691 struct rpc_message msg = {
6692 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04006693 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006694 };
6695 struct rpc_task_setup task_setup_data = {
6696 .rpc_client = clp->cl_rpcclient,
6697 .rpc_message = &msg,
6698 .callback_ops = &nfs4_reclaim_complete_call_ops,
6699 .flags = RPC_TASK_ASYNC,
6700 };
6701 int status = -ENOMEM;
6702
6703 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006704 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006705 if (calldata == NULL)
6706 goto out;
6707 calldata->clp = clp;
6708 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006709
Trond Myklebust9d12b212012-01-17 22:04:25 -05006710 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006711 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006712 msg.rpc_argp = &calldata->arg;
6713 msg.rpc_resp = &calldata->res;
6714 task_setup_data.callback_data = calldata;
6715 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006716 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006717 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006718 goto out;
6719 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006720 status = nfs4_wait_for_completion_rpc_task(task);
6721 if (status == 0)
6722 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006723 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006724 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006725out:
6726 dprintk("<-- %s status=%d\n", __func__, status);
6727 return status;
6728}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006729
6730static void
6731nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6732{
6733 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006734 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006735 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006736
6737 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006738 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6739 * right now covering the LAYOUTGET we are about to send.
6740 * However, that is not so catastrophic, and there seems
6741 * to be no way to prevent it completely.
6742 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006743 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006744 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006745 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006746 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6747 NFS_I(lgp->args.inode)->layout,
6748 lgp->args.ctx->state)) {
6749 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00006750 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006751}
6752
6753static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6754{
6755 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006756 struct inode *inode = lgp->args.inode;
6757 struct nfs_server *server = NFS_SERVER(inode);
6758 struct pnfs_layout_hdr *lo;
6759 struct nfs4_state *state = NULL;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006760 unsigned long timeo, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006761
6762 dprintk("--> %s\n", __func__);
6763
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006764 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006765 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006766
6767 switch (task->tk_status) {
6768 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006769 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006770 case -NFS4ERR_LAYOUTTRYLATER:
6771 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05006772 timeo = rpc_get_timeout(task->tk_client);
6773 giveup = lgp->args.timestamp + timeo;
6774 if (time_after(giveup, jiffies))
6775 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006776 break;
6777 case -NFS4ERR_EXPIRED:
6778 case -NFS4ERR_BAD_STATEID:
6779 spin_lock(&inode->i_lock);
6780 lo = NFS_I(inode)->layout;
6781 if (!lo || list_empty(&lo->plh_segs)) {
6782 spin_unlock(&inode->i_lock);
6783 /* If the open stateid was bad, then recover it. */
6784 state = lgp->args.ctx->state;
6785 } else {
6786 LIST_HEAD(head);
6787
6788 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6789 spin_unlock(&inode->i_lock);
6790 /* Mark the bad layout state as invalid, then
6791 * retry using the open stateid. */
6792 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006793 }
6794 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006795 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6796 rpc_restart_call_prepare(task);
6797out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006798 dprintk("<-- %s\n", __func__);
6799}
6800
Idan Kedar85541162012-08-02 11:47:10 +03006801static size_t max_response_pages(struct nfs_server *server)
6802{
6803 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6804 return nfs_page_array_len(0, max_resp_sz);
6805}
6806
6807static void nfs4_free_pages(struct page **pages, size_t size)
6808{
6809 int i;
6810
6811 if (!pages)
6812 return;
6813
6814 for (i = 0; i < size; i++) {
6815 if (!pages[i])
6816 break;
6817 __free_page(pages[i]);
6818 }
6819 kfree(pages);
6820}
6821
6822static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6823{
6824 struct page **pages;
6825 int i;
6826
6827 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6828 if (!pages) {
6829 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6830 return NULL;
6831 }
6832
6833 for (i = 0; i < size; i++) {
6834 pages[i] = alloc_page(gfp_flags);
6835 if (!pages[i]) {
6836 dprintk("%s: failed to allocate page\n", __func__);
6837 nfs4_free_pages(pages, size);
6838 return NULL;
6839 }
6840 }
6841
6842 return pages;
6843}
6844
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006845static void nfs4_layoutget_release(void *calldata)
6846{
6847 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006848 struct inode *inode = lgp->args.inode;
6849 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006850 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006851
6852 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006853 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006854 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006855 put_nfs_open_context(lgp->args.ctx);
6856 kfree(calldata);
6857 dprintk("<-- %s\n", __func__);
6858}
6859
6860static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6861 .rpc_call_prepare = nfs4_layoutget_prepare,
6862 .rpc_call_done = nfs4_layoutget_done,
6863 .rpc_release = nfs4_layoutget_release,
6864};
6865
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006866struct pnfs_layout_segment *
6867nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006868{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006869 struct inode *inode = lgp->args.inode;
6870 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006871 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006872 struct rpc_task *task;
6873 struct rpc_message msg = {
6874 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6875 .rpc_argp = &lgp->args,
6876 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04006877 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006878 };
6879 struct rpc_task_setup task_setup_data = {
6880 .rpc_client = server->client,
6881 .rpc_message = &msg,
6882 .callback_ops = &nfs4_layoutget_call_ops,
6883 .callback_data = lgp,
6884 .flags = RPC_TASK_ASYNC,
6885 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006886 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006887 int status = 0;
6888
6889 dprintk("--> %s\n", __func__);
6890
Idan Kedar85541162012-08-02 11:47:10 +03006891 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6892 if (!lgp->args.layout.pages) {
6893 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006894 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006895 }
6896 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006897 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03006898
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006899 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006900 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006901 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006902
6903 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
6904 pnfs_get_layout_hdr(NFS_I(inode)->layout);
6905
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006906 task = rpc_run_task(&task_setup_data);
6907 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006908 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006909 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006910 if (status == 0)
6911 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04006912 trace_nfs4_layoutget(lgp->args.ctx,
6913 &lgp->args.range,
6914 &lgp->res.range,
6915 status);
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05006916 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
6917 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006918 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006919 rpc_put_task(task);
6920 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006921 if (status)
6922 return ERR_PTR(status);
6923 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006924}
6925
Benny Halevycbe82602011-05-22 19:52:37 +03006926static void
6927nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6928{
6929 struct nfs4_layoutreturn *lrp = calldata;
6930
6931 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006932 nfs41_setup_sequence(lrp->clp->cl_session,
6933 &lrp->args.seq_args,
6934 &lrp->res.seq_res,
6935 task);
Benny Halevycbe82602011-05-22 19:52:37 +03006936}
6937
6938static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6939{
6940 struct nfs4_layoutreturn *lrp = calldata;
6941 struct nfs_server *server;
6942
6943 dprintk("--> %s\n", __func__);
6944
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006945 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03006946 return;
6947
6948 server = NFS_SERVER(lrp->args.inode);
6949 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006950 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006951 return;
6952 }
Benny Halevycbe82602011-05-22 19:52:37 +03006953 dprintk("<-- %s\n", __func__);
6954}
6955
6956static void nfs4_layoutreturn_release(void *calldata)
6957{
6958 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006959 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006960
6961 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006962 spin_lock(&lo->plh_inode->i_lock);
6963 if (lrp->res.lrs_present)
6964 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6965 lo->plh_block_lgets--;
6966 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006967 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006968 kfree(calldata);
6969 dprintk("<-- %s\n", __func__);
6970}
6971
6972static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6973 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6974 .rpc_call_done = nfs4_layoutreturn_done,
6975 .rpc_release = nfs4_layoutreturn_release,
6976};
6977
6978int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6979{
6980 struct rpc_task *task;
6981 struct rpc_message msg = {
6982 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6983 .rpc_argp = &lrp->args,
6984 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04006985 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03006986 };
6987 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04006988 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03006989 .rpc_message = &msg,
6990 .callback_ops = &nfs4_layoutreturn_call_ops,
6991 .callback_data = lrp,
6992 };
6993 int status;
6994
6995 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006996 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006997 task = rpc_run_task(&task_setup_data);
6998 if (IS_ERR(task))
6999 return PTR_ERR(task);
7000 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007001 trace_nfs4_layoutreturn(lrp->args.inode, status);
Benny Halevycbe82602011-05-22 19:52:37 +03007002 dprintk("<-- %s status=%d\n", __func__, status);
7003 rpc_put_task(task);
7004 return status;
7005}
7006
Andy Adamson7f11d8d2011-07-30 20:52:35 -04007007/*
7008 * Retrieve the list of Data Server devices from the MDS.
7009 */
7010static int _nfs4_getdevicelist(struct nfs_server *server,
7011 const struct nfs_fh *fh,
7012 struct pnfs_devicelist *devlist)
7013{
7014 struct nfs4_getdevicelist_args args = {
7015 .fh = fh,
7016 .layoutclass = server->pnfs_curr_ld->id,
7017 };
7018 struct nfs4_getdevicelist_res res = {
7019 .devlist = devlist,
7020 };
7021 struct rpc_message msg = {
7022 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
7023 .rpc_argp = &args,
7024 .rpc_resp = &res,
7025 };
7026 int status;
7027
7028 dprintk("--> %s\n", __func__);
7029 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
7030 &res.seq_res, 0);
7031 dprintk("<-- %s status=%d\n", __func__, status);
7032 return status;
7033}
7034
7035int nfs4_proc_getdevicelist(struct nfs_server *server,
7036 const struct nfs_fh *fh,
7037 struct pnfs_devicelist *devlist)
7038{
7039 struct nfs4_exception exception = { };
7040 int err;
7041
7042 do {
7043 err = nfs4_handle_exception(server,
7044 _nfs4_getdevicelist(server, fh, devlist),
7045 &exception);
7046 } while (exception.retry);
7047
7048 dprintk("%s: err=%d, num_devs=%u\n", __func__,
7049 err, devlist->num_devs);
7050
7051 return err;
7052}
7053EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
7054
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007055static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007056_nfs4_proc_getdeviceinfo(struct nfs_server *server,
7057 struct pnfs_device *pdev,
7058 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007059{
7060 struct nfs4_getdeviceinfo_args args = {
7061 .pdev = pdev,
7062 };
7063 struct nfs4_getdeviceinfo_res res = {
7064 .pdev = pdev,
7065 };
7066 struct rpc_message msg = {
7067 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
7068 .rpc_argp = &args,
7069 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007070 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007071 };
7072 int status;
7073
7074 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00007075 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007076 dprintk("<-- %s status=%d\n", __func__, status);
7077
7078 return status;
7079}
7080
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007081int nfs4_proc_getdeviceinfo(struct nfs_server *server,
7082 struct pnfs_device *pdev,
7083 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007084{
7085 struct nfs4_exception exception = { };
7086 int err;
7087
7088 do {
7089 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007090 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007091 &exception);
7092 } while (exception.retry);
7093 return err;
7094}
7095EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
7096
Andy Adamson863a3c62011-03-23 13:27:54 +00007097static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
7098{
7099 struct nfs4_layoutcommit_data *data = calldata;
7100 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007101 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00007102
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007103 nfs41_setup_sequence(session,
7104 &data->args.seq_args,
7105 &data->res.seq_res,
7106 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00007107}
7108
7109static void
7110nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
7111{
7112 struct nfs4_layoutcommit_data *data = calldata;
7113 struct nfs_server *server = NFS_SERVER(data->args.inode);
7114
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007115 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00007116 return;
7117
7118 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007119 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
7120 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
7121 case -NFS4ERR_BADLAYOUT: /* no layout */
7122 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00007123 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007124 break;
7125 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00007126 nfs_post_op_update_inode_force_wcc(data->args.inode,
7127 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007128 break;
7129 default:
7130 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
7131 rpc_restart_call_prepare(task);
7132 return;
7133 }
7134 }
Andy Adamson863a3c62011-03-23 13:27:54 +00007135}
7136
7137static void nfs4_layoutcommit_release(void *calldata)
7138{
7139 struct nfs4_layoutcommit_data *data = calldata;
7140
Andy Adamsondb29c082011-07-30 20:52:38 -04007141 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00007142 put_rpccred(data->cred);
7143 kfree(data);
7144}
7145
7146static const struct rpc_call_ops nfs4_layoutcommit_ops = {
7147 .rpc_call_prepare = nfs4_layoutcommit_prepare,
7148 .rpc_call_done = nfs4_layoutcommit_done,
7149 .rpc_release = nfs4_layoutcommit_release,
7150};
7151
7152int
Andy Adamsonef311532011-03-12 02:58:10 -05007153nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00007154{
7155 struct rpc_message msg = {
7156 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
7157 .rpc_argp = &data->args,
7158 .rpc_resp = &data->res,
7159 .rpc_cred = data->cred,
7160 };
7161 struct rpc_task_setup task_setup_data = {
7162 .task = &data->task,
7163 .rpc_client = NFS_CLIENT(data->args.inode),
7164 .rpc_message = &msg,
7165 .callback_ops = &nfs4_layoutcommit_ops,
7166 .callback_data = data,
7167 .flags = RPC_TASK_ASYNC,
7168 };
7169 struct rpc_task *task;
7170 int status = 0;
7171
7172 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
7173 "lbw: %llu inode %lu\n",
7174 data->task.tk_pid, sync,
7175 data->args.lastbytewritten,
7176 data->args.inode->i_ino);
7177
Trond Myklebust9d12b212012-01-17 22:04:25 -05007178 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00007179 task = rpc_run_task(&task_setup_data);
7180 if (IS_ERR(task))
7181 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05007182 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00007183 goto out;
7184 status = nfs4_wait_for_completion_rpc_task(task);
7185 if (status != 0)
7186 goto out;
7187 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007188 trace_nfs4_layoutcommit(data->args.inode, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00007189out:
7190 dprintk("%s: status %d\n", __func__, status);
7191 rpc_put_task(task);
7192 return status;
7193}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007194
Andy Adamson97431202013-08-08 10:57:56 -04007195/**
7196 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
7197 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
7198 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007199static int
7200_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7201 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
7202{
7203 struct nfs41_secinfo_no_name_args args = {
7204 .style = SECINFO_STYLE_CURRENT_FH,
7205 };
7206 struct nfs4_secinfo_res res = {
7207 .flavors = flavors,
7208 };
7209 struct rpc_message msg = {
7210 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
7211 .rpc_argp = &args,
7212 .rpc_resp = &res,
7213 };
Andy Adamson97431202013-08-08 10:57:56 -04007214 return nfs4_call_sync(server->nfs_client->cl_rpcclient, server, &msg,
7215 &args.seq_args, &res.seq_res, 0);
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007216}
7217
7218static int
7219nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7220 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
7221{
7222 struct nfs4_exception exception = { };
7223 int err;
7224 do {
7225 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
7226 switch (err) {
7227 case 0:
7228 case -NFS4ERR_WRONGSEC:
7229 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007230 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007231 default:
7232 err = nfs4_handle_exception(server, err, &exception);
7233 }
7234 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007235out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007236 return err;
7237}
7238
7239static int
7240nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
7241 struct nfs_fsinfo *info)
7242{
7243 int err;
7244 struct page *page;
7245 rpc_authflavor_t flavor;
7246 struct nfs4_secinfo_flavors *flavors;
7247
7248 page = alloc_page(GFP_KERNEL);
7249 if (!page) {
7250 err = -ENOMEM;
7251 goto out;
7252 }
7253
7254 flavors = page_address(page);
7255 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
7256
7257 /*
7258 * Fall back on "guess and check" method if
7259 * the server doesn't support SECINFO_NO_NAME
7260 */
7261 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
7262 err = nfs4_find_root_sec(server, fhandle, info);
7263 goto out_freepage;
7264 }
7265 if (err)
7266 goto out_freepage;
7267
7268 flavor = nfs_find_best_sec(flavors);
7269 if (err == 0)
7270 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
7271
7272out_freepage:
7273 put_page(page);
7274 if (err == -EACCES)
7275 return -EPERM;
7276out:
7277 return err;
7278}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007279
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007280static int _nfs41_test_stateid(struct nfs_server *server,
7281 nfs4_stateid *stateid,
7282 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04007283{
7284 int status;
7285 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007286 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04007287 };
7288 struct nfs41_test_stateid_res res;
7289 struct rpc_message msg = {
7290 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
7291 .rpc_argp = &args,
7292 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007293 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04007294 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007295
Chuck Lever38527b12012-07-11 16:30:23 -04007296 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05007297 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007298 nfs4_set_sequence_privileged(&args.seq_args);
7299 status = nfs4_call_sync_sequence(server->client, server, &msg,
7300 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04007301 if (status != NFS_OK) {
7302 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04007303 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04007304 }
7305 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04007306 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04007307}
7308
Chuck Lever38527b12012-07-11 16:30:23 -04007309/**
7310 * nfs41_test_stateid - perform a TEST_STATEID operation
7311 *
7312 * @server: server / transport on which to perform the operation
7313 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007314 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04007315 *
7316 * Returns NFS_OK if the server recognizes that "stateid" is valid.
7317 * Otherwise a negative NFS4ERR value is returned if the operation
7318 * failed or the state ID is not currently valid.
7319 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007320static int nfs41_test_stateid(struct nfs_server *server,
7321 nfs4_stateid *stateid,
7322 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04007323{
7324 struct nfs4_exception exception = { };
7325 int err;
7326 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007327 err = _nfs41_test_stateid(server, stateid, cred);
Chuck Lever377e5072012-07-11 16:29:45 -04007328 if (err != -NFS4ERR_DELAY)
7329 break;
7330 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04007331 } while (exception.retry);
7332 return err;
7333}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007334
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007335struct nfs_free_stateid_data {
7336 struct nfs_server *server;
7337 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007338 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007339};
7340
7341static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
7342{
7343 struct nfs_free_stateid_data *data = calldata;
7344 nfs41_setup_sequence(nfs4_get_session(data->server),
7345 &data->args.seq_args,
7346 &data->res.seq_res,
7347 task);
7348}
7349
7350static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
7351{
7352 struct nfs_free_stateid_data *data = calldata;
7353
7354 nfs41_sequence_done(task, &data->res.seq_res);
7355
7356 switch (task->tk_status) {
7357 case -NFS4ERR_DELAY:
7358 if (nfs4_async_handle_error(task, data->server, NULL) == -EAGAIN)
7359 rpc_restart_call_prepare(task);
7360 }
7361}
7362
7363static void nfs41_free_stateid_release(void *calldata)
7364{
7365 kfree(calldata);
7366}
7367
Trond Myklebust17f26b12013-08-21 15:48:42 -04007368static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007369 .rpc_call_prepare = nfs41_free_stateid_prepare,
7370 .rpc_call_done = nfs41_free_stateid_done,
7371 .rpc_release = nfs41_free_stateid_release,
7372};
7373
7374static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
7375 nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007376 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007377 bool privileged)
7378{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007379 struct rpc_message msg = {
7380 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007381 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007382 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007383 struct rpc_task_setup task_setup = {
7384 .rpc_client = server->client,
7385 .rpc_message = &msg,
7386 .callback_ops = &nfs41_free_stateid_ops,
7387 .flags = RPC_TASK_ASYNC,
7388 };
7389 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007390
Chuck Lever38527b12012-07-11 16:30:23 -04007391 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007392 data = kmalloc(sizeof(*data), GFP_NOFS);
7393 if (!data)
7394 return ERR_PTR(-ENOMEM);
7395 data->server = server;
7396 nfs4_stateid_copy(&data->args.stateid, stateid);
7397
7398 task_setup.callback_data = data;
7399
7400 msg.rpc_argp = &data->args;
7401 msg.rpc_resp = &data->res;
7402 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
7403 if (privileged)
7404 nfs4_set_sequence_privileged(&data->args.seq_args);
7405
7406 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007407}
7408
Chuck Lever38527b12012-07-11 16:30:23 -04007409/**
7410 * nfs41_free_stateid - perform a FREE_STATEID operation
7411 *
7412 * @server: server / transport on which to perform the operation
7413 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007414 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04007415 *
7416 * Returns NFS_OK if the server freed "stateid". Otherwise a
7417 * negative NFS4ERR value is returned.
7418 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007419static int nfs41_free_stateid(struct nfs_server *server,
7420 nfs4_stateid *stateid,
7421 struct rpc_cred *cred)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007422{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007423 struct rpc_task *task;
7424 int ret;
7425
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007426 task = _nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007427 if (IS_ERR(task))
7428 return PTR_ERR(task);
7429 ret = rpc_wait_for_completion_task(task);
7430 if (!ret)
7431 ret = task->tk_status;
7432 rpc_put_task(task);
7433 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007434}
Trond Myklebust36281ca2012-03-04 18:13:56 -05007435
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007436static int nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
7437{
7438 struct rpc_task *task;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007439 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007440
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007441 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007442 nfs4_free_lock_state(server, lsp);
7443 if (IS_ERR(task))
7444 return PTR_ERR(task);
7445 rpc_put_task(task);
7446 return 0;
7447}
7448
Trond Myklebust36281ca2012-03-04 18:13:56 -05007449static bool nfs41_match_stateid(const nfs4_stateid *s1,
7450 const nfs4_stateid *s2)
7451{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007452 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007453 return false;
7454
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007455 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007456 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007457 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007458 return true;
7459
7460 return false;
7461}
7462
Andy Adamson557134a2009-04-01 09:21:53 -04007463#endif /* CONFIG_NFS_V4_1 */
7464
Trond Myklebust36281ca2012-03-04 18:13:56 -05007465static bool nfs4_match_stateid(const nfs4_stateid *s1,
7466 const nfs4_stateid *s2)
7467{
Trond Myklebustf597c532012-03-04 18:13:56 -05007468 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05007469}
7470
7471
Trond Myklebust17280172012-03-11 13:11:00 -04007472static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007473 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007474 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007475 .recover_open = nfs4_open_reclaim,
7476 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04007477 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04007478 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007479};
7480
Andy Adamson591d71c2009-04-01 09:22:47 -04007481#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007482static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007483 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
7484 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
7485 .recover_open = nfs4_open_reclaim,
7486 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05007487 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007488 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04007489 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04007490};
7491#endif /* CONFIG_NFS_V4_1 */
7492
Trond Myklebust17280172012-03-11 13:11:00 -04007493static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007494 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007495 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007496 .recover_open = nfs4_open_expired,
7497 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04007498 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007499};
7500
Andy Adamson591d71c2009-04-01 09:22:47 -04007501#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007502static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007503 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
7504 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007505 .recover_open = nfs41_open_expired,
7506 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05007507 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04007508};
7509#endif /* CONFIG_NFS_V4_1 */
7510
Trond Myklebust17280172012-03-11 13:11:00 -04007511static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007512 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04007513 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007514 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04007515};
7516
7517#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007518static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007519 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04007520 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007521 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04007522};
7523#endif
7524
Trond Myklebust97dc1352010-06-16 09:52:26 -04007525static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
7526 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04007527 .init_caps = NFS_CAP_READDIRPLUS
7528 | NFS_CAP_ATOMIC_OPEN
7529 | NFS_CAP_CHANGE_ATTR
7530 | NFS_CAP_POSIX_LOCK,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007531 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007532 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007533 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007534 .free_lock_state = nfs4_release_lockowner,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007535 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
7536 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
7537 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007538};
7539
7540#if defined(CONFIG_NFS_V4_1)
7541static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
7542 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04007543 .init_caps = NFS_CAP_READDIRPLUS
7544 | NFS_CAP_ATOMIC_OPEN
7545 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04007546 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04007547 | NFS_CAP_STATEID_NFSV41
7548 | NFS_CAP_ATOMIC_OPEN_V1,
Bryan Schumaker104287c2012-11-12 14:13:13 -05007549 .call_sync = nfs4_call_sync_sequence,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007550 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007551 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007552 .free_lock_state = nfs41_free_lock_state,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007553 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7554 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7555 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007556};
7557#endif
7558
Steve Dickson42c2c422013-05-22 12:50:38 -04007559#if defined(CONFIG_NFS_V4_2)
7560static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
7561 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04007562 .init_caps = NFS_CAP_READDIRPLUS
7563 | NFS_CAP_ATOMIC_OPEN
7564 | NFS_CAP_CHANGE_ATTR
7565 | NFS_CAP_POSIX_LOCK
7566 | NFS_CAP_STATEID_NFSV41
7567 | NFS_CAP_ATOMIC_OPEN_V1,
Steve Dickson42c2c422013-05-22 12:50:38 -04007568 .call_sync = nfs4_call_sync_sequence,
7569 .match_stateid = nfs41_match_stateid,
7570 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04007571 .free_lock_state = nfs41_free_lock_state,
Steve Dickson42c2c422013-05-22 12:50:38 -04007572 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7573 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7574 .state_renewal_ops = &nfs41_state_renewal_ops,
7575};
7576#endif
7577
Trond Myklebust97dc1352010-06-16 09:52:26 -04007578const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
7579 [0] = &nfs_v4_0_minor_ops,
7580#if defined(CONFIG_NFS_V4_1)
7581 [1] = &nfs_v4_1_minor_ops,
7582#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04007583#if defined(CONFIG_NFS_V4_2)
7584 [2] = &nfs_v4_2_minor_ops,
7585#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04007586};
7587
Trond Myklebust17f26b12013-08-21 15:48:42 -04007588static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04007589 .create = nfs_create,
7590 .lookup = nfs_lookup,
7591 .atomic_open = nfs_atomic_open,
7592 .link = nfs_link,
7593 .unlink = nfs_unlink,
7594 .symlink = nfs_symlink,
7595 .mkdir = nfs_mkdir,
7596 .rmdir = nfs_rmdir,
7597 .mknod = nfs_mknod,
7598 .rename = nfs_rename,
7599 .permission = nfs_permission,
7600 .getattr = nfs_getattr,
7601 .setattr = nfs_setattr,
7602 .getxattr = generic_getxattr,
7603 .setxattr = generic_setxattr,
7604 .listxattr = generic_listxattr,
7605 .removexattr = generic_removexattr,
7606};
7607
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08007608static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007609 .permission = nfs_permission,
7610 .getattr = nfs_getattr,
7611 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007612 .getxattr = generic_getxattr,
7613 .setxattr = generic_setxattr,
7614 .listxattr = generic_listxattr,
7615 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007616};
7617
David Howells509de812006-08-22 20:06:11 -04007618const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007619 .version = 4, /* protocol version */
7620 .dentry_ops = &nfs4_dentry_operations,
7621 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007622 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04007623 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007624 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04007625 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04007626 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007627 .getattr = nfs4_proc_getattr,
7628 .setattr = nfs4_proc_setattr,
7629 .lookup = nfs4_proc_lookup,
7630 .access = nfs4_proc_access,
7631 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007632 .create = nfs4_proc_create,
7633 .remove = nfs4_proc_remove,
7634 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04007635 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007636 .unlink_done = nfs4_proc_unlink_done,
7637 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04007638 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04007639 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04007640 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007641 .link = nfs4_proc_link,
7642 .symlink = nfs4_proc_symlink,
7643 .mkdir = nfs4_proc_mkdir,
7644 .rmdir = nfs4_proc_remove,
7645 .readdir = nfs4_proc_readdir,
7646 .mknod = nfs4_proc_mknod,
7647 .statfs = nfs4_proc_statfs,
7648 .fsinfo = nfs4_proc_fsinfo,
7649 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04007650 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007651 .decode_dirent = nfs4_decode_dirent,
7652 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04007653 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04007654 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05007655 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007656 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04007657 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04007658 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007659 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007660 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04007661 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007662 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007663 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00007664 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04007665 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04007666 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04007667 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04007668 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04007669 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00007670 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04007671 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04007672 .create_server = nfs4_create_server,
7673 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007674};
7675
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007676static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7677 .prefix = XATTR_NAME_NFSV4_ACL,
7678 .list = nfs4_xattr_list_nfs4_acl,
7679 .get = nfs4_xattr_get_nfs4_acl,
7680 .set = nfs4_xattr_set_nfs4_acl,
7681};
7682
7683const struct xattr_handler *nfs4_xattr_handlers[] = {
7684 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04007685#ifdef CONFIG_NFS_V4_SECURITY_LABEL
7686 &nfs4_xattr_nfs4_label_handler,
7687#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007688 NULL
7689};
7690
Linus Torvalds1da177e2005-04-16 15:20:36 -07007691/*
7692 * Local variables:
7693 * c-basic-offset: 8
7694 * End:
7695 */