blob: c8b5ff6effe946ca7f5368e2991bc2189eaa57ca [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Trond Myklebust6926afd2012-01-07 13:22:46 -050054#include <linux/nfs_idmap.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000055#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050056#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010057#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050066#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000067#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Trond Myklebustc6d01c62013-08-09 11:51:26 -040069#include "nfs4trace.h"
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define NFSDBG_FACILITY NFSDBG_PROC
72
Trond Myklebust2066fe82006-09-15 08:30:46 -040073#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define NFS4_POLL_RETRY_MAX (15*HZ)
75
Trond Myklebustcdd4e682006-01-03 09:55:12 +010076struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010077static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080078static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050080static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Chuck Lever81934dd2012-03-01 17:01:57 -050081static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040082static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
83static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040084static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
85 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -040086 struct nfs4_state *state, struct nfs4_label *ilabel,
87 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040088#ifdef CONFIG_NFS_V4_1
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040089static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
90 struct rpc_cred *);
91static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *,
92 struct rpc_cred *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040093#endif
David Quigleyaa9c2662013-05-22 12:50:44 -040094
95#ifdef CONFIG_NFS_V4_SECURITY_LABEL
96static inline struct nfs4_label *
97nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
98 struct iattr *sattr, struct nfs4_label *label)
99{
100 int err;
101
102 if (label == NULL)
103 return NULL;
104
105 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
106 return NULL;
107
108 if (NFS_SERVER(dir)->nfs_client->cl_minorversion < 2)
109 return NULL;
110
111 err = security_dentry_init_security(dentry, sattr->ia_mode,
112 &dentry->d_name, (void **)&label->label, &label->len);
113 if (err == 0)
114 return label;
115
116 return NULL;
117}
118static inline void
119nfs4_label_release_security(struct nfs4_label *label)
120{
121 if (label)
122 security_release_secctx(label->label, label->len);
123}
124static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
125{
126 if (label)
127 return server->attr_bitmask;
128
129 return server->attr_bitmask_nl;
130}
131#else
132static inline struct nfs4_label *
133nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
134 struct iattr *sattr, struct nfs4_label *l)
135{ return NULL; }
136static inline void
137nfs4_label_release_security(struct nfs4_label *label)
138{ return; }
139static inline u32 *
140nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
141{ return server->attr_bitmask; }
142#endif
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500145static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Trond Myklebust52567b02009-10-23 14:46:42 -0400147 if (err >= -1000)
148 return err;
149 switch (err) {
150 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500151 case -NFS4ERR_LAYOUTTRYLATER:
152 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400153 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000154 case -NFS4ERR_WRONGSEC:
155 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800156 case -NFS4ERR_BADOWNER:
157 case -NFS4ERR_BADNAME:
158 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400159 case -NFS4ERR_SHARE_DENIED:
160 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400161 case -NFS4ERR_MINOR_VERS_MISMATCH:
162 return -EPROTONOSUPPORT;
Weston Andros Adamson6168f622012-09-10 14:00:46 -0400163 case -NFS4ERR_ACCESS:
164 return -EACCES;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400165 case -NFS4ERR_FILE_OPEN:
166 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400167 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700169 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400170 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400172 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
175/*
176 * This is our standard bitmap for GETATTR requests.
177 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400178const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 FATTR4_WORD0_TYPE
180 | FATTR4_WORD0_CHANGE
181 | FATTR4_WORD0_SIZE
182 | FATTR4_WORD0_FSID
183 | FATTR4_WORD0_FILEID,
184 FATTR4_WORD1_MODE
185 | FATTR4_WORD1_NUMLINKS
186 | FATTR4_WORD1_OWNER
187 | FATTR4_WORD1_OWNER_GROUP
188 | FATTR4_WORD1_RAWDEV
189 | FATTR4_WORD1_SPACE_USED
190 | FATTR4_WORD1_TIME_ACCESS
191 | FATTR4_WORD1_TIME_METADATA
David Quigleyaa9c2662013-05-22 12:50:44 -0400192 | FATTR4_WORD1_TIME_MODIFY,
193#ifdef CONFIG_NFS_V4_SECURITY_LABEL
194 FATTR4_WORD2_SECURITY_LABEL
195#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196};
197
Trond Myklebust1549210f2012-06-05 09:16:47 -0400198static const u32 nfs4_pnfs_open_bitmap[3] = {
199 FATTR4_WORD0_TYPE
200 | FATTR4_WORD0_CHANGE
201 | FATTR4_WORD0_SIZE
202 | FATTR4_WORD0_FSID
203 | FATTR4_WORD0_FILEID,
204 FATTR4_WORD1_MODE
205 | FATTR4_WORD1_NUMLINKS
206 | FATTR4_WORD1_OWNER
207 | FATTR4_WORD1_OWNER_GROUP
208 | FATTR4_WORD1_RAWDEV
209 | FATTR4_WORD1_SPACE_USED
210 | FATTR4_WORD1_TIME_ACCESS
211 | FATTR4_WORD1_TIME_METADATA
212 | FATTR4_WORD1_TIME_MODIFY,
213 FATTR4_WORD2_MDSTHRESHOLD
214};
215
Andy Adamsone23008e2012-10-02 21:07:32 -0400216static const u32 nfs4_open_noattr_bitmap[3] = {
217 FATTR4_WORD0_TYPE
218 | FATTR4_WORD0_CHANGE
219 | FATTR4_WORD0_FILEID,
220};
221
David Quigleya09df2c2013-05-22 12:50:41 -0400222const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 FATTR4_WORD0_FILES_AVAIL
224 | FATTR4_WORD0_FILES_FREE
225 | FATTR4_WORD0_FILES_TOTAL,
226 FATTR4_WORD1_SPACE_AVAIL
227 | FATTR4_WORD1_SPACE_FREE
228 | FATTR4_WORD1_SPACE_TOTAL
229};
230
David Quigleya09df2c2013-05-22 12:50:41 -0400231const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 FATTR4_WORD0_MAXLINK
233 | FATTR4_WORD0_MAXNAME,
234 0
235};
236
Fred Isamandae100c2011-07-30 20:52:37 -0400237const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 | FATTR4_WORD0_MAXREAD
239 | FATTR4_WORD0_MAXWRITE
240 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700241 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400242 | FATTR4_WORD1_FS_LAYOUT_TYPES,
243 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244};
245
David Quigleya09df2c2013-05-22 12:50:41 -0400246const u32 nfs4_fs_locations_bitmap[3] = {
Manoj Naik830b8e32006-06-09 09:34:25 -0400247 FATTR4_WORD0_TYPE
248 | FATTR4_WORD0_CHANGE
249 | FATTR4_WORD0_SIZE
250 | FATTR4_WORD0_FSID
251 | FATTR4_WORD0_FILEID
252 | FATTR4_WORD0_FS_LOCATIONS,
253 FATTR4_WORD1_MODE
254 | FATTR4_WORD1_NUMLINKS
255 | FATTR4_WORD1_OWNER
256 | FATTR4_WORD1_OWNER_GROUP
257 | FATTR4_WORD1_RAWDEV
258 | FATTR4_WORD1_SPACE_USED
259 | FATTR4_WORD1_TIME_ACCESS
260 | FATTR4_WORD1_TIME_METADATA
261 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400262 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400263};
264
Al Virobc4785c2006-10-19 23:28:51 -0700265static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 struct nfs4_readdir_arg *readdir)
267{
Al Viro0dbb4c62006-10-19 23:28:49 -0700268 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000271 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
273 return;
274 }
275
276 readdir->cookie = 0;
277 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
278 if (cookie == 2)
279 return;
280
281 /*
282 * NFSv4 servers do not return entries for '.' and '..'
283 * Therefore, we fake these entries here. We let '.'
284 * have cookie 0 and '..' have cookie 1. Note that
285 * when talking to the server, we always send cookie 0
286 * instead of 1 or 2.
287 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800288 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 if (cookie == 0) {
291 *p++ = xdr_one; /* next */
292 *p++ = xdr_zero; /* cookie, first word */
293 *p++ = xdr_one; /* cookie, second word */
294 *p++ = xdr_one; /* entry len */
295 memcpy(p, ".\0\0\0", 4); /* entry */
296 p++;
297 *p++ = xdr_one; /* bitmap length */
298 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
299 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400300 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302
303 *p++ = xdr_one; /* next */
304 *p++ = xdr_zero; /* cookie, first word */
305 *p++ = xdr_two; /* cookie, second word */
306 *p++ = xdr_two; /* entry len */
307 memcpy(p, "..\0\0", 4); /* entry */
308 p++;
309 *p++ = xdr_one; /* bitmap length */
310 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
311 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400312 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 readdir->pgbase = (char *)p - (char *)start;
315 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800316 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
Trond Myklebust65de8722008-12-23 15:21:44 -0500319static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
320{
321 int res = 0;
322
323 might_sleep();
324
325 if (*timeout <= 0)
326 *timeout = NFS4_POLL_RETRY_MIN;
327 if (*timeout > NFS4_POLL_RETRY_MAX)
328 *timeout = NFS4_POLL_RETRY_MAX;
Colin Cross416ad3c2013-05-06 23:50:06 +0000329 freezable_schedule_timeout_killable_unsafe(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500330 if (fatal_signal_pending(current))
331 res = -ERESTARTSYS;
332 *timeout <<= 1;
333 return res;
334}
335
336/* This is the error handling routine for processes that are allowed
337 * to sleep.
338 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800339static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500340{
341 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500342 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500343 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500344 int ret = errorcode;
345
346 exception->retry = 0;
347 switch(errorcode) {
348 case 0:
349 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500350 case -NFS4ERR_OPENMODE:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400351 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400352 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500353 exception->retry = 1;
354 return 0;
355 }
356 if (state == NULL)
357 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400358 ret = nfs4_schedule_stateid_recovery(server, state);
359 if (ret < 0)
360 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500361 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500362 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500363 case -NFS4ERR_ADMIN_REVOKED:
364 case -NFS4ERR_BAD_STATEID:
Trond Myklebustb570a972013-04-12 15:04:51 -0400365 if (inode != NULL && nfs4_have_delegation(inode, FMODE_READ)) {
366 nfs_remove_bad_delegation(inode);
367 exception->retry = 1;
368 break;
369 }
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500370 if (state == NULL)
371 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400372 ret = nfs4_schedule_stateid_recovery(server, state);
373 if (ret < 0)
374 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500375 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400376 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -0400377 if (state != NULL) {
378 ret = nfs4_schedule_stateid_recovery(server, state);
379 if (ret < 0)
380 break;
381 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500382 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500383 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500384 nfs4_schedule_lease_recovery(clp);
385 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500386#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400387 case -NFS4ERR_BADSESSION:
388 case -NFS4ERR_BADSLOT:
389 case -NFS4ERR_BAD_HIGH_SLOT:
390 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
391 case -NFS4ERR_DEADSESSION:
392 case -NFS4ERR_SEQ_FALSE_RETRY:
393 case -NFS4ERR_SEQ_MISORDERED:
394 dprintk("%s ERROR: %d Reset session\n", __func__,
395 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400396 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400397 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500398#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500399 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500400 if (exception->timeout > HZ) {
401 /* We have retried a decent amount, time to
402 * fail
403 */
404 ret = -EBUSY;
405 break;
406 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500407 case -NFS4ERR_GRACE:
408 case -NFS4ERR_DELAY:
409 ret = nfs4_delay(server->client, &exception->timeout);
410 if (ret != 0)
411 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400412 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500413 case -NFS4ERR_OLD_STATEID:
414 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800415 break;
416 case -NFS4ERR_BADOWNER:
417 /* The following works around a Linux server bug! */
418 case -NFS4ERR_BADNAME:
419 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
420 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
421 exception->retry = 1;
422 printk(KERN_WARNING "NFS: v4 server %s "
423 "does not accept raw "
424 "uid/gids. "
425 "Reenabling the idmapper.\n",
426 server->nfs_client->cl_hostname);
427 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500428 }
429 /* We failed to handle the error */
430 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500431wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500432 ret = nfs4_wait_clnt_recover(clp);
433 if (ret == 0)
434 exception->retry = 1;
435 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500436}
437
438
Trond Myklebust452e9352010-07-31 14:29:06 -0400439static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 spin_lock(&clp->cl_lock);
442 if (time_before(clp->cl_last_renewal,timestamp))
443 clp->cl_last_renewal = timestamp;
444 spin_unlock(&clp->cl_lock);
445}
446
Trond Myklebust452e9352010-07-31 14:29:06 -0400447static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
448{
449 do_renew_lease(server->nfs_client, timestamp);
450}
451
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400452#if defined(CONFIG_NFS_V4_1)
453
Trond Myklebustd185a332010-06-16 09:52:25 -0400454static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400455{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500456 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400457 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500458 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400459
Benny Halevydfb4f3092010-09-24 09:17:01 -0400460 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400461 /* just wake up the next guy waiting since
462 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500463 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500464 return;
Andy Adamson13615872009-04-01 09:22:17 -0400465 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500466 tbl = res->sr_slot->table;
467 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500468
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500469 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500470 /* Be nice to the server: try to ensure that the last transmitted
471 * value for highest_user_slotid <= target_highest_slotid
472 */
473 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
474 send_new_highest_used_slotid = true;
475
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500476 if (nfs41_wake_and_assign_slot(tbl, res->sr_slot)) {
477 send_new_highest_used_slotid = false;
478 goto out_unlock;
479 }
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500480 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500481
482 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
483 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500484out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500485 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400486 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500487 if (send_new_highest_used_slotid)
488 nfs41_server_notify_highest_slotid_update(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400489}
490
Trond Myklebust14516c32010-07-31 14:29:06 -0400491static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400492{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500493 struct nfs4_session *session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500494 struct nfs4_slot *slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400495 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500496 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500497 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400498
Bryan Schumaker468f8612011-04-18 15:57:32 -0400499 /* don't increment the sequence number if the task wasn't sent */
500 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400501 goto out;
502
Trond Myklebust933602e2012-11-16 12:12:38 -0500503 slot = res->sr_slot;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500504 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500505
Trond Myklebustac20d162012-12-15 15:36:07 -0500506 if (slot->interrupted) {
507 slot->interrupted = 0;
508 interrupted = true;
509 }
510
Andy Adamson691daf32009-12-04 15:55:39 -0500511 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400512 switch (res->sr_status) {
513 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400514 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500515 ++slot->seq_nr;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500516 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500517 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500518 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500519 if (res->sr_status_flags != 0)
520 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500521 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400522 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500523 case 1:
524 /*
525 * sr_status remains 1 if an RPC level error occurred.
526 * The server may or may not have processed the sequence
527 * operation..
528 * Mark the slot as having hosted an interrupted RPC call.
529 */
530 slot->interrupted = 1;
531 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400532 case -NFS4ERR_DELAY:
533 /* The server detected a resend of the RPC call and
534 * returned NFS4ERR_DELAY as per Section 2.10.6.2
535 * of RFC5661.
536 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500537 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400538 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500539 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500540 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400541 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500542 case -NFS4ERR_BADSLOT:
543 /*
544 * The slot id we used was probably retired. Try again
545 * using a different slot id.
546 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500547 goto retry_nowait;
548 case -NFS4ERR_SEQ_MISORDERED:
549 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500550 * Was the last operation on this sequence interrupted?
551 * If so, retry after bumping the sequence number.
552 */
553 if (interrupted) {
554 ++slot->seq_nr;
555 goto retry_nowait;
556 }
557 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500558 * Could this slot have been previously retired?
559 * If so, then the server may be expecting seq_nr = 1!
560 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500561 if (slot->seq_nr != 1) {
562 slot->seq_nr = 1;
563 goto retry_nowait;
564 }
565 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500566 case -NFS4ERR_SEQ_FALSE_RETRY:
567 ++slot->seq_nr;
568 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400569 default:
570 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500571 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400572 }
573out:
574 /* The session may be reset by one of the error handlers. */
575 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400576 nfs41_sequence_free_slot(res);
Trond Myklebust85563072012-12-11 10:31:12 -0500577 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500578retry_nowait:
579 if (rpc_restart_call_prepare(task)) {
580 task->tk_status = 0;
581 ret = 0;
582 }
583 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400584out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400585 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400586 goto out;
587 rpc_delay(task, NFS4_POLL_RETRY_MAX);
588 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400589}
590
Trond Myklebust14516c32010-07-31 14:29:06 -0400591static int nfs4_sequence_done(struct rpc_task *task,
592 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400593{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500594 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400595 return 1;
596 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400597}
598
Trond Myklebust9d12b212012-01-17 22:04:25 -0500599static void nfs41_init_sequence(struct nfs4_sequence_args *args,
600 struct nfs4_sequence_res *res, int cache_reply)
601{
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500602 args->sa_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500603 args->sa_cache_this = 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400604 args->sa_privileged = 0;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500605 if (cache_reply)
606 args->sa_cache_this = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500607 res->sr_slot = NULL;
608}
609
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400610static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
611{
612 args->sa_privileged = 1;
613}
614
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000615int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400616 struct nfs4_sequence_args *args,
617 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400618 struct rpc_task *task)
619{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400620 struct nfs4_slot *slot;
621 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400622
623 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400624 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400625 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400626 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400627
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400628 tbl = &session->fc_slot_table;
629
Trond Myklebust69d206b2012-11-22 13:21:02 -0500630 task->tk_timeout = 0;
631
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400632 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400633 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400634 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500635 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500636 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400637 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400638 }
639
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500640 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500641 if (IS_ERR(slot)) {
642 /* If out of memory, try again in 1/4 second */
643 if (slot == ERR_PTR(-ENOMEM))
644 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400645 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400646 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400647 }
648 spin_unlock(&tbl->slot_tbl_lock);
649
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500650 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400651
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500652 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
653 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400654
Benny Halevydfb4f3092010-09-24 09:17:01 -0400655 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500656 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400657 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400658 /*
659 * sr_status is only set in decode_sequence, and so will remain
660 * set to 1 if an rpc level failure occurs.
661 */
662 res->sr_status = 1;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400663out_success:
664 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400665 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400666out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400667 /* Privileged tasks are queued with top priority */
668 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400669 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
670 NULL, RPC_PRIORITY_PRIVILEGED);
671 else
672 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400673 spin_unlock(&tbl->slot_tbl_lock);
674 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400675}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000676EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400677
Trond Myklebust035168a2010-06-16 09:52:26 -0400678int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400679 struct nfs4_sequence_args *args,
680 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400681 struct rpc_task *task)
682{
Trond Myklebust035168a2010-06-16 09:52:26 -0400683 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400684 int ret = 0;
685
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400686 if (session == NULL) {
687 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400688 goto out;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400689 }
Trond Myklebust035168a2010-06-16 09:52:26 -0400690
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500691 dprintk("--> %s clp %p session %p sr_slot %d\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400692 __func__, session->clp, session, res->sr_slot ?
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500693 res->sr_slot->slot_nr : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400694
Trond Myklebust9d12b212012-01-17 22:04:25 -0500695 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400696out:
697 dprintk("<-- %s status=%d\n", __func__, ret);
698 return ret;
699}
700
701struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400702 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400703 struct nfs4_sequence_args *seq_args;
704 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400705};
706
707static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
708{
709 struct nfs41_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400710 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400711
Trond Myklebust035168a2010-06-16 09:52:26 -0400712 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
713
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400714 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400715}
716
Andy Adamson69ab40c2009-04-01 09:22:19 -0400717static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
718{
719 struct nfs41_call_sync_data *data = calldata;
720
Trond Myklebust14516c32010-07-31 14:29:06 -0400721 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400722}
723
Trond Myklebust17280172012-03-11 13:11:00 -0400724static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400725 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400726 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400727};
728
Bryan Schumaker7c513052011-03-24 17:12:24 +0000729static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
730 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400731 struct rpc_message *msg,
732 struct nfs4_sequence_args *args,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400733 struct nfs4_sequence_res *res)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400734{
735 int ret;
736 struct rpc_task *task;
737 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400738 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400739 .seq_args = args,
740 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400741 };
742 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000743 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400744 .rpc_message = msg,
745 .callback_ops = &nfs41_call_sync_ops,
746 .callback_data = &data
747 };
748
Andy Adamsonce5039c2009-04-01 09:22:13 -0400749 task = rpc_run_task(&task_setup);
750 if (IS_ERR(task))
751 ret = PTR_ERR(task);
752 else {
753 ret = task->tk_status;
754 rpc_put_task(task);
755 }
756 return ret;
757}
758
Trond Myklebustdf896452010-06-16 09:52:26 -0400759#else
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400760static
Trond Myklebust9d12b212012-01-17 22:04:25 -0500761void nfs41_init_sequence(struct nfs4_sequence_args *args,
762 struct nfs4_sequence_res *res, int cache_reply)
763{
764}
765
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400766static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
767{
768}
769
770
Trond Myklebust14516c32010-07-31 14:29:06 -0400771static int nfs4_sequence_done(struct rpc_task *task,
772 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400773{
Trond Myklebust14516c32010-07-31 14:29:06 -0400774 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400775}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400776#endif /* CONFIG_NFS_V4_1 */
777
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400778static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000779int _nfs4_call_sync(struct rpc_clnt *clnt,
780 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400781 struct rpc_message *msg,
782 struct nfs4_sequence_args *args,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400783 struct nfs4_sequence_res *res)
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400784{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000785 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400786}
787
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400788static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000789int nfs4_call_sync(struct rpc_clnt *clnt,
790 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000791 struct rpc_message *msg,
792 struct nfs4_sequence_args *args,
793 struct nfs4_sequence_res *res,
794 int cache_reply)
795{
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400796 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000797 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400798 args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000799}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400800
Trond Myklebust38478b22006-05-25 01:40:57 -0400801static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
Trond Myklebust38478b22006-05-25 01:40:57 -0400803 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Trond Myklebust38478b22006-05-25 01:40:57 -0400805 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400806 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700807 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400808 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700809 dir->i_version = cinfo->after;
David Howellsde242c02012-12-20 21:52:38 +0000810 nfs_fscache_invalidate(dir);
Trond Myklebust38478b22006-05-25 01:40:57 -0400811 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812}
813
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100814struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400815 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100816 struct nfs_openargs o_arg;
817 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100818 struct nfs_open_confirmargs c_arg;
819 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500820 struct nfs4_string owner_name;
821 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100822 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400823 struct nfs4_label *f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100824 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400825 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100826 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400827 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100828 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100829 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400830 unsigned int rpc_done : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -0400831 unsigned int is_recover : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100832 int rpc_status;
833 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100834};
835
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400836static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
837 int err, struct nfs4_exception *exception)
838{
839 if (err != -EINVAL)
840 return false;
841 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
842 return false;
843 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
844 exception->retry = 1;
845 return true;
846}
847
848static enum open_claim_type4
849nfs4_map_atomic_open_claim(struct nfs_server *server,
850 enum open_claim_type4 claim)
851{
852 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
853 return claim;
854 switch (claim) {
855 default:
856 return claim;
857 case NFS4_OPEN_CLAIM_FH:
858 return NFS4_OPEN_CLAIM_NULL;
859 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
860 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
861 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
862 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
863 }
864}
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400865
866static void nfs4_init_opendata_res(struct nfs4_opendata *p)
867{
868 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400869 p->o_res.f_label = p->f_label;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400870 p->o_res.seqid = p->o_arg.seqid;
871 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400872 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400873 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400874 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500875 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400876}
877
Al Viro82a2c1b2011-06-22 18:30:55 -0400878static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500879 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400880 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -0400881 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400882 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400883 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100884{
Al Viro82a2c1b2011-06-22 18:30:55 -0400885 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100886 struct inode *dir = parent->d_inode;
887 struct nfs_server *server = NFS_SERVER(dir);
888 struct nfs4_opendata *p;
889
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400890 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100891 if (p == NULL)
892 goto err;
David Quigley14c43f72013-05-22 12:50:43 -0400893
894 p->f_label = nfs4_label_alloc(server, gfp_mask);
895 if (IS_ERR(p->f_label))
896 goto err_free_p;
897
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400898 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100899 if (p->o_arg.seqid == NULL)
David Quigley14c43f72013-05-22 12:50:43 -0400900 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -0400901 nfs_sb_active(dentry->d_sb);
902 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100903 p->dir = parent;
904 p->owner = sp;
905 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500906 p->o_arg.open_flags = flags;
907 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700908 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
909 * will return permission denied for all bits until close */
910 if (!(flags & O_EXCL)) {
911 /* ask server to check for all possible rights as results
912 * are cached */
913 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
914 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700915 }
David Howells7539bba2006-08-22 20:06:09 -0400916 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400917 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
918 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400919 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100920 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -0400921 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -0400922 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
David Quigley1775fd32013-05-22 12:50:42 -0400923 p->o_arg.label = label;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400924 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
925 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400926 case NFS4_OPEN_CLAIM_NULL:
927 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
928 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
929 p->o_arg.fh = NFS_FH(dir);
930 break;
931 case NFS4_OPEN_CLAIM_PREVIOUS:
932 case NFS4_OPEN_CLAIM_FH:
933 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
934 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
935 p->o_arg.fh = NFS_FH(dentry->d_inode);
936 }
Trond Myklebust536e43d2012-01-17 22:04:26 -0500937 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -0400938 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400939
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100940 p->o_arg.u.attrs = &p->attrs;
941 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500942
943 verf[0] = jiffies;
944 verf[1] = current->pid;
945 memcpy(p->o_arg.u.verifier.data, verf,
946 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100947 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100948 p->c_arg.fh = &p->o_res.fh;
949 p->c_arg.stateid = &p->o_res.stateid;
950 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400951 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400952 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100953 return p;
David Quigley14c43f72013-05-22 12:50:43 -0400954
955err_free_label:
956 nfs4_label_free(p->f_label);
957err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100958 kfree(p);
959err:
960 dput(parent);
961 return NULL;
962}
963
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400964static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100965{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400966 struct nfs4_opendata *p = container_of(kref,
967 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400968 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400969
970 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400971 if (p->state != NULL)
972 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400973 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -0400974
975 nfs4_label_free(p->f_label);
976
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400977 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400978 dput(p->dentry);
979 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500980 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400981 kfree(p);
982}
983
984static void nfs4_opendata_put(struct nfs4_opendata *p)
985{
986 if (p != NULL)
987 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100988}
989
Trond Myklebust06f814a2006-01-03 09:55:07 +0100990static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
991{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100992 int ret;
993
Trond Myklebust06f814a2006-01-03 09:55:07 +0100994 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100995 return ret;
996}
997
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500998static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400999{
1000 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001001
Trond Myklebust536e43d2012-01-17 22:04:26 -05001002 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001003 goto out;
1004 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001005 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001006 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1007 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001008 break;
1009 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001010 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1011 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001012 break;
1013 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001014 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1015 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001016 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001017out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001018 return ret;
1019}
1020
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001021static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001022{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001023 if (delegation == NULL)
1024 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001025 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001026 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -05001027 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -04001028 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001029 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1030 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -05001031 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001032 return 1;
1033}
1034
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001035static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001036{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001037 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001038 case FMODE_WRITE:
1039 state->n_wronly++;
1040 break;
1041 case FMODE_READ:
1042 state->n_rdonly++;
1043 break;
1044 case FMODE_READ|FMODE_WRITE:
1045 state->n_rdwr++;
1046 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001047 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001048}
1049
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001050static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001051{
1052 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001053 nfs4_stateid_copy(&state->stateid, stateid);
1054 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust92b40e92013-04-20 01:25:45 -04001055 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001056 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001057 case FMODE_READ:
1058 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1059 break;
1060 case FMODE_WRITE:
1061 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1062 break;
1063 case FMODE_READ|FMODE_WRITE:
1064 set_bit(NFS_O_RDWR_STATE, &state->flags);
1065 }
1066}
1067
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001068static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001069{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001070 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001071 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001072 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001073}
1074
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001075static 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 -07001076{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001077 /*
1078 * Protect the call to nfs4_state_set_mode_locked and
1079 * serialise the stateid update
1080 */
1081 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001082 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001083 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001084 set_bit(NFS_DELEGATED_STATE, &state->flags);
1085 }
1086 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001087 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001088 write_sequnlock(&state->seqlock);
1089 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001090 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001091 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092}
1093
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001094static 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 -05001095{
1096 struct nfs_inode *nfsi = NFS_I(state->inode);
1097 struct nfs_delegation *deleg_cur;
1098 int ret = 0;
1099
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001100 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001101
1102 rcu_read_lock();
1103 deleg_cur = rcu_dereference(nfsi->delegation);
1104 if (deleg_cur == NULL)
1105 goto no_delegation;
1106
1107 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001108 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001109 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001110 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001111 goto no_delegation_unlock;
1112
1113 if (delegation == NULL)
1114 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001115 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001116 goto no_delegation_unlock;
1117
Trond Myklebustb7391f42008-12-23 15:21:52 -05001118 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001119 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001120 ret = 1;
1121no_delegation_unlock:
1122 spin_unlock(&deleg_cur->lock);
1123no_delegation:
1124 rcu_read_unlock();
1125
1126 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001127 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001128 ret = 1;
1129 }
1130
1131 return ret;
1132}
1133
1134
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001135static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001136{
1137 struct nfs_delegation *delegation;
1138
1139 rcu_read_lock();
1140 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001141 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001142 rcu_read_unlock();
1143 return;
1144 }
1145 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001146 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001147}
1148
Trond Myklebust6ee41262007-07-08 14:11:36 -04001149static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001150{
1151 struct nfs4_state *state = opendata->state;
1152 struct nfs_inode *nfsi = NFS_I(state->inode);
1153 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001154 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001155 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001156 nfs4_stateid stateid;
1157 int ret = -EAGAIN;
1158
Trond Myklebustaac00a82007-07-05 19:02:21 -04001159 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001160 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001161 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001162 if (can_open_cached(state, fmode, open_mode)) {
1163 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001164 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001165 goto out_return_state;
1166 }
1167 spin_unlock(&state->owner->so_lock);
1168 }
Trond Myklebust34310432008-12-23 15:21:38 -05001169 rcu_read_lock();
1170 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001171 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001172 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001173 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001174 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001175 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001176 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001177 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001178 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001179 if (!opendata->is_recover) {
1180 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1181 if (ret != 0)
1182 goto out;
1183 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001184 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001185
1186 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001187 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001188 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001189 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001190out:
1191 return ERR_PTR(ret);
1192out_return_state:
1193 atomic_inc(&state->count);
1194 return state;
1195}
1196
Andy Adamsone23008e2012-10-02 21:07:32 -04001197static void
1198nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1199{
1200 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1201 struct nfs_delegation *delegation;
1202 int delegation_flags = 0;
1203
1204 rcu_read_lock();
1205 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1206 if (delegation)
1207 delegation_flags = delegation->flags;
1208 rcu_read_unlock();
1209 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1210 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1211 "returning a delegation for "
1212 "OPEN(CLAIM_DELEGATE_CUR)\n",
1213 clp->cl_hostname);
1214 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1215 nfs_inode_set_delegation(state->inode,
1216 data->owner->so_cred,
1217 &data->o_res);
1218 else
1219 nfs_inode_reclaim_delegation(state->inode,
1220 data->owner->so_cred,
1221 &data->o_res);
1222}
1223
1224/*
1225 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1226 * and update the nfs4_state.
1227 */
1228static struct nfs4_state *
1229_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1230{
1231 struct inode *inode = data->state->inode;
1232 struct nfs4_state *state = data->state;
1233 int ret;
1234
1235 if (!data->rpc_done) {
1236 ret = data->rpc_status;
1237 goto err;
1238 }
1239
1240 ret = -ESTALE;
1241 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1242 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1243 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1244 goto err;
1245
1246 ret = -ENOMEM;
1247 state = nfs4_get_open_state(inode, data->owner);
1248 if (state == NULL)
1249 goto err;
1250
1251 ret = nfs_refresh_inode(inode, &data->f_attr);
1252 if (ret)
1253 goto err;
1254
David Quigleyaa9c2662013-05-22 12:50:44 -04001255 nfs_setsecurity(inode, &data->f_attr, data->f_label);
1256
Andy Adamsone23008e2012-10-02 21:07:32 -04001257 if (data->o_res.delegation_type != 0)
1258 nfs4_opendata_check_deleg(data, state);
1259 update_open_stateid(state, &data->o_res.stateid, NULL,
1260 data->o_arg.fmode);
1261
1262 return state;
1263err:
1264 return ERR_PTR(ret);
1265
1266}
1267
1268static struct nfs4_state *
1269_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001270{
1271 struct inode *inode;
1272 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001273 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001274
Trond Myklebustaac00a82007-07-05 19:02:21 -04001275 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001276 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001277 goto out;
1278 }
1279
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001280 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001281 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001282 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001283 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001284 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001285 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001286 goto err;
1287 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001288 state = nfs4_get_open_state(inode, data->owner);
1289 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001290 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001291 if (data->o_res.delegation_type != 0)
1292 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001293 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001294 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001295 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001296out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001297 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001298 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001299err_put_inode:
1300 iput(inode);
1301err:
1302 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001303}
1304
Andy Adamsone23008e2012-10-02 21:07:32 -04001305static struct nfs4_state *
1306nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1307{
1308 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1309 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1310 return _nfs4_opendata_to_nfs4_state(data);
1311}
1312
Trond Myklebust864472e2006-01-03 09:55:15 +01001313static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1314{
1315 struct nfs_inode *nfsi = NFS_I(state->inode);
1316 struct nfs_open_context *ctx;
1317
1318 spin_lock(&state->inode->i_lock);
1319 list_for_each_entry(ctx, &nfsi->open_files, list) {
1320 if (ctx->state != state)
1321 continue;
1322 get_nfs_open_context(ctx);
1323 spin_unlock(&state->inode->i_lock);
1324 return ctx;
1325 }
1326 spin_unlock(&state->inode->i_lock);
1327 return ERR_PTR(-ENOENT);
1328}
1329
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001330static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1331 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001332{
1333 struct nfs4_opendata *opendata;
1334
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001335 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001336 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001337 if (opendata == NULL)
1338 return ERR_PTR(-ENOMEM);
1339 opendata->state = state;
1340 atomic_inc(&state->count);
1341 return opendata;
1342}
1343
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001344static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001345{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001346 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001347 int ret;
1348
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001349 opendata->o_arg.open_flags = 0;
1350 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001351 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1352 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1353 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001354 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001355 if (ret != 0)
1356 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001357 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001358 if (IS_ERR(newstate))
1359 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001360 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001361 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001362 return 0;
1363}
1364
1365static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1366{
Trond Myklebust864472e2006-01-03 09:55:15 +01001367 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001368 int ret;
1369
1370 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001371 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001372 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001373 smp_rmb();
1374 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001375 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001376 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001377 if (ret != 0)
1378 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001379 if (newstate != state)
1380 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001381 }
1382 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001383 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001384 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001385 if (ret != 0)
1386 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001387 if (newstate != state)
1388 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001389 }
1390 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001391 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001392 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &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 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001398 /*
1399 * We may have performed cached opens for all three recoveries.
1400 * Check if we need to update the current stateid.
1401 */
1402 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001403 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001404 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001405 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001406 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001407 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001408 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001409 return 0;
1410}
1411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412/*
1413 * OPEN_RECLAIM:
1414 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001416static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001418 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001419 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001420 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 int status;
1422
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001423 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1424 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001425 if (IS_ERR(opendata))
1426 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001427 rcu_read_lock();
1428 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001429 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001430 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001431 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001432 opendata->o_arg.u.delegation_type = delegation_type;
1433 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001434 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 return status;
1436}
1437
Trond Myklebust539cd032007-06-05 11:46:42 -04001438static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
1440 struct nfs_server *server = NFS_SERVER(state->inode);
1441 struct nfs4_exception exception = { };
1442 int err;
1443 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001444 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001445 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001446 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1447 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001448 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001449 break;
1450 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 } while (exception.retry);
1452 return err;
1453}
1454
Trond Myklebust864472e2006-01-03 09:55:15 +01001455static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1456{
1457 struct nfs_open_context *ctx;
1458 int ret;
1459
1460 ctx = nfs4_state_find_open_context(state);
1461 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001462 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001463 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001464 put_nfs_open_context(ctx);
1465 return ret;
1466}
1467
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001468static 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 -07001469{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001470 switch (err) {
1471 default:
1472 printk(KERN_ERR "NFS: %s: unhandled error "
1473 "%d.\n", __func__, err);
1474 case 0:
1475 case -ENOENT:
1476 case -ESTALE:
1477 break;
1478 case -NFS4ERR_BADSESSION:
1479 case -NFS4ERR_BADSLOT:
1480 case -NFS4ERR_BAD_HIGH_SLOT:
1481 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1482 case -NFS4ERR_DEADSESSION:
1483 set_bit(NFS_DELEGATED_STATE, &state->flags);
1484 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1485 return -EAGAIN;
1486 case -NFS4ERR_STALE_CLIENTID:
1487 case -NFS4ERR_STALE_STATEID:
1488 set_bit(NFS_DELEGATED_STATE, &state->flags);
1489 case -NFS4ERR_EXPIRED:
1490 /* Don't recall a delegation if it was lost */
1491 nfs4_schedule_lease_recovery(server->nfs_client);
1492 return -EAGAIN;
1493 case -NFS4ERR_DELEG_REVOKED:
1494 case -NFS4ERR_ADMIN_REVOKED:
1495 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001496 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001497 nfs_inode_find_state_and_recover(state->inode,
1498 stateid);
1499 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001500 return 0;
1501 case -NFS4ERR_DELAY:
1502 case -NFS4ERR_GRACE:
1503 set_bit(NFS_DELEGATED_STATE, &state->flags);
1504 ssleep(1);
1505 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001506 case -ENOMEM:
1507 case -NFS4ERR_DENIED:
1508 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1509 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 return err;
1512}
1513
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001514int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1515{
1516 struct nfs_server *server = NFS_SERVER(state->inode);
1517 struct nfs4_opendata *opendata;
1518 int err;
1519
1520 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1521 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1522 if (IS_ERR(opendata))
1523 return PTR_ERR(opendata);
1524 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1525 err = nfs4_open_recover(opendata, state);
1526 nfs4_opendata_put(opendata);
1527 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1528}
1529
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001530static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1531{
1532 struct nfs4_opendata *data = calldata;
1533
1534 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001535 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001536 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001537 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001538 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001539 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001540 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001541}
1542
1543static void nfs4_open_confirm_release(void *calldata)
1544{
1545 struct nfs4_opendata *data = calldata;
1546 struct nfs4_state *state = NULL;
1547
1548 /* If this request hasn't been cancelled, do nothing */
1549 if (data->cancelled == 0)
1550 goto out_free;
1551 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001552 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001553 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001554 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001555 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001556 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001557out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001558 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001559}
1560
1561static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001562 .rpc_call_done = nfs4_open_confirm_done,
1563 .rpc_release = nfs4_open_confirm_release,
1564};
1565
1566/*
1567 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1568 */
1569static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1570{
1571 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1572 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001573 struct rpc_message msg = {
1574 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1575 .rpc_argp = &data->c_arg,
1576 .rpc_resp = &data->c_res,
1577 .rpc_cred = data->owner->so_cred,
1578 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001579 struct rpc_task_setup task_setup_data = {
1580 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001581 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001582 .callback_ops = &nfs4_open_confirm_ops,
1583 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001584 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001585 .flags = RPC_TASK_ASYNC,
1586 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 int status;
1588
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001589 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001590 data->rpc_done = 0;
1591 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001592 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001593 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001594 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001595 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001596 status = nfs4_wait_for_completion_rpc_task(task);
1597 if (status != 0) {
1598 data->cancelled = 1;
1599 smp_wmb();
1600 } else
1601 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001602 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 return status;
1604}
1605
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001606static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001608 struct nfs4_opendata *data = calldata;
1609 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001610 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001611
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001612 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001613 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001614 /*
1615 * Check if we still need to send an OPEN call, or if we can use
1616 * a delegation instead.
1617 */
1618 if (data->state != NULL) {
1619 struct nfs_delegation *delegation;
1620
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001621 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001622 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001623 rcu_read_lock();
1624 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001625 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
Trond Myklebustcd4c9be2013-04-23 14:46:25 -04001626 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
Trond Myklebust652f89f2011-12-09 19:05:58 -05001627 can_open_delegated(delegation, data->o_arg.fmode))
1628 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001629 rcu_read_unlock();
1630 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001631 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04001632 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust8188df12013-04-23 14:31:19 -04001633 switch (data->o_arg.claim) {
1634 case NFS4_OPEN_CLAIM_PREVIOUS:
1635 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1636 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001637 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04001638 case NFS4_OPEN_CLAIM_FH:
1639 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001640 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1641 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001642 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001643 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001644 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001645 &data->o_res.seq_res,
1646 task) != 0)
1647 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04001648
1649 /* Set the create mode (note dependency on the session type) */
1650 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1651 if (data->o_arg.open_flags & O_EXCL) {
1652 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1653 if (nfs4_has_persistent_session(clp))
1654 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1655 else if (clp->cl_mvops->minor_version > 0)
1656 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1657 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04001658 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001659unlock_no_action:
1660 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001661out_no_action:
1662 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001663out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001664 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001665}
1666
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001667static void nfs4_open_done(struct rpc_task *task, void *calldata)
1668{
1669 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001671 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001672
Trond Myklebust14516c32010-07-31 14:29:06 -04001673 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1674 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001675
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001676 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001677 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1678 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001679 case S_IFREG:
1680 break;
1681 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001682 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001683 break;
1684 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001685 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001686 break;
1687 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001688 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001689 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001690 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001691 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001692 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1693 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001694 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001695 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001696}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001697
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001698static void nfs4_open_release(void *calldata)
1699{
1700 struct nfs4_opendata *data = calldata;
1701 struct nfs4_state *state = NULL;
1702
1703 /* If this request hasn't been cancelled, do nothing */
1704 if (data->cancelled == 0)
1705 goto out_free;
1706 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001707 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001708 goto out_free;
1709 /* In case we need an open_confirm, no cleanup! */
1710 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1711 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001712 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001713 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001714 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001715out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001716 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001717}
1718
1719static const struct rpc_call_ops nfs4_open_ops = {
1720 .rpc_call_prepare = nfs4_open_prepare,
1721 .rpc_call_done = nfs4_open_done,
1722 .rpc_release = nfs4_open_release,
1723};
1724
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001725static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001726{
1727 struct inode *dir = data->dir->d_inode;
1728 struct nfs_server *server = NFS_SERVER(dir);
1729 struct nfs_openargs *o_arg = &data->o_arg;
1730 struct nfs_openres *o_res = &data->o_res;
1731 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001732 struct rpc_message msg = {
1733 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1734 .rpc_argp = o_arg,
1735 .rpc_resp = o_res,
1736 .rpc_cred = data->owner->so_cred,
1737 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001738 struct rpc_task_setup task_setup_data = {
1739 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001740 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001741 .callback_ops = &nfs4_open_ops,
1742 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001743 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001744 .flags = RPC_TASK_ASYNC,
1745 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001746 int status;
1747
Trond Myklebust9d12b212012-01-17 22:04:25 -05001748 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001749 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001750 data->rpc_done = 0;
1751 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001752 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001753 data->is_recover = 0;
1754 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001755 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001756 data->is_recover = 1;
1757 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04001758 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001759 if (IS_ERR(task))
1760 return PTR_ERR(task);
1761 status = nfs4_wait_for_completion_rpc_task(task);
1762 if (status != 0) {
1763 data->cancelled = 1;
1764 smp_wmb();
1765 } else
1766 status = data->rpc_status;
1767 rpc_put_task(task);
1768
1769 return status;
1770}
1771
1772static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1773{
1774 struct inode *dir = data->dir->d_inode;
1775 struct nfs_openres *o_res = &data->o_res;
1776 int status;
1777
1778 status = nfs4_run_open_task(data, 1);
1779 if (status != 0 || !data->rpc_done)
1780 return status;
1781
Trond Myklebust6926afd2012-01-07 13:22:46 -05001782 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1783
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001784 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1785 status = _nfs4_proc_open_confirm(data);
1786 if (status != 0)
1787 return status;
1788 }
1789
1790 return status;
1791}
1792
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001793static int nfs4_opendata_access(struct rpc_cred *cred,
1794 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001795 struct nfs4_state *state, fmode_t fmode,
1796 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001797{
1798 struct nfs_access_entry cache;
1799 u32 mask;
1800
1801 /* access call failed or for some reason the server doesn't
1802 * support any access modes -- defer access call until later */
1803 if (opendata->o_res.access_supported == 0)
1804 return 0;
1805
1806 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001807 /* don't check MAY_WRITE - a newly created file may not have
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001808 * write mode bits, but POSIX allows the creating process to write.
1809 * use openflags to check for exec, because fmode won't
1810 * always have FMODE_EXEC set when file open for exec. */
1811 if (openflags & __FMODE_EXEC) {
1812 /* ONLY check for exec rights */
1813 mask = MAY_EXEC;
1814 } else if (fmode & FMODE_READ)
1815 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001816
1817 cache.cred = cred;
1818 cache.jiffies = jiffies;
1819 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1820 nfs_access_add_cache(state->inode, &cache);
1821
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001822 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001823 return 0;
1824
1825 /* even though OPEN succeeded, access is denied. Close the file */
1826 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001827 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001828}
1829
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001830/*
1831 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1832 */
1833static int _nfs4_proc_open(struct nfs4_opendata *data)
1834{
1835 struct inode *dir = data->dir->d_inode;
1836 struct nfs_server *server = NFS_SERVER(dir);
1837 struct nfs_openargs *o_arg = &data->o_arg;
1838 struct nfs_openres *o_res = &data->o_res;
1839 int status;
1840
1841 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001842 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001843 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001844 if (status != 0) {
1845 if (status == -NFS4ERR_BADNAME &&
1846 !(o_arg->open_flags & O_CREAT))
1847 return -ENOENT;
1848 return status;
1849 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001850
Trond Myklebust6926afd2012-01-07 13:22:46 -05001851 nfs_fattr_map_and_free_names(server, &data->f_attr);
1852
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001853 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001854 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001855 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1856 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001858 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001860 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 }
1862 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
David Quigley1775fd32013-05-22 12:50:42 -04001863 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001864 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865}
1866
Andy Adamsond83217c2011-03-01 01:34:17 +00001867static int nfs4_recover_expired_lease(struct nfs_server *server)
1868{
1869 return nfs4_client_recover_expired_lease(server->nfs_client);
1870}
1871
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872/*
1873 * OPEN_EXPIRED:
1874 * reclaim state on the server after a network partition.
1875 * Assumes caller holds the appropriate lock
1876 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001877static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001879 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001880 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001882 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001883 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001884 if (IS_ERR(opendata))
1885 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001886 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001887 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001888 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001889 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001890 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891}
1892
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001893static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001894{
Trond Myklebust539cd032007-06-05 11:46:42 -04001895 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001896 struct nfs4_exception exception = { };
1897 int err;
1898
1899 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001900 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001901 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001902 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1903 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001904 switch (err) {
1905 default:
1906 goto out;
1907 case -NFS4ERR_GRACE:
1908 case -NFS4ERR_DELAY:
1909 nfs4_handle_exception(server, err, &exception);
1910 err = 0;
1911 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001912 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001913out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001914 return err;
1915}
1916
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1918{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001920 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
Trond Myklebust864472e2006-01-03 09:55:15 +01001922 ctx = nfs4_state_find_open_context(state);
1923 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001924 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001925 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001926 put_nfs_open_context(ctx);
1927 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928}
1929
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001930#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001931static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001932{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001933 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001934 nfs4_stateid *stateid = &state->stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001935 struct nfs_delegation *delegation;
1936 struct rpc_cred *cred = NULL;
1937 int status = -NFS4ERR_BAD_STATEID;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001938
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001939 /* If a state reset has been done, test_stateid is unneeded */
1940 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1941 return;
1942
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001943 /* Get the delegation credential for use by test/free_stateid */
1944 rcu_read_lock();
1945 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1946 if (delegation != NULL &&
1947 nfs4_stateid_match(&delegation->stateid, stateid)) {
1948 cred = get_rpccred(delegation->cred);
1949 rcu_read_unlock();
1950 status = nfs41_test_stateid(server, stateid, cred);
1951 } else
1952 rcu_read_unlock();
1953
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001954 if (status != NFS_OK) {
1955 /* Free the stateid unless the server explicitly
1956 * informs us the stateid is unrecognized. */
1957 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001958 nfs41_free_stateid(server, stateid, cred);
Bryan Schumaker69388672012-09-26 15:25:52 -04001959 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001960
Bryan Schumaker69388672012-09-26 15:25:52 -04001961 write_seqlock(&state->seqlock);
1962 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1963 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001964 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1965 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001966
1967 if (cred != NULL)
1968 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001969}
1970
1971/**
1972 * nfs41_check_open_stateid - possibly free an open stateid
1973 *
1974 * @state: NFSv4 state for an inode
1975 *
1976 * Returns NFS_OK if recovery for this stateid is now finished.
1977 * Otherwise a negative NFS4ERR value is returned.
1978 */
1979static int nfs41_check_open_stateid(struct nfs4_state *state)
1980{
1981 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001982 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001983 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001984 int status;
1985
1986 /* If a state reset has been done, test_stateid is unneeded */
1987 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1988 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1989 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1990 return -NFS4ERR_BAD_STATEID;
1991
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001992 status = nfs41_test_stateid(server, stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001993 if (status != NFS_OK) {
1994 /* Free the stateid unless the server explicitly
1995 * informs us the stateid is unrecognized. */
1996 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001997 nfs41_free_stateid(server, stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001998
1999 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2000 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2001 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002002 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002003 }
2004 return status;
2005}
2006
2007static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2008{
Chuck Levereb64cf92012-07-11 16:30:05 -04002009 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002010
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002011 nfs41_clear_delegation_stateid(state);
2012 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002013 if (status != NFS_OK)
2014 status = nfs4_open_expired(sp, state);
2015 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002016}
2017#endif
2018
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002020 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2021 * fields corresponding to attributes that were used to store the verifier.
2022 * Make sure we clobber those fields in the later setattr call
2023 */
2024static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2025{
2026 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2027 !(sattr->ia_valid & ATTR_ATIME_SET))
2028 sattr->ia_valid |= ATTR_ATIME;
2029
2030 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2031 !(sattr->ia_valid & ATTR_MTIME_SET))
2032 sattr->ia_valid |= ATTR_MTIME;
2033}
2034
Trond Myklebustc21443c2013-02-07 14:26:21 -05002035static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2036 fmode_t fmode,
2037 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002038 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002039{
2040 struct nfs4_state_owner *sp = opendata->owner;
2041 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002042 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002043 struct nfs4_state *state;
2044 unsigned int seq;
2045 int ret;
2046
2047 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2048
2049 ret = _nfs4_proc_open(opendata);
2050 if (ret != 0)
2051 goto out;
2052
2053 state = nfs4_opendata_to_nfs4_state(opendata);
2054 ret = PTR_ERR(state);
2055 if (IS_ERR(state))
2056 goto out;
2057 if (server->caps & NFS_CAP_POSIX_LOCK)
2058 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2059
Trond Myklebust275bb302013-05-29 13:11:28 -04002060 dentry = opendata->dentry;
2061 if (dentry->d_inode == NULL) {
2062 /* FIXME: Is this d_drop() ever needed? */
2063 d_drop(dentry);
2064 dentry = d_add_unique(dentry, igrab(state->inode));
2065 if (dentry == NULL) {
2066 dentry = opendata->dentry;
2067 } else if (dentry != ctx->dentry) {
2068 dput(ctx->dentry);
2069 ctx->dentry = dget(dentry);
2070 }
2071 nfs_set_verifier(dentry,
2072 nfs_save_change_attribute(opendata->dir->d_inode));
2073 }
2074
Trond Myklebustc21443c2013-02-07 14:26:21 -05002075 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2076 if (ret != 0)
2077 goto out;
2078
Trond Myklebust3efb9722013-05-29 13:17:04 -04002079 ctx->state = state;
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002080 if (dentry->d_inode == state->inode) {
2081 nfs_inode_attach_open_context(ctx);
2082 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2083 nfs4_schedule_stateid_recovery(server, state);
2084 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002085out:
2086 return ret;
2087}
2088
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002089/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002090 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 */
Andy Adamson82be4172012-05-23 05:02:35 -04002092static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002093 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002094 int flags,
2095 struct iattr *sattr,
Trond Myklebust959d9212013-06-28 16:29:51 -04002096 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097{
2098 struct nfs4_state_owner *sp;
2099 struct nfs4_state *state = NULL;
2100 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002101 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002102 struct dentry *dentry = ctx->dentry;
2103 struct rpc_cred *cred = ctx->cred;
2104 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2105 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002106 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002107 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002108 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
2110 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002112 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2113 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2115 goto out_err;
2116 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002117 status = nfs4_recover_expired_lease(server);
2118 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002119 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04002120 if (dentry->d_inode != NULL)
2121 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002122 status = -ENOMEM;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002123 if (dentry->d_inode)
2124 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002125 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002126 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002127 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002128 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
David Quigley14c43f72013-05-22 12:50:43 -04002130 if (label) {
2131 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2132 if (IS_ERR(olabel)) {
2133 status = PTR_ERR(olabel);
2134 goto err_opendata_put;
2135 }
2136 }
2137
Andy Adamson82be4172012-05-23 05:02:35 -04002138 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2139 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2140 if (!opendata->f_attr.mdsthreshold)
David Quigley14c43f72013-05-22 12:50:43 -04002141 goto err_free_label;
Trond Myklebust1549210f2012-06-05 09:16:47 -04002142 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002143 }
Al Viro82a2c1b2011-06-22 18:30:55 -04002144 if (dentry->d_inode != NULL)
2145 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002146
Trond Myklebust3efb9722013-05-29 13:17:04 -04002147 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002148 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002149 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002150 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002151
Trond Myklebust549b19c2013-04-16 18:42:34 -04002152 if ((opendata->o_arg.open_flags & O_EXCL) &&
2153 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002154 nfs4_exclusive_attrset(opendata, sattr);
2155
2156 nfs_fattr_init(opendata->o_res.f_attr);
2157 status = nfs4_do_setattr(state->inode, cred,
2158 opendata->o_res.f_attr, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002159 state, label, olabel);
2160 if (status == 0) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002161 nfs_setattr_update_inode(state->inode, sattr);
David Quigley1775fd32013-05-22 12:50:42 -04002162 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
David Quigleyaa9c2662013-05-22 12:50:44 -04002163 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
David Quigley1775fd32013-05-22 12:50:42 -04002164 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002165 }
Andy Adamson82be4172012-05-23 05:02:35 -04002166
2167 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2168 *ctx_th = opendata->f_attr.mdsthreshold;
2169 else
2170 kfree(opendata->f_attr.mdsthreshold);
2171 opendata->f_attr.mdsthreshold = NULL;
2172
David Quigley14c43f72013-05-22 12:50:43 -04002173 nfs4_label_free(olabel);
2174
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002175 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002178err_free_label:
2179 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002180err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002181 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002182 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002183err_put_state_owner:
2184 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 return status;
2187}
2188
2189
Andy Adamson82be4172012-05-23 05:02:35 -04002190static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002191 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002192 int flags,
2193 struct iattr *sattr,
Trond Myklebust959d9212013-06-28 16:29:51 -04002194 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002196 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 struct nfs4_exception exception = { };
2198 struct nfs4_state *res;
2199 int status;
2200
2201 do {
Trond Myklebust959d9212013-06-28 16:29:51 -04002202 status = _nfs4_do_open(dir, ctx, flags, sattr, label);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002203 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002204 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 if (status == 0)
2206 break;
2207 /* NOTE: BAD_SEQID means the server and client disagree about the
2208 * book-keeping w.r.t. state-changing operations
2209 * (OPEN/CLOSE/LOCK/LOCKU...)
2210 * It is actually a sign of a bug on the client or on the server.
2211 *
2212 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002213 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 * have unhashed the old state_owner for us, and that we can
2215 * therefore safely retry using a new one. We should still warn
2216 * the user though...
2217 */
2218 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002219 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002220 " returned a bad sequence-id error!\n",
2221 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 exception.retry = 1;
2223 continue;
2224 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002225 /*
2226 * BAD_STATEID on OPEN means that the server cancelled our
2227 * state before it received the OPEN_CONFIRM.
2228 * Recover by retrying the request as per the discussion
2229 * on Page 181 of RFC3530.
2230 */
2231 if (status == -NFS4ERR_BAD_STATEID) {
2232 exception.retry = 1;
2233 continue;
2234 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002235 if (status == -EAGAIN) {
2236 /* We must have found a delegation */
2237 exception.retry = 1;
2238 continue;
2239 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002240 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2241 continue;
2242 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 status, &exception));
2244 } while (exception.retry);
2245 return res;
2246}
2247
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002248static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2249 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002250 struct nfs4_state *state, struct nfs4_label *ilabel,
2251 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002253 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002255 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 .iap = sattr,
2257 .server = server,
2258 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002259 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 };
2261 struct nfs_setattrres res = {
2262 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002263 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 .server = server,
2265 };
2266 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002267 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2268 .rpc_argp = &arg,
2269 .rpc_resp = &res,
2270 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002272 unsigned long timestamp = jiffies;
Trond Myklebustee3ae842013-04-29 10:35:36 -04002273 fmode_t fmode;
2274 bool truncate;
Trond Myklebust65e43082005-08-16 11:49:44 -04002275 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
David Quigleyaa9c2662013-05-22 12:50:44 -04002277 arg.bitmask = nfs4_bitmask(server, ilabel);
2278 if (ilabel)
2279 arg.bitmask = nfs4_bitmask(server, olabel);
2280
Trond Myklebust0e574af2005-10-27 22:12:38 -04002281 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
Trond Myklebustee3ae842013-04-29 10:35:36 -04002283 /* Servers should only apply open mode checks for file size changes */
2284 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2285 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2286
2287 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2288 /* Use that stateid */
2289 } else if (truncate && state != NULL && nfs4_valid_open_stateid(state)) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002290 struct nfs_lockowner lockowner = {
2291 .l_owner = current->files,
2292 .l_pid = current->tgid,
2293 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002294 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002295 &lockowner);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002296 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002297 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298
Bryan Schumaker7c513052011-03-24 17:12:24 +00002299 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002300 if (status == 0 && state != NULL)
2301 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002302 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303}
2304
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002305static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2306 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002307 struct nfs4_state *state, struct nfs4_label *ilabel,
2308 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002310 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002311 struct nfs4_exception exception = {
2312 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002313 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002314 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 int err;
2316 do {
David Quigley1775fd32013-05-22 12:50:42 -04002317 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
Trond Myklebustc1578b72013-08-12 16:58:42 -04002318 trace_nfs4_setattr(inode, err);
Trond Myklebust451146b2012-04-18 16:29:11 -04002319 switch (err) {
2320 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002321 if (!(sattr->ia_valid & ATTR_SIZE)) {
2322 pr_warn_once("NFSv4: server %s is incorrectly "
2323 "applying open mode checks to "
2324 "a SETATTR that is not "
2325 "changing file size.\n",
2326 server->nfs_client->cl_hostname);
2327 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002328 if (state && !(state->state & FMODE_WRITE)) {
2329 err = -EBADF;
2330 if (sattr->ia_valid & ATTR_OPEN)
2331 err = -EACCES;
2332 goto out;
2333 }
2334 }
2335 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002337out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 return err;
2339}
2340
2341struct nfs4_closedata {
2342 struct inode *inode;
2343 struct nfs4_state *state;
2344 struct nfs_closeargs arg;
2345 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002346 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002347 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002348 bool roc;
2349 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350};
2351
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002352static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002353{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002354 struct nfs4_closedata *calldata = data;
2355 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002356 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002357
Fred Isamanf7e89172011-01-06 11:36:32 +00002358 if (calldata->roc)
2359 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002360 nfs4_put_open_state(calldata->state);
2361 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002362 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002363 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002364 kfree(calldata);
2365}
2366
Trond Myklebust88069f72009-12-08 08:33:16 -05002367static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2368 fmode_t fmode)
2369{
2370 spin_lock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002371 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002372 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
2373 case FMODE_WRITE:
2374 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2375 break;
2376 case FMODE_READ:
2377 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2378 break;
2379 case 0:
2380 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2381 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2382 clear_bit(NFS_OPEN_STATE, &state->flags);
2383 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002384 spin_unlock(&state->owner->so_lock);
2385}
2386
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002387static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002389 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 struct nfs_server *server = NFS_SERVER(calldata->inode);
2392
Chuck Levera3ca5652012-03-01 17:00:40 -05002393 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002394 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2395 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002396 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 /* hmm. we are done with the inode, and in the process of freeing
2398 * the state_owner. we keep this around to process errors
2399 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 switch (task->tk_status) {
2401 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002402 if (calldata->roc)
2403 pnfs_roc_set_barrier(state->inode,
2404 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002405 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002406 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002407 nfs4_close_clear_stateid_flags(state,
2408 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 break;
2410 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002411 case -NFS4ERR_OLD_STATEID:
2412 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002414 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002415 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002417 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2418 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002420 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002421 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002422 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423}
2424
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002425static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002427 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002428 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002429 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002430 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002431
Chuck Levera3ca5652012-03-01 17:00:40 -05002432 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002433 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002434 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002435
Trond Myklebust88069f72009-12-08 08:33:16 -05002436 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2437 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002438 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002439 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002440 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002441 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002442 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2443 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2444 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002445 }
2446 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002447 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2448 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2449 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002450 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002451 }
Trond Myklebust5d422302013-03-14 16:57:48 -04002452 if (!nfs4_valid_open_stateid(state))
2453 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002454 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002455
2456 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002457 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002458 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002459 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002460
Fred Isamanf7e89172011-01-06 11:36:32 +00002461 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002462 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002463 if (calldata->roc &&
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002464 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2465 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002466 goto out_wait;
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002467 }
Fred Isamanf7e89172011-01-06 11:36:32 +00002468 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002469
Trond Myklebust516a6af2005-10-27 22:12:41 -04002470 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002471 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002472 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002473 &calldata->arg.seq_args,
2474 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002475 task) != 0)
2476 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002477 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002478 return;
2479out_no_action:
2480 task->tk_action = NULL;
2481out_wait:
2482 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483}
2484
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002485static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002486 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002487 .rpc_call_done = nfs4_close_done,
2488 .rpc_release = nfs4_free_closedata,
2489};
2490
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491/*
2492 * It is possible for data to be read/written from a mem-mapped file
2493 * after the sys_close call (which hits the vfs layer as a flush).
2494 * This means that we can't safely call nfsv4 close on a file until
2495 * the inode is cleared. This in turn means that we are not good
2496 * NFSv4 citizens - we do not indicate to the server to update the file's
2497 * share state even when we are done with one of the three share
2498 * stateid's in the inode.
2499 *
2500 * NOTE: Caller must be holding the sp->so_owner semaphore!
2501 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002502int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002504 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002506 struct nfs4_state_owner *sp = state->owner;
2507 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002508 struct rpc_message msg = {
2509 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2510 .rpc_cred = state->owner->so_cred,
2511 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002512 struct rpc_task_setup task_setup_data = {
2513 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002514 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002515 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002516 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002517 .flags = RPC_TASK_ASYNC,
2518 };
Trond Myklebust95121352005-10-18 14:20:12 -07002519 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002521 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002523 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002524 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002525 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002527 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002528 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002530 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002531 if (calldata->arg.seqid == NULL)
2532 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002533 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002534 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002535 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002536 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002537 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002538 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002539 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002540
Trond Myklebust1174dd12010-12-21 10:52:24 -05002541 msg.rpc_argp = &calldata->arg;
2542 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002543 task_setup_data.callback_data = calldata;
2544 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002545 if (IS_ERR(task))
2546 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002547 status = 0;
2548 if (wait)
2549 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002550 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002551 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002552out_free_calldata:
2553 kfree(calldata);
2554out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002555 nfs4_put_open_state(state);
2556 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002557 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558}
2559
Trond Myklebust2b484292010-09-17 10:56:51 -04002560static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002561nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04002564 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2565
2566 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567
Trond Myklebust565277f2007-10-15 18:17:53 -04002568 /* Protect against concurrent sillydeletes */
Trond Myklebust959d9212013-06-28 16:29:51 -04002569 state = nfs4_do_open(dir, ctx, open_flags, attr, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04002570
2571 nfs4_label_release_security(label);
2572
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002573 if (IS_ERR(state))
2574 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04002575 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576}
2577
Trond Myklebust1185a552009-12-03 15:54:02 -05002578static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002579{
2580 if (ctx->state == NULL)
2581 return;
2582 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002583 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002584 else
Al Viro643168c2011-06-22 18:20:23 -04002585 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002586}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
2588static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2589{
Benny Halevy43652ad2009-04-01 09:21:54 -04002590 struct nfs4_server_caps_arg args = {
2591 .fhandle = fhandle,
2592 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 struct nfs4_server_caps_res res = {};
2594 struct rpc_message msg = {
2595 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002596 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 .rpc_resp = &res,
2598 };
2599 int status;
2600
Bryan Schumaker7c513052011-03-24 17:12:24 +00002601 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 if (status == 0) {
2603 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002604 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2605 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2606 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2607 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2608 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2610 server->caps |= NFS_CAP_ACLS;
2611 if (res.has_links != 0)
2612 server->caps |= NFS_CAP_HARDLINKS;
2613 if (res.has_symlinks != 0)
2614 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002615 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2616 server->caps |= NFS_CAP_FILEID;
2617 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2618 server->caps |= NFS_CAP_MODE;
2619 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2620 server->caps |= NFS_CAP_NLINK;
2621 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2622 server->caps |= NFS_CAP_OWNER;
2623 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2624 server->caps |= NFS_CAP_OWNER_GROUP;
2625 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2626 server->caps |= NFS_CAP_ATIME;
2627 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2628 server->caps |= NFS_CAP_CTIME;
2629 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2630 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04002631#ifdef CONFIG_NFS_V4_SECURITY_LABEL
2632 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
2633 server->caps |= NFS_CAP_SECURITY_LABEL;
2634#endif
2635 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
2636 sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002637
David Quigleyaa9c2662013-05-22 12:50:44 -04002638 if (server->caps & NFS_CAP_SECURITY_LABEL) {
2639 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2640 res.attr_bitmask[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2641 }
Trond Myklebusta65318b2009-03-11 14:10:28 -04002642 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2643 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2644 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002646 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002648
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 return status;
2650}
2651
Trond Myklebust55a97592006-06-09 09:34:19 -04002652int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653{
2654 struct nfs4_exception exception = { };
2655 int err;
2656 do {
2657 err = nfs4_handle_exception(server,
2658 _nfs4_server_capabilities(server, fhandle),
2659 &exception);
2660 } while (exception.retry);
2661 return err;
2662}
2663
2664static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2665 struct nfs_fsinfo *info)
2666{
David Quigleyaa9c2662013-05-22 12:50:44 -04002667 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04002669 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 };
2671 struct nfs4_lookup_res res = {
2672 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002673 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 .fh = fhandle,
2675 };
2676 struct rpc_message msg = {
2677 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2678 .rpc_argp = &args,
2679 .rpc_resp = &res,
2680 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002681
David Quigleyaa9c2662013-05-22 12:50:44 -04002682 bitmask[0] = nfs4_fattr_bitmap[0];
2683 bitmask[1] = nfs4_fattr_bitmap[1];
2684 /*
2685 * Process the label in the upcoming getfattr
2686 */
2687 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
2688
Trond Myklebust0e574af2005-10-27 22:12:38 -04002689 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002690 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691}
2692
2693static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2694 struct nfs_fsinfo *info)
2695{
2696 struct nfs4_exception exception = { };
2697 int err;
2698 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002699 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04002700 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002701 switch (err) {
2702 case 0:
2703 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002704 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002705 default:
2706 err = nfs4_handle_exception(server, err, &exception);
2707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002709out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 return err;
2711}
2712
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002713static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2714 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2715{
2716 struct rpc_auth *auth;
2717 int ret;
2718
2719 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002720 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04002721 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002722 goto out;
2723 }
2724 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002725out:
2726 return ret;
2727}
2728
Chuck Lever9a744ba2013-03-16 15:56:02 -04002729/*
2730 * Retry pseudoroot lookup with various security flavors. We do this when:
2731 *
2732 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2733 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2734 *
2735 * Returns zero on success, or a negative NFS4ERR value, or a
2736 * negative errno value.
2737 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002738static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002739 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740{
Chuck Lever9a744ba2013-03-16 15:56:02 -04002741 /* Per 3530bis 15.33.5 */
2742 static const rpc_authflavor_t flav_array[] = {
2743 RPC_AUTH_GSS_KRB5P,
2744 RPC_AUTH_GSS_KRB5I,
2745 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04002746 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04002747 RPC_AUTH_NULL,
2748 };
2749 int status = -EPERM;
2750 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751
Chuck Lever9a744ba2013-03-16 15:56:02 -04002752 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002753 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002754 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002755 continue;
2756 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002757 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04002758
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002759 /*
2760 * -EACCESS could mean that the user doesn't have correct permissions
2761 * to access the mount. It could also mean that we tried to mount
2762 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2763 * existing mount programs don't handle -EACCES very well so it should
2764 * be mapped to -EPERM instead.
2765 */
2766 if (status == -EACCES)
2767 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002768 return status;
2769}
2770
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002771static int nfs4_do_find_root_sec(struct nfs_server *server,
2772 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
2773{
2774 int mv = server->nfs_client->cl_minorversion;
2775 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
2776}
2777
2778/**
2779 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
2780 * @server: initialized nfs_server handle
2781 * @fhandle: we fill in the pseudo-fs root file handle
2782 * @info: we fill in an FSINFO struct
2783 *
2784 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002785 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002786int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2787 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002788{
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002789 int status;
2790
2791 status = nfs4_lookup_root(server, fhandle, info);
2792 if ((status == -NFS4ERR_WRONGSEC) &&
2793 !(server->flags & NFS_MOUNT_SECFLAVOUR))
2794 status = nfs4_do_find_root_sec(server, fhandle, info);
2795
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 if (status == 0)
2797 status = nfs4_server_capabilities(server, fhandle);
2798 if (status == 0)
2799 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002800
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002801 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802}
2803
Bryan Schumakerbae36242012-05-10 15:07:31 -04002804static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2805 struct nfs_fsinfo *info)
2806{
2807 int error;
2808 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04002809 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04002810
2811 error = nfs4_server_capabilities(server, mntfh);
2812 if (error < 0) {
2813 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2814 return error;
2815 }
2816
David Quigley14c43f72013-05-22 12:50:43 -04002817 label = nfs4_label_alloc(server, GFP_KERNEL);
2818 if (IS_ERR(label))
2819 return PTR_ERR(label);
2820
David Quigley1775fd32013-05-22 12:50:42 -04002821 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04002822 if (error < 0) {
2823 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04002824 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04002825 }
2826
2827 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2828 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2829 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2830
David Quigley14c43f72013-05-22 12:50:43 -04002831err_free_label:
2832 nfs4_label_free(label);
2833
Bryan Schumakerbae36242012-05-10 15:07:31 -04002834 return error;
2835}
2836
Manoj Naik6b97fd32006-06-09 09:34:29 -04002837/*
2838 * Get locations and (maybe) other attributes of a referral.
2839 * Note that we'll actually follow the referral later when
2840 * we detect fsid mismatch in inode revalidation
2841 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002842static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2843 const struct qstr *name, struct nfs_fattr *fattr,
2844 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002845{
2846 int status = -ENOMEM;
2847 struct page *page = NULL;
2848 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002849
2850 page = alloc_page(GFP_KERNEL);
2851 if (page == NULL)
2852 goto out;
2853 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2854 if (locations == NULL)
2855 goto out;
2856
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002857 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002858 if (status != 0)
2859 goto out;
2860 /* Make sure server returned a different fsid for the referral */
2861 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002862 dprintk("%s: server did not return a different fsid for"
2863 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002864 status = -EIO;
2865 goto out;
2866 }
Andy Adamson533eb462011-06-13 18:25:56 -04002867 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2868 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002869
Andy Adamson533eb462011-06-13 18:25:56 -04002870 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002871 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002872 memset(fhandle, 0, sizeof(struct nfs_fh));
2873out:
2874 if (page)
2875 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002876 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002877 return status;
2878}
2879
David Quigley1775fd32013-05-22 12:50:42 -04002880static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
2881 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882{
2883 struct nfs4_getattr_arg args = {
2884 .fh = fhandle,
2885 .bitmask = server->attr_bitmask,
2886 };
2887 struct nfs4_getattr_res res = {
2888 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002889 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 .server = server,
2891 };
2892 struct rpc_message msg = {
2893 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2894 .rpc_argp = &args,
2895 .rpc_resp = &res,
2896 };
David Quigleyaa9c2662013-05-22 12:50:44 -04002897
2898 args.bitmask = nfs4_bitmask(server, label);
2899
Trond Myklebust0e574af2005-10-27 22:12:38 -04002900 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002901 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902}
2903
David Quigley1775fd32013-05-22 12:50:42 -04002904static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
2905 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906{
2907 struct nfs4_exception exception = { };
2908 int err;
2909 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04002910 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
2911 trace_nfs4_getattr(server, fhandle, fattr, err);
2912 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 &exception);
2914 } while (exception.retry);
2915 return err;
2916}
2917
2918/*
2919 * The file is not closed if it is opened due to the a request to change
2920 * the size of the file. The open call will not be needed once the
2921 * VFS layer lookup-intents are implemented.
2922 *
2923 * Close is called when the inode is destroyed.
2924 * If we haven't opened the file for O_WRONLY, we
2925 * need to in the size_change case to obtain a stateid.
2926 *
2927 * Got race?
2928 * Because OPEN is always done by name in nfsv4, it is
2929 * possible that we opened a different file by the same
2930 * name. We can recognize this race condition, but we
2931 * can't do anything about it besides returning an error.
2932 *
2933 * This will be fixed with VFS changes (lookup-intent).
2934 */
2935static int
2936nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2937 struct iattr *sattr)
2938{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002939 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002940 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002941 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04002942 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 int status;
2944
Benny Halevy8a1636c2010-07-14 15:43:57 -04002945 if (pnfs_ld_layoutret_on_setattr(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04002946 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04002947
Trond Myklebust0e574af2005-10-27 22:12:38 -04002948 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949
Andy Adamson26699402012-05-30 16:12:24 -04002950 /* Deal with open(O_TRUNC) */
2951 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03002952 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04002953
2954 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03002955 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04002956 return 0;
2957
Trond Myklebustd5308382005-11-04 15:33:38 -05002958 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002959 if (sattr->ia_valid & ATTR_FILE) {
2960 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002961
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002962 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002963 if (ctx) {
2964 cred = ctx->cred;
2965 state = ctx->state;
2966 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002967 }
2968
David Quigley14c43f72013-05-22 12:50:43 -04002969 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
2970 if (IS_ERR(label))
2971 return PTR_ERR(label);
2972
2973 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04002974 if (status == 0) {
Trond Myklebust65e43082005-08-16 11:49:44 -04002975 nfs_setattr_update_inode(inode, sattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04002976 nfs_setsecurity(inode, fattr, label);
2977 }
David Quigley14c43f72013-05-22 12:50:43 -04002978 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 return status;
2980}
2981
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002982static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2983 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04002984 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04002985{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002986 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002987 int status;
2988 struct nfs4_lookup_arg args = {
2989 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002990 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002991 .name = name,
2992 };
2993 struct nfs4_lookup_res res = {
2994 .server = server,
2995 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04002996 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04002997 .fh = fhandle,
2998 };
2999 struct rpc_message msg = {
3000 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3001 .rpc_argp = &args,
3002 .rpc_resp = &res,
3003 };
3004
David Quigleyaa9c2662013-05-22 12:50:44 -04003005 args.bitmask = nfs4_bitmask(server, label);
3006
David Howells2b3de442006-08-22 20:06:09 -04003007 nfs_fattr_init(fattr);
3008
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003010 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 dprintk("NFS reply lookup: %d\n", status);
3012 return status;
3013}
3014
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003015static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003016{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003017 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003018 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003019 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3020 fattr->nlink = 2;
3021}
3022
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003023static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3024 struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003025 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003026{
3027 struct nfs4_exception exception = { };
3028 struct rpc_clnt *client = *clnt;
3029 int err;
3030 do {
David Quigley1775fd32013-05-22 12:50:42 -04003031 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003032 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003033 switch (err) {
3034 case -NFS4ERR_BADNAME:
3035 err = -ENOENT;
3036 goto out;
3037 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003038 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003039 goto out;
3040 case -NFS4ERR_WRONGSEC:
3041 err = -EPERM;
3042 if (client != *clnt)
3043 goto out;
3044
3045 client = nfs4_create_sec_client(client, dir, name);
3046 if (IS_ERR(client))
3047 return PTR_ERR(client);
3048
3049 exception.retry = 1;
3050 break;
3051 default:
3052 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3053 }
3054 } while (exception.retry);
3055
3056out:
3057 if (err == 0)
3058 *clnt = client;
3059 else if (client != *clnt)
3060 rpc_shutdown_client(client);
3061
3062 return err;
3063}
3064
Bryan Schumaker80a16b22012-04-27 13:27:46 -04003065static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003066 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3067 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003069 int status;
3070 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003071
David Quigley1775fd32013-05-22 12:50:42 -04003072 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003073 if (client != NFS_CLIENT(dir)) {
3074 rpc_shutdown_client(client);
3075 nfs_fixup_secinfo_attributes(fattr);
3076 }
3077 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078}
3079
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003080struct rpc_clnt *
3081nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3082 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3083{
3084 int status;
3085 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
3086
David Quigley1775fd32013-05-22 12:50:42 -04003087 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003088 if (status < 0) {
3089 rpc_shutdown_client(client);
3090 return ERR_PTR(status);
3091 }
3092 return client;
3093}
3094
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3096{
Trond Myklebust76b32992007-08-10 17:45:11 -04003097 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 struct nfs4_accessargs args = {
3099 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003100 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003102 struct nfs4_accessres res = {
3103 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003104 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 struct rpc_message msg = {
3106 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3107 .rpc_argp = &args,
3108 .rpc_resp = &res,
3109 .rpc_cred = entry->cred,
3110 };
3111 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003112 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113
3114 /*
3115 * Determine which access bits we want to ask for...
3116 */
3117 if (mode & MAY_READ)
3118 args.access |= NFS4_ACCESS_READ;
3119 if (S_ISDIR(inode->i_mode)) {
3120 if (mode & MAY_WRITE)
3121 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3122 if (mode & MAY_EXEC)
3123 args.access |= NFS4_ACCESS_LOOKUP;
3124 } else {
3125 if (mode & MAY_WRITE)
3126 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3127 if (mode & MAY_EXEC)
3128 args.access |= NFS4_ACCESS_EXECUTE;
3129 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003130
3131 res.fattr = nfs_alloc_fattr();
3132 if (res.fattr == NULL)
3133 return -ENOMEM;
3134
Bryan Schumaker7c513052011-03-24 17:12:24 +00003135 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003137 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003138 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003140 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 return status;
3142}
3143
3144static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3145{
3146 struct nfs4_exception exception = { };
3147 int err;
3148 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003149 err = _nfs4_proc_access(inode, entry);
3150 trace_nfs4_access(inode, err);
3151 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 &exception);
3153 } while (exception.retry);
3154 return err;
3155}
3156
3157/*
3158 * TODO: For the time being, we don't try to get any attributes
3159 * along with any of the zero-copy operations READ, READDIR,
3160 * READLINK, WRITE.
3161 *
3162 * In the case of the first three, we want to put the GETATTR
3163 * after the read-type operation -- this is because it is hard
3164 * to predict the length of a GETATTR response in v4, and thus
3165 * align the READ data correctly. This means that the GETATTR
3166 * may end up partially falling into the page cache, and we should
3167 * shift it into the 'tail' of the xdr_buf before processing.
3168 * To do this efficiently, we need to know the total length
3169 * of data received, which doesn't seem to be available outside
3170 * of the RPC layer.
3171 *
3172 * In the case of WRITE, we also want to put the GETATTR after
3173 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003174 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 *
3176 * Both of these changes to the XDR layer would in fact be quite
3177 * minor, but I decided to leave them for a subsequent patch.
3178 */
3179static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3180 unsigned int pgbase, unsigned int pglen)
3181{
3182 struct nfs4_readlink args = {
3183 .fh = NFS_FH(inode),
3184 .pgbase = pgbase,
3185 .pglen = pglen,
3186 .pages = &page,
3187 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003188 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 struct rpc_message msg = {
3190 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3191 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003192 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 };
3194
Bryan Schumaker7c513052011-03-24 17:12:24 +00003195 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 -07003196}
3197
3198static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3199 unsigned int pgbase, unsigned int pglen)
3200{
3201 struct nfs4_exception exception = { };
3202 int err;
3203 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003204 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3205 trace_nfs4_readlink(inode, err);
3206 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 &exception);
3208 } while (exception.retry);
3209 return err;
3210}
3211
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003213 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215static int
3216nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003217 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218{
David Quigleyaa9c2662013-05-22 12:50:44 -04003219 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003220 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 int status = 0;
3223
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003224 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3225 if (IS_ERR(ctx))
3226 return PTR_ERR(ctx);
3227
David Quigleyaa9c2662013-05-22 12:50:44 -04003228 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3229
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003230 sattr->ia_mode &= ~current_umask();
Trond Myklebust959d9212013-06-28 16:29:51 -04003231 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 if (IS_ERR(state)) {
3233 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003234 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003237 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003238 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 return status;
3240}
3241
3242static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3243{
Trond Myklebust16e42952005-10-27 22:12:44 -04003244 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003245 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003247 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003249 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003250 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003251 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003253 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3254 .rpc_argp = &args,
3255 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003257 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003258
Bryan Schumaker7c513052011-03-24 17:12:24 +00003259 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003260 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003261 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 return status;
3263}
3264
3265static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3266{
3267 struct nfs4_exception exception = { };
3268 int err;
3269 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003270 err = _nfs4_proc_remove(dir, name);
3271 trace_nfs4_remove(dir, name, err);
3272 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 &exception);
3274 } while (exception.retry);
3275 return err;
3276}
3277
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003278static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003280 struct nfs_server *server = NFS_SERVER(dir);
3281 struct nfs_removeargs *args = msg->rpc_argp;
3282 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003284 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003286 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003287
3288 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289}
3290
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003291static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3292{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003293 nfs4_setup_sequence(NFS_SERVER(data->dir),
3294 &data->args.seq_args,
3295 &data->res.seq_res,
3296 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297}
3298
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003299static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003301 struct nfs_unlinkdata *data = task->tk_calldata;
3302 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003303
Trond Myklebust14516c32010-07-31 14:29:06 -04003304 if (!nfs4_sequence_done(task, &res->seq_res))
3305 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003306 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003307 return 0;
3308 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003309 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310}
3311
Jeff Laytond3d41522010-09-17 17:31:57 -04003312static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3313{
3314 struct nfs_server *server = NFS_SERVER(dir);
3315 struct nfs_renameargs *arg = msg->rpc_argp;
3316 struct nfs_renameres *res = msg->rpc_resp;
3317
3318 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003319 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003320 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003321}
3322
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003323static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3324{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003325 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3326 &data->args.seq_args,
3327 &data->res.seq_res,
3328 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003329}
3330
3331static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3332 struct inode *new_dir)
3333{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003334 struct nfs_renamedata *data = task->tk_calldata;
3335 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04003336
3337 if (!nfs4_sequence_done(task, &res->seq_res))
3338 return 0;
3339 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3340 return 0;
3341
3342 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003343 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003344 return 1;
3345}
3346
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3348 struct inode *new_dir, struct qstr *new_name)
3349{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003350 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003351 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 .old_dir = NFS_FH(old_dir),
3353 .new_dir = NFS_FH(new_dir),
3354 .old_name = old_name,
3355 .new_name = new_name,
3356 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003357 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003358 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003359 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 struct rpc_message msg = {
3361 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3362 .rpc_argp = &arg,
3363 .rpc_resp = &res,
3364 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003365 int status = -ENOMEM;
David Quigley14c43f72013-05-22 12:50:43 -04003366
Bryan Schumaker7c513052011-03-24 17:12:24 +00003367 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 if (!status) {
3369 update_changeattr(old_dir, &res.old_cinfo);
3370 update_changeattr(new_dir, &res.new_cinfo);
3371 }
3372 return status;
3373}
3374
3375static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3376 struct inode *new_dir, struct qstr *new_name)
3377{
3378 struct nfs4_exception exception = { };
3379 int err;
3380 do {
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003381 err = _nfs4_proc_rename(old_dir, old_name,
3382 new_dir, new_name);
3383 trace_nfs4_rename(old_dir, old_name, new_dir, new_name, err);
3384 err = nfs4_handle_exception(NFS_SERVER(old_dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 &exception);
3386 } while (exception.retry);
3387 return err;
3388}
3389
3390static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3391{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003392 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 struct nfs4_link_arg arg = {
3394 .fh = NFS_FH(inode),
3395 .dir_fh = NFS_FH(dir),
3396 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003397 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003399 struct nfs4_link_res res = {
3400 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003401 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003402 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 struct rpc_message msg = {
3404 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3405 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003406 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003408 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409
Trond Myklebust136f2622010-04-16 16:22:49 -04003410 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003411 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003412 goto out;
3413
David Quigley14c43f72013-05-22 12:50:43 -04003414 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3415 if (IS_ERR(res.label)) {
3416 status = PTR_ERR(res.label);
3417 goto out;
3418 }
David Quigleyaa9c2662013-05-22 12:50:44 -04003419 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04003420
Bryan Schumaker7c513052011-03-24 17:12:24 +00003421 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003422 if (!status) {
3423 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04003424 status = nfs_post_op_update_inode(inode, res.fattr);
3425 if (!status)
3426 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003427 }
David Quigley14c43f72013-05-22 12:50:43 -04003428
3429
3430 nfs4_label_free(res.label);
3431
Trond Myklebust136f2622010-04-16 16:22:49 -04003432out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003433 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 return status;
3435}
3436
3437static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3438{
3439 struct nfs4_exception exception = { };
3440 int err;
3441 do {
3442 err = nfs4_handle_exception(NFS_SERVER(inode),
3443 _nfs4_proc_link(inode, dir, name),
3444 &exception);
3445 } while (exception.retry);
3446 return err;
3447}
3448
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003449struct nfs4_createdata {
3450 struct rpc_message msg;
3451 struct nfs4_create_arg arg;
3452 struct nfs4_create_res res;
3453 struct nfs_fh fh;
3454 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003455 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003456};
3457
3458static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3459 struct qstr *name, struct iattr *sattr, u32 ftype)
3460{
3461 struct nfs4_createdata *data;
3462
3463 data = kzalloc(sizeof(*data), GFP_KERNEL);
3464 if (data != NULL) {
3465 struct nfs_server *server = NFS_SERVER(dir);
3466
David Quigley14c43f72013-05-22 12:50:43 -04003467 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3468 if (IS_ERR(data->label))
3469 goto out_free;
3470
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003471 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3472 data->msg.rpc_argp = &data->arg;
3473 data->msg.rpc_resp = &data->res;
3474 data->arg.dir_fh = NFS_FH(dir);
3475 data->arg.server = server;
3476 data->arg.name = name;
3477 data->arg.attrs = sattr;
3478 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04003479 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003480 data->res.server = server;
3481 data->res.fh = &data->fh;
3482 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003483 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003484 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003485 }
3486 return data;
David Quigley14c43f72013-05-22 12:50:43 -04003487out_free:
3488 kfree(data);
3489 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003490}
3491
3492static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3493{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003494 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003495 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003496 if (status == 0) {
3497 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04003498 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003499 }
3500 return status;
3501}
3502
3503static void nfs4_free_createdata(struct nfs4_createdata *data)
3504{
David Quigley14c43f72013-05-22 12:50:43 -04003505 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003506 kfree(data);
3507}
3508
Chuck Lever4f390c12006-08-22 20:06:22 -04003509static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003510 struct page *page, unsigned int len, struct iattr *sattr,
3511 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003513 struct nfs4_createdata *data;
3514 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515
Chuck Lever94a6d752006-08-22 20:06:23 -04003516 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003517 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003518
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003519 status = -ENOMEM;
3520 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3521 if (data == NULL)
3522 goto out;
3523
3524 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3525 data->arg.u.symlink.pages = &page;
3526 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04003527 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003529 status = nfs4_do_create(dir, dentry, data);
3530
3531 nfs4_free_createdata(data);
3532out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 return status;
3534}
3535
Chuck Lever4f390c12006-08-22 20:06:22 -04003536static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003537 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538{
3539 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003540 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04003542
3543 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3544
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003546 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3547 trace_nfs4_symlink(dir, &dentry->d_name, err);
3548 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 &exception);
3550 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003551
3552 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 return err;
3554}
3555
3556static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003557 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003559 struct nfs4_createdata *data;
3560 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003562 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3563 if (data == NULL)
3564 goto out;
3565
David Quigley1775fd32013-05-22 12:50:42 -04003566 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003567 status = nfs4_do_create(dir, dentry, data);
3568
3569 nfs4_free_createdata(data);
3570out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 return status;
3572}
3573
3574static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3575 struct iattr *sattr)
3576{
3577 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003578 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003580
David Quigleyaa9c2662013-05-22 12:50:44 -04003581 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3582
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003583 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003585 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3586 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3587 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 &exception);
3589 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003590 nfs4_label_release_security(label);
3591
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 return err;
3593}
3594
3595static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003596 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597{
3598 struct inode *dir = dentry->d_inode;
3599 struct nfs4_readdir_arg args = {
3600 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003601 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 .pgbase = 0,
3603 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003604 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003605 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 };
3607 struct nfs4_readdir_res res;
3608 struct rpc_message msg = {
3609 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3610 .rpc_argp = &args,
3611 .rpc_resp = &res,
3612 .rpc_cred = cred,
3613 };
3614 int status;
3615
Harvey Harrison3110ff82008-05-02 13:42:44 -07003616 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003617 dentry->d_parent->d_name.name,
3618 dentry->d_name.name,
3619 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003620 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003622 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 -05003623 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003624 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003625 status += args.pgbase;
3626 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003627
3628 nfs_invalidate_atime(dir);
3629
Harvey Harrison3110ff82008-05-02 13:42:44 -07003630 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 return status;
3632}
3633
3634static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003635 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636{
3637 struct nfs4_exception exception = { };
3638 int err;
3639 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003640 err = _nfs4_proc_readdir(dentry, cred, cookie,
3641 pages, count, plus);
3642 trace_nfs4_readdir(dentry->d_inode, err);
3643 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 &exception);
3645 } while (exception.retry);
3646 return err;
3647}
3648
3649static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04003650 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003652 struct nfs4_createdata *data;
3653 int mode = sattr->ia_mode;
3654 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003656 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3657 if (data == NULL)
3658 goto out;
3659
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003661 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003663 data->arg.ftype = NF4BLK;
3664 data->arg.u.device.specdata1 = MAJOR(rdev);
3665 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 }
3667 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003668 data->arg.ftype = NF4CHR;
3669 data->arg.u.device.specdata1 = MAJOR(rdev);
3670 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003671 } else if (!S_ISSOCK(mode)) {
3672 status = -EINVAL;
3673 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 }
David Quigley1775fd32013-05-22 12:50:42 -04003675
David Quigleyaa9c2662013-05-22 12:50:44 -04003676 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003677 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003678out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003679 nfs4_free_createdata(data);
3680out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 return status;
3682}
3683
3684static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3685 struct iattr *sattr, dev_t rdev)
3686{
3687 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003688 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003690
David Quigleyaa9c2662013-05-22 12:50:44 -04003691 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3692
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003693 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003695 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
3696 trace_nfs4_mknod(dir, &dentry->d_name, err);
3697 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 &exception);
3699 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003700
3701 nfs4_label_release_security(label);
3702
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 return err;
3704}
3705
3706static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3707 struct nfs_fsstat *fsstat)
3708{
3709 struct nfs4_statfs_arg args = {
3710 .fh = fhandle,
3711 .bitmask = server->attr_bitmask,
3712 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003713 struct nfs4_statfs_res res = {
3714 .fsstat = fsstat,
3715 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 struct rpc_message msg = {
3717 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3718 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003719 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 };
3721
Trond Myklebust0e574af2005-10-27 22:12:38 -04003722 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003723 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724}
3725
3726static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3727{
3728 struct nfs4_exception exception = { };
3729 int err;
3730 do {
3731 err = nfs4_handle_exception(server,
3732 _nfs4_proc_statfs(server, fhandle, fsstat),
3733 &exception);
3734 } while (exception.retry);
3735 return err;
3736}
3737
3738static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3739 struct nfs_fsinfo *fsinfo)
3740{
3741 struct nfs4_fsinfo_arg args = {
3742 .fh = fhandle,
3743 .bitmask = server->attr_bitmask,
3744 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003745 struct nfs4_fsinfo_res res = {
3746 .fsinfo = fsinfo,
3747 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 struct rpc_message msg = {
3749 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3750 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003751 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 };
3753
Bryan Schumaker7c513052011-03-24 17:12:24 +00003754 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755}
3756
3757static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3758{
3759 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04003760 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 int err;
3762
3763 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04003764 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003765 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04003766 if (err == 0) {
3767 struct nfs_client *clp = server->nfs_client;
3768
3769 spin_lock(&clp->cl_lock);
3770 clp->cl_lease_time = fsinfo->lease_time * HZ;
3771 clp->cl_last_renewal = now;
3772 spin_unlock(&clp->cl_lock);
3773 break;
3774 }
3775 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 } while (exception.retry);
3777 return err;
3778}
3779
3780static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3781{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003782 int error;
3783
Trond Myklebust0e574af2005-10-27 22:12:38 -04003784 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003785 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003786 if (error == 0) {
3787 /* block layout checks this! */
3788 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003789 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003790 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003791
3792 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793}
3794
3795static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3796 struct nfs_pathconf *pathconf)
3797{
3798 struct nfs4_pathconf_arg args = {
3799 .fh = fhandle,
3800 .bitmask = server->attr_bitmask,
3801 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003802 struct nfs4_pathconf_res res = {
3803 .pathconf = pathconf,
3804 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 struct rpc_message msg = {
3806 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3807 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003808 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 };
3810
3811 /* None of the pathconf attributes are mandatory to implement */
3812 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3813 memset(pathconf, 0, sizeof(*pathconf));
3814 return 0;
3815 }
3816
Trond Myklebust0e574af2005-10-27 22:12:38 -04003817 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003818 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819}
3820
3821static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3822 struct nfs_pathconf *pathconf)
3823{
3824 struct nfs4_exception exception = { };
3825 int err;
3826
3827 do {
3828 err = nfs4_handle_exception(server,
3829 _nfs4_proc_pathconf(server, fhandle, pathconf),
3830 &exception);
3831 } while (exception.retry);
3832 return err;
3833}
3834
Trond Myklebust5521abf2013-03-16 20:54:34 -04003835int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04003836 const struct nfs_open_context *ctx,
3837 const struct nfs_lock_context *l_ctx,
3838 fmode_t fmode)
3839{
3840 const struct nfs_lockowner *lockowner = NULL;
3841
3842 if (l_ctx != NULL)
3843 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003844 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04003845}
3846EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
3847
Trond Myklebust5521abf2013-03-16 20:54:34 -04003848static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
3849 const struct nfs_open_context *ctx,
3850 const struct nfs_lock_context *l_ctx,
3851 fmode_t fmode)
3852{
3853 nfs4_stateid current_stateid;
3854
3855 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode))
3856 return false;
3857 return nfs4_stateid_match(stateid, &current_stateid);
3858}
3859
3860static bool nfs4_error_stateid_expired(int err)
3861{
3862 switch (err) {
3863 case -NFS4ERR_DELEG_REVOKED:
3864 case -NFS4ERR_ADMIN_REVOKED:
3865 case -NFS4ERR_BAD_STATEID:
3866 case -NFS4ERR_STALE_STATEID:
3867 case -NFS4ERR_OLD_STATEID:
3868 case -NFS4ERR_OPENMODE:
3869 case -NFS4ERR_EXPIRED:
3870 return true;
3871 }
3872 return false;
3873}
3874
Benny Halevyd20581a2011-05-22 19:52:03 +03003875void __nfs4_read_done_cb(struct nfs_read_data *data)
3876{
Fred Isamancd841602012-04-20 14:47:44 -04003877 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003878}
3879
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003880static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881{
Fred Isamancd841602012-04-20 14:47:44 -04003882 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883
Trond Myklebustcc668ab2013-08-14 15:31:28 -04003884 trace_nfs4_read(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003885 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003886 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003887 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003889
Benny Halevyd20581a2011-05-22 19:52:03 +03003890 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003892 renew_lease(server, data->timestamp);
3893 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894}
3895
Trond Myklebust5521abf2013-03-16 20:54:34 -04003896static bool nfs4_read_stateid_changed(struct rpc_task *task,
3897 struct nfs_readargs *args)
3898{
3899
3900 if (!nfs4_error_stateid_expired(task->tk_status) ||
3901 nfs4_stateid_is_current(&args->stateid,
3902 args->context,
3903 args->lock_context,
3904 FMODE_READ))
3905 return false;
3906 rpc_restart_call_prepare(task);
3907 return true;
3908}
3909
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003910static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3911{
3912
3913 dprintk("--> %s\n", __func__);
3914
3915 if (!nfs4_sequence_done(task, &data->res.seq_res))
3916 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003917 if (nfs4_read_stateid_changed(task, &data->args))
3918 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003919 return data->read_done_cb ? data->read_done_cb(task, data) :
3920 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003921}
3922
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003923static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003926 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003927 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003928 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929}
3930
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003931static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3932{
Trond Myklebust9b206142013-03-17 15:52:00 -04003933 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003934 &data->args.seq_args,
3935 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04003936 task))
3937 return;
3938 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3939 data->args.lock_context, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940}
3941
Fred Isamanb029bc92011-03-03 15:13:42 +00003942static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943{
Fred Isamancd841602012-04-20 14:47:44 -04003944 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945
Trond Myklebustcc668ab2013-08-14 15:31:28 -04003946 trace_nfs4_write(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003947 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003948 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003949 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003951 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003953 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003954 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003955 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956}
3957
Trond Myklebust5521abf2013-03-16 20:54:34 -04003958static bool nfs4_write_stateid_changed(struct rpc_task *task,
3959 struct nfs_writeargs *args)
3960{
3961
3962 if (!nfs4_error_stateid_expired(task->tk_status) ||
3963 nfs4_stateid_is_current(&args->stateid,
3964 args->context,
3965 args->lock_context,
3966 FMODE_WRITE))
3967 return false;
3968 rpc_restart_call_prepare(task);
3969 return true;
3970}
3971
Fred Isamanb029bc92011-03-03 15:13:42 +00003972static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3973{
3974 if (!nfs4_sequence_done(task, &data->res.seq_res))
3975 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003976 if (nfs4_write_stateid_changed(task, &data->args))
3977 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003978 return data->write_done_cb ? data->write_done_cb(task, data) :
3979 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003980}
3981
Trond Myklebust5a37f852012-04-28 14:55:16 -04003982static
3983bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003984{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003985 const struct nfs_pgio_header *hdr = data->header;
3986
3987 /* Don't request attributes for pNFS or O_DIRECT writes */
3988 if (data->ds_clp != NULL || hdr->dreq != NULL)
3989 return false;
3990 /* Otherwise, request attributes if and only if we don't hold
3991 * a delegation
3992 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003993 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003994}
Fred Isamana69aef12011-03-03 15:13:47 +00003995
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003996static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997{
Fred Isamancd841602012-04-20 14:47:44 -04003998 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003999
Trond Myklebust5a37f852012-04-28 14:55:16 -04004000 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00004001 data->args.bitmask = NULL;
4002 data->res.fattr = NULL;
4003 } else
4004 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004005
Fred Isamanb029bc92011-03-03 15:13:42 +00004006 if (!data->write_done_cb)
4007 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004008 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 data->timestamp = jiffies;
4010
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004011 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05004012 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013}
4014
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04004015static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
4016{
Trond Myklebust9b206142013-03-17 15:52:00 -04004017 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004018 &data->args.seq_args,
4019 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004020 task))
4021 return;
4022 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4023 data->args.lock_context, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024}
4025
Fred Isaman0b7c0152012-04-20 14:47:39 -04004026static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4027{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004028 nfs4_setup_sequence(NFS_SERVER(data->inode),
4029 &data->args.seq_args,
4030 &data->res.seq_res,
4031 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032}
4033
Fred Isaman0b7c0152012-04-20 14:47:39 -04004034static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004037
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004038 trace_nfs4_commit(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004039 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004040 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004041 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004043 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044}
4045
Fred Isaman0b7c0152012-04-20 14:47:39 -04004046static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004047{
4048 if (!nfs4_sequence_done(task, &data->res.seq_res))
4049 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004050 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004051}
4052
Fred Isaman0b7c0152012-04-20 14:47:39 -04004053static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004055 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004056
Fred Isaman0b7c0152012-04-20 14:47:39 -04004057 if (data->commit_done_cb == NULL)
4058 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004059 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004060 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05004061 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062}
4063
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004064struct nfs4_renewdata {
4065 struct nfs_client *client;
4066 unsigned long timestamp;
4067};
4068
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069/*
4070 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4071 * standalone procedure for queueing an asynchronous RENEW.
4072 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004073static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004074{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004075 struct nfs4_renewdata *data = calldata;
4076 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004077
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004078 if (atomic_read(&clp->cl_count) > 1)
4079 nfs4_schedule_state_renewal(clp);
4080 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004081 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004082}
4083
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004084static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004086 struct nfs4_renewdata *data = calldata;
4087 struct nfs_client *clp = data->client;
4088 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004090 trace_nfs4_renew_async(clp, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04004092 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004093 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4094 return;
4095 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004096 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004097 return;
4098 }
4099 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004101 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102}
4103
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004104static const struct rpc_call_ops nfs4_renew_ops = {
4105 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004106 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004107};
4108
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004109static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110{
4111 struct rpc_message msg = {
4112 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4113 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004114 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004116 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004118 if (renew_flags == 0)
4119 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004120 if (!atomic_inc_not_zero(&clp->cl_count))
4121 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004122 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004123 if (data == NULL)
4124 return -ENOMEM;
4125 data->client = clp;
4126 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004127 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004128 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129}
4130
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004131static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132{
4133 struct rpc_message msg = {
4134 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4135 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004136 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 };
4138 unsigned long now = jiffies;
4139 int status;
4140
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004141 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 if (status < 0)
4143 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004144 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 return 0;
4146}
4147
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004148static inline int nfs4_server_supports_acls(struct nfs_server *server)
4149{
4150 return (server->caps & NFS_CAP_ACLS)
4151 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
4152 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
4153}
4154
Trond Myklebust21f498c2012-08-24 10:59:25 -04004155/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4156 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004157 * the stack.
4158 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004159#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004160
Neil Hormane9e3d722011-03-04 19:26:03 -05004161static int buf_to_pages_noslab(const void *buf, size_t buflen,
4162 struct page **pages, unsigned int *pgbase)
4163{
4164 struct page *newpage, **spages;
4165 int rc = 0;
4166 size_t len;
4167 spages = pages;
4168
4169 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004170 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004171 newpage = alloc_page(GFP_KERNEL);
4172
4173 if (newpage == NULL)
4174 goto unwind;
4175 memcpy(page_address(newpage), buf, len);
4176 buf += len;
4177 buflen -= len;
4178 *pages++ = newpage;
4179 rc++;
4180 } while (buflen != 0);
4181
4182 return rc;
4183
4184unwind:
4185 for(; rc > 0; rc--)
4186 __free_page(spages[rc-1]);
4187 return -ENOMEM;
4188}
4189
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004190struct nfs4_cached_acl {
4191 int cached;
4192 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004193 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004194};
4195
4196static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004197{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004198 struct nfs_inode *nfsi = NFS_I(inode);
4199
4200 spin_lock(&inode->i_lock);
4201 kfree(nfsi->nfs4_acl);
4202 nfsi->nfs4_acl = acl;
4203 spin_unlock(&inode->i_lock);
4204}
4205
4206static void nfs4_zap_acl_attr(struct inode *inode)
4207{
4208 nfs4_set_cached_acl(inode, NULL);
4209}
4210
4211static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4212{
4213 struct nfs_inode *nfsi = NFS_I(inode);
4214 struct nfs4_cached_acl *acl;
4215 int ret = -ENOENT;
4216
4217 spin_lock(&inode->i_lock);
4218 acl = nfsi->nfs4_acl;
4219 if (acl == NULL)
4220 goto out;
4221 if (buf == NULL) /* user is just asking for length */
4222 goto out_len;
4223 if (acl->cached == 0)
4224 goto out;
4225 ret = -ERANGE; /* see getxattr(2) man page */
4226 if (acl->len > buflen)
4227 goto out;
4228 memcpy(buf, acl->data, acl->len);
4229out_len:
4230 ret = acl->len;
4231out:
4232 spin_unlock(&inode->i_lock);
4233 return ret;
4234}
4235
Sachin Prabhu5794d212012-04-17 14:36:40 +01004236static 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 +00004237{
4238 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004239 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004240
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004241 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004242 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004243 if (acl == NULL)
4244 goto out;
4245 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004246 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004247 } else {
4248 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4249 if (acl == NULL)
4250 goto out;
4251 acl->cached = 0;
4252 }
4253 acl->len = acl_len;
4254out:
4255 nfs4_set_cached_acl(inode, acl);
4256}
4257
Andy Adamsonbf118a32011-12-07 11:55:27 -05004258/*
4259 * The getxattr API returns the required buffer length when called with a
4260 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4261 * the required buf. On a NULL buf, we send a page of data to the server
4262 * guessing that the ACL request can be serviced by a page. If so, we cache
4263 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4264 * the cache. If not so, we throw away the page, and cache the required
4265 * length. The next getxattr call will then produce another round trip to
4266 * the server, this time with the input buf of the required size.
4267 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004268static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004269{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004270 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004271 struct nfs_getaclargs args = {
4272 .fh = NFS_FH(inode),
4273 .acl_pages = pages,
4274 .acl_len = buflen,
4275 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004276 struct nfs_getaclres res = {
4277 .acl_len = buflen,
4278 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004279 struct rpc_message msg = {
4280 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4281 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004282 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004283 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004284 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4285 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004286
Andy Adamsonbf118a32011-12-07 11:55:27 -05004287 /* As long as we're doing a round trip to the server anyway,
4288 * let's be prepared for a page of acl data. */
4289 if (npages == 0)
4290 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004291 if (npages > ARRAY_SIZE(pages))
4292 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004293
Andy Adamsonbf118a32011-12-07 11:55:27 -05004294 for (i = 0; i < npages; i++) {
4295 pages[i] = alloc_page(GFP_KERNEL);
4296 if (!pages[i])
4297 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004298 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004299
4300 /* for decoding across pages */
4301 res.acl_scratch = alloc_page(GFP_KERNEL);
4302 if (!res.acl_scratch)
4303 goto out_free;
4304
Andy Adamsonbf118a32011-12-07 11:55:27 -05004305 args.acl_len = npages * PAGE_SIZE;
4306 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004307
Peng Taode040be2012-01-10 22:42:47 +08004308 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004309 __func__, buf, buflen, npages, args.acl_len);
4310 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4311 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004312 if (ret)
4313 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004314
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004315 /* Handle the case where the passed-in buffer is too short */
4316 if (res.acl_flags & NFS4_ACL_TRUNC) {
4317 /* Did the user only issue a request for the acl length? */
4318 if (buf == NULL)
4319 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004320 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004321 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004322 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004323 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004324 if (buf) {
4325 if (res.acl_len > buflen) {
4326 ret = -ERANGE;
4327 goto out_free;
4328 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004329 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004330 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004331out_ok:
4332 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004333out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004334 for (i = 0; i < npages; i++)
4335 if (pages[i])
4336 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004337 if (res.acl_scratch)
4338 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004339 return ret;
4340}
4341
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004342static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4343{
4344 struct nfs4_exception exception = { };
4345 ssize_t ret;
4346 do {
4347 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04004348 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004349 if (ret >= 0)
4350 break;
4351 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4352 } while (exception.retry);
4353 return ret;
4354}
4355
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004356static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4357{
4358 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004359 int ret;
4360
4361 if (!nfs4_server_supports_acls(server))
4362 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004363 ret = nfs_revalidate_inode(server, inode);
4364 if (ret < 0)
4365 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004366 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4367 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004368 ret = nfs4_read_cached_acl(inode, buf, buflen);
4369 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004370 /* -ENOENT is returned if there is no ACL or if there is an ACL
4371 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004372 return ret;
4373 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004374}
4375
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004376static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004377{
4378 struct nfs_server *server = NFS_SERVER(inode);
4379 struct page *pages[NFS4ACL_MAXPAGES];
4380 struct nfs_setaclargs arg = {
4381 .fh = NFS_FH(inode),
4382 .acl_pages = pages,
4383 .acl_len = buflen,
4384 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004385 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004386 struct rpc_message msg = {
4387 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4388 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004389 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004390 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004391 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004392 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004393
4394 if (!nfs4_server_supports_acls(server))
4395 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004396 if (npages > ARRAY_SIZE(pages))
4397 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004398 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4399 if (i < 0)
4400 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004401 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004402 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004403
4404 /*
4405 * Free each page after tx, so the only ref left is
4406 * held by the network stack
4407 */
4408 for (; i > 0; i--)
4409 put_page(pages[i-1]);
4410
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004411 /*
4412 * Acl update can result in inode attribute update.
4413 * so mark the attribute cache invalid.
4414 */
4415 spin_lock(&inode->i_lock);
4416 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4417 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004418 nfs_access_zap_cache(inode);
4419 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004420 return ret;
4421}
4422
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004423static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4424{
4425 struct nfs4_exception exception = { };
4426 int err;
4427 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004428 err = __nfs4_proc_set_acl(inode, buf, buflen);
4429 trace_nfs4_set_acl(inode, err);
4430 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004431 &exception);
4432 } while (exception.retry);
4433 return err;
4434}
4435
David Quigleyaa9c2662013-05-22 12:50:44 -04004436#ifdef CONFIG_NFS_V4_SECURITY_LABEL
4437static int _nfs4_get_security_label(struct inode *inode, void *buf,
4438 size_t buflen)
4439{
4440 struct nfs_server *server = NFS_SERVER(inode);
4441 struct nfs_fattr fattr;
4442 struct nfs4_label label = {0, 0, buflen, buf};
4443
4444 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4445 struct nfs4_getattr_arg args = {
4446 .fh = NFS_FH(inode),
4447 .bitmask = bitmask,
4448 };
4449 struct nfs4_getattr_res res = {
4450 .fattr = &fattr,
4451 .label = &label,
4452 .server = server,
4453 };
4454 struct rpc_message msg = {
4455 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4456 .rpc_argp = &args,
4457 .rpc_resp = &res,
4458 };
4459 int ret;
4460
4461 nfs_fattr_init(&fattr);
4462
4463 ret = rpc_call_sync(server->client, &msg, 0);
4464 if (ret)
4465 return ret;
4466 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4467 return -ENOENT;
4468 if (buflen < label.len)
4469 return -ERANGE;
4470 return 0;
4471}
4472
4473static int nfs4_get_security_label(struct inode *inode, void *buf,
4474 size_t buflen)
4475{
4476 struct nfs4_exception exception = { };
4477 int err;
4478
4479 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4480 return -EOPNOTSUPP;
4481
4482 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004483 err = _nfs4_get_security_label(inode, buf, buflen);
4484 trace_nfs4_get_security_label(inode, err);
4485 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004486 &exception);
4487 } while (exception.retry);
4488 return err;
4489}
4490
4491static int _nfs4_do_set_security_label(struct inode *inode,
4492 struct nfs4_label *ilabel,
4493 struct nfs_fattr *fattr,
4494 struct nfs4_label *olabel)
4495{
4496
4497 struct iattr sattr = {0};
4498 struct nfs_server *server = NFS_SERVER(inode);
4499 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4500 struct nfs_setattrargs args = {
4501 .fh = NFS_FH(inode),
4502 .iap = &sattr,
4503 .server = server,
4504 .bitmask = bitmask,
4505 .label = ilabel,
4506 };
4507 struct nfs_setattrres res = {
4508 .fattr = fattr,
4509 .label = olabel,
4510 .server = server,
4511 };
4512 struct rpc_message msg = {
4513 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
4514 .rpc_argp = &args,
4515 .rpc_resp = &res,
4516 };
4517 int status;
4518
4519 nfs4_stateid_copy(&args.stateid, &zero_stateid);
4520
4521 status = rpc_call_sync(server->client, &msg, 0);
4522 if (status)
4523 dprintk("%s failed: %d\n", __func__, status);
4524
4525 return status;
4526}
4527
4528static int nfs4_do_set_security_label(struct inode *inode,
4529 struct nfs4_label *ilabel,
4530 struct nfs_fattr *fattr,
4531 struct nfs4_label *olabel)
4532{
4533 struct nfs4_exception exception = { };
4534 int err;
4535
4536 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004537 err = _nfs4_do_set_security_label(inode, ilabel,
4538 fattr, olabel);
4539 trace_nfs4_set_security_label(inode, err);
4540 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004541 &exception);
4542 } while (exception.retry);
4543 return err;
4544}
4545
4546static int
4547nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4548{
4549 struct nfs4_label ilabel, *olabel = NULL;
4550 struct nfs_fattr fattr;
4551 struct rpc_cred *cred;
4552 struct inode *inode = dentry->d_inode;
4553 int status;
4554
4555 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4556 return -EOPNOTSUPP;
4557
4558 nfs_fattr_init(&fattr);
4559
4560 ilabel.pi = 0;
4561 ilabel.lfs = 0;
4562 ilabel.label = (char *)buf;
4563 ilabel.len = buflen;
4564
4565 cred = rpc_lookup_cred();
4566 if (IS_ERR(cred))
4567 return PTR_ERR(cred);
4568
4569 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4570 if (IS_ERR(olabel)) {
4571 status = -PTR_ERR(olabel);
4572 goto out;
4573 }
4574
4575 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4576 if (status == 0)
4577 nfs_setsecurity(inode, &fattr, olabel);
4578
4579 nfs4_label_free(olabel);
4580out:
4581 put_rpccred(cred);
4582 return status;
4583}
4584#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4585
4586
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004588nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004590 struct nfs_client *clp = server->nfs_client;
4591
4592 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 return 0;
4594 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004595 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004596 case -NFS4ERR_ADMIN_REVOKED:
4597 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004598 if (state == NULL)
4599 break;
4600 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004601 case -NFS4ERR_OPENMODE:
4602 if (state == NULL)
4603 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004604 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4605 goto stateid_invalid;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004606 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004607 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004608 if (state != NULL) {
4609 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4610 goto stateid_invalid;
4611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004613 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004614 nfs4_schedule_lease_recovery(clp);
4615 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004616#if defined(CONFIG_NFS_V4_1)
4617 case -NFS4ERR_BADSESSION:
4618 case -NFS4ERR_BADSLOT:
4619 case -NFS4ERR_BAD_HIGH_SLOT:
4620 case -NFS4ERR_DEADSESSION:
4621 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4622 case -NFS4ERR_SEQ_FALSE_RETRY:
4623 case -NFS4ERR_SEQ_MISORDERED:
4624 dprintk("%s ERROR %d, Reset session\n", __func__,
4625 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004626 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004627 task->tk_status = 0;
4628 return -EAGAIN;
4629#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004631 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004632 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4634 task->tk_status = 0;
4635 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004636 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 case -NFS4ERR_OLD_STATEID:
4638 task->tk_status = 0;
4639 return -EAGAIN;
4640 }
4641 task->tk_status = nfs4_map_errors(task->tk_status);
4642 return 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004643stateid_invalid:
4644 task->tk_status = -EIO;
4645 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004646wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004647 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004648 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4649 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4650 task->tk_status = 0;
4651 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652}
4653
Chuck Leverf0920752012-05-21 22:45:41 -04004654static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4655 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004656{
4657 __be32 verf[2];
4658
Chuck Lever2c820d92012-05-21 22:45:33 -04004659 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4660 /* An impossible timestamp guarantees this value
4661 * will never match a generated boot time. */
4662 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04004663 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04004664 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004665 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04004666 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
4667 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04004668 }
Chuck Levercd937102012-03-02 17:14:31 -05004669 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4670}
4671
Chuck Levere984a552012-09-14 17:24:21 -04004672static unsigned int
4673nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4674 char *buf, size_t len)
4675{
4676 unsigned int result;
4677
4678 rcu_read_lock();
4679 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4680 clp->cl_ipaddr,
4681 rpc_peeraddr2str(clp->cl_rpcclient,
4682 RPC_DISPLAY_ADDR),
4683 rpc_peeraddr2str(clp->cl_rpcclient,
4684 RPC_DISPLAY_PROTO));
4685 rcu_read_unlock();
4686 return result;
4687}
4688
4689static unsigned int
4690nfs4_init_uniform_client_string(const struct nfs_client *clp,
4691 char *buf, size_t len)
4692{
Trond Myklebust55b59292013-07-24 16:36:35 -04004693 const char *nodename = clp->cl_rpcclient->cl_nodename;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004694
4695 if (nfs4_client_id_uniquifier[0] != '\0')
Trond Myklebust55b59292013-07-24 16:36:35 -04004696 return scnprintf(buf, len, "Linux NFSv%u.%u %s/%s",
4697 clp->rpc_ops->version,
4698 clp->cl_minorversion,
4699 nfs4_client_id_uniquifier,
4700 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004701 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4702 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004703 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004704}
4705
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004706/**
4707 * nfs4_proc_setclientid - Negotiate client ID
4708 * @clp: state data structure
4709 * @program: RPC program for NFSv4 callback service
4710 * @port: IP port number for NFS4 callback service
4711 * @cred: RPC credential to use for this call
4712 * @res: where to place the result
4713 *
4714 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4715 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004716int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4717 unsigned short port, struct rpc_cred *cred,
4718 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719{
4720 nfs4_verifier sc_verifier;
4721 struct nfs4_setclientid setclientid = {
4722 .sc_verifier = &sc_verifier,
4723 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004724 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 };
4726 struct rpc_message msg = {
4727 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4728 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004729 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004730 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004732 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733
Chuck Leverde734832012-07-11 16:30:50 -04004734 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004735 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004736 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4737 setclientid.sc_name_len =
4738 nfs4_init_uniform_client_string(clp,
4739 setclientid.sc_name,
4740 sizeof(setclientid.sc_name));
4741 else
4742 setclientid.sc_name_len =
4743 nfs4_init_nonuniform_client_string(clp,
4744 setclientid.sc_name,
4745 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004746 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004747 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004748 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Djalal Harounife2d5392013-06-18 17:58:12 +01004749 sizeof(setclientid.sc_netid), "%s",
Chuck Leverd4d3c502007-12-10 14:57:09 -05004750 rpc_peeraddr2str(clp->cl_rpcclient,
4751 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004752 rcu_read_unlock();
4753 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004754 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 clp->cl_ipaddr, port >> 8, port & 255);
4756
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004757 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4758 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4759 setclientid.sc_name_len, setclientid.sc_name);
4760 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004761 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004762 dprintk("NFS reply setclientid: %d\n", status);
4763 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764}
4765
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004766/**
4767 * nfs4_proc_setclientid_confirm - Confirm client ID
4768 * @clp: state data structure
4769 * @res: result of a previous SETCLIENTID
4770 * @cred: RPC credential to use for this call
4771 *
4772 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4773 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004774int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004775 struct nfs4_setclientid_res *arg,
4776 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 struct rpc_message msg = {
4779 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004780 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004781 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 int status;
4784
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004785 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4786 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4787 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004788 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004789 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004790 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 return status;
4792}
4793
Trond Myklebustfe650402006-01-03 09:55:18 +01004794struct nfs4_delegreturndata {
4795 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004796 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004797 struct nfs_fh fh;
4798 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004799 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004800 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004801 int rpc_status;
4802};
4803
Trond Myklebustfe650402006-01-03 09:55:18 +01004804static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4805{
4806 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004807
Trond Myklebust14516c32010-07-31 14:29:06 -04004808 if (!nfs4_sequence_done(task, &data->res.seq_res))
4809 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004810
Trond Myklebustca8acf82013-08-13 10:36:56 -04004811 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004812 switch (task->tk_status) {
4813 case -NFS4ERR_STALE_STATEID:
4814 case -NFS4ERR_EXPIRED:
4815 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004816 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004817 break;
4818 default:
4819 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4820 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004821 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004822 return;
4823 }
4824 }
4825 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004826}
4827
4828static void nfs4_delegreturn_release(void *calldata)
4829{
Trond Myklebustfe650402006-01-03 09:55:18 +01004830 kfree(calldata);
4831}
4832
Andy Adamson938e1012009-04-01 09:22:28 -04004833#if defined(CONFIG_NFS_V4_1)
4834static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4835{
4836 struct nfs4_delegreturndata *d_data;
4837
4838 d_data = (struct nfs4_delegreturndata *)data;
4839
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004840 nfs4_setup_sequence(d_data->res.server,
4841 &d_data->args.seq_args,
4842 &d_data->res.seq_res,
4843 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004844}
4845#endif /* CONFIG_NFS_V4_1 */
4846
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004847static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004848#if defined(CONFIG_NFS_V4_1)
4849 .rpc_call_prepare = nfs4_delegreturn_prepare,
4850#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004851 .rpc_call_done = nfs4_delegreturn_done,
4852 .rpc_release = nfs4_delegreturn_release,
4853};
4854
Trond Myklebuste6f81072008-01-24 18:14:34 -05004855static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004856{
4857 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004858 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004859 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004860 struct rpc_message msg = {
4861 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4862 .rpc_cred = cred,
4863 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004864 struct rpc_task_setup task_setup_data = {
4865 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004866 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004867 .callback_ops = &nfs4_delegreturn_ops,
4868 .flags = RPC_TASK_ASYNC,
4869 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004870 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004871
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004872 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004873 if (data == NULL)
4874 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004875 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004876 data->args.fhandle = &data->fh;
4877 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004878 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004879 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004880 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004881 data->res.fattr = &data->fattr;
4882 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004883 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004884 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004885 data->rpc_status = 0;
4886
Trond Myklebustc970aa82007-07-14 15:39:59 -04004887 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004888 msg.rpc_argp = &data->args;
4889 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004890 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004891 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004892 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004893 if (!issync)
4894 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004895 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004896 if (status != 0)
4897 goto out;
4898 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004899 if (status == 0)
4900 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4901 else
4902 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004903out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004904 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004905 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906}
4907
Trond Myklebuste6f81072008-01-24 18:14:34 -05004908int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909{
4910 struct nfs_server *server = NFS_SERVER(inode);
4911 struct nfs4_exception exception = { };
4912 int err;
4913 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004914 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Trond Myklebustca8acf82013-08-13 10:36:56 -04004915 trace_nfs4_delegreturn(inode, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 switch (err) {
4917 case -NFS4ERR_STALE_STATEID:
4918 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 case 0:
4920 return 0;
4921 }
4922 err = nfs4_handle_exception(server, err, &exception);
4923 } while (exception.retry);
4924 return err;
4925}
4926
4927#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4928#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4929
4930/*
4931 * sleep, with exponential backoff, and retry the LOCK operation.
4932 */
4933static unsigned long
4934nfs4_set_lock_task_retry(unsigned long timeout)
4935{
Colin Cross416ad3c2013-05-06 23:50:06 +00004936 freezable_schedule_timeout_killable_unsafe(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 timeout <<= 1;
4938 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4939 return NFS4_LOCK_MAXTIMEOUT;
4940 return timeout;
4941}
4942
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4944{
4945 struct inode *inode = state->inode;
4946 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004947 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004948 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004950 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004952 struct nfs_lockt_res res = {
4953 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954 };
4955 struct rpc_message msg = {
4956 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4957 .rpc_argp = &arg,
4958 .rpc_resp = &res,
4959 .rpc_cred = state->owner->so_cred,
4960 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 struct nfs4_lock_state *lsp;
4962 int status;
4963
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004964 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004965 status = nfs4_set_lock_state(state, request);
4966 if (status != 0)
4967 goto out;
4968 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004969 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004970 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004971 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004972 switch (status) {
4973 case 0:
4974 request->fl_type = F_UNLCK;
4975 break;
4976 case -NFS4ERR_DENIED:
4977 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004979 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004980out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 return status;
4982}
4983
4984static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4985{
4986 struct nfs4_exception exception = { };
4987 int err;
4988
4989 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04004990 err = _nfs4_proc_getlk(state, cmd, request);
4991 trace_nfs4_get_lock(request, state, cmd, err);
4992 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993 &exception);
4994 } while (exception.retry);
4995 return err;
4996}
4997
4998static int do_vfs_lock(struct file *file, struct file_lock *fl)
4999{
5000 int res = 0;
5001 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5002 case FL_POSIX:
5003 res = posix_lock_file_wait(file, fl);
5004 break;
5005 case FL_FLOCK:
5006 res = flock_lock_file_wait(file, fl);
5007 break;
5008 default:
5009 BUG();
5010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011 return res;
5012}
5013
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005014struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005015 struct nfs_locku_args arg;
5016 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005017 struct nfs4_lock_state *lsp;
5018 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005019 struct file_lock fl;
5020 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005021 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005022};
5023
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005024static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5025 struct nfs_open_context *ctx,
5026 struct nfs4_lock_state *lsp,
5027 struct nfs_seqid *seqid)
5028{
5029 struct nfs4_unlockdata *p;
5030 struct inode *inode = lsp->ls_state->inode;
5031
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005032 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005033 if (p == NULL)
5034 return NULL;
5035 p->arg.fh = NFS_FH(inode);
5036 p->arg.fl = &p->fl;
5037 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005038 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005039 p->arg.stateid = &lsp->ls_stateid;
5040 p->lsp = lsp;
5041 atomic_inc(&lsp->ls_count);
5042 /* Ensure we don't close file until we're done freeing locks! */
5043 p->ctx = get_nfs_open_context(ctx);
5044 memcpy(&p->fl, fl, sizeof(p->fl));
5045 p->server = NFS_SERVER(inode);
5046 return p;
5047}
5048
Trond Myklebust06f814a2006-01-03 09:55:07 +01005049static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005050{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005051 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005052 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005053 nfs4_put_lock_state(calldata->lsp);
5054 put_nfs_open_context(calldata->ctx);
5055 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005056}
5057
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005058static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005059{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005060 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005061
Trond Myklebust14516c32010-07-31 14:29:06 -04005062 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5063 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005064 switch (task->tk_status) {
5065 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05005066 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
5067 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005068 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005069 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005070 case -NFS4ERR_BAD_STATEID:
5071 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005072 case -NFS4ERR_STALE_STATEID:
5073 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005074 break;
5075 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005076 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005077 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005078 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005079 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005080}
5081
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005082static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005083{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005084 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005086 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005087 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04005088 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005089 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005090 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005091 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005092 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005093 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005094 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005095 &calldata->res.seq_res,
5096 task) != 0)
5097 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005098 return;
5099out_no_action:
5100 task->tk_action = NULL;
5101out_wait:
5102 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103}
5104
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005105static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005106 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005107 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005108 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005109};
5110
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005111static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5112 struct nfs_open_context *ctx,
5113 struct nfs4_lock_state *lsp,
5114 struct nfs_seqid *seqid)
5115{
5116 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005117 struct rpc_message msg = {
5118 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5119 .rpc_cred = ctx->cred,
5120 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005121 struct rpc_task_setup task_setup_data = {
5122 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005123 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005124 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005125 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005126 .flags = RPC_TASK_ASYNC,
5127 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005128
Frank Filz137d6ac2007-07-09 15:32:29 -07005129 /* Ensure this is an unlock - when canceling a lock, the
5130 * canceled lock is passed in, and it won't be an unlock.
5131 */
5132 fl->fl_type = F_UNLCK;
5133
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005134 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5135 if (data == NULL) {
5136 nfs_free_seqid(seqid);
5137 return ERR_PTR(-ENOMEM);
5138 }
5139
Trond Myklebust9d12b212012-01-17 22:04:25 -05005140 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005141 msg.rpc_argp = &data->arg;
5142 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005143 task_setup_data.callback_data = data;
5144 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005145}
5146
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5148{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005149 struct inode *inode = state->inode;
5150 struct nfs4_state_owner *sp = state->owner;
5151 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005152 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005153 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005154 struct rpc_task *task;
5155 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005156 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157
Trond Myklebust9b073572006-06-29 16:38:34 -04005158 status = nfs4_set_lock_state(state, request);
5159 /* Unlock _before_ we do the RPC call */
5160 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005161 /* Exclude nfs_delegation_claim_locks() */
5162 mutex_lock(&sp->so_delegreturn_mutex);
5163 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005164 down_read(&nfsi->rwsem);
5165 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
5166 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005167 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005168 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005169 }
5170 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005171 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005172 if (status != 0)
5173 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005174 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005175 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005176 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5177 goto out;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005178 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005179 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005180 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04005181 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005182 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005183 status = PTR_ERR(task);
5184 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005185 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005186 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005187 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005188out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005189 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005190 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005191 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192}
5193
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005194struct nfs4_lockdata {
5195 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005196 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005197 struct nfs4_lock_state *lsp;
5198 struct nfs_open_context *ctx;
5199 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005200 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005201 int rpc_status;
5202 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005203 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005204};
5205
5206static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005207 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5208 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005209{
5210 struct nfs4_lockdata *p;
5211 struct inode *inode = lsp->ls_state->inode;
5212 struct nfs_server *server = NFS_SERVER(inode);
5213
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005214 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005215 if (p == NULL)
5216 return NULL;
5217
5218 p->arg.fh = NFS_FH(inode);
5219 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005220 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005221 if (p->arg.open_seqid == NULL)
5222 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005223 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005224 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005225 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005226 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04005227 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005228 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005229 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005230 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005231 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005232 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005233 atomic_inc(&lsp->ls_count);
5234 p->ctx = get_nfs_open_context(ctx);
5235 memcpy(&p->fl, fl, sizeof(p->fl));
5236 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005237out_free_seqid:
5238 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005239out_free:
5240 kfree(p);
5241 return NULL;
5242}
5243
5244static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5245{
5246 struct nfs4_lockdata *data = calldata;
5247 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248
Harvey Harrison3110ff82008-05-02 13:42:44 -07005249 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005250 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005251 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005252 /* Do we need to do an open_to_lock_owner? */
5253 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005254 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005255 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005256 }
Trond Myklebust8e472f32013-04-20 01:30:53 -04005257 data->arg.open_stateid = &state->open_stateid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005258 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005259 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005260 } else
5261 data->arg.new_lock_owner = 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04005262 if (!nfs4_valid_open_stateid(state)) {
5263 data->rpc_status = -EBADF;
5264 task->tk_action = NULL;
5265 goto out_release_open_seqid;
5266 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005267 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005268 if (nfs4_setup_sequence(data->server,
5269 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005270 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005271 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04005272 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04005273out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005274 nfs_release_seqid(data->arg.open_seqid);
5275out_release_lock_seqid:
5276 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005277out_wait:
5278 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005279 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005280}
5281
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005282static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5283{
5284 struct nfs4_lockdata *data = calldata;
5285
Harvey Harrison3110ff82008-05-02 13:42:44 -07005286 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005287
Trond Myklebust14516c32010-07-31 14:29:06 -04005288 if (!nfs4_sequence_done(task, &data->res.seq_res))
5289 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04005290
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005291 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005292 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005293 if (data->rpc_status == 0)
5294 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
5295 else
5296 goto out;
5297 }
5298 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05005299 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005300 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04005301 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005302 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005303out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07005304 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005305}
5306
5307static void nfs4_lock_release(void *calldata)
5308{
5309 struct nfs4_lockdata *data = calldata;
5310
Harvey Harrison3110ff82008-05-02 13:42:44 -07005311 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005312 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005313 if (data->cancelled != 0) {
5314 struct rpc_task *task;
5315 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5316 data->arg.lock_seqid);
5317 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08005318 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005319 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005320 } else
5321 nfs_free_seqid(data->arg.lock_seqid);
5322 nfs4_put_lock_state(data->lsp);
5323 put_nfs_open_context(data->ctx);
5324 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005325 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005326}
5327
5328static const struct rpc_call_ops nfs4_lock_ops = {
5329 .rpc_call_prepare = nfs4_lock_prepare,
5330 .rpc_call_done = nfs4_lock_done,
5331 .rpc_release = nfs4_lock_release,
5332};
5333
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005334static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5335{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005336 switch (error) {
5337 case -NFS4ERR_ADMIN_REVOKED:
5338 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05005339 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005340 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04005341 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05005342 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005343 break;
5344 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005345 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05005346 case -NFS4ERR_EXPIRED:
5347 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005348 };
5349}
5350
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005351static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005352{
5353 struct nfs4_lockdata *data;
5354 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005355 struct rpc_message msg = {
5356 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5357 .rpc_cred = state->owner->so_cred,
5358 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005359 struct rpc_task_setup task_setup_data = {
5360 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005361 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005362 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005363 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005364 .flags = RPC_TASK_ASYNC,
5365 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005366 int ret;
5367
Harvey Harrison3110ff82008-05-02 13:42:44 -07005368 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005369 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005370 fl->fl_u.nfs4_fl.owner,
5371 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005372 if (data == NULL)
5373 return -ENOMEM;
5374 if (IS_SETLKW(cmd))
5375 data->arg.block = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -05005376 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005377 msg.rpc_argp = &data->arg;
5378 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005379 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005380 if (recovery_type > NFS_LOCK_NEW) {
5381 if (recovery_type == NFS_LOCK_RECLAIM)
5382 data->arg.reclaim = NFS_LOCK_RECLAIM;
5383 nfs4_set_sequence_privileged(&data->arg.seq_args);
5384 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04005385 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005386 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005387 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005388 ret = nfs4_wait_for_completion_rpc_task(task);
5389 if (ret == 0) {
5390 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005391 if (ret)
5392 nfs4_handle_setlk_error(data->server, data->lsp,
5393 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005394 } else
5395 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005396 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005397 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005398 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399}
5400
5401static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5402{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005403 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005404 struct nfs4_exception exception = {
5405 .inode = state->inode,
5406 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005407 int err;
5408
5409 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005410 /* Cache the lock if possible... */
5411 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5412 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005413 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005414 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
Trond Myklebust168667c2010-10-19 19:47:49 -04005415 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00005416 break;
5417 nfs4_handle_exception(server, err, &exception);
5418 } while (exception.retry);
5419 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420}
5421
5422static int nfs4_lock_expired(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
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005430 err = nfs4_set_lock_state(state, request);
5431 if (err != 0)
5432 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00005433 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005434 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5435 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005436 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005437 trace_nfs4_lock_expired(request, state, F_SETLK, err);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005438 switch (err) {
5439 default:
5440 goto out;
5441 case -NFS4ERR_GRACE:
5442 case -NFS4ERR_DELAY:
5443 nfs4_handle_exception(server, err, &exception);
5444 err = 0;
5445 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005446 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005447out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00005448 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449}
5450
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005451#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005452/**
5453 * nfs41_check_expired_locks - possibly free a lock stateid
5454 *
5455 * @state: NFSv4 state for an inode
5456 *
5457 * Returns NFS_OK if recovery for this stateid is now finished.
5458 * Otherwise a negative NFS4ERR value is returned.
5459 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005460static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005461{
Chuck Levereb64cf92012-07-11 16:30:05 -04005462 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005463 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005464 struct nfs_server *server = NFS_SERVER(state->inode);
5465
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005466 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04005467 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005468 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
5469
5470 status = nfs41_test_stateid(server,
5471 &lsp->ls_stateid,
5472 cred);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005473 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005474 /* Free the stateid unless the server
5475 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04005476 if (status != -NFS4ERR_BAD_STATEID)
5477 nfs41_free_stateid(server,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005478 &lsp->ls_stateid,
5479 cred);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005480 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005481 ret = status;
5482 }
5483 }
5484 };
5485
5486 return ret;
5487}
5488
5489static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5490{
5491 int status = NFS_OK;
5492
5493 if (test_bit(LK_STATE_IN_USE, &state->flags))
5494 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005495 if (status != NFS_OK)
5496 status = nfs4_lock_expired(state, request);
5497 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005498}
5499#endif
5500
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5502{
Trond Myklebust9a99af492013-02-04 20:17:49 -05005503 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005504 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005505 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005506 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005507 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005509 if ((fl_flags & FL_POSIX) &&
5510 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5511 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005512 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005513 status = nfs4_set_lock_state(state, request);
5514 if (status != 0)
5515 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005516 request->fl_flags |= FL_ACCESS;
5517 status = do_vfs_lock(request->fl_file, request);
5518 if (status < 0)
5519 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005520 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005521 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005522 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005523 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005524 request->fl_flags = fl_flags & ~FL_SLEEP;
5525 status = do_vfs_lock(request->fl_file, request);
5526 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005527 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05005528 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5529 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005530 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005531 if (status != 0)
Trond Myklebust9a99af492013-02-04 20:17:49 -05005532 goto out;
5533 down_read(&nfsi->rwsem);
5534 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5535 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005536 goto out_unlock;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005537 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005538 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005539 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005540 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005541 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5542 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005543out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005544 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005545out:
5546 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547 return status;
5548}
5549
5550static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5551{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005552 struct nfs4_exception exception = {
5553 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005554 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005555 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556 int err;
5557
5558 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005559 err = _nfs4_proc_setlk(state, cmd, request);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005560 trace_nfs4_set_lock(request, state, cmd, err);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005561 if (err == -NFS4ERR_DENIED)
5562 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005564 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565 } while (exception.retry);
5566 return err;
5567}
5568
5569static int
5570nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5571{
5572 struct nfs_open_context *ctx;
5573 struct nfs4_state *state;
5574 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5575 int status;
5576
5577 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005578 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579 state = ctx->state;
5580
5581 if (request->fl_start < 0 || request->fl_end < 0)
5582 return -EINVAL;
5583
Trond Myklebustd9531262009-07-21 19:22:38 -04005584 if (IS_GETLK(cmd)) {
5585 if (state != NULL)
5586 return nfs4_proc_getlk(state, F_GETLK, request);
5587 return 0;
5588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589
5590 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5591 return -EINVAL;
5592
Trond Myklebustd9531262009-07-21 19:22:38 -04005593 if (request->fl_type == F_UNLCK) {
5594 if (state != NULL)
5595 return nfs4_proc_unlck(state, cmd, request);
5596 return 0;
5597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598
Trond Myklebustd9531262009-07-21 19:22:38 -04005599 if (state == NULL)
5600 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005601 /*
5602 * Don't rely on the VFS having checked the file open mode,
5603 * since it won't do this for flock() locks.
5604 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005605 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005606 case F_RDLCK:
5607 if (!(filp->f_mode & FMODE_READ))
5608 return -EBADF;
5609 break;
5610 case F_WRLCK:
5611 if (!(filp->f_mode & FMODE_WRITE))
5612 return -EBADF;
5613 }
5614
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615 do {
5616 status = nfs4_proc_setlk(state, cmd, request);
5617 if ((status != -EAGAIN) || IS_SETLK(cmd))
5618 break;
5619 timeout = nfs4_set_lock_task_retry(timeout);
5620 status = -ERESTARTSYS;
5621 if (signalled())
5622 break;
5623 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624 return status;
5625}
5626
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005627int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05005628{
5629 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05005630 int err;
5631
5632 err = nfs4_set_lock_state(state, fl);
5633 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005634 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04005635 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005636 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05005637}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005638
Trond Myklebustcf470c32012-03-07 13:49:12 -05005639struct nfs_release_lockowner_data {
5640 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005641 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005642 struct nfs_release_lockowner_args args;
5643};
5644
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005645static void nfs4_release_lockowner_release(void *calldata)
5646{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005647 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005648 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005649 kfree(calldata);
5650}
5651
Trond Myklebust17280172012-03-11 13:11:00 -04005652static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005653 .rpc_release = nfs4_release_lockowner_release,
5654};
5655
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04005656static int nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005657{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005658 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005659 struct rpc_message msg = {
5660 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5661 };
5662
5663 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005664 return -EINVAL;
5665 data = kmalloc(sizeof(*data), GFP_NOFS);
5666 if (!data)
5667 return -ENOMEM;
5668 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005669 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005670 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5671 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5672 data->args.lock_owner.s_dev = server->s_dev;
5673 msg.rpc_argp = &data->args;
5674 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5675 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005676}
5677
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005678#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5679
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005680static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5681 const void *buf, size_t buflen,
5682 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005683{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005684 if (strcmp(key, "") != 0)
5685 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005686
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005687 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005688}
5689
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005690static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5691 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005692{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005693 if (strcmp(key, "") != 0)
5694 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005695
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005696 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005697}
5698
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005699static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5700 size_t list_len, const char *name,
5701 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005702{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005703 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005704
J. Bruce Fields096455a2006-03-20 23:23:42 -05005705 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5706 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005707
5708 if (list && len <= list_len)
5709 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005710 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005711}
5712
David Quigleyc9bccef2013-05-22 12:50:45 -04005713#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5714static inline int nfs4_server_supports_labels(struct nfs_server *server)
5715{
5716 return server->caps & NFS_CAP_SECURITY_LABEL;
5717}
5718
5719static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
5720 const void *buf, size_t buflen,
5721 int flags, int type)
5722{
5723 if (security_ismaclabel(key))
5724 return nfs4_set_security_label(dentry, buf, buflen);
5725
5726 return -EOPNOTSUPP;
5727}
5728
5729static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
5730 void *buf, size_t buflen, int type)
5731{
5732 if (security_ismaclabel(key))
5733 return nfs4_get_security_label(dentry->d_inode, buf, buflen);
5734 return -EOPNOTSUPP;
5735}
5736
5737static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
5738 size_t list_len, const char *name,
5739 size_t name_len, int type)
5740{
5741 size_t len = 0;
5742
5743 if (nfs_server_capable(dentry->d_inode, NFS_CAP_SECURITY_LABEL)) {
5744 len = security_inode_listsecurity(dentry->d_inode, NULL, 0);
5745 if (list && len <= list_len)
5746 security_inode_listsecurity(dentry->d_inode, list, len);
5747 }
5748 return len;
5749}
5750
5751static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
5752 .prefix = XATTR_SECURITY_PREFIX,
5753 .list = nfs4_xattr_list_nfs4_label,
5754 .get = nfs4_xattr_get_nfs4_label,
5755 .set = nfs4_xattr_set_nfs4_label,
5756};
5757#endif
5758
5759
Andy Adamson533eb462011-06-13 18:25:56 -04005760/*
5761 * nfs_fhget will use either the mounted_on_fileid or the fileid
5762 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005763static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5764{
Andy Adamson533eb462011-06-13 18:25:56 -04005765 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5766 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5767 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005768 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005769 return;
5770
5771 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005772 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005773 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5774 fattr->nlink = 2;
5775}
5776
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005777static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5778 const struct qstr *name,
5779 struct nfs4_fs_locations *fs_locations,
5780 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005781{
5782 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04005783 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005784 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005785 };
5786 struct nfs4_fs_locations_arg args = {
5787 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005788 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005789 .page = page,
5790 .bitmask = bitmask,
5791 };
Benny Halevy22958462009-04-01 09:22:02 -04005792 struct nfs4_fs_locations_res res = {
5793 .fs_locations = fs_locations,
5794 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005795 struct rpc_message msg = {
5796 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5797 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005798 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005799 };
5800 int status;
5801
Harvey Harrison3110ff82008-05-02 13:42:44 -07005802 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005803
5804 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5805 * is not supported */
5806 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5807 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5808 else
5809 bitmask[0] |= FATTR4_WORD0_FILEID;
5810
Trond Myklebustc228fd32007-01-13 02:28:11 -05005811 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005812 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005813 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005814 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005815 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005816 return status;
5817}
5818
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005819int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5820 const struct qstr *name,
5821 struct nfs4_fs_locations *fs_locations,
5822 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005823{
5824 struct nfs4_exception exception = { };
5825 int err;
5826 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04005827 err = _nfs4_proc_fs_locations(client, dir, name,
5828 fs_locations, page);
5829 trace_nfs4_get_fs_locations(dir, name, err);
5830 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005831 &exception);
5832 } while (exception.retry);
5833 return err;
5834}
5835
Andy Adamson5ec16a82013-08-08 10:57:55 -04005836/**
5837 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
5838 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
5839 */
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005840static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5841{
5842 int status;
5843 struct nfs4_secinfo_arg args = {
5844 .dir_fh = NFS_FH(dir),
5845 .name = name,
5846 };
5847 struct nfs4_secinfo_res res = {
5848 .flavors = flavors,
5849 };
5850 struct rpc_message msg = {
5851 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5852 .rpc_argp = &args,
5853 .rpc_resp = &res,
5854 };
Andy Adamson5ec16a82013-08-08 10:57:55 -04005855 struct rpc_clnt *clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005856
5857 dprintk("NFS call secinfo %s\n", name->name);
Andy Adamson5ec16a82013-08-08 10:57:55 -04005858 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005859 dprintk("NFS reply secinfo: %d\n", status);
5860 return status;
5861}
5862
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005863int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5864 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005865{
5866 struct nfs4_exception exception = { };
5867 int err;
5868 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04005869 err = _nfs4_proc_secinfo(dir, name, flavors);
5870 trace_nfs4_secinfo(dir, name, err);
5871 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005872 &exception);
5873 } while (exception.retry);
5874 return err;
5875}
5876
Andy Adamson557134a2009-04-01 09:21:53 -04005877#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005878/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005879 * Check the exchange flags returned by the server for invalid flags, having
5880 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5881 * DS flags set.
5882 */
5883static int nfs4_check_cl_exchange_flags(u32 flags)
5884{
5885 if (flags & ~EXCHGID4_FLAG_MASK_R)
5886 goto out_inval;
5887 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5888 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5889 goto out_inval;
5890 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5891 goto out_inval;
5892 return NFS_OK;
5893out_inval:
5894 return -NFS4ERR_INVAL;
5895}
5896
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005897static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005898nfs41_same_server_scope(struct nfs41_server_scope *a,
5899 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005900{
5901 if (a->server_scope_sz == b->server_scope_sz &&
5902 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5903 return true;
5904
5905 return false;
5906}
5907
Andy Adamson357f54d2010-12-14 10:11:57 -05005908/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005909 * nfs4_proc_bind_conn_to_session()
5910 *
5911 * The 4.1 client currently uses the same TCP connection for the
5912 * fore and backchannel.
5913 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005914int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005915{
5916 int status;
5917 struct nfs41_bind_conn_to_session_res res;
5918 struct rpc_message msg = {
5919 .rpc_proc =
5920 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5921 .rpc_argp = clp,
5922 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005923 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005924 };
5925
5926 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005927
5928 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5929 if (unlikely(res.session == NULL)) {
5930 status = -ENOMEM;
5931 goto out;
5932 }
5933
5934 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005935 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005936 if (status == 0) {
5937 if (memcmp(res.session->sess_id.data,
5938 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5939 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5940 status = -EIO;
5941 goto out_session;
5942 }
5943 if (res.dir != NFS4_CDFS4_BOTH) {
5944 dprintk("NFS: %s: Unexpected direction from server\n",
5945 __func__);
5946 status = -EIO;
5947 goto out_session;
5948 }
5949 if (res.use_conn_in_rdma_mode) {
5950 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5951 __func__);
5952 status = -EIO;
5953 goto out_session;
5954 }
5955 }
5956out_session:
5957 kfree(res.session);
5958out:
5959 dprintk("<-- %s status= %d\n", __func__, status);
5960 return status;
5961}
5962
5963/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005964 * nfs4_proc_exchange_id()
5965 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005966 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5967 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005968 * Since the clientid has expired, all compounds using sessions
5969 * associated with the stale clientid will be returning
5970 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5971 * be in some phase of session reset.
5972 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005973int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005974{
5975 nfs4_verifier verifier;
5976 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005977 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005978 .client = clp,
Trond Myklebust4f0b4292013-05-20 12:24:03 -04005979 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
5980 EXCHGID4_FLAG_BIND_PRINC_STATEID,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005981 };
5982 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005983 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005984 };
5985 int status;
5986 struct rpc_message msg = {
5987 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5988 .rpc_argp = &args,
5989 .rpc_resp = &res,
5990 .rpc_cred = cred,
5991 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005992
Chuck Leverf0920752012-05-21 22:45:41 -04005993 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005994 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5995 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005996 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5997 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5998 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005999
Chuck Leveracdeb692012-05-21 22:46:16 -04006000 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006001 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04006002 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006003 status = -ENOMEM;
6004 goto out;
6005 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006006
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006007 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006008 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006009 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04006010 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04006011 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04006012 }
6013
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006014 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006015 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006016 status = -ENOMEM;
6017 goto out_server_scope;
6018 }
6019
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006020 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006021 trace_nfs4_exchange_id(clp, status);
Chuck Lever177313f2012-05-21 22:44:58 -04006022 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04006023 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006024
Chuck Lever177313f2012-05-21 22:44:58 -04006025 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04006026 clp->cl_clientid = res.clientid;
6027 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
6028 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
6029 clp->cl_seqid = res.seqid;
6030
Chuck Leveracdeb692012-05-21 22:46:16 -04006031 kfree(clp->cl_serverowner);
6032 clp->cl_serverowner = res.server_owner;
6033 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04006034
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006035 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04006036 kfree(clp->cl_implid);
6037 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006038
Chuck Lever177313f2012-05-21 22:44:58 -04006039 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006040 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006041 res.server_scope)) {
6042 dprintk("%s: server_scope mismatch detected\n",
6043 __func__);
6044 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006045 kfree(clp->cl_serverscope);
6046 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006047 }
6048
Chuck Lever177313f2012-05-21 22:44:58 -04006049 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006050 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006051 goto out;
6052 }
Trond Myklebust32b01312012-05-26 13:41:04 -04006053 } else
6054 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006055
Chuck Leveracdeb692012-05-21 22:46:16 -04006056out_server_owner:
6057 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006058out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006059 kfree(res.server_scope);
6060out:
Chuck Lever177313f2012-05-21 22:44:58 -04006061 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006062 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006063 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006064 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04006065 clp->cl_implid->date.seconds,
6066 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006067 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006068 return status;
6069}
6070
Trond Myklebust66245532012-05-25 17:18:09 -04006071static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
6072 struct rpc_cred *cred)
6073{
6074 struct rpc_message msg = {
6075 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
6076 .rpc_argp = clp,
6077 .rpc_cred = cred,
6078 };
6079 int status;
6080
6081 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006082 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04006083 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04006084 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04006085 "DESTROY_CLIENTID.", status, clp->cl_hostname);
6086 return status;
6087}
6088
6089static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
6090 struct rpc_cred *cred)
6091{
6092 unsigned int loop;
6093 int ret;
6094
6095 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
6096 ret = _nfs4_proc_destroy_clientid(clp, cred);
6097 switch (ret) {
6098 case -NFS4ERR_DELAY:
6099 case -NFS4ERR_CLIENTID_BUSY:
6100 ssleep(1);
6101 break;
6102 default:
6103 return ret;
6104 }
6105 }
6106 return 0;
6107}
6108
6109int nfs4_destroy_clientid(struct nfs_client *clp)
6110{
6111 struct rpc_cred *cred;
6112 int ret = 0;
6113
6114 if (clp->cl_mvops->minor_version < 1)
6115 goto out;
6116 if (clp->cl_exchange_flags == 0)
6117 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04006118 if (clp->cl_preserve_clid)
6119 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04006120 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04006121 ret = nfs4_proc_destroy_clientid(clp, cred);
6122 if (cred)
6123 put_rpccred(cred);
6124 switch (ret) {
6125 case 0:
6126 case -NFS4ERR_STALE_CLIENTID:
6127 clp->cl_exchange_flags = 0;
6128 }
6129out:
6130 return ret;
6131}
6132
Andy Adamson2050f0c2009-04-01 09:22:30 -04006133struct nfs4_get_lease_time_data {
6134 struct nfs4_get_lease_time_args *args;
6135 struct nfs4_get_lease_time_res *res;
6136 struct nfs_client *clp;
6137};
6138
6139static void nfs4_get_lease_time_prepare(struct rpc_task *task,
6140 void *calldata)
6141{
Andy Adamson2050f0c2009-04-01 09:22:30 -04006142 struct nfs4_get_lease_time_data *data =
6143 (struct nfs4_get_lease_time_data *)calldata;
6144
6145 dprintk("--> %s\n", __func__);
6146 /* just setup sequence, do not trigger session recovery
6147 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006148 nfs41_setup_sequence(data->clp->cl_session,
6149 &data->args->la_seq_args,
6150 &data->res->lr_seq_res,
6151 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006152 dprintk("<-- %s\n", __func__);
6153}
6154
6155/*
6156 * Called from nfs4_state_manager thread for session setup, so don't recover
6157 * from sequence operation or clientid errors.
6158 */
6159static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
6160{
6161 struct nfs4_get_lease_time_data *data =
6162 (struct nfs4_get_lease_time_data *)calldata;
6163
6164 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006165 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
6166 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04006167 switch (task->tk_status) {
6168 case -NFS4ERR_DELAY:
6169 case -NFS4ERR_GRACE:
6170 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
6171 rpc_delay(task, NFS4_POLL_RETRY_MIN);
6172 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006173 /* fall through */
6174 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006175 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006176 return;
6177 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04006178 dprintk("<-- %s\n", __func__);
6179}
6180
Trond Myklebust17280172012-03-11 13:11:00 -04006181static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04006182 .rpc_call_prepare = nfs4_get_lease_time_prepare,
6183 .rpc_call_done = nfs4_get_lease_time_done,
6184};
6185
6186int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
6187{
6188 struct rpc_task *task;
6189 struct nfs4_get_lease_time_args args;
6190 struct nfs4_get_lease_time_res res = {
6191 .lr_fsinfo = fsinfo,
6192 };
6193 struct nfs4_get_lease_time_data data = {
6194 .args = &args,
6195 .res = &res,
6196 .clp = clp,
6197 };
6198 struct rpc_message msg = {
6199 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
6200 .rpc_argp = &args,
6201 .rpc_resp = &res,
6202 };
6203 struct rpc_task_setup task_setup = {
6204 .rpc_client = clp->cl_rpcclient,
6205 .rpc_message = &msg,
6206 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006207 .callback_data = &data,
6208 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04006209 };
6210 int status;
6211
Trond Myklebust9d12b212012-01-17 22:04:25 -05006212 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006213 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006214 dprintk("--> %s\n", __func__);
6215 task = rpc_run_task(&task_setup);
6216
6217 if (IS_ERR(task))
6218 status = PTR_ERR(task);
6219 else {
6220 status = task->tk_status;
6221 rpc_put_task(task);
6222 }
6223 dprintk("<-- %s return %d\n", __func__, status);
6224
6225 return status;
6226}
6227
Andy Adamsonfc931582009-04-01 09:22:31 -04006228/*
6229 * Initialize the values to be used by the client in CREATE_SESSION
6230 * If nfs4_init_session set the fore channel request and response sizes,
6231 * use them.
6232 *
6233 * Set the back channel max_resp_sz_cached to zero to force the client to
6234 * always set csa_cachethis to FALSE because the current implementation
6235 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
6236 */
6237static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
6238{
Andy Adamson18aad3d2013-06-26 12:21:49 -04006239 unsigned int max_rqst_sz, max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006240
Andy Adamson18aad3d2013-06-26 12:21:49 -04006241 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
6242 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
6243
Andy Adamsonfc931582009-04-01 09:22:31 -04006244 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04006245 args->fc_attrs.max_rqst_sz = max_rqst_sz;
6246 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006247 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05006248 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04006249
6250 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05006251 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04006252 __func__,
6253 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05006254 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04006255
6256 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04006257 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
6258 args->bc_attrs.max_resp_sz = PAGE_SIZE;
6259 args->bc_attrs.max_resp_sz_cached = 0;
6260 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
6261 args->bc_attrs.max_reqs = 1;
6262
6263 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
6264 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
6265 __func__,
6266 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
6267 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
6268 args->bc_attrs.max_reqs);
6269}
6270
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006271static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04006272{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006273 struct nfs4_channel_attrs *sent = &args->fc_attrs;
6274 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
6275
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006276 if (rcvd->max_resp_sz > sent->max_resp_sz)
6277 return -EINVAL;
6278 /*
6279 * Our requested max_ops is the minimum we need; we're not
6280 * prepared to break up compounds into smaller pieces than that.
6281 * So, no point even trying to continue if the server won't
6282 * cooperate:
6283 */
6284 if (rcvd->max_ops < sent->max_ops)
6285 return -EINVAL;
6286 if (rcvd->max_reqs == 0)
6287 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006288 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
6289 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006290 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04006291}
6292
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006293static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
6294{
6295 struct nfs4_channel_attrs *sent = &args->bc_attrs;
6296 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04006297
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006298 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
6299 return -EINVAL;
6300 if (rcvd->max_resp_sz < sent->max_resp_sz)
6301 return -EINVAL;
6302 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
6303 return -EINVAL;
6304 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006305 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006306 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006307 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006308 return -EINVAL;
6309 return 0;
6310}
Andy Adamson8d353012009-04-01 09:22:32 -04006311
Andy Adamson8d353012009-04-01 09:22:32 -04006312static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
6313 struct nfs4_session *session)
6314{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006315 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04006316
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006317 ret = nfs4_verify_fore_channel_attrs(args, session);
6318 if (ret)
6319 return ret;
6320 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04006321}
6322
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006323static int _nfs4_proc_create_session(struct nfs_client *clp,
6324 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04006325{
6326 struct nfs4_session *session = clp->cl_session;
6327 struct nfs41_create_session_args args = {
6328 .client = clp,
6329 .cb_program = NFS4_CALLBACK,
6330 };
6331 struct nfs41_create_session_res res = {
6332 .client = clp,
6333 };
6334 struct rpc_message msg = {
6335 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
6336 .rpc_argp = &args,
6337 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006338 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04006339 };
6340 int status;
6341
6342 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04006343 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04006344
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006345 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006346 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04006347
Trond Myklebust43095d32012-11-20 11:13:12 -05006348 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04006349 /* Verify the session's negotiated channel_attrs values */
6350 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04006351 /* Increment the clientid slot sequence id */
6352 clp->cl_seqid++;
6353 }
6354
6355 return status;
6356}
6357
6358/*
6359 * Issues a CREATE_SESSION operation to the server.
6360 * It is the responsibility of the caller to verify the session is
6361 * expired before calling this routine.
6362 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006363int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04006364{
6365 int status;
6366 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04006367 struct nfs4_session *session = clp->cl_session;
6368
6369 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
6370
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006371 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04006372 if (status)
6373 goto out;
6374
Andy Adamsonaacd5532011-11-09 13:58:21 -05006375 /* Init or reset the session slot tables */
6376 status = nfs4_setup_session_slot_tables(session);
6377 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04006378 if (status)
6379 goto out;
6380
6381 ptr = (unsigned *)&session->sess_id.data[0];
6382 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
6383 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04006384out:
6385 dprintk("<-- %s\n", __func__);
6386 return status;
6387}
6388
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006389/*
6390 * Issue the over-the-wire RPC DESTROY_SESSION.
6391 * The caller must serialize access to this routine.
6392 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006393int nfs4_proc_destroy_session(struct nfs4_session *session,
6394 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006395{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006396 struct rpc_message msg = {
6397 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
6398 .rpc_argp = session,
6399 .rpc_cred = cred,
6400 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006401 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006402
6403 dprintk("--> nfs4_proc_destroy_session\n");
6404
6405 /* session is still being setup */
6406 if (session->clp->cl_cons_state != NFS_CS_READY)
6407 return status;
6408
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006409 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006410 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006411
6412 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04006413 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006414 "Session has been destroyed regardless...\n", status);
6415
6416 dprintk("<-- nfs4_proc_destroy_session\n");
6417 return status;
6418}
6419
Trond Myklebust7b38c362012-05-23 13:23:31 -04006420/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006421 * Renew the cl_session lease.
6422 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006423struct nfs4_sequence_data {
6424 struct nfs_client *clp;
6425 struct nfs4_sequence_args args;
6426 struct nfs4_sequence_res res;
6427};
6428
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006429static void nfs41_sequence_release(void *data)
6430{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006431 struct nfs4_sequence_data *calldata = data;
6432 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006433
Alexandros Batsakis71358402010-02-05 03:45:05 -08006434 if (atomic_read(&clp->cl_count) > 1)
6435 nfs4_schedule_state_renewal(clp);
6436 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006437 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006438}
6439
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006440static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6441{
6442 switch(task->tk_status) {
6443 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006444 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6445 return -EAGAIN;
6446 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006447 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006448 }
6449 return 0;
6450}
6451
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006452static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006453{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006454 struct nfs4_sequence_data *calldata = data;
6455 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006456
Trond Myklebust14516c32010-07-31 14:29:06 -04006457 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6458 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006459
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006460 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006461 if (task->tk_status < 0) {
6462 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006463 if (atomic_read(&clp->cl_count) == 1)
6464 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006465
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006466 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6467 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006468 return;
6469 }
6470 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006471 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006472out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006473 dprintk("<-- %s\n", __func__);
6474}
6475
6476static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6477{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006478 struct nfs4_sequence_data *calldata = data;
6479 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006480 struct nfs4_sequence_args *args;
6481 struct nfs4_sequence_res *res;
6482
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006483 args = task->tk_msg.rpc_argp;
6484 res = task->tk_msg.rpc_resp;
6485
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006486 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006487}
6488
6489static const struct rpc_call_ops nfs41_sequence_ops = {
6490 .rpc_call_done = nfs41_sequence_call_done,
6491 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006492 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006493};
6494
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006495static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
6496 struct rpc_cred *cred,
6497 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006498{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006499 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006500 struct rpc_message msg = {
6501 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6502 .rpc_cred = cred,
6503 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006504 struct rpc_task_setup task_setup_data = {
6505 .rpc_client = clp->cl_rpcclient,
6506 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006507 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04006508 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006509 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006510
Alexandros Batsakis71358402010-02-05 03:45:05 -08006511 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006512 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006513 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006514 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006515 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006516 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006517 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05006518 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006519 if (is_privileged)
6520 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006521 msg.rpc_argp = &calldata->args;
6522 msg.rpc_resp = &calldata->res;
6523 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006524 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006525
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006526 return rpc_run_task(&task_setup_data);
6527}
6528
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006529static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006530{
6531 struct rpc_task *task;
6532 int ret = 0;
6533
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006534 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6535 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006536 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006537 if (IS_ERR(task))
6538 ret = PTR_ERR(task);
6539 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006540 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006541 dprintk("<-- %s status=%d\n", __func__, ret);
6542 return ret;
6543}
6544
6545static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6546{
6547 struct rpc_task *task;
6548 int ret;
6549
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006550 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006551 if (IS_ERR(task)) {
6552 ret = PTR_ERR(task);
6553 goto out;
6554 }
6555 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006556 if (!ret) {
6557 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6558
6559 if (task->tk_status == 0)
6560 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006561 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006562 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006563 rpc_put_task(task);
6564out:
6565 dprintk("<-- %s status=%d\n", __func__, ret);
6566 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006567}
6568
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006569struct nfs4_reclaim_complete_data {
6570 struct nfs_client *clp;
6571 struct nfs41_reclaim_complete_args arg;
6572 struct nfs41_reclaim_complete_res res;
6573};
6574
6575static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6576{
6577 struct nfs4_reclaim_complete_data *calldata = data;
6578
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006579 nfs41_setup_sequence(calldata->clp->cl_session,
6580 &calldata->arg.seq_args,
6581 &calldata->res.seq_res,
6582 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006583}
6584
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006585static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6586{
6587 switch(task->tk_status) {
6588 case 0:
6589 case -NFS4ERR_COMPLETE_ALREADY:
6590 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6591 break;
6592 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006593 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006594 /* fall through */
6595 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006596 return -EAGAIN;
6597 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006598 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006599 }
6600 return 0;
6601}
6602
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006603static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6604{
6605 struct nfs4_reclaim_complete_data *calldata = data;
6606 struct nfs_client *clp = calldata->clp;
6607 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6608
6609 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006610 if (!nfs41_sequence_done(task, res))
6611 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006612
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006613 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006614 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6615 rpc_restart_call_prepare(task);
6616 return;
6617 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006618 dprintk("<-- %s\n", __func__);
6619}
6620
6621static void nfs4_free_reclaim_complete_data(void *data)
6622{
6623 struct nfs4_reclaim_complete_data *calldata = data;
6624
6625 kfree(calldata);
6626}
6627
6628static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6629 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6630 .rpc_call_done = nfs4_reclaim_complete_done,
6631 .rpc_release = nfs4_free_reclaim_complete_data,
6632};
6633
6634/*
6635 * Issue a global reclaim complete.
6636 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04006637static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
6638 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006639{
6640 struct nfs4_reclaim_complete_data *calldata;
6641 struct rpc_task *task;
6642 struct rpc_message msg = {
6643 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04006644 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006645 };
6646 struct rpc_task_setup task_setup_data = {
6647 .rpc_client = clp->cl_rpcclient,
6648 .rpc_message = &msg,
6649 .callback_ops = &nfs4_reclaim_complete_call_ops,
6650 .flags = RPC_TASK_ASYNC,
6651 };
6652 int status = -ENOMEM;
6653
6654 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006655 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006656 if (calldata == NULL)
6657 goto out;
6658 calldata->clp = clp;
6659 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006660
Trond Myklebust9d12b212012-01-17 22:04:25 -05006661 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006662 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006663 msg.rpc_argp = &calldata->arg;
6664 msg.rpc_resp = &calldata->res;
6665 task_setup_data.callback_data = calldata;
6666 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006667 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006668 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006669 goto out;
6670 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006671 status = nfs4_wait_for_completion_rpc_task(task);
6672 if (status == 0)
6673 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006674 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006675 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006676out:
6677 dprintk("<-- %s status=%d\n", __func__, status);
6678 return status;
6679}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006680
6681static void
6682nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6683{
6684 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006685 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006686 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006687
6688 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006689 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6690 * right now covering the LAYOUTGET we are about to send.
6691 * However, that is not so catastrophic, and there seems
6692 * to be no way to prevent it completely.
6693 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006694 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006695 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006696 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006697 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6698 NFS_I(lgp->args.inode)->layout,
6699 lgp->args.ctx->state)) {
6700 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00006701 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006702}
6703
6704static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6705{
6706 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006707 struct inode *inode = lgp->args.inode;
6708 struct nfs_server *server = NFS_SERVER(inode);
6709 struct pnfs_layout_hdr *lo;
6710 struct nfs4_state *state = NULL;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006711 unsigned long timeo, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006712
6713 dprintk("--> %s\n", __func__);
6714
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006715 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006716 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006717
6718 switch (task->tk_status) {
6719 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006720 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006721 case -NFS4ERR_LAYOUTTRYLATER:
6722 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05006723 timeo = rpc_get_timeout(task->tk_client);
6724 giveup = lgp->args.timestamp + timeo;
6725 if (time_after(giveup, jiffies))
6726 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006727 break;
6728 case -NFS4ERR_EXPIRED:
6729 case -NFS4ERR_BAD_STATEID:
6730 spin_lock(&inode->i_lock);
6731 lo = NFS_I(inode)->layout;
6732 if (!lo || list_empty(&lo->plh_segs)) {
6733 spin_unlock(&inode->i_lock);
6734 /* If the open stateid was bad, then recover it. */
6735 state = lgp->args.ctx->state;
6736 } else {
6737 LIST_HEAD(head);
6738
6739 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6740 spin_unlock(&inode->i_lock);
6741 /* Mark the bad layout state as invalid, then
6742 * retry using the open stateid. */
6743 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006744 }
6745 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006746 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6747 rpc_restart_call_prepare(task);
6748out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006749 dprintk("<-- %s\n", __func__);
6750}
6751
Idan Kedar85541162012-08-02 11:47:10 +03006752static size_t max_response_pages(struct nfs_server *server)
6753{
6754 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6755 return nfs_page_array_len(0, max_resp_sz);
6756}
6757
6758static void nfs4_free_pages(struct page **pages, size_t size)
6759{
6760 int i;
6761
6762 if (!pages)
6763 return;
6764
6765 for (i = 0; i < size; i++) {
6766 if (!pages[i])
6767 break;
6768 __free_page(pages[i]);
6769 }
6770 kfree(pages);
6771}
6772
6773static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6774{
6775 struct page **pages;
6776 int i;
6777
6778 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6779 if (!pages) {
6780 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6781 return NULL;
6782 }
6783
6784 for (i = 0; i < size; i++) {
6785 pages[i] = alloc_page(gfp_flags);
6786 if (!pages[i]) {
6787 dprintk("%s: failed to allocate page\n", __func__);
6788 nfs4_free_pages(pages, size);
6789 return NULL;
6790 }
6791 }
6792
6793 return pages;
6794}
6795
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006796static void nfs4_layoutget_release(void *calldata)
6797{
6798 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006799 struct inode *inode = lgp->args.inode;
6800 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006801 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006802
6803 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006804 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006805 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006806 put_nfs_open_context(lgp->args.ctx);
6807 kfree(calldata);
6808 dprintk("<-- %s\n", __func__);
6809}
6810
6811static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6812 .rpc_call_prepare = nfs4_layoutget_prepare,
6813 .rpc_call_done = nfs4_layoutget_done,
6814 .rpc_release = nfs4_layoutget_release,
6815};
6816
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006817struct pnfs_layout_segment *
6818nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006819{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006820 struct inode *inode = lgp->args.inode;
6821 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006822 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006823 struct rpc_task *task;
6824 struct rpc_message msg = {
6825 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6826 .rpc_argp = &lgp->args,
6827 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04006828 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006829 };
6830 struct rpc_task_setup task_setup_data = {
6831 .rpc_client = server->client,
6832 .rpc_message = &msg,
6833 .callback_ops = &nfs4_layoutget_call_ops,
6834 .callback_data = lgp,
6835 .flags = RPC_TASK_ASYNC,
6836 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006837 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006838 int status = 0;
6839
6840 dprintk("--> %s\n", __func__);
6841
Idan Kedar85541162012-08-02 11:47:10 +03006842 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6843 if (!lgp->args.layout.pages) {
6844 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006845 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006846 }
6847 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006848 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03006849
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006850 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006851 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006852 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006853
6854 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
6855 pnfs_get_layout_hdr(NFS_I(inode)->layout);
6856
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006857 task = rpc_run_task(&task_setup_data);
6858 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006859 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006860 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006861 if (status == 0)
6862 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04006863 trace_nfs4_layoutget(lgp->args.ctx,
6864 &lgp->args.range,
6865 &lgp->res.range,
6866 status);
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05006867 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
6868 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006869 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006870 rpc_put_task(task);
6871 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006872 if (status)
6873 return ERR_PTR(status);
6874 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006875}
6876
Benny Halevycbe82602011-05-22 19:52:37 +03006877static void
6878nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6879{
6880 struct nfs4_layoutreturn *lrp = calldata;
6881
6882 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006883 nfs41_setup_sequence(lrp->clp->cl_session,
6884 &lrp->args.seq_args,
6885 &lrp->res.seq_res,
6886 task);
Benny Halevycbe82602011-05-22 19:52:37 +03006887}
6888
6889static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6890{
6891 struct nfs4_layoutreturn *lrp = calldata;
6892 struct nfs_server *server;
6893
6894 dprintk("--> %s\n", __func__);
6895
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006896 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03006897 return;
6898
6899 server = NFS_SERVER(lrp->args.inode);
6900 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006901 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006902 return;
6903 }
Benny Halevycbe82602011-05-22 19:52:37 +03006904 dprintk("<-- %s\n", __func__);
6905}
6906
6907static void nfs4_layoutreturn_release(void *calldata)
6908{
6909 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006910 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006911
6912 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006913 spin_lock(&lo->plh_inode->i_lock);
6914 if (lrp->res.lrs_present)
6915 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6916 lo->plh_block_lgets--;
6917 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006918 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006919 kfree(calldata);
6920 dprintk("<-- %s\n", __func__);
6921}
6922
6923static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6924 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6925 .rpc_call_done = nfs4_layoutreturn_done,
6926 .rpc_release = nfs4_layoutreturn_release,
6927};
6928
6929int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6930{
6931 struct rpc_task *task;
6932 struct rpc_message msg = {
6933 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6934 .rpc_argp = &lrp->args,
6935 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04006936 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03006937 };
6938 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04006939 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03006940 .rpc_message = &msg,
6941 .callback_ops = &nfs4_layoutreturn_call_ops,
6942 .callback_data = lrp,
6943 };
6944 int status;
6945
6946 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006947 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006948 task = rpc_run_task(&task_setup_data);
6949 if (IS_ERR(task))
6950 return PTR_ERR(task);
6951 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04006952 trace_nfs4_layoutreturn(lrp->args.inode, status);
Benny Halevycbe82602011-05-22 19:52:37 +03006953 dprintk("<-- %s status=%d\n", __func__, status);
6954 rpc_put_task(task);
6955 return status;
6956}
6957
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006958/*
6959 * Retrieve the list of Data Server devices from the MDS.
6960 */
6961static int _nfs4_getdevicelist(struct nfs_server *server,
6962 const struct nfs_fh *fh,
6963 struct pnfs_devicelist *devlist)
6964{
6965 struct nfs4_getdevicelist_args args = {
6966 .fh = fh,
6967 .layoutclass = server->pnfs_curr_ld->id,
6968 };
6969 struct nfs4_getdevicelist_res res = {
6970 .devlist = devlist,
6971 };
6972 struct rpc_message msg = {
6973 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6974 .rpc_argp = &args,
6975 .rpc_resp = &res,
6976 };
6977 int status;
6978
6979 dprintk("--> %s\n", __func__);
6980 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6981 &res.seq_res, 0);
6982 dprintk("<-- %s status=%d\n", __func__, status);
6983 return status;
6984}
6985
6986int nfs4_proc_getdevicelist(struct nfs_server *server,
6987 const struct nfs_fh *fh,
6988 struct pnfs_devicelist *devlist)
6989{
6990 struct nfs4_exception exception = { };
6991 int err;
6992
6993 do {
6994 err = nfs4_handle_exception(server,
6995 _nfs4_getdevicelist(server, fh, devlist),
6996 &exception);
6997 } while (exception.retry);
6998
6999 dprintk("%s: err=%d, num_devs=%u\n", __func__,
7000 err, devlist->num_devs);
7001
7002 return err;
7003}
7004EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
7005
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007006static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007007_nfs4_proc_getdeviceinfo(struct nfs_server *server,
7008 struct pnfs_device *pdev,
7009 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007010{
7011 struct nfs4_getdeviceinfo_args args = {
7012 .pdev = pdev,
7013 };
7014 struct nfs4_getdeviceinfo_res res = {
7015 .pdev = pdev,
7016 };
7017 struct rpc_message msg = {
7018 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
7019 .rpc_argp = &args,
7020 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007021 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007022 };
7023 int status;
7024
7025 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00007026 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007027 dprintk("<-- %s status=%d\n", __func__, status);
7028
7029 return status;
7030}
7031
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007032int nfs4_proc_getdeviceinfo(struct nfs_server *server,
7033 struct pnfs_device *pdev,
7034 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007035{
7036 struct nfs4_exception exception = { };
7037 int err;
7038
7039 do {
7040 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007041 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007042 &exception);
7043 } while (exception.retry);
7044 return err;
7045}
7046EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
7047
Andy Adamson863a3c62011-03-23 13:27:54 +00007048static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
7049{
7050 struct nfs4_layoutcommit_data *data = calldata;
7051 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007052 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00007053
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007054 nfs41_setup_sequence(session,
7055 &data->args.seq_args,
7056 &data->res.seq_res,
7057 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00007058}
7059
7060static void
7061nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
7062{
7063 struct nfs4_layoutcommit_data *data = calldata;
7064 struct nfs_server *server = NFS_SERVER(data->args.inode);
7065
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007066 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00007067 return;
7068
7069 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007070 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
7071 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
7072 case -NFS4ERR_BADLAYOUT: /* no layout */
7073 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00007074 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007075 break;
7076 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00007077 nfs_post_op_update_inode_force_wcc(data->args.inode,
7078 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007079 break;
7080 default:
7081 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
7082 rpc_restart_call_prepare(task);
7083 return;
7084 }
7085 }
Andy Adamson863a3c62011-03-23 13:27:54 +00007086}
7087
7088static void nfs4_layoutcommit_release(void *calldata)
7089{
7090 struct nfs4_layoutcommit_data *data = calldata;
7091
Andy Adamsondb29c082011-07-30 20:52:38 -04007092 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00007093 put_rpccred(data->cred);
7094 kfree(data);
7095}
7096
7097static const struct rpc_call_ops nfs4_layoutcommit_ops = {
7098 .rpc_call_prepare = nfs4_layoutcommit_prepare,
7099 .rpc_call_done = nfs4_layoutcommit_done,
7100 .rpc_release = nfs4_layoutcommit_release,
7101};
7102
7103int
Andy Adamsonef311532011-03-12 02:58:10 -05007104nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00007105{
7106 struct rpc_message msg = {
7107 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
7108 .rpc_argp = &data->args,
7109 .rpc_resp = &data->res,
7110 .rpc_cred = data->cred,
7111 };
7112 struct rpc_task_setup task_setup_data = {
7113 .task = &data->task,
7114 .rpc_client = NFS_CLIENT(data->args.inode),
7115 .rpc_message = &msg,
7116 .callback_ops = &nfs4_layoutcommit_ops,
7117 .callback_data = data,
7118 .flags = RPC_TASK_ASYNC,
7119 };
7120 struct rpc_task *task;
7121 int status = 0;
7122
7123 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
7124 "lbw: %llu inode %lu\n",
7125 data->task.tk_pid, sync,
7126 data->args.lastbytewritten,
7127 data->args.inode->i_ino);
7128
Trond Myklebust9d12b212012-01-17 22:04:25 -05007129 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00007130 task = rpc_run_task(&task_setup_data);
7131 if (IS_ERR(task))
7132 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05007133 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00007134 goto out;
7135 status = nfs4_wait_for_completion_rpc_task(task);
7136 if (status != 0)
7137 goto out;
7138 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007139 trace_nfs4_layoutcommit(data->args.inode, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00007140out:
7141 dprintk("%s: status %d\n", __func__, status);
7142 rpc_put_task(task);
7143 return status;
7144}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007145
Andy Adamson97431202013-08-08 10:57:56 -04007146/**
7147 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
7148 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
7149 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007150static int
7151_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7152 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
7153{
7154 struct nfs41_secinfo_no_name_args args = {
7155 .style = SECINFO_STYLE_CURRENT_FH,
7156 };
7157 struct nfs4_secinfo_res res = {
7158 .flavors = flavors,
7159 };
7160 struct rpc_message msg = {
7161 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
7162 .rpc_argp = &args,
7163 .rpc_resp = &res,
7164 };
Andy Adamson97431202013-08-08 10:57:56 -04007165 return nfs4_call_sync(server->nfs_client->cl_rpcclient, server, &msg,
7166 &args.seq_args, &res.seq_res, 0);
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007167}
7168
7169static int
7170nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7171 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
7172{
7173 struct nfs4_exception exception = { };
7174 int err;
7175 do {
7176 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
7177 switch (err) {
7178 case 0:
7179 case -NFS4ERR_WRONGSEC:
7180 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007181 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007182 default:
7183 err = nfs4_handle_exception(server, err, &exception);
7184 }
7185 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007186out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007187 return err;
7188}
7189
7190static int
7191nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
7192 struct nfs_fsinfo *info)
7193{
7194 int err;
7195 struct page *page;
7196 rpc_authflavor_t flavor;
7197 struct nfs4_secinfo_flavors *flavors;
7198
7199 page = alloc_page(GFP_KERNEL);
7200 if (!page) {
7201 err = -ENOMEM;
7202 goto out;
7203 }
7204
7205 flavors = page_address(page);
7206 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
7207
7208 /*
7209 * Fall back on "guess and check" method if
7210 * the server doesn't support SECINFO_NO_NAME
7211 */
7212 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
7213 err = nfs4_find_root_sec(server, fhandle, info);
7214 goto out_freepage;
7215 }
7216 if (err)
7217 goto out_freepage;
7218
7219 flavor = nfs_find_best_sec(flavors);
7220 if (err == 0)
7221 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
7222
7223out_freepage:
7224 put_page(page);
7225 if (err == -EACCES)
7226 return -EPERM;
7227out:
7228 return err;
7229}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007230
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007231static int _nfs41_test_stateid(struct nfs_server *server,
7232 nfs4_stateid *stateid,
7233 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04007234{
7235 int status;
7236 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007237 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04007238 };
7239 struct nfs41_test_stateid_res res;
7240 struct rpc_message msg = {
7241 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
7242 .rpc_argp = &args,
7243 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007244 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04007245 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007246
Chuck Lever38527b12012-07-11 16:30:23 -04007247 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05007248 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007249 nfs4_set_sequence_privileged(&args.seq_args);
7250 status = nfs4_call_sync_sequence(server->client, server, &msg,
7251 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04007252 if (status != NFS_OK) {
7253 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04007254 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04007255 }
7256 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04007257 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04007258}
7259
Chuck Lever38527b12012-07-11 16:30:23 -04007260/**
7261 * nfs41_test_stateid - perform a TEST_STATEID operation
7262 *
7263 * @server: server / transport on which to perform the operation
7264 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007265 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04007266 *
7267 * Returns NFS_OK if the server recognizes that "stateid" is valid.
7268 * Otherwise a negative NFS4ERR value is returned if the operation
7269 * failed or the state ID is not currently valid.
7270 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007271static int nfs41_test_stateid(struct nfs_server *server,
7272 nfs4_stateid *stateid,
7273 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04007274{
7275 struct nfs4_exception exception = { };
7276 int err;
7277 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007278 err = _nfs41_test_stateid(server, stateid, cred);
Chuck Lever377e5072012-07-11 16:29:45 -04007279 if (err != -NFS4ERR_DELAY)
7280 break;
7281 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04007282 } while (exception.retry);
7283 return err;
7284}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007285
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007286struct nfs_free_stateid_data {
7287 struct nfs_server *server;
7288 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007289 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007290};
7291
7292static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
7293{
7294 struct nfs_free_stateid_data *data = calldata;
7295 nfs41_setup_sequence(nfs4_get_session(data->server),
7296 &data->args.seq_args,
7297 &data->res.seq_res,
7298 task);
7299}
7300
7301static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
7302{
7303 struct nfs_free_stateid_data *data = calldata;
7304
7305 nfs41_sequence_done(task, &data->res.seq_res);
7306
7307 switch (task->tk_status) {
7308 case -NFS4ERR_DELAY:
7309 if (nfs4_async_handle_error(task, data->server, NULL) == -EAGAIN)
7310 rpc_restart_call_prepare(task);
7311 }
7312}
7313
7314static void nfs41_free_stateid_release(void *calldata)
7315{
7316 kfree(calldata);
7317}
7318
Trond Myklebust17f26b12013-08-21 15:48:42 -04007319static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007320 .rpc_call_prepare = nfs41_free_stateid_prepare,
7321 .rpc_call_done = nfs41_free_stateid_done,
7322 .rpc_release = nfs41_free_stateid_release,
7323};
7324
7325static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
7326 nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007327 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007328 bool privileged)
7329{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007330 struct rpc_message msg = {
7331 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007332 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007333 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007334 struct rpc_task_setup task_setup = {
7335 .rpc_client = server->client,
7336 .rpc_message = &msg,
7337 .callback_ops = &nfs41_free_stateid_ops,
7338 .flags = RPC_TASK_ASYNC,
7339 };
7340 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007341
Chuck Lever38527b12012-07-11 16:30:23 -04007342 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007343 data = kmalloc(sizeof(*data), GFP_NOFS);
7344 if (!data)
7345 return ERR_PTR(-ENOMEM);
7346 data->server = server;
7347 nfs4_stateid_copy(&data->args.stateid, stateid);
7348
7349 task_setup.callback_data = data;
7350
7351 msg.rpc_argp = &data->args;
7352 msg.rpc_resp = &data->res;
7353 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
7354 if (privileged)
7355 nfs4_set_sequence_privileged(&data->args.seq_args);
7356
7357 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007358}
7359
Chuck Lever38527b12012-07-11 16:30:23 -04007360/**
7361 * nfs41_free_stateid - perform a FREE_STATEID operation
7362 *
7363 * @server: server / transport on which to perform the operation
7364 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007365 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04007366 *
7367 * Returns NFS_OK if the server freed "stateid". Otherwise a
7368 * negative NFS4ERR value is returned.
7369 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007370static int nfs41_free_stateid(struct nfs_server *server,
7371 nfs4_stateid *stateid,
7372 struct rpc_cred *cred)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007373{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007374 struct rpc_task *task;
7375 int ret;
7376
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007377 task = _nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007378 if (IS_ERR(task))
7379 return PTR_ERR(task);
7380 ret = rpc_wait_for_completion_task(task);
7381 if (!ret)
7382 ret = task->tk_status;
7383 rpc_put_task(task);
7384 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007385}
Trond Myklebust36281ca2012-03-04 18:13:56 -05007386
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007387static int nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
7388{
7389 struct rpc_task *task;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007390 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007391
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007392 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007393 nfs4_free_lock_state(server, lsp);
7394 if (IS_ERR(task))
7395 return PTR_ERR(task);
7396 rpc_put_task(task);
7397 return 0;
7398}
7399
Trond Myklebust36281ca2012-03-04 18:13:56 -05007400static bool nfs41_match_stateid(const nfs4_stateid *s1,
7401 const nfs4_stateid *s2)
7402{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007403 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007404 return false;
7405
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007406 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007407 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007408 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007409 return true;
7410
7411 return false;
7412}
7413
Andy Adamson557134a2009-04-01 09:21:53 -04007414#endif /* CONFIG_NFS_V4_1 */
7415
Trond Myklebust36281ca2012-03-04 18:13:56 -05007416static bool nfs4_match_stateid(const nfs4_stateid *s1,
7417 const nfs4_stateid *s2)
7418{
Trond Myklebustf597c532012-03-04 18:13:56 -05007419 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05007420}
7421
7422
Trond Myklebust17280172012-03-11 13:11:00 -04007423static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007424 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007425 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007426 .recover_open = nfs4_open_reclaim,
7427 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04007428 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04007429 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007430};
7431
Andy Adamson591d71c2009-04-01 09:22:47 -04007432#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007433static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007434 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
7435 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
7436 .recover_open = nfs4_open_reclaim,
7437 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05007438 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007439 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04007440 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04007441};
7442#endif /* CONFIG_NFS_V4_1 */
7443
Trond Myklebust17280172012-03-11 13:11:00 -04007444static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007445 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007446 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007447 .recover_open = nfs4_open_expired,
7448 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04007449 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007450};
7451
Andy Adamson591d71c2009-04-01 09:22:47 -04007452#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007453static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007454 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
7455 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007456 .recover_open = nfs41_open_expired,
7457 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05007458 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04007459};
7460#endif /* CONFIG_NFS_V4_1 */
7461
Trond Myklebust17280172012-03-11 13:11:00 -04007462static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007463 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04007464 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007465 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04007466};
7467
7468#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007469static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007470 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04007471 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007472 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04007473};
7474#endif
7475
Trond Myklebust97dc1352010-06-16 09:52:26 -04007476static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
7477 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04007478 .init_caps = NFS_CAP_READDIRPLUS
7479 | NFS_CAP_ATOMIC_OPEN
7480 | NFS_CAP_CHANGE_ATTR
7481 | NFS_CAP_POSIX_LOCK,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007482 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007483 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007484 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007485 .free_lock_state = nfs4_release_lockowner,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007486 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
7487 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
7488 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007489};
7490
7491#if defined(CONFIG_NFS_V4_1)
7492static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
7493 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04007494 .init_caps = NFS_CAP_READDIRPLUS
7495 | NFS_CAP_ATOMIC_OPEN
7496 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04007497 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04007498 | NFS_CAP_STATEID_NFSV41
7499 | NFS_CAP_ATOMIC_OPEN_V1,
Bryan Schumaker104287c2012-11-12 14:13:13 -05007500 .call_sync = nfs4_call_sync_sequence,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007501 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007502 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007503 .free_lock_state = nfs41_free_lock_state,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007504 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7505 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7506 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007507};
7508#endif
7509
Steve Dickson42c2c422013-05-22 12:50:38 -04007510#if defined(CONFIG_NFS_V4_2)
7511static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
7512 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04007513 .init_caps = NFS_CAP_READDIRPLUS
7514 | NFS_CAP_ATOMIC_OPEN
7515 | NFS_CAP_CHANGE_ATTR
7516 | NFS_CAP_POSIX_LOCK
7517 | NFS_CAP_STATEID_NFSV41
7518 | NFS_CAP_ATOMIC_OPEN_V1,
Steve Dickson42c2c422013-05-22 12:50:38 -04007519 .call_sync = nfs4_call_sync_sequence,
7520 .match_stateid = nfs41_match_stateid,
7521 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04007522 .free_lock_state = nfs41_free_lock_state,
Steve Dickson42c2c422013-05-22 12:50:38 -04007523 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7524 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7525 .state_renewal_ops = &nfs41_state_renewal_ops,
7526};
7527#endif
7528
Trond Myklebust97dc1352010-06-16 09:52:26 -04007529const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
7530 [0] = &nfs_v4_0_minor_ops,
7531#if defined(CONFIG_NFS_V4_1)
7532 [1] = &nfs_v4_1_minor_ops,
7533#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04007534#if defined(CONFIG_NFS_V4_2)
7535 [2] = &nfs_v4_2_minor_ops,
7536#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04007537};
7538
Trond Myklebust17f26b12013-08-21 15:48:42 -04007539static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04007540 .create = nfs_create,
7541 .lookup = nfs_lookup,
7542 .atomic_open = nfs_atomic_open,
7543 .link = nfs_link,
7544 .unlink = nfs_unlink,
7545 .symlink = nfs_symlink,
7546 .mkdir = nfs_mkdir,
7547 .rmdir = nfs_rmdir,
7548 .mknod = nfs_mknod,
7549 .rename = nfs_rename,
7550 .permission = nfs_permission,
7551 .getattr = nfs_getattr,
7552 .setattr = nfs_setattr,
7553 .getxattr = generic_getxattr,
7554 .setxattr = generic_setxattr,
7555 .listxattr = generic_listxattr,
7556 .removexattr = generic_removexattr,
7557};
7558
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08007559static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007560 .permission = nfs_permission,
7561 .getattr = nfs_getattr,
7562 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007563 .getxattr = generic_getxattr,
7564 .setxattr = generic_setxattr,
7565 .listxattr = generic_listxattr,
7566 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007567};
7568
David Howells509de812006-08-22 20:06:11 -04007569const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007570 .version = 4, /* protocol version */
7571 .dentry_ops = &nfs4_dentry_operations,
7572 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007573 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04007574 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007575 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04007576 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04007577 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007578 .getattr = nfs4_proc_getattr,
7579 .setattr = nfs4_proc_setattr,
7580 .lookup = nfs4_proc_lookup,
7581 .access = nfs4_proc_access,
7582 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007583 .create = nfs4_proc_create,
7584 .remove = nfs4_proc_remove,
7585 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04007586 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007587 .unlink_done = nfs4_proc_unlink_done,
7588 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04007589 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04007590 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04007591 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007592 .link = nfs4_proc_link,
7593 .symlink = nfs4_proc_symlink,
7594 .mkdir = nfs4_proc_mkdir,
7595 .rmdir = nfs4_proc_remove,
7596 .readdir = nfs4_proc_readdir,
7597 .mknod = nfs4_proc_mknod,
7598 .statfs = nfs4_proc_statfs,
7599 .fsinfo = nfs4_proc_fsinfo,
7600 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04007601 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007602 .decode_dirent = nfs4_decode_dirent,
7603 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04007604 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04007605 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05007606 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007607 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04007608 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04007609 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007610 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007611 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04007612 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007613 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007614 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00007615 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04007616 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04007617 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04007618 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04007619 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04007620 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00007621 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04007622 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04007623 .create_server = nfs4_create_server,
7624 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007625};
7626
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007627static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7628 .prefix = XATTR_NAME_NFSV4_ACL,
7629 .list = nfs4_xattr_list_nfs4_acl,
7630 .get = nfs4_xattr_get_nfs4_acl,
7631 .set = nfs4_xattr_set_nfs4_acl,
7632};
7633
7634const struct xattr_handler *nfs4_xattr_handlers[] = {
7635 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04007636#ifdef CONFIG_NFS_V4_SECURITY_LABEL
7637 &nfs4_xattr_nfs4_label_handler,
7638#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007639 NULL
7640};
7641
Linus Torvalds1da177e2005-04-16 15:20:36 -07007642/*
7643 * Local variables:
7644 * c-basic-offset: 8
7645 * End:
7646 */