blob: 1671e1f1fe0dac198837c93b0ec2d656d80e1ae1 [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
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400511 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500512 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400513 switch (res->sr_status) {
514 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400515 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500516 ++slot->seq_nr;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500517 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500518 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500519 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500520 if (res->sr_status_flags != 0)
521 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500522 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400523 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500524 case 1:
525 /*
526 * sr_status remains 1 if an RPC level error occurred.
527 * The server may or may not have processed the sequence
528 * operation..
529 * Mark the slot as having hosted an interrupted RPC call.
530 */
531 slot->interrupted = 1;
532 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400533 case -NFS4ERR_DELAY:
534 /* The server detected a resend of the RPC call and
535 * returned NFS4ERR_DELAY as per Section 2.10.6.2
536 * of RFC5661.
537 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500538 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400539 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500540 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500541 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400542 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500543 case -NFS4ERR_BADSLOT:
544 /*
545 * The slot id we used was probably retired. Try again
546 * using a different slot id.
547 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500548 goto retry_nowait;
549 case -NFS4ERR_SEQ_MISORDERED:
550 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500551 * Was the last operation on this sequence interrupted?
552 * If so, retry after bumping the sequence number.
553 */
554 if (interrupted) {
555 ++slot->seq_nr;
556 goto retry_nowait;
557 }
558 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500559 * Could this slot have been previously retired?
560 * If so, then the server may be expecting seq_nr = 1!
561 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500562 if (slot->seq_nr != 1) {
563 slot->seq_nr = 1;
564 goto retry_nowait;
565 }
566 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500567 case -NFS4ERR_SEQ_FALSE_RETRY:
568 ++slot->seq_nr;
569 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400570 default:
571 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500572 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400573 }
574out:
575 /* The session may be reset by one of the error handlers. */
576 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400577 nfs41_sequence_free_slot(res);
Trond Myklebust85563072012-12-11 10:31:12 -0500578 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500579retry_nowait:
580 if (rpc_restart_call_prepare(task)) {
581 task->tk_status = 0;
582 ret = 0;
583 }
584 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400585out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400586 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400587 goto out;
588 rpc_delay(task, NFS4_POLL_RETRY_MAX);
589 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400590}
591
Trond Myklebust14516c32010-07-31 14:29:06 -0400592static int nfs4_sequence_done(struct rpc_task *task,
593 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400594{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500595 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400596 return 1;
597 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400598}
599
Trond Myklebust9d12b212012-01-17 22:04:25 -0500600static void nfs41_init_sequence(struct nfs4_sequence_args *args,
601 struct nfs4_sequence_res *res, int cache_reply)
602{
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500603 args->sa_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500604 args->sa_cache_this = 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400605 args->sa_privileged = 0;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500606 if (cache_reply)
607 args->sa_cache_this = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500608 res->sr_slot = NULL;
609}
610
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400611static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
612{
613 args->sa_privileged = 1;
614}
615
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000616int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400617 struct nfs4_sequence_args *args,
618 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400619 struct rpc_task *task)
620{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400621 struct nfs4_slot *slot;
622 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400623
624 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400625 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400626 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400627 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400628
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400629 tbl = &session->fc_slot_table;
630
Trond Myklebust69d206b2012-11-22 13:21:02 -0500631 task->tk_timeout = 0;
632
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400633 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400634 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400635 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500636 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500637 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400638 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400639 }
640
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500641 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500642 if (IS_ERR(slot)) {
643 /* If out of memory, try again in 1/4 second */
644 if (slot == ERR_PTR(-ENOMEM))
645 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400646 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400647 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400648 }
649 spin_unlock(&tbl->slot_tbl_lock);
650
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500651 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400652
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500653 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
654 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400655
Benny Halevydfb4f3092010-09-24 09:17:01 -0400656 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500657 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400658 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400659 /*
660 * sr_status is only set in decode_sequence, and so will remain
661 * set to 1 if an rpc level failure occurs.
662 */
663 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400664 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400665out_success:
666 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400667 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400668out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400669 /* Privileged tasks are queued with top priority */
670 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400671 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
672 NULL, RPC_PRIORITY_PRIVILEGED);
673 else
674 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400675 spin_unlock(&tbl->slot_tbl_lock);
676 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400677}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000678EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400679
Trond Myklebust035168a2010-06-16 09:52:26 -0400680int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400681 struct nfs4_sequence_args *args,
682 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400683 struct rpc_task *task)
684{
Trond Myklebust035168a2010-06-16 09:52:26 -0400685 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400686 int ret = 0;
687
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400688 if (session == NULL) {
689 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400690 goto out;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400691 }
Trond Myklebust035168a2010-06-16 09:52:26 -0400692
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500693 dprintk("--> %s clp %p session %p sr_slot %d\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400694 __func__, session->clp, session, res->sr_slot ?
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500695 res->sr_slot->slot_nr : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400696
Trond Myklebust9d12b212012-01-17 22:04:25 -0500697 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400698out:
699 dprintk("<-- %s status=%d\n", __func__, ret);
700 return ret;
701}
702
703struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400704 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400705 struct nfs4_sequence_args *seq_args;
706 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400707};
708
709static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
710{
711 struct nfs41_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400712 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400713
Trond Myklebust035168a2010-06-16 09:52:26 -0400714 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
715
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400716 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400717}
718
Andy Adamson69ab40c2009-04-01 09:22:19 -0400719static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
720{
721 struct nfs41_call_sync_data *data = calldata;
722
Trond Myklebust14516c32010-07-31 14:29:06 -0400723 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400724}
725
Trond Myklebust17280172012-03-11 13:11:00 -0400726static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400727 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400728 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400729};
730
Bryan Schumaker7c513052011-03-24 17:12:24 +0000731static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
732 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400733 struct rpc_message *msg,
734 struct nfs4_sequence_args *args,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400735 struct nfs4_sequence_res *res)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400736{
737 int ret;
738 struct rpc_task *task;
739 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400740 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400741 .seq_args = args,
742 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400743 };
744 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000745 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400746 .rpc_message = msg,
747 .callback_ops = &nfs41_call_sync_ops,
748 .callback_data = &data
749 };
750
Andy Adamsonce5039c2009-04-01 09:22:13 -0400751 task = rpc_run_task(&task_setup);
752 if (IS_ERR(task))
753 ret = PTR_ERR(task);
754 else {
755 ret = task->tk_status;
756 rpc_put_task(task);
757 }
758 return ret;
759}
760
Trond Myklebustdf896452010-06-16 09:52:26 -0400761#else
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400762static
Trond Myklebust9d12b212012-01-17 22:04:25 -0500763void nfs41_init_sequence(struct nfs4_sequence_args *args,
764 struct nfs4_sequence_res *res, int cache_reply)
765{
766}
767
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400768static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
769{
770}
771
772
Trond Myklebust14516c32010-07-31 14:29:06 -0400773static int nfs4_sequence_done(struct rpc_task *task,
774 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400775{
Trond Myklebust14516c32010-07-31 14:29:06 -0400776 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400777}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400778#endif /* CONFIG_NFS_V4_1 */
779
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400780static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000781int _nfs4_call_sync(struct rpc_clnt *clnt,
782 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400783 struct rpc_message *msg,
784 struct nfs4_sequence_args *args,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400785 struct nfs4_sequence_res *res)
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400786{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000787 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400788}
789
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400790static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000791int nfs4_call_sync(struct rpc_clnt *clnt,
792 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000793 struct rpc_message *msg,
794 struct nfs4_sequence_args *args,
795 struct nfs4_sequence_res *res,
796 int cache_reply)
797{
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400798 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000799 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400800 args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000801}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400802
Trond Myklebust38478b22006-05-25 01:40:57 -0400803static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
Trond Myklebust38478b22006-05-25 01:40:57 -0400805 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Trond Myklebust38478b22006-05-25 01:40:57 -0400807 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400808 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700809 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400810 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700811 dir->i_version = cinfo->after;
David Howellsde242c02012-12-20 21:52:38 +0000812 nfs_fscache_invalidate(dir);
Trond Myklebust38478b22006-05-25 01:40:57 -0400813 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814}
815
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100816struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400817 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100818 struct nfs_openargs o_arg;
819 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100820 struct nfs_open_confirmargs c_arg;
821 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500822 struct nfs4_string owner_name;
823 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100824 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400825 struct nfs4_label *f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100826 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400827 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100828 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400829 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100830 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100831 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400832 unsigned int rpc_done : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -0400833 unsigned int is_recover : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100834 int rpc_status;
835 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100836};
837
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400838static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
839 int err, struct nfs4_exception *exception)
840{
841 if (err != -EINVAL)
842 return false;
843 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
844 return false;
845 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
846 exception->retry = 1;
847 return true;
848}
849
850static enum open_claim_type4
851nfs4_map_atomic_open_claim(struct nfs_server *server,
852 enum open_claim_type4 claim)
853{
854 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
855 return claim;
856 switch (claim) {
857 default:
858 return claim;
859 case NFS4_OPEN_CLAIM_FH:
860 return NFS4_OPEN_CLAIM_NULL;
861 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
862 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
863 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
864 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
865 }
866}
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400867
868static void nfs4_init_opendata_res(struct nfs4_opendata *p)
869{
870 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400871 p->o_res.f_label = p->f_label;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400872 p->o_res.seqid = p->o_arg.seqid;
873 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400874 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400875 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400876 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500877 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400878}
879
Al Viro82a2c1b2011-06-22 18:30:55 -0400880static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500881 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400882 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -0400883 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400884 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400885 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100886{
Al Viro82a2c1b2011-06-22 18:30:55 -0400887 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100888 struct inode *dir = parent->d_inode;
889 struct nfs_server *server = NFS_SERVER(dir);
890 struct nfs4_opendata *p;
891
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400892 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100893 if (p == NULL)
894 goto err;
David Quigley14c43f72013-05-22 12:50:43 -0400895
896 p->f_label = nfs4_label_alloc(server, gfp_mask);
897 if (IS_ERR(p->f_label))
898 goto err_free_p;
899
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400900 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100901 if (p->o_arg.seqid == NULL)
David Quigley14c43f72013-05-22 12:50:43 -0400902 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -0400903 nfs_sb_active(dentry->d_sb);
904 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100905 p->dir = parent;
906 p->owner = sp;
907 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500908 p->o_arg.open_flags = flags;
909 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700910 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
911 * will return permission denied for all bits until close */
912 if (!(flags & O_EXCL)) {
913 /* ask server to check for all possible rights as results
914 * are cached */
915 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
916 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700917 }
David Howells7539bba2006-08-22 20:06:09 -0400918 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400919 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
920 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400921 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100922 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -0400923 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -0400924 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
David Quigley1775fd32013-05-22 12:50:42 -0400925 p->o_arg.label = label;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400926 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
927 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400928 case NFS4_OPEN_CLAIM_NULL:
929 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
930 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
931 p->o_arg.fh = NFS_FH(dir);
932 break;
933 case NFS4_OPEN_CLAIM_PREVIOUS:
934 case NFS4_OPEN_CLAIM_FH:
935 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
936 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
937 p->o_arg.fh = NFS_FH(dentry->d_inode);
938 }
Trond Myklebust536e43d2012-01-17 22:04:26 -0500939 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -0400940 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400941
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100942 p->o_arg.u.attrs = &p->attrs;
943 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500944
945 verf[0] = jiffies;
946 verf[1] = current->pid;
947 memcpy(p->o_arg.u.verifier.data, verf,
948 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100949 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100950 p->c_arg.fh = &p->o_res.fh;
951 p->c_arg.stateid = &p->o_res.stateid;
952 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400953 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400954 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100955 return p;
David Quigley14c43f72013-05-22 12:50:43 -0400956
957err_free_label:
958 nfs4_label_free(p->f_label);
959err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100960 kfree(p);
961err:
962 dput(parent);
963 return NULL;
964}
965
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400966static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100967{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400968 struct nfs4_opendata *p = container_of(kref,
969 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400970 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400971
972 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400973 if (p->state != NULL)
974 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400975 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -0400976
977 nfs4_label_free(p->f_label);
978
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400979 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400980 dput(p->dentry);
981 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500982 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400983 kfree(p);
984}
985
986static void nfs4_opendata_put(struct nfs4_opendata *p)
987{
988 if (p != NULL)
989 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100990}
991
Trond Myklebust06f814a2006-01-03 09:55:07 +0100992static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
993{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100994 int ret;
995
Trond Myklebust06f814a2006-01-03 09:55:07 +0100996 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100997 return ret;
998}
999
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001000static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001001{
1002 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001003
Trond Myklebust536e43d2012-01-17 22:04:26 -05001004 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001005 goto out;
1006 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001007 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001008 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1009 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001010 break;
1011 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001012 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1013 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001014 break;
1015 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001016 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1017 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001018 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001019out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001020 return ret;
1021}
1022
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001023static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001024{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001025 if (delegation == NULL)
1026 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001027 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001028 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -05001029 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -04001030 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001031 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1032 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -05001033 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001034 return 1;
1035}
1036
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001037static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001038{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001039 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001040 case FMODE_WRITE:
1041 state->n_wronly++;
1042 break;
1043 case FMODE_READ:
1044 state->n_rdonly++;
1045 break;
1046 case FMODE_READ|FMODE_WRITE:
1047 state->n_rdwr++;
1048 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001049 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001050}
1051
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001052static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001053{
1054 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001055 nfs4_stateid_copy(&state->stateid, stateid);
1056 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust92b40e92013-04-20 01:25:45 -04001057 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001058 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001059 case FMODE_READ:
1060 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1061 break;
1062 case FMODE_WRITE:
1063 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1064 break;
1065 case FMODE_READ|FMODE_WRITE:
1066 set_bit(NFS_O_RDWR_STATE, &state->flags);
1067 }
1068}
1069
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001070static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001071{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001072 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001073 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001074 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001075}
1076
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001077static 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 -07001078{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001079 /*
1080 * Protect the call to nfs4_state_set_mode_locked and
1081 * serialise the stateid update
1082 */
1083 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001084 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001085 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001086 set_bit(NFS_DELEGATED_STATE, &state->flags);
1087 }
1088 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001089 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001090 write_sequnlock(&state->seqlock);
1091 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001092 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001093 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094}
1095
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001096static 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 -05001097{
1098 struct nfs_inode *nfsi = NFS_I(state->inode);
1099 struct nfs_delegation *deleg_cur;
1100 int ret = 0;
1101
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001102 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001103
1104 rcu_read_lock();
1105 deleg_cur = rcu_dereference(nfsi->delegation);
1106 if (deleg_cur == NULL)
1107 goto no_delegation;
1108
1109 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001110 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001111 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001112 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001113 goto no_delegation_unlock;
1114
1115 if (delegation == NULL)
1116 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001117 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001118 goto no_delegation_unlock;
1119
Trond Myklebustb7391f42008-12-23 15:21:52 -05001120 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001121 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001122 ret = 1;
1123no_delegation_unlock:
1124 spin_unlock(&deleg_cur->lock);
1125no_delegation:
1126 rcu_read_unlock();
1127
1128 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001129 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001130 ret = 1;
1131 }
1132
1133 return ret;
1134}
1135
1136
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001137static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001138{
1139 struct nfs_delegation *delegation;
1140
1141 rcu_read_lock();
1142 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001143 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001144 rcu_read_unlock();
1145 return;
1146 }
1147 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001148 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001149}
1150
Trond Myklebust6ee41262007-07-08 14:11:36 -04001151static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001152{
1153 struct nfs4_state *state = opendata->state;
1154 struct nfs_inode *nfsi = NFS_I(state->inode);
1155 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001156 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001157 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001158 nfs4_stateid stateid;
1159 int ret = -EAGAIN;
1160
Trond Myklebustaac00a82007-07-05 19:02:21 -04001161 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001162 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001163 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001164 if (can_open_cached(state, fmode, open_mode)) {
1165 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001166 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001167 goto out_return_state;
1168 }
1169 spin_unlock(&state->owner->so_lock);
1170 }
Trond Myklebust34310432008-12-23 15:21:38 -05001171 rcu_read_lock();
1172 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001173 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001174 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001175 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001176 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001177 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001178 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001179 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001180 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001181 if (!opendata->is_recover) {
1182 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1183 if (ret != 0)
1184 goto out;
1185 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001186 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001187
1188 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001189 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001190 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001191 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001192out:
1193 return ERR_PTR(ret);
1194out_return_state:
1195 atomic_inc(&state->count);
1196 return state;
1197}
1198
Andy Adamsone23008e2012-10-02 21:07:32 -04001199static void
1200nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1201{
1202 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1203 struct nfs_delegation *delegation;
1204 int delegation_flags = 0;
1205
1206 rcu_read_lock();
1207 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1208 if (delegation)
1209 delegation_flags = delegation->flags;
1210 rcu_read_unlock();
1211 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1212 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1213 "returning a delegation for "
1214 "OPEN(CLAIM_DELEGATE_CUR)\n",
1215 clp->cl_hostname);
1216 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1217 nfs_inode_set_delegation(state->inode,
1218 data->owner->so_cred,
1219 &data->o_res);
1220 else
1221 nfs_inode_reclaim_delegation(state->inode,
1222 data->owner->so_cred,
1223 &data->o_res);
1224}
1225
1226/*
1227 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1228 * and update the nfs4_state.
1229 */
1230static struct nfs4_state *
1231_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1232{
1233 struct inode *inode = data->state->inode;
1234 struct nfs4_state *state = data->state;
1235 int ret;
1236
1237 if (!data->rpc_done) {
1238 ret = data->rpc_status;
1239 goto err;
1240 }
1241
1242 ret = -ESTALE;
1243 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1244 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1245 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1246 goto err;
1247
1248 ret = -ENOMEM;
1249 state = nfs4_get_open_state(inode, data->owner);
1250 if (state == NULL)
1251 goto err;
1252
1253 ret = nfs_refresh_inode(inode, &data->f_attr);
1254 if (ret)
1255 goto err;
1256
David Quigleyaa9c2662013-05-22 12:50:44 -04001257 nfs_setsecurity(inode, &data->f_attr, data->f_label);
1258
Andy Adamsone23008e2012-10-02 21:07:32 -04001259 if (data->o_res.delegation_type != 0)
1260 nfs4_opendata_check_deleg(data, state);
1261 update_open_stateid(state, &data->o_res.stateid, NULL,
1262 data->o_arg.fmode);
1263
1264 return state;
1265err:
1266 return ERR_PTR(ret);
1267
1268}
1269
1270static struct nfs4_state *
1271_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001272{
1273 struct inode *inode;
1274 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001275 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001276
Trond Myklebustaac00a82007-07-05 19:02:21 -04001277 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001278 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001279 goto out;
1280 }
1281
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001282 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001283 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001284 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001285 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001286 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001287 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001288 goto err;
1289 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001290 state = nfs4_get_open_state(inode, data->owner);
1291 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001292 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001293 if (data->o_res.delegation_type != 0)
1294 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001295 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001296 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001297 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001298out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001299 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001300 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001301err_put_inode:
1302 iput(inode);
1303err:
1304 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001305}
1306
Andy Adamsone23008e2012-10-02 21:07:32 -04001307static struct nfs4_state *
1308nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1309{
1310 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1311 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1312 return _nfs4_opendata_to_nfs4_state(data);
1313}
1314
Trond Myklebust864472e2006-01-03 09:55:15 +01001315static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1316{
1317 struct nfs_inode *nfsi = NFS_I(state->inode);
1318 struct nfs_open_context *ctx;
1319
1320 spin_lock(&state->inode->i_lock);
1321 list_for_each_entry(ctx, &nfsi->open_files, list) {
1322 if (ctx->state != state)
1323 continue;
1324 get_nfs_open_context(ctx);
1325 spin_unlock(&state->inode->i_lock);
1326 return ctx;
1327 }
1328 spin_unlock(&state->inode->i_lock);
1329 return ERR_PTR(-ENOENT);
1330}
1331
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001332static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1333 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001334{
1335 struct nfs4_opendata *opendata;
1336
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001337 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001338 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001339 if (opendata == NULL)
1340 return ERR_PTR(-ENOMEM);
1341 opendata->state = state;
1342 atomic_inc(&state->count);
1343 return opendata;
1344}
1345
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001346static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001347{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001348 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001349 int ret;
1350
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001351 opendata->o_arg.open_flags = 0;
1352 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001353 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1354 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1355 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001356 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001357 if (ret != 0)
1358 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001359 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001360 if (IS_ERR(newstate))
1361 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001362 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001363 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001364 return 0;
1365}
1366
1367static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1368{
Trond Myklebust864472e2006-01-03 09:55:15 +01001369 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001370 int ret;
1371
1372 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001373 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001374 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001375 smp_rmb();
1376 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001377 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001378 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001379 if (ret != 0)
1380 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001381 if (newstate != state)
1382 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001383 }
1384 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001385 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001386 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001387 if (ret != 0)
1388 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001389 if (newstate != state)
1390 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001391 }
1392 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001393 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001394 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001395 if (ret != 0)
1396 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001397 if (newstate != state)
1398 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001399 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001400 /*
1401 * We may have performed cached opens for all three recoveries.
1402 * Check if we need to update the current stateid.
1403 */
1404 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001405 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001406 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001407 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001408 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001409 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001410 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001411 return 0;
1412}
1413
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414/*
1415 * OPEN_RECLAIM:
1416 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001418static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001420 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001421 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001422 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 int status;
1424
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001425 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1426 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001427 if (IS_ERR(opendata))
1428 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001429 rcu_read_lock();
1430 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001431 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001432 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001433 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001434 opendata->o_arg.u.delegation_type = delegation_type;
1435 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001436 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 return status;
1438}
1439
Trond Myklebust539cd032007-06-05 11:46:42 -04001440static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
1442 struct nfs_server *server = NFS_SERVER(state->inode);
1443 struct nfs4_exception exception = { };
1444 int err;
1445 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001446 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001447 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001448 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1449 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001450 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001451 break;
1452 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 } while (exception.retry);
1454 return err;
1455}
1456
Trond Myklebust864472e2006-01-03 09:55:15 +01001457static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1458{
1459 struct nfs_open_context *ctx;
1460 int ret;
1461
1462 ctx = nfs4_state_find_open_context(state);
1463 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001464 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001465 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001466 put_nfs_open_context(ctx);
1467 return ret;
1468}
1469
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001470static 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 -07001471{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001472 switch (err) {
1473 default:
1474 printk(KERN_ERR "NFS: %s: unhandled error "
1475 "%d.\n", __func__, err);
1476 case 0:
1477 case -ENOENT:
1478 case -ESTALE:
1479 break;
1480 case -NFS4ERR_BADSESSION:
1481 case -NFS4ERR_BADSLOT:
1482 case -NFS4ERR_BAD_HIGH_SLOT:
1483 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1484 case -NFS4ERR_DEADSESSION:
1485 set_bit(NFS_DELEGATED_STATE, &state->flags);
1486 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1487 return -EAGAIN;
1488 case -NFS4ERR_STALE_CLIENTID:
1489 case -NFS4ERR_STALE_STATEID:
1490 set_bit(NFS_DELEGATED_STATE, &state->flags);
1491 case -NFS4ERR_EXPIRED:
1492 /* Don't recall a delegation if it was lost */
1493 nfs4_schedule_lease_recovery(server->nfs_client);
1494 return -EAGAIN;
1495 case -NFS4ERR_DELEG_REVOKED:
1496 case -NFS4ERR_ADMIN_REVOKED:
1497 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001498 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001499 nfs_inode_find_state_and_recover(state->inode,
1500 stateid);
1501 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001502 return 0;
1503 case -NFS4ERR_DELAY:
1504 case -NFS4ERR_GRACE:
1505 set_bit(NFS_DELEGATED_STATE, &state->flags);
1506 ssleep(1);
1507 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001508 case -ENOMEM:
1509 case -NFS4ERR_DENIED:
1510 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1511 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 return err;
1514}
1515
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001516int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1517{
1518 struct nfs_server *server = NFS_SERVER(state->inode);
1519 struct nfs4_opendata *opendata;
1520 int err;
1521
1522 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1523 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1524 if (IS_ERR(opendata))
1525 return PTR_ERR(opendata);
1526 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1527 err = nfs4_open_recover(opendata, state);
1528 nfs4_opendata_put(opendata);
1529 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1530}
1531
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001532static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1533{
1534 struct nfs4_opendata *data = calldata;
1535
1536 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001537 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001538 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001539 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001540 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001541 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001542 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001543}
1544
1545static void nfs4_open_confirm_release(void *calldata)
1546{
1547 struct nfs4_opendata *data = calldata;
1548 struct nfs4_state *state = NULL;
1549
1550 /* If this request hasn't been cancelled, do nothing */
1551 if (data->cancelled == 0)
1552 goto out_free;
1553 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001554 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001555 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001556 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001557 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001558 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001559out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001560 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001561}
1562
1563static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001564 .rpc_call_done = nfs4_open_confirm_done,
1565 .rpc_release = nfs4_open_confirm_release,
1566};
1567
1568/*
1569 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1570 */
1571static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1572{
1573 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1574 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001575 struct rpc_message msg = {
1576 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1577 .rpc_argp = &data->c_arg,
1578 .rpc_resp = &data->c_res,
1579 .rpc_cred = data->owner->so_cred,
1580 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001581 struct rpc_task_setup task_setup_data = {
1582 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001583 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001584 .callback_ops = &nfs4_open_confirm_ops,
1585 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001586 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001587 .flags = RPC_TASK_ASYNC,
1588 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 int status;
1590
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001591 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001592 data->rpc_done = 0;
1593 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001594 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001595 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001596 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001597 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001598 status = nfs4_wait_for_completion_rpc_task(task);
1599 if (status != 0) {
1600 data->cancelled = 1;
1601 smp_wmb();
1602 } else
1603 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001604 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 return status;
1606}
1607
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001608static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001610 struct nfs4_opendata *data = calldata;
1611 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001612 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001613
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001614 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001615 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001616 /*
1617 * Check if we still need to send an OPEN call, or if we can use
1618 * a delegation instead.
1619 */
1620 if (data->state != NULL) {
1621 struct nfs_delegation *delegation;
1622
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001623 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001624 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001625 rcu_read_lock();
1626 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001627 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
Trond Myklebustcd4c9be2013-04-23 14:46:25 -04001628 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
Trond Myklebust652f89f2011-12-09 19:05:58 -05001629 can_open_delegated(delegation, data->o_arg.fmode))
1630 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001631 rcu_read_unlock();
1632 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001633 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04001634 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust8188df12013-04-23 14:31:19 -04001635 switch (data->o_arg.claim) {
1636 case NFS4_OPEN_CLAIM_PREVIOUS:
1637 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1638 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001639 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04001640 case NFS4_OPEN_CLAIM_FH:
1641 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001642 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1643 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001644 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001645 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001646 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001647 &data->o_res.seq_res,
1648 task) != 0)
1649 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04001650
1651 /* Set the create mode (note dependency on the session type) */
1652 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1653 if (data->o_arg.open_flags & O_EXCL) {
1654 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1655 if (nfs4_has_persistent_session(clp))
1656 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1657 else if (clp->cl_mvops->minor_version > 0)
1658 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1659 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04001660 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001661unlock_no_action:
1662 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001663out_no_action:
1664 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001665out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001666 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001667}
1668
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001669static void nfs4_open_done(struct rpc_task *task, void *calldata)
1670{
1671 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001673 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001674
Trond Myklebust14516c32010-07-31 14:29:06 -04001675 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1676 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001677
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001678 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001679 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1680 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001681 case S_IFREG:
1682 break;
1683 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001684 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001685 break;
1686 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001687 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001688 break;
1689 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001690 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001691 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001692 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001693 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001694 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1695 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001696 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001697 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001698}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001699
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001700static void nfs4_open_release(void *calldata)
1701{
1702 struct nfs4_opendata *data = calldata;
1703 struct nfs4_state *state = NULL;
1704
1705 /* If this request hasn't been cancelled, do nothing */
1706 if (data->cancelled == 0)
1707 goto out_free;
1708 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001709 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001710 goto out_free;
1711 /* In case we need an open_confirm, no cleanup! */
1712 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1713 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001714 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001715 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001716 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001717out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001718 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001719}
1720
1721static const struct rpc_call_ops nfs4_open_ops = {
1722 .rpc_call_prepare = nfs4_open_prepare,
1723 .rpc_call_done = nfs4_open_done,
1724 .rpc_release = nfs4_open_release,
1725};
1726
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001727static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001728{
1729 struct inode *dir = data->dir->d_inode;
1730 struct nfs_server *server = NFS_SERVER(dir);
1731 struct nfs_openargs *o_arg = &data->o_arg;
1732 struct nfs_openres *o_res = &data->o_res;
1733 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001734 struct rpc_message msg = {
1735 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1736 .rpc_argp = o_arg,
1737 .rpc_resp = o_res,
1738 .rpc_cred = data->owner->so_cred,
1739 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001740 struct rpc_task_setup task_setup_data = {
1741 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001742 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001743 .callback_ops = &nfs4_open_ops,
1744 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001745 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001746 .flags = RPC_TASK_ASYNC,
1747 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001748 int status;
1749
Trond Myklebust9d12b212012-01-17 22:04:25 -05001750 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001751 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001752 data->rpc_done = 0;
1753 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001754 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001755 data->is_recover = 0;
1756 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001757 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001758 data->is_recover = 1;
1759 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04001760 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001761 if (IS_ERR(task))
1762 return PTR_ERR(task);
1763 status = nfs4_wait_for_completion_rpc_task(task);
1764 if (status != 0) {
1765 data->cancelled = 1;
1766 smp_wmb();
1767 } else
1768 status = data->rpc_status;
1769 rpc_put_task(task);
1770
1771 return status;
1772}
1773
1774static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1775{
1776 struct inode *dir = data->dir->d_inode;
1777 struct nfs_openres *o_res = &data->o_res;
1778 int status;
1779
1780 status = nfs4_run_open_task(data, 1);
1781 if (status != 0 || !data->rpc_done)
1782 return status;
1783
Trond Myklebust6926afd2012-01-07 13:22:46 -05001784 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1785
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001786 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1787 status = _nfs4_proc_open_confirm(data);
1788 if (status != 0)
1789 return status;
1790 }
1791
1792 return status;
1793}
1794
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001795static int nfs4_opendata_access(struct rpc_cred *cred,
1796 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001797 struct nfs4_state *state, fmode_t fmode,
1798 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001799{
1800 struct nfs_access_entry cache;
1801 u32 mask;
1802
1803 /* access call failed or for some reason the server doesn't
1804 * support any access modes -- defer access call until later */
1805 if (opendata->o_res.access_supported == 0)
1806 return 0;
1807
1808 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001809 /* don't check MAY_WRITE - a newly created file may not have
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001810 * write mode bits, but POSIX allows the creating process to write.
1811 * use openflags to check for exec, because fmode won't
1812 * always have FMODE_EXEC set when file open for exec. */
1813 if (openflags & __FMODE_EXEC) {
1814 /* ONLY check for exec rights */
1815 mask = MAY_EXEC;
1816 } else if (fmode & FMODE_READ)
1817 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001818
1819 cache.cred = cred;
1820 cache.jiffies = jiffies;
1821 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1822 nfs_access_add_cache(state->inode, &cache);
1823
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001824 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001825 return 0;
1826
1827 /* even though OPEN succeeded, access is denied. Close the file */
1828 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001829 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001830}
1831
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001832/*
1833 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1834 */
1835static int _nfs4_proc_open(struct nfs4_opendata *data)
1836{
1837 struct inode *dir = data->dir->d_inode;
1838 struct nfs_server *server = NFS_SERVER(dir);
1839 struct nfs_openargs *o_arg = &data->o_arg;
1840 struct nfs_openres *o_res = &data->o_res;
1841 int status;
1842
1843 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001844 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001845 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001846 if (status != 0) {
1847 if (status == -NFS4ERR_BADNAME &&
1848 !(o_arg->open_flags & O_CREAT))
1849 return -ENOENT;
1850 return status;
1851 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001852
Trond Myklebust6926afd2012-01-07 13:22:46 -05001853 nfs_fattr_map_and_free_names(server, &data->f_attr);
1854
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001855 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001856 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001857 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1858 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001860 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001862 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 }
1864 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
David Quigley1775fd32013-05-22 12:50:42 -04001865 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001866 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867}
1868
Andy Adamsond83217c2011-03-01 01:34:17 +00001869static int nfs4_recover_expired_lease(struct nfs_server *server)
1870{
1871 return nfs4_client_recover_expired_lease(server->nfs_client);
1872}
1873
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874/*
1875 * OPEN_EXPIRED:
1876 * reclaim state on the server after a network partition.
1877 * Assumes caller holds the appropriate lock
1878 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001879static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001881 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001882 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001884 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001885 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001886 if (IS_ERR(opendata))
1887 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001888 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001889 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001890 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001891 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001892 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893}
1894
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001895static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001896{
Trond Myklebust539cd032007-06-05 11:46:42 -04001897 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001898 struct nfs4_exception exception = { };
1899 int err;
1900
1901 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001902 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001903 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001904 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1905 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001906 switch (err) {
1907 default:
1908 goto out;
1909 case -NFS4ERR_GRACE:
1910 case -NFS4ERR_DELAY:
1911 nfs4_handle_exception(server, err, &exception);
1912 err = 0;
1913 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001914 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001915out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001916 return err;
1917}
1918
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1920{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001922 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
Trond Myklebust864472e2006-01-03 09:55:15 +01001924 ctx = nfs4_state_find_open_context(state);
1925 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001926 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001927 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001928 put_nfs_open_context(ctx);
1929 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930}
1931
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001932#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001933static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001934{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001935 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001936 nfs4_stateid *stateid = &state->stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001937 struct nfs_delegation *delegation;
1938 struct rpc_cred *cred = NULL;
1939 int status = -NFS4ERR_BAD_STATEID;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001940
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001941 /* If a state reset has been done, test_stateid is unneeded */
1942 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1943 return;
1944
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001945 /* Get the delegation credential for use by test/free_stateid */
1946 rcu_read_lock();
1947 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1948 if (delegation != NULL &&
1949 nfs4_stateid_match(&delegation->stateid, stateid)) {
1950 cred = get_rpccred(delegation->cred);
1951 rcu_read_unlock();
1952 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04001953 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001954 } else
1955 rcu_read_unlock();
1956
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001957 if (status != NFS_OK) {
1958 /* Free the stateid unless the server explicitly
1959 * informs us the stateid is unrecognized. */
1960 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001961 nfs41_free_stateid(server, stateid, cred);
Bryan Schumaker69388672012-09-26 15:25:52 -04001962 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001963
Bryan Schumaker69388672012-09-26 15:25:52 -04001964 write_seqlock(&state->seqlock);
1965 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1966 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001967 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1968 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001969
1970 if (cred != NULL)
1971 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001972}
1973
1974/**
1975 * nfs41_check_open_stateid - possibly free an open stateid
1976 *
1977 * @state: NFSv4 state for an inode
1978 *
1979 * Returns NFS_OK if recovery for this stateid is now finished.
1980 * Otherwise a negative NFS4ERR value is returned.
1981 */
1982static int nfs41_check_open_stateid(struct nfs4_state *state)
1983{
1984 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001985 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001986 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001987 int status;
1988
1989 /* If a state reset has been done, test_stateid is unneeded */
1990 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1991 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1992 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1993 return -NFS4ERR_BAD_STATEID;
1994
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001995 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04001996 trace_nfs4_test_open_stateid(state, NULL, status);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001997 if (status != NFS_OK) {
1998 /* Free the stateid unless the server explicitly
1999 * informs us the stateid is unrecognized. */
2000 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002001 nfs41_free_stateid(server, stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002002
2003 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2004 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2005 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002006 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002007 }
2008 return status;
2009}
2010
2011static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2012{
Chuck Levereb64cf92012-07-11 16:30:05 -04002013 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002014
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002015 nfs41_clear_delegation_stateid(state);
2016 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002017 if (status != NFS_OK)
2018 status = nfs4_open_expired(sp, state);
2019 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002020}
2021#endif
2022
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002024 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2025 * fields corresponding to attributes that were used to store the verifier.
2026 * Make sure we clobber those fields in the later setattr call
2027 */
2028static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2029{
2030 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2031 !(sattr->ia_valid & ATTR_ATIME_SET))
2032 sattr->ia_valid |= ATTR_ATIME;
2033
2034 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2035 !(sattr->ia_valid & ATTR_MTIME_SET))
2036 sattr->ia_valid |= ATTR_MTIME;
2037}
2038
Trond Myklebustc21443c2013-02-07 14:26:21 -05002039static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2040 fmode_t fmode,
2041 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002042 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002043{
2044 struct nfs4_state_owner *sp = opendata->owner;
2045 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002046 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002047 struct nfs4_state *state;
2048 unsigned int seq;
2049 int ret;
2050
2051 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2052
2053 ret = _nfs4_proc_open(opendata);
2054 if (ret != 0)
2055 goto out;
2056
2057 state = nfs4_opendata_to_nfs4_state(opendata);
2058 ret = PTR_ERR(state);
2059 if (IS_ERR(state))
2060 goto out;
2061 if (server->caps & NFS_CAP_POSIX_LOCK)
2062 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2063
Trond Myklebust275bb302013-05-29 13:11:28 -04002064 dentry = opendata->dentry;
2065 if (dentry->d_inode == NULL) {
2066 /* FIXME: Is this d_drop() ever needed? */
2067 d_drop(dentry);
2068 dentry = d_add_unique(dentry, igrab(state->inode));
2069 if (dentry == NULL) {
2070 dentry = opendata->dentry;
2071 } else if (dentry != ctx->dentry) {
2072 dput(ctx->dentry);
2073 ctx->dentry = dget(dentry);
2074 }
2075 nfs_set_verifier(dentry,
2076 nfs_save_change_attribute(opendata->dir->d_inode));
2077 }
2078
Trond Myklebustc21443c2013-02-07 14:26:21 -05002079 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2080 if (ret != 0)
2081 goto out;
2082
Trond Myklebust3efb9722013-05-29 13:17:04 -04002083 ctx->state = state;
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002084 if (dentry->d_inode == state->inode) {
2085 nfs_inode_attach_open_context(ctx);
2086 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2087 nfs4_schedule_stateid_recovery(server, state);
2088 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002089out:
2090 return ret;
2091}
2092
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002093/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002094 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 */
Andy Adamson82be4172012-05-23 05:02:35 -04002096static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002097 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002098 int flags,
2099 struct iattr *sattr,
Trond Myklebust959d9212013-06-28 16:29:51 -04002100 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101{
2102 struct nfs4_state_owner *sp;
2103 struct nfs4_state *state = NULL;
2104 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002105 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002106 struct dentry *dentry = ctx->dentry;
2107 struct rpc_cred *cred = ctx->cred;
2108 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2109 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002110 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002111 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002112 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
2114 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002116 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2117 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2119 goto out_err;
2120 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002121 status = nfs4_recover_expired_lease(server);
2122 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002123 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04002124 if (dentry->d_inode != NULL)
2125 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002126 status = -ENOMEM;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002127 if (dentry->d_inode)
2128 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002129 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002130 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002131 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002132 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
David Quigley14c43f72013-05-22 12:50:43 -04002134 if (label) {
2135 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2136 if (IS_ERR(olabel)) {
2137 status = PTR_ERR(olabel);
2138 goto err_opendata_put;
2139 }
2140 }
2141
Andy Adamson82be4172012-05-23 05:02:35 -04002142 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2143 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2144 if (!opendata->f_attr.mdsthreshold)
David Quigley14c43f72013-05-22 12:50:43 -04002145 goto err_free_label;
Trond Myklebust1549210f2012-06-05 09:16:47 -04002146 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002147 }
Al Viro82a2c1b2011-06-22 18:30:55 -04002148 if (dentry->d_inode != NULL)
2149 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002150
Trond Myklebust3efb9722013-05-29 13:17:04 -04002151 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002152 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002153 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002154 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002155
Trond Myklebust549b19c2013-04-16 18:42:34 -04002156 if ((opendata->o_arg.open_flags & O_EXCL) &&
2157 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002158 nfs4_exclusive_attrset(opendata, sattr);
2159
2160 nfs_fattr_init(opendata->o_res.f_attr);
2161 status = nfs4_do_setattr(state->inode, cred,
2162 opendata->o_res.f_attr, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002163 state, label, olabel);
2164 if (status == 0) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002165 nfs_setattr_update_inode(state->inode, sattr);
David Quigley1775fd32013-05-22 12:50:42 -04002166 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
David Quigleyaa9c2662013-05-22 12:50:44 -04002167 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
David Quigley1775fd32013-05-22 12:50:42 -04002168 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002169 }
Andy Adamson82be4172012-05-23 05:02:35 -04002170
2171 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2172 *ctx_th = opendata->f_attr.mdsthreshold;
2173 else
2174 kfree(opendata->f_attr.mdsthreshold);
2175 opendata->f_attr.mdsthreshold = NULL;
2176
David Quigley14c43f72013-05-22 12:50:43 -04002177 nfs4_label_free(olabel);
2178
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002179 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002182err_free_label:
2183 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002184err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002185 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002186 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002187err_put_state_owner:
2188 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 return status;
2191}
2192
2193
Andy Adamson82be4172012-05-23 05:02:35 -04002194static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002195 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002196 int flags,
2197 struct iattr *sattr,
Trond Myklebust959d9212013-06-28 16:29:51 -04002198 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002200 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 struct nfs4_exception exception = { };
2202 struct nfs4_state *res;
2203 int status;
2204
2205 do {
Trond Myklebust959d9212013-06-28 16:29:51 -04002206 status = _nfs4_do_open(dir, ctx, flags, sattr, label);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002207 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002208 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 if (status == 0)
2210 break;
2211 /* NOTE: BAD_SEQID means the server and client disagree about the
2212 * book-keeping w.r.t. state-changing operations
2213 * (OPEN/CLOSE/LOCK/LOCKU...)
2214 * It is actually a sign of a bug on the client or on the server.
2215 *
2216 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002217 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 * have unhashed the old state_owner for us, and that we can
2219 * therefore safely retry using a new one. We should still warn
2220 * the user though...
2221 */
2222 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002223 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002224 " returned a bad sequence-id error!\n",
2225 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 exception.retry = 1;
2227 continue;
2228 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002229 /*
2230 * BAD_STATEID on OPEN means that the server cancelled our
2231 * state before it received the OPEN_CONFIRM.
2232 * Recover by retrying the request as per the discussion
2233 * on Page 181 of RFC3530.
2234 */
2235 if (status == -NFS4ERR_BAD_STATEID) {
2236 exception.retry = 1;
2237 continue;
2238 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002239 if (status == -EAGAIN) {
2240 /* We must have found a delegation */
2241 exception.retry = 1;
2242 continue;
2243 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002244 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2245 continue;
2246 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 status, &exception));
2248 } while (exception.retry);
2249 return res;
2250}
2251
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002252static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2253 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002254 struct nfs4_state *state, struct nfs4_label *ilabel,
2255 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002257 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002259 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 .iap = sattr,
2261 .server = server,
2262 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002263 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 };
2265 struct nfs_setattrres res = {
2266 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002267 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 .server = server,
2269 };
2270 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002271 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2272 .rpc_argp = &arg,
2273 .rpc_resp = &res,
2274 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002276 unsigned long timestamp = jiffies;
Trond Myklebustee3ae842013-04-29 10:35:36 -04002277 fmode_t fmode;
2278 bool truncate;
Trond Myklebust65e43082005-08-16 11:49:44 -04002279 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
David Quigleyaa9c2662013-05-22 12:50:44 -04002281 arg.bitmask = nfs4_bitmask(server, ilabel);
2282 if (ilabel)
2283 arg.bitmask = nfs4_bitmask(server, olabel);
2284
Trond Myklebust0e574af2005-10-27 22:12:38 -04002285 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
Trond Myklebustee3ae842013-04-29 10:35:36 -04002287 /* Servers should only apply open mode checks for file size changes */
2288 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2289 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2290
2291 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2292 /* Use that stateid */
2293 } else if (truncate && state != NULL && nfs4_valid_open_stateid(state)) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002294 struct nfs_lockowner lockowner = {
2295 .l_owner = current->files,
2296 .l_pid = current->tgid,
2297 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002298 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002299 &lockowner);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002300 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002301 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
Bryan Schumaker7c513052011-03-24 17:12:24 +00002303 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002304 if (status == 0 && state != NULL)
2305 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002306 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307}
2308
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002309static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2310 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002311 struct nfs4_state *state, struct nfs4_label *ilabel,
2312 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002314 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002315 struct nfs4_exception exception = {
2316 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002317 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002318 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 int err;
2320 do {
David Quigley1775fd32013-05-22 12:50:42 -04002321 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
Trond Myklebustc1578b72013-08-12 16:58:42 -04002322 trace_nfs4_setattr(inode, err);
Trond Myklebust451146b2012-04-18 16:29:11 -04002323 switch (err) {
2324 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002325 if (!(sattr->ia_valid & ATTR_SIZE)) {
2326 pr_warn_once("NFSv4: server %s is incorrectly "
2327 "applying open mode checks to "
2328 "a SETATTR that is not "
2329 "changing file size.\n",
2330 server->nfs_client->cl_hostname);
2331 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002332 if (state && !(state->state & FMODE_WRITE)) {
2333 err = -EBADF;
2334 if (sattr->ia_valid & ATTR_OPEN)
2335 err = -EACCES;
2336 goto out;
2337 }
2338 }
2339 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002341out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 return err;
2343}
2344
2345struct nfs4_closedata {
2346 struct inode *inode;
2347 struct nfs4_state *state;
2348 struct nfs_closeargs arg;
2349 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002350 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002351 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002352 bool roc;
2353 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354};
2355
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002356static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002357{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002358 struct nfs4_closedata *calldata = data;
2359 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002360 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002361
Fred Isamanf7e89172011-01-06 11:36:32 +00002362 if (calldata->roc)
2363 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002364 nfs4_put_open_state(calldata->state);
2365 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002366 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002367 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002368 kfree(calldata);
2369}
2370
Trond Myklebust88069f72009-12-08 08:33:16 -05002371static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2372 fmode_t fmode)
2373{
2374 spin_lock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002375 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002376 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
2377 case FMODE_WRITE:
2378 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2379 break;
2380 case FMODE_READ:
2381 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2382 break;
2383 case 0:
2384 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2385 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2386 clear_bit(NFS_OPEN_STATE, &state->flags);
2387 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002388 spin_unlock(&state->owner->so_lock);
2389}
2390
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002391static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002393 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 struct nfs_server *server = NFS_SERVER(calldata->inode);
2396
Chuck Levera3ca5652012-03-01 17:00:40 -05002397 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002398 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2399 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002400 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 /* hmm. we are done with the inode, and in the process of freeing
2402 * the state_owner. we keep this around to process errors
2403 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 switch (task->tk_status) {
2405 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002406 if (calldata->roc)
2407 pnfs_roc_set_barrier(state->inode,
2408 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002409 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002410 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002411 nfs4_close_clear_stateid_flags(state,
2412 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 break;
2414 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002415 case -NFS4ERR_OLD_STATEID:
2416 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002418 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002419 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002421 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2422 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002424 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002425 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002426 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427}
2428
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002429static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002431 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002432 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002433 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002434 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002435
Chuck Levera3ca5652012-03-01 17:00:40 -05002436 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002437 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002438 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002439
Trond Myklebust88069f72009-12-08 08:33:16 -05002440 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2441 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002442 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002443 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002444 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002445 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002446 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2447 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2448 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002449 }
2450 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002451 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2452 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2453 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002454 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002455 }
Trond Myklebust5d422302013-03-14 16:57:48 -04002456 if (!nfs4_valid_open_stateid(state))
2457 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002458 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002459
2460 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002461 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002462 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002463 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002464
Fred Isamanf7e89172011-01-06 11:36:32 +00002465 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002466 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002467 if (calldata->roc &&
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002468 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2469 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002470 goto out_wait;
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002471 }
Fred Isamanf7e89172011-01-06 11:36:32 +00002472 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002473
Trond Myklebust516a6af2005-10-27 22:12:41 -04002474 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002475 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002476 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002477 &calldata->arg.seq_args,
2478 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002479 task) != 0)
2480 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002481 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002482 return;
2483out_no_action:
2484 task->tk_action = NULL;
2485out_wait:
2486 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487}
2488
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002489static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002490 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002491 .rpc_call_done = nfs4_close_done,
2492 .rpc_release = nfs4_free_closedata,
2493};
2494
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495/*
2496 * It is possible for data to be read/written from a mem-mapped file
2497 * after the sys_close call (which hits the vfs layer as a flush).
2498 * This means that we can't safely call nfsv4 close on a file until
2499 * the inode is cleared. This in turn means that we are not good
2500 * NFSv4 citizens - we do not indicate to the server to update the file's
2501 * share state even when we are done with one of the three share
2502 * stateid's in the inode.
2503 *
2504 * NOTE: Caller must be holding the sp->so_owner semaphore!
2505 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002506int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002508 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002510 struct nfs4_state_owner *sp = state->owner;
2511 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002512 struct rpc_message msg = {
2513 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2514 .rpc_cred = state->owner->so_cred,
2515 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002516 struct rpc_task_setup task_setup_data = {
2517 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002518 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002519 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002520 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002521 .flags = RPC_TASK_ASYNC,
2522 };
Trond Myklebust95121352005-10-18 14:20:12 -07002523 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002525 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002527 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002528 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002529 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002531 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002532 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002534 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002535 if (calldata->arg.seqid == NULL)
2536 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002537 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002538 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002539 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002540 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002541 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002542 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002543 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002544
Trond Myklebust1174dd12010-12-21 10:52:24 -05002545 msg.rpc_argp = &calldata->arg;
2546 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002547 task_setup_data.callback_data = calldata;
2548 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002549 if (IS_ERR(task))
2550 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002551 status = 0;
2552 if (wait)
2553 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002554 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002555 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002556out_free_calldata:
2557 kfree(calldata);
2558out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002559 nfs4_put_open_state(state);
2560 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002561 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562}
2563
Trond Myklebust2b484292010-09-17 10:56:51 -04002564static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002565nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04002568 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2569
2570 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
Trond Myklebust565277f2007-10-15 18:17:53 -04002572 /* Protect against concurrent sillydeletes */
Trond Myklebust959d9212013-06-28 16:29:51 -04002573 state = nfs4_do_open(dir, ctx, open_flags, attr, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04002574
2575 nfs4_label_release_security(label);
2576
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002577 if (IS_ERR(state))
2578 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04002579 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580}
2581
Trond Myklebust1185a552009-12-03 15:54:02 -05002582static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002583{
2584 if (ctx->state == NULL)
2585 return;
2586 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002587 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002588 else
Al Viro643168c2011-06-22 18:20:23 -04002589 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002590}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
2592static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2593{
Benny Halevy43652ad2009-04-01 09:21:54 -04002594 struct nfs4_server_caps_arg args = {
2595 .fhandle = fhandle,
2596 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 struct nfs4_server_caps_res res = {};
2598 struct rpc_message msg = {
2599 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002600 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 .rpc_resp = &res,
2602 };
2603 int status;
2604
Bryan Schumaker7c513052011-03-24 17:12:24 +00002605 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 if (status == 0) {
2607 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002608 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2609 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2610 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2611 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2612 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2614 server->caps |= NFS_CAP_ACLS;
2615 if (res.has_links != 0)
2616 server->caps |= NFS_CAP_HARDLINKS;
2617 if (res.has_symlinks != 0)
2618 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002619 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2620 server->caps |= NFS_CAP_FILEID;
2621 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2622 server->caps |= NFS_CAP_MODE;
2623 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2624 server->caps |= NFS_CAP_NLINK;
2625 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2626 server->caps |= NFS_CAP_OWNER;
2627 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2628 server->caps |= NFS_CAP_OWNER_GROUP;
2629 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2630 server->caps |= NFS_CAP_ATIME;
2631 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2632 server->caps |= NFS_CAP_CTIME;
2633 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2634 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04002635#ifdef CONFIG_NFS_V4_SECURITY_LABEL
2636 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
2637 server->caps |= NFS_CAP_SECURITY_LABEL;
2638#endif
2639 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
2640 sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002641
David Quigleyaa9c2662013-05-22 12:50:44 -04002642 if (server->caps & NFS_CAP_SECURITY_LABEL) {
2643 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2644 res.attr_bitmask[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2645 }
Trond Myklebusta65318b2009-03-11 14:10:28 -04002646 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2647 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2648 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002650 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002652
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 return status;
2654}
2655
Trond Myklebust55a97592006-06-09 09:34:19 -04002656int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657{
2658 struct nfs4_exception exception = { };
2659 int err;
2660 do {
2661 err = nfs4_handle_exception(server,
2662 _nfs4_server_capabilities(server, fhandle),
2663 &exception);
2664 } while (exception.retry);
2665 return err;
2666}
2667
2668static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2669 struct nfs_fsinfo *info)
2670{
David Quigleyaa9c2662013-05-22 12:50:44 -04002671 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04002673 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 };
2675 struct nfs4_lookup_res res = {
2676 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002677 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 .fh = fhandle,
2679 };
2680 struct rpc_message msg = {
2681 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2682 .rpc_argp = &args,
2683 .rpc_resp = &res,
2684 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002685
David Quigleyaa9c2662013-05-22 12:50:44 -04002686 bitmask[0] = nfs4_fattr_bitmap[0];
2687 bitmask[1] = nfs4_fattr_bitmap[1];
2688 /*
2689 * Process the label in the upcoming getfattr
2690 */
2691 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
2692
Trond Myklebust0e574af2005-10-27 22:12:38 -04002693 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002694 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695}
2696
2697static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2698 struct nfs_fsinfo *info)
2699{
2700 struct nfs4_exception exception = { };
2701 int err;
2702 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002703 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04002704 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002705 switch (err) {
2706 case 0:
2707 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002708 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002709 default:
2710 err = nfs4_handle_exception(server, err, &exception);
2711 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002713out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 return err;
2715}
2716
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002717static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2718 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2719{
2720 struct rpc_auth *auth;
2721 int ret;
2722
2723 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002724 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04002725 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002726 goto out;
2727 }
2728 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002729out:
2730 return ret;
2731}
2732
Chuck Lever9a744ba2013-03-16 15:56:02 -04002733/*
2734 * Retry pseudoroot lookup with various security flavors. We do this when:
2735 *
2736 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2737 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2738 *
2739 * Returns zero on success, or a negative NFS4ERR value, or a
2740 * negative errno value.
2741 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002742static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002743 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744{
Chuck Lever9a744ba2013-03-16 15:56:02 -04002745 /* Per 3530bis 15.33.5 */
2746 static const rpc_authflavor_t flav_array[] = {
2747 RPC_AUTH_GSS_KRB5P,
2748 RPC_AUTH_GSS_KRB5I,
2749 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04002750 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04002751 RPC_AUTH_NULL,
2752 };
2753 int status = -EPERM;
2754 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755
Chuck Lever9a744ba2013-03-16 15:56:02 -04002756 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002757 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002758 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002759 continue;
2760 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002761 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04002762
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002763 /*
2764 * -EACCESS could mean that the user doesn't have correct permissions
2765 * to access the mount. It could also mean that we tried to mount
2766 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2767 * existing mount programs don't handle -EACCES very well so it should
2768 * be mapped to -EPERM instead.
2769 */
2770 if (status == -EACCES)
2771 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002772 return status;
2773}
2774
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002775static int nfs4_do_find_root_sec(struct nfs_server *server,
2776 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
2777{
2778 int mv = server->nfs_client->cl_minorversion;
2779 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
2780}
2781
2782/**
2783 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
2784 * @server: initialized nfs_server handle
2785 * @fhandle: we fill in the pseudo-fs root file handle
2786 * @info: we fill in an FSINFO struct
2787 *
2788 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002789 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002790int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2791 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002792{
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002793 int status;
2794
2795 status = nfs4_lookup_root(server, fhandle, info);
2796 if ((status == -NFS4ERR_WRONGSEC) &&
2797 !(server->flags & NFS_MOUNT_SECFLAVOUR))
2798 status = nfs4_do_find_root_sec(server, fhandle, info);
2799
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 if (status == 0)
2801 status = nfs4_server_capabilities(server, fhandle);
2802 if (status == 0)
2803 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002804
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002805 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806}
2807
Bryan Schumakerbae36242012-05-10 15:07:31 -04002808static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2809 struct nfs_fsinfo *info)
2810{
2811 int error;
2812 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04002813 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04002814
2815 error = nfs4_server_capabilities(server, mntfh);
2816 if (error < 0) {
2817 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2818 return error;
2819 }
2820
David Quigley14c43f72013-05-22 12:50:43 -04002821 label = nfs4_label_alloc(server, GFP_KERNEL);
2822 if (IS_ERR(label))
2823 return PTR_ERR(label);
2824
David Quigley1775fd32013-05-22 12:50:42 -04002825 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04002826 if (error < 0) {
2827 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04002828 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04002829 }
2830
2831 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2832 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2833 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2834
David Quigley14c43f72013-05-22 12:50:43 -04002835err_free_label:
2836 nfs4_label_free(label);
2837
Bryan Schumakerbae36242012-05-10 15:07:31 -04002838 return error;
2839}
2840
Manoj Naik6b97fd32006-06-09 09:34:29 -04002841/*
2842 * Get locations and (maybe) other attributes of a referral.
2843 * Note that we'll actually follow the referral later when
2844 * we detect fsid mismatch in inode revalidation
2845 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002846static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2847 const struct qstr *name, struct nfs_fattr *fattr,
2848 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002849{
2850 int status = -ENOMEM;
2851 struct page *page = NULL;
2852 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002853
2854 page = alloc_page(GFP_KERNEL);
2855 if (page == NULL)
2856 goto out;
2857 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2858 if (locations == NULL)
2859 goto out;
2860
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002861 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002862 if (status != 0)
2863 goto out;
2864 /* Make sure server returned a different fsid for the referral */
2865 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002866 dprintk("%s: server did not return a different fsid for"
2867 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002868 status = -EIO;
2869 goto out;
2870 }
Andy Adamson533eb462011-06-13 18:25:56 -04002871 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2872 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002873
Andy Adamson533eb462011-06-13 18:25:56 -04002874 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002875 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002876 memset(fhandle, 0, sizeof(struct nfs_fh));
2877out:
2878 if (page)
2879 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002880 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002881 return status;
2882}
2883
David Quigley1775fd32013-05-22 12:50:42 -04002884static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
2885 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886{
2887 struct nfs4_getattr_arg args = {
2888 .fh = fhandle,
2889 .bitmask = server->attr_bitmask,
2890 };
2891 struct nfs4_getattr_res res = {
2892 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002893 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 .server = server,
2895 };
2896 struct rpc_message msg = {
2897 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2898 .rpc_argp = &args,
2899 .rpc_resp = &res,
2900 };
David Quigleyaa9c2662013-05-22 12:50:44 -04002901
2902 args.bitmask = nfs4_bitmask(server, label);
2903
Trond Myklebust0e574af2005-10-27 22:12:38 -04002904 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002905 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906}
2907
David Quigley1775fd32013-05-22 12:50:42 -04002908static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
2909 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910{
2911 struct nfs4_exception exception = { };
2912 int err;
2913 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04002914 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
2915 trace_nfs4_getattr(server, fhandle, fattr, err);
2916 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 &exception);
2918 } while (exception.retry);
2919 return err;
2920}
2921
2922/*
2923 * The file is not closed if it is opened due to the a request to change
2924 * the size of the file. The open call will not be needed once the
2925 * VFS layer lookup-intents are implemented.
2926 *
2927 * Close is called when the inode is destroyed.
2928 * If we haven't opened the file for O_WRONLY, we
2929 * need to in the size_change case to obtain a stateid.
2930 *
2931 * Got race?
2932 * Because OPEN is always done by name in nfsv4, it is
2933 * possible that we opened a different file by the same
2934 * name. We can recognize this race condition, but we
2935 * can't do anything about it besides returning an error.
2936 *
2937 * This will be fixed with VFS changes (lookup-intent).
2938 */
2939static int
2940nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2941 struct iattr *sattr)
2942{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002943 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002944 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002945 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04002946 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 int status;
2948
Benny Halevy8a1636c2010-07-14 15:43:57 -04002949 if (pnfs_ld_layoutret_on_setattr(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04002950 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04002951
Trond Myklebust0e574af2005-10-27 22:12:38 -04002952 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953
Andy Adamson26699402012-05-30 16:12:24 -04002954 /* Deal with open(O_TRUNC) */
2955 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03002956 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04002957
2958 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03002959 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04002960 return 0;
2961
Trond Myklebustd5308382005-11-04 15:33:38 -05002962 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002963 if (sattr->ia_valid & ATTR_FILE) {
2964 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002965
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002966 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002967 if (ctx) {
2968 cred = ctx->cred;
2969 state = ctx->state;
2970 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002971 }
2972
David Quigley14c43f72013-05-22 12:50:43 -04002973 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
2974 if (IS_ERR(label))
2975 return PTR_ERR(label);
2976
2977 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04002978 if (status == 0) {
Trond Myklebust65e43082005-08-16 11:49:44 -04002979 nfs_setattr_update_inode(inode, sattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04002980 nfs_setsecurity(inode, fattr, label);
2981 }
David Quigley14c43f72013-05-22 12:50:43 -04002982 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 return status;
2984}
2985
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002986static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2987 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04002988 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04002989{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002990 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002991 int status;
2992 struct nfs4_lookup_arg args = {
2993 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002994 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002995 .name = name,
2996 };
2997 struct nfs4_lookup_res res = {
2998 .server = server,
2999 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003000 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003001 .fh = fhandle,
3002 };
3003 struct rpc_message msg = {
3004 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3005 .rpc_argp = &args,
3006 .rpc_resp = &res,
3007 };
3008
David Quigleyaa9c2662013-05-22 12:50:44 -04003009 args.bitmask = nfs4_bitmask(server, label);
3010
David Howells2b3de442006-08-22 20:06:09 -04003011 nfs_fattr_init(fattr);
3012
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003014 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 dprintk("NFS reply lookup: %d\n", status);
3016 return status;
3017}
3018
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003019static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003020{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003021 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003022 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003023 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3024 fattr->nlink = 2;
3025}
3026
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003027static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3028 struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003029 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003030{
3031 struct nfs4_exception exception = { };
3032 struct rpc_clnt *client = *clnt;
3033 int err;
3034 do {
David Quigley1775fd32013-05-22 12:50:42 -04003035 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003036 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003037 switch (err) {
3038 case -NFS4ERR_BADNAME:
3039 err = -ENOENT;
3040 goto out;
3041 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003042 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003043 goto out;
3044 case -NFS4ERR_WRONGSEC:
3045 err = -EPERM;
3046 if (client != *clnt)
3047 goto out;
3048
3049 client = nfs4_create_sec_client(client, dir, name);
3050 if (IS_ERR(client))
3051 return PTR_ERR(client);
3052
3053 exception.retry = 1;
3054 break;
3055 default:
3056 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3057 }
3058 } while (exception.retry);
3059
3060out:
3061 if (err == 0)
3062 *clnt = client;
3063 else if (client != *clnt)
3064 rpc_shutdown_client(client);
3065
3066 return err;
3067}
3068
Bryan Schumaker80a16b22012-04-27 13:27:46 -04003069static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003070 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3071 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003073 int status;
3074 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003075
David Quigley1775fd32013-05-22 12:50:42 -04003076 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003077 if (client != NFS_CLIENT(dir)) {
3078 rpc_shutdown_client(client);
3079 nfs_fixup_secinfo_attributes(fattr);
3080 }
3081 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082}
3083
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003084struct rpc_clnt *
3085nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3086 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3087{
3088 int status;
3089 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
3090
David Quigley1775fd32013-05-22 12:50:42 -04003091 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003092 if (status < 0) {
3093 rpc_shutdown_client(client);
3094 return ERR_PTR(status);
3095 }
3096 return client;
3097}
3098
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3100{
Trond Myklebust76b32992007-08-10 17:45:11 -04003101 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 struct nfs4_accessargs args = {
3103 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003104 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003106 struct nfs4_accessres res = {
3107 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003108 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 struct rpc_message msg = {
3110 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3111 .rpc_argp = &args,
3112 .rpc_resp = &res,
3113 .rpc_cred = entry->cred,
3114 };
3115 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003116 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117
3118 /*
3119 * Determine which access bits we want to ask for...
3120 */
3121 if (mode & MAY_READ)
3122 args.access |= NFS4_ACCESS_READ;
3123 if (S_ISDIR(inode->i_mode)) {
3124 if (mode & MAY_WRITE)
3125 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3126 if (mode & MAY_EXEC)
3127 args.access |= NFS4_ACCESS_LOOKUP;
3128 } else {
3129 if (mode & MAY_WRITE)
3130 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3131 if (mode & MAY_EXEC)
3132 args.access |= NFS4_ACCESS_EXECUTE;
3133 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003134
3135 res.fattr = nfs_alloc_fattr();
3136 if (res.fattr == NULL)
3137 return -ENOMEM;
3138
Bryan Schumaker7c513052011-03-24 17:12:24 +00003139 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003141 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003142 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003144 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 return status;
3146}
3147
3148static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3149{
3150 struct nfs4_exception exception = { };
3151 int err;
3152 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003153 err = _nfs4_proc_access(inode, entry);
3154 trace_nfs4_access(inode, err);
3155 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 &exception);
3157 } while (exception.retry);
3158 return err;
3159}
3160
3161/*
3162 * TODO: For the time being, we don't try to get any attributes
3163 * along with any of the zero-copy operations READ, READDIR,
3164 * READLINK, WRITE.
3165 *
3166 * In the case of the first three, we want to put the GETATTR
3167 * after the read-type operation -- this is because it is hard
3168 * to predict the length of a GETATTR response in v4, and thus
3169 * align the READ data correctly. This means that the GETATTR
3170 * may end up partially falling into the page cache, and we should
3171 * shift it into the 'tail' of the xdr_buf before processing.
3172 * To do this efficiently, we need to know the total length
3173 * of data received, which doesn't seem to be available outside
3174 * of the RPC layer.
3175 *
3176 * In the case of WRITE, we also want to put the GETATTR after
3177 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003178 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 *
3180 * Both of these changes to the XDR layer would in fact be quite
3181 * minor, but I decided to leave them for a subsequent patch.
3182 */
3183static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3184 unsigned int pgbase, unsigned int pglen)
3185{
3186 struct nfs4_readlink args = {
3187 .fh = NFS_FH(inode),
3188 .pgbase = pgbase,
3189 .pglen = pglen,
3190 .pages = &page,
3191 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003192 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 struct rpc_message msg = {
3194 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3195 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003196 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 };
3198
Bryan Schumaker7c513052011-03-24 17:12:24 +00003199 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 -07003200}
3201
3202static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3203 unsigned int pgbase, unsigned int pglen)
3204{
3205 struct nfs4_exception exception = { };
3206 int err;
3207 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003208 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3209 trace_nfs4_readlink(inode, err);
3210 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 &exception);
3212 } while (exception.retry);
3213 return err;
3214}
3215
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003217 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219static int
3220nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003221 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222{
David Quigleyaa9c2662013-05-22 12:50:44 -04003223 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003224 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 int status = 0;
3227
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003228 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3229 if (IS_ERR(ctx))
3230 return PTR_ERR(ctx);
3231
David Quigleyaa9c2662013-05-22 12:50:44 -04003232 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3233
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003234 sattr->ia_mode &= ~current_umask();
Trond Myklebust959d9212013-06-28 16:29:51 -04003235 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 if (IS_ERR(state)) {
3237 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003238 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003241 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003242 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 return status;
3244}
3245
3246static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3247{
Trond Myklebust16e42952005-10-27 22:12:44 -04003248 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003249 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003251 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003253 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003254 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003255 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003257 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3258 .rpc_argp = &args,
3259 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003261 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003262
Bryan Schumaker7c513052011-03-24 17:12:24 +00003263 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003264 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003265 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 return status;
3267}
3268
3269static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3270{
3271 struct nfs4_exception exception = { };
3272 int err;
3273 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003274 err = _nfs4_proc_remove(dir, name);
3275 trace_nfs4_remove(dir, name, err);
3276 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 &exception);
3278 } while (exception.retry);
3279 return err;
3280}
3281
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003282static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003284 struct nfs_server *server = NFS_SERVER(dir);
3285 struct nfs_removeargs *args = msg->rpc_argp;
3286 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003288 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003290 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003291
3292 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293}
3294
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003295static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3296{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003297 nfs4_setup_sequence(NFS_SERVER(data->dir),
3298 &data->args.seq_args,
3299 &data->res.seq_res,
3300 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301}
3302
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003303static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003305 struct nfs_unlinkdata *data = task->tk_calldata;
3306 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003307
Trond Myklebust14516c32010-07-31 14:29:06 -04003308 if (!nfs4_sequence_done(task, &res->seq_res))
3309 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003310 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003311 return 0;
3312 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003313 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314}
3315
Jeff Laytond3d41522010-09-17 17:31:57 -04003316static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3317{
3318 struct nfs_server *server = NFS_SERVER(dir);
3319 struct nfs_renameargs *arg = msg->rpc_argp;
3320 struct nfs_renameres *res = msg->rpc_resp;
3321
3322 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003323 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003324 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003325}
3326
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003327static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3328{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003329 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3330 &data->args.seq_args,
3331 &data->res.seq_res,
3332 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003333}
3334
3335static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3336 struct inode *new_dir)
3337{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003338 struct nfs_renamedata *data = task->tk_calldata;
3339 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04003340
3341 if (!nfs4_sequence_done(task, &res->seq_res))
3342 return 0;
3343 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3344 return 0;
3345
3346 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003347 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003348 return 1;
3349}
3350
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3352 struct inode *new_dir, struct qstr *new_name)
3353{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003354 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003355 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 .old_dir = NFS_FH(old_dir),
3357 .new_dir = NFS_FH(new_dir),
3358 .old_name = old_name,
3359 .new_name = new_name,
3360 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003361 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003362 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003363 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 struct rpc_message msg = {
3365 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3366 .rpc_argp = &arg,
3367 .rpc_resp = &res,
3368 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003369 int status = -ENOMEM;
David Quigley14c43f72013-05-22 12:50:43 -04003370
Bryan Schumaker7c513052011-03-24 17:12:24 +00003371 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 if (!status) {
3373 update_changeattr(old_dir, &res.old_cinfo);
3374 update_changeattr(new_dir, &res.new_cinfo);
3375 }
3376 return status;
3377}
3378
3379static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3380 struct inode *new_dir, struct qstr *new_name)
3381{
3382 struct nfs4_exception exception = { };
3383 int err;
3384 do {
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003385 err = _nfs4_proc_rename(old_dir, old_name,
3386 new_dir, new_name);
3387 trace_nfs4_rename(old_dir, old_name, new_dir, new_name, err);
3388 err = nfs4_handle_exception(NFS_SERVER(old_dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 &exception);
3390 } while (exception.retry);
3391 return err;
3392}
3393
3394static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3395{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003396 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 struct nfs4_link_arg arg = {
3398 .fh = NFS_FH(inode),
3399 .dir_fh = NFS_FH(dir),
3400 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003401 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003403 struct nfs4_link_res res = {
3404 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003405 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003406 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 struct rpc_message msg = {
3408 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3409 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003410 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003412 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413
Trond Myklebust136f2622010-04-16 16:22:49 -04003414 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003415 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003416 goto out;
3417
David Quigley14c43f72013-05-22 12:50:43 -04003418 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3419 if (IS_ERR(res.label)) {
3420 status = PTR_ERR(res.label);
3421 goto out;
3422 }
David Quigleyaa9c2662013-05-22 12:50:44 -04003423 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04003424
Bryan Schumaker7c513052011-03-24 17:12:24 +00003425 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003426 if (!status) {
3427 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04003428 status = nfs_post_op_update_inode(inode, res.fattr);
3429 if (!status)
3430 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003431 }
David Quigley14c43f72013-05-22 12:50:43 -04003432
3433
3434 nfs4_label_free(res.label);
3435
Trond Myklebust136f2622010-04-16 16:22:49 -04003436out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003437 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 return status;
3439}
3440
3441static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3442{
3443 struct nfs4_exception exception = { };
3444 int err;
3445 do {
3446 err = nfs4_handle_exception(NFS_SERVER(inode),
3447 _nfs4_proc_link(inode, dir, name),
3448 &exception);
3449 } while (exception.retry);
3450 return err;
3451}
3452
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003453struct nfs4_createdata {
3454 struct rpc_message msg;
3455 struct nfs4_create_arg arg;
3456 struct nfs4_create_res res;
3457 struct nfs_fh fh;
3458 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003459 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003460};
3461
3462static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3463 struct qstr *name, struct iattr *sattr, u32 ftype)
3464{
3465 struct nfs4_createdata *data;
3466
3467 data = kzalloc(sizeof(*data), GFP_KERNEL);
3468 if (data != NULL) {
3469 struct nfs_server *server = NFS_SERVER(dir);
3470
David Quigley14c43f72013-05-22 12:50:43 -04003471 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3472 if (IS_ERR(data->label))
3473 goto out_free;
3474
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003475 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3476 data->msg.rpc_argp = &data->arg;
3477 data->msg.rpc_resp = &data->res;
3478 data->arg.dir_fh = NFS_FH(dir);
3479 data->arg.server = server;
3480 data->arg.name = name;
3481 data->arg.attrs = sattr;
3482 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04003483 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003484 data->res.server = server;
3485 data->res.fh = &data->fh;
3486 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003487 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003488 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003489 }
3490 return data;
David Quigley14c43f72013-05-22 12:50:43 -04003491out_free:
3492 kfree(data);
3493 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003494}
3495
3496static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3497{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003498 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003499 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003500 if (status == 0) {
3501 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04003502 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003503 }
3504 return status;
3505}
3506
3507static void nfs4_free_createdata(struct nfs4_createdata *data)
3508{
David Quigley14c43f72013-05-22 12:50:43 -04003509 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003510 kfree(data);
3511}
3512
Chuck Lever4f390c12006-08-22 20:06:22 -04003513static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003514 struct page *page, unsigned int len, struct iattr *sattr,
3515 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003517 struct nfs4_createdata *data;
3518 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519
Chuck Lever94a6d752006-08-22 20:06:23 -04003520 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003521 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003522
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003523 status = -ENOMEM;
3524 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3525 if (data == NULL)
3526 goto out;
3527
3528 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3529 data->arg.u.symlink.pages = &page;
3530 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04003531 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003533 status = nfs4_do_create(dir, dentry, data);
3534
3535 nfs4_free_createdata(data);
3536out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 return status;
3538}
3539
Chuck Lever4f390c12006-08-22 20:06:22 -04003540static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003541 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542{
3543 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003544 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04003546
3547 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3548
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003550 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3551 trace_nfs4_symlink(dir, &dentry->d_name, err);
3552 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 &exception);
3554 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003555
3556 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 return err;
3558}
3559
3560static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003561 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003563 struct nfs4_createdata *data;
3564 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003566 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3567 if (data == NULL)
3568 goto out;
3569
David Quigley1775fd32013-05-22 12:50:42 -04003570 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003571 status = nfs4_do_create(dir, dentry, data);
3572
3573 nfs4_free_createdata(data);
3574out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 return status;
3576}
3577
3578static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3579 struct iattr *sattr)
3580{
3581 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003582 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003584
David Quigleyaa9c2662013-05-22 12:50:44 -04003585 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3586
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003587 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003589 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3590 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3591 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 &exception);
3593 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003594 nfs4_label_release_security(label);
3595
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 return err;
3597}
3598
3599static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003600 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601{
3602 struct inode *dir = dentry->d_inode;
3603 struct nfs4_readdir_arg args = {
3604 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003605 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 .pgbase = 0,
3607 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003608 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003609 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 };
3611 struct nfs4_readdir_res res;
3612 struct rpc_message msg = {
3613 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3614 .rpc_argp = &args,
3615 .rpc_resp = &res,
3616 .rpc_cred = cred,
3617 };
3618 int status;
3619
Harvey Harrison3110ff82008-05-02 13:42:44 -07003620 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003621 dentry->d_parent->d_name.name,
3622 dentry->d_name.name,
3623 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003624 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003626 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 -05003627 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003628 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003629 status += args.pgbase;
3630 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003631
3632 nfs_invalidate_atime(dir);
3633
Harvey Harrison3110ff82008-05-02 13:42:44 -07003634 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 return status;
3636}
3637
3638static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003639 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640{
3641 struct nfs4_exception exception = { };
3642 int err;
3643 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003644 err = _nfs4_proc_readdir(dentry, cred, cookie,
3645 pages, count, plus);
3646 trace_nfs4_readdir(dentry->d_inode, err);
3647 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 &exception);
3649 } while (exception.retry);
3650 return err;
3651}
3652
3653static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04003654 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003656 struct nfs4_createdata *data;
3657 int mode = sattr->ia_mode;
3658 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003660 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3661 if (data == NULL)
3662 goto out;
3663
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003665 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003667 data->arg.ftype = NF4BLK;
3668 data->arg.u.device.specdata1 = MAJOR(rdev);
3669 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 }
3671 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003672 data->arg.ftype = NF4CHR;
3673 data->arg.u.device.specdata1 = MAJOR(rdev);
3674 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003675 } else if (!S_ISSOCK(mode)) {
3676 status = -EINVAL;
3677 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 }
David Quigley1775fd32013-05-22 12:50:42 -04003679
David Quigleyaa9c2662013-05-22 12:50:44 -04003680 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003681 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003682out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003683 nfs4_free_createdata(data);
3684out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 return status;
3686}
3687
3688static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3689 struct iattr *sattr, dev_t rdev)
3690{
3691 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003692 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003694
David Quigleyaa9c2662013-05-22 12:50:44 -04003695 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3696
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003697 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003699 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
3700 trace_nfs4_mknod(dir, &dentry->d_name, err);
3701 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 &exception);
3703 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003704
3705 nfs4_label_release_security(label);
3706
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 return err;
3708}
3709
3710static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3711 struct nfs_fsstat *fsstat)
3712{
3713 struct nfs4_statfs_arg args = {
3714 .fh = fhandle,
3715 .bitmask = server->attr_bitmask,
3716 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003717 struct nfs4_statfs_res res = {
3718 .fsstat = fsstat,
3719 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 struct rpc_message msg = {
3721 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3722 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003723 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 };
3725
Trond Myklebust0e574af2005-10-27 22:12:38 -04003726 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003727 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728}
3729
3730static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3731{
3732 struct nfs4_exception exception = { };
3733 int err;
3734 do {
3735 err = nfs4_handle_exception(server,
3736 _nfs4_proc_statfs(server, fhandle, fsstat),
3737 &exception);
3738 } while (exception.retry);
3739 return err;
3740}
3741
3742static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3743 struct nfs_fsinfo *fsinfo)
3744{
3745 struct nfs4_fsinfo_arg args = {
3746 .fh = fhandle,
3747 .bitmask = server->attr_bitmask,
3748 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003749 struct nfs4_fsinfo_res res = {
3750 .fsinfo = fsinfo,
3751 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 struct rpc_message msg = {
3753 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3754 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003755 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 };
3757
Bryan Schumaker7c513052011-03-24 17:12:24 +00003758 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759}
3760
3761static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3762{
3763 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04003764 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 int err;
3766
3767 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04003768 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003769 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04003770 if (err == 0) {
3771 struct nfs_client *clp = server->nfs_client;
3772
3773 spin_lock(&clp->cl_lock);
3774 clp->cl_lease_time = fsinfo->lease_time * HZ;
3775 clp->cl_last_renewal = now;
3776 spin_unlock(&clp->cl_lock);
3777 break;
3778 }
3779 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 } while (exception.retry);
3781 return err;
3782}
3783
3784static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3785{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003786 int error;
3787
Trond Myklebust0e574af2005-10-27 22:12:38 -04003788 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003789 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003790 if (error == 0) {
3791 /* block layout checks this! */
3792 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003793 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003794 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003795
3796 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797}
3798
3799static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3800 struct nfs_pathconf *pathconf)
3801{
3802 struct nfs4_pathconf_arg args = {
3803 .fh = fhandle,
3804 .bitmask = server->attr_bitmask,
3805 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003806 struct nfs4_pathconf_res res = {
3807 .pathconf = pathconf,
3808 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 struct rpc_message msg = {
3810 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3811 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003812 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 };
3814
3815 /* None of the pathconf attributes are mandatory to implement */
3816 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3817 memset(pathconf, 0, sizeof(*pathconf));
3818 return 0;
3819 }
3820
Trond Myklebust0e574af2005-10-27 22:12:38 -04003821 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003822 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823}
3824
3825static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3826 struct nfs_pathconf *pathconf)
3827{
3828 struct nfs4_exception exception = { };
3829 int err;
3830
3831 do {
3832 err = nfs4_handle_exception(server,
3833 _nfs4_proc_pathconf(server, fhandle, pathconf),
3834 &exception);
3835 } while (exception.retry);
3836 return err;
3837}
3838
Trond Myklebust5521abf2013-03-16 20:54:34 -04003839int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04003840 const struct nfs_open_context *ctx,
3841 const struct nfs_lock_context *l_ctx,
3842 fmode_t fmode)
3843{
3844 const struct nfs_lockowner *lockowner = NULL;
3845
3846 if (l_ctx != NULL)
3847 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003848 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04003849}
3850EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
3851
Trond Myklebust5521abf2013-03-16 20:54:34 -04003852static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
3853 const struct nfs_open_context *ctx,
3854 const struct nfs_lock_context *l_ctx,
3855 fmode_t fmode)
3856{
3857 nfs4_stateid current_stateid;
3858
3859 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode))
3860 return false;
3861 return nfs4_stateid_match(stateid, &current_stateid);
3862}
3863
3864static bool nfs4_error_stateid_expired(int err)
3865{
3866 switch (err) {
3867 case -NFS4ERR_DELEG_REVOKED:
3868 case -NFS4ERR_ADMIN_REVOKED:
3869 case -NFS4ERR_BAD_STATEID:
3870 case -NFS4ERR_STALE_STATEID:
3871 case -NFS4ERR_OLD_STATEID:
3872 case -NFS4ERR_OPENMODE:
3873 case -NFS4ERR_EXPIRED:
3874 return true;
3875 }
3876 return false;
3877}
3878
Benny Halevyd20581a2011-05-22 19:52:03 +03003879void __nfs4_read_done_cb(struct nfs_read_data *data)
3880{
Fred Isamancd841602012-04-20 14:47:44 -04003881 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003882}
3883
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003884static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885{
Fred Isamancd841602012-04-20 14:47:44 -04003886 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887
Trond Myklebustcc668ab2013-08-14 15:31:28 -04003888 trace_nfs4_read(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003889 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003890 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003891 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003893
Benny Halevyd20581a2011-05-22 19:52:03 +03003894 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003896 renew_lease(server, data->timestamp);
3897 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898}
3899
Trond Myklebust5521abf2013-03-16 20:54:34 -04003900static bool nfs4_read_stateid_changed(struct rpc_task *task,
3901 struct nfs_readargs *args)
3902{
3903
3904 if (!nfs4_error_stateid_expired(task->tk_status) ||
3905 nfs4_stateid_is_current(&args->stateid,
3906 args->context,
3907 args->lock_context,
3908 FMODE_READ))
3909 return false;
3910 rpc_restart_call_prepare(task);
3911 return true;
3912}
3913
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003914static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3915{
3916
3917 dprintk("--> %s\n", __func__);
3918
3919 if (!nfs4_sequence_done(task, &data->res.seq_res))
3920 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003921 if (nfs4_read_stateid_changed(task, &data->args))
3922 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003923 return data->read_done_cb ? data->read_done_cb(task, data) :
3924 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003925}
3926
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003927static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003930 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003931 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003932 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933}
3934
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003935static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3936{
Trond Myklebust9b206142013-03-17 15:52:00 -04003937 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003938 &data->args.seq_args,
3939 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04003940 task))
3941 return;
3942 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3943 data->args.lock_context, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944}
3945
Fred Isamanb029bc92011-03-03 15:13:42 +00003946static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947{
Fred Isamancd841602012-04-20 14:47:44 -04003948 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949
Trond Myklebustcc668ab2013-08-14 15:31:28 -04003950 trace_nfs4_write(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003951 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003952 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003953 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003955 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003957 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003958 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003959 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960}
3961
Trond Myklebust5521abf2013-03-16 20:54:34 -04003962static bool nfs4_write_stateid_changed(struct rpc_task *task,
3963 struct nfs_writeargs *args)
3964{
3965
3966 if (!nfs4_error_stateid_expired(task->tk_status) ||
3967 nfs4_stateid_is_current(&args->stateid,
3968 args->context,
3969 args->lock_context,
3970 FMODE_WRITE))
3971 return false;
3972 rpc_restart_call_prepare(task);
3973 return true;
3974}
3975
Fred Isamanb029bc92011-03-03 15:13:42 +00003976static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3977{
3978 if (!nfs4_sequence_done(task, &data->res.seq_res))
3979 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003980 if (nfs4_write_stateid_changed(task, &data->args))
3981 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003982 return data->write_done_cb ? data->write_done_cb(task, data) :
3983 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003984}
3985
Trond Myklebust5a37f852012-04-28 14:55:16 -04003986static
3987bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003988{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003989 const struct nfs_pgio_header *hdr = data->header;
3990
3991 /* Don't request attributes for pNFS or O_DIRECT writes */
3992 if (data->ds_clp != NULL || hdr->dreq != NULL)
3993 return false;
3994 /* Otherwise, request attributes if and only if we don't hold
3995 * a delegation
3996 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003997 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003998}
Fred Isamana69aef12011-03-03 15:13:47 +00003999
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004000static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001{
Fred Isamancd841602012-04-20 14:47:44 -04004002 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004003
Trond Myklebust5a37f852012-04-28 14:55:16 -04004004 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00004005 data->args.bitmask = NULL;
4006 data->res.fattr = NULL;
4007 } else
4008 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004009
Fred Isamanb029bc92011-03-03 15:13:42 +00004010 if (!data->write_done_cb)
4011 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004012 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 data->timestamp = jiffies;
4014
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004015 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05004016 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017}
4018
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04004019static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
4020{
Trond Myklebust9b206142013-03-17 15:52:00 -04004021 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004022 &data->args.seq_args,
4023 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004024 task))
4025 return;
4026 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4027 data->args.lock_context, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028}
4029
Fred Isaman0b7c0152012-04-20 14:47:39 -04004030static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4031{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004032 nfs4_setup_sequence(NFS_SERVER(data->inode),
4033 &data->args.seq_args,
4034 &data->res.seq_res,
4035 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036}
4037
Fred Isaman0b7c0152012-04-20 14:47:39 -04004038static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004041
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004042 trace_nfs4_commit(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004043 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004044 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004045 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004047 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048}
4049
Fred Isaman0b7c0152012-04-20 14:47:39 -04004050static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004051{
4052 if (!nfs4_sequence_done(task, &data->res.seq_res))
4053 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004054 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004055}
4056
Fred Isaman0b7c0152012-04-20 14:47:39 -04004057static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004059 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004060
Fred Isaman0b7c0152012-04-20 14:47:39 -04004061 if (data->commit_done_cb == NULL)
4062 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004063 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004064 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05004065 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066}
4067
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004068struct nfs4_renewdata {
4069 struct nfs_client *client;
4070 unsigned long timestamp;
4071};
4072
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073/*
4074 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4075 * standalone procedure for queueing an asynchronous RENEW.
4076 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004077static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004078{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004079 struct nfs4_renewdata *data = calldata;
4080 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004081
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004082 if (atomic_read(&clp->cl_count) > 1)
4083 nfs4_schedule_state_renewal(clp);
4084 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004085 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004086}
4087
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004088static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004090 struct nfs4_renewdata *data = calldata;
4091 struct nfs_client *clp = data->client;
4092 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004094 trace_nfs4_renew_async(clp, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04004096 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004097 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4098 return;
4099 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004100 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004101 return;
4102 }
4103 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004105 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106}
4107
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004108static const struct rpc_call_ops nfs4_renew_ops = {
4109 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004110 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004111};
4112
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004113static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114{
4115 struct rpc_message msg = {
4116 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4117 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004118 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004120 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004122 if (renew_flags == 0)
4123 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004124 if (!atomic_inc_not_zero(&clp->cl_count))
4125 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004126 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004127 if (data == NULL)
4128 return -ENOMEM;
4129 data->client = clp;
4130 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004131 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004132 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133}
4134
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004135static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136{
4137 struct rpc_message msg = {
4138 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4139 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004140 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 };
4142 unsigned long now = jiffies;
4143 int status;
4144
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004145 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 if (status < 0)
4147 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004148 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 return 0;
4150}
4151
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004152static inline int nfs4_server_supports_acls(struct nfs_server *server)
4153{
4154 return (server->caps & NFS_CAP_ACLS)
4155 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
4156 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
4157}
4158
Trond Myklebust21f498c2012-08-24 10:59:25 -04004159/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4160 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004161 * the stack.
4162 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004163#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004164
Neil Hormane9e3d722011-03-04 19:26:03 -05004165static int buf_to_pages_noslab(const void *buf, size_t buflen,
4166 struct page **pages, unsigned int *pgbase)
4167{
4168 struct page *newpage, **spages;
4169 int rc = 0;
4170 size_t len;
4171 spages = pages;
4172
4173 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004174 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004175 newpage = alloc_page(GFP_KERNEL);
4176
4177 if (newpage == NULL)
4178 goto unwind;
4179 memcpy(page_address(newpage), buf, len);
4180 buf += len;
4181 buflen -= len;
4182 *pages++ = newpage;
4183 rc++;
4184 } while (buflen != 0);
4185
4186 return rc;
4187
4188unwind:
4189 for(; rc > 0; rc--)
4190 __free_page(spages[rc-1]);
4191 return -ENOMEM;
4192}
4193
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004194struct nfs4_cached_acl {
4195 int cached;
4196 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004197 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004198};
4199
4200static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004201{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004202 struct nfs_inode *nfsi = NFS_I(inode);
4203
4204 spin_lock(&inode->i_lock);
4205 kfree(nfsi->nfs4_acl);
4206 nfsi->nfs4_acl = acl;
4207 spin_unlock(&inode->i_lock);
4208}
4209
4210static void nfs4_zap_acl_attr(struct inode *inode)
4211{
4212 nfs4_set_cached_acl(inode, NULL);
4213}
4214
4215static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4216{
4217 struct nfs_inode *nfsi = NFS_I(inode);
4218 struct nfs4_cached_acl *acl;
4219 int ret = -ENOENT;
4220
4221 spin_lock(&inode->i_lock);
4222 acl = nfsi->nfs4_acl;
4223 if (acl == NULL)
4224 goto out;
4225 if (buf == NULL) /* user is just asking for length */
4226 goto out_len;
4227 if (acl->cached == 0)
4228 goto out;
4229 ret = -ERANGE; /* see getxattr(2) man page */
4230 if (acl->len > buflen)
4231 goto out;
4232 memcpy(buf, acl->data, acl->len);
4233out_len:
4234 ret = acl->len;
4235out:
4236 spin_unlock(&inode->i_lock);
4237 return ret;
4238}
4239
Sachin Prabhu5794d212012-04-17 14:36:40 +01004240static 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 +00004241{
4242 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004243 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004244
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004245 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004246 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004247 if (acl == NULL)
4248 goto out;
4249 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004250 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004251 } else {
4252 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4253 if (acl == NULL)
4254 goto out;
4255 acl->cached = 0;
4256 }
4257 acl->len = acl_len;
4258out:
4259 nfs4_set_cached_acl(inode, acl);
4260}
4261
Andy Adamsonbf118a32011-12-07 11:55:27 -05004262/*
4263 * The getxattr API returns the required buffer length when called with a
4264 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4265 * the required buf. On a NULL buf, we send a page of data to the server
4266 * guessing that the ACL request can be serviced by a page. If so, we cache
4267 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4268 * the cache. If not so, we throw away the page, and cache the required
4269 * length. The next getxattr call will then produce another round trip to
4270 * the server, this time with the input buf of the required size.
4271 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004272static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004273{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004274 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004275 struct nfs_getaclargs args = {
4276 .fh = NFS_FH(inode),
4277 .acl_pages = pages,
4278 .acl_len = buflen,
4279 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004280 struct nfs_getaclres res = {
4281 .acl_len = buflen,
4282 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004283 struct rpc_message msg = {
4284 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4285 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004286 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004287 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004288 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4289 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004290
Andy Adamsonbf118a32011-12-07 11:55:27 -05004291 /* As long as we're doing a round trip to the server anyway,
4292 * let's be prepared for a page of acl data. */
4293 if (npages == 0)
4294 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004295 if (npages > ARRAY_SIZE(pages))
4296 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004297
Andy Adamsonbf118a32011-12-07 11:55:27 -05004298 for (i = 0; i < npages; i++) {
4299 pages[i] = alloc_page(GFP_KERNEL);
4300 if (!pages[i])
4301 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004302 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004303
4304 /* for decoding across pages */
4305 res.acl_scratch = alloc_page(GFP_KERNEL);
4306 if (!res.acl_scratch)
4307 goto out_free;
4308
Andy Adamsonbf118a32011-12-07 11:55:27 -05004309 args.acl_len = npages * PAGE_SIZE;
4310 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004311
Peng Taode040be2012-01-10 22:42:47 +08004312 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004313 __func__, buf, buflen, npages, args.acl_len);
4314 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4315 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004316 if (ret)
4317 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004318
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004319 /* Handle the case where the passed-in buffer is too short */
4320 if (res.acl_flags & NFS4_ACL_TRUNC) {
4321 /* Did the user only issue a request for the acl length? */
4322 if (buf == NULL)
4323 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004324 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004325 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004326 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004327 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004328 if (buf) {
4329 if (res.acl_len > buflen) {
4330 ret = -ERANGE;
4331 goto out_free;
4332 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004333 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004334 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004335out_ok:
4336 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004337out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004338 for (i = 0; i < npages; i++)
4339 if (pages[i])
4340 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004341 if (res.acl_scratch)
4342 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004343 return ret;
4344}
4345
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004346static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4347{
4348 struct nfs4_exception exception = { };
4349 ssize_t ret;
4350 do {
4351 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04004352 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004353 if (ret >= 0)
4354 break;
4355 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4356 } while (exception.retry);
4357 return ret;
4358}
4359
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004360static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4361{
4362 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004363 int ret;
4364
4365 if (!nfs4_server_supports_acls(server))
4366 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004367 ret = nfs_revalidate_inode(server, inode);
4368 if (ret < 0)
4369 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004370 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4371 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004372 ret = nfs4_read_cached_acl(inode, buf, buflen);
4373 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004374 /* -ENOENT is returned if there is no ACL or if there is an ACL
4375 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004376 return ret;
4377 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004378}
4379
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004380static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004381{
4382 struct nfs_server *server = NFS_SERVER(inode);
4383 struct page *pages[NFS4ACL_MAXPAGES];
4384 struct nfs_setaclargs arg = {
4385 .fh = NFS_FH(inode),
4386 .acl_pages = pages,
4387 .acl_len = buflen,
4388 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004389 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004390 struct rpc_message msg = {
4391 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4392 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004393 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004394 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004395 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004396 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004397
4398 if (!nfs4_server_supports_acls(server))
4399 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004400 if (npages > ARRAY_SIZE(pages))
4401 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004402 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4403 if (i < 0)
4404 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004405 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004406 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004407
4408 /*
4409 * Free each page after tx, so the only ref left is
4410 * held by the network stack
4411 */
4412 for (; i > 0; i--)
4413 put_page(pages[i-1]);
4414
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004415 /*
4416 * Acl update can result in inode attribute update.
4417 * so mark the attribute cache invalid.
4418 */
4419 spin_lock(&inode->i_lock);
4420 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4421 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004422 nfs_access_zap_cache(inode);
4423 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004424 return ret;
4425}
4426
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004427static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4428{
4429 struct nfs4_exception exception = { };
4430 int err;
4431 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004432 err = __nfs4_proc_set_acl(inode, buf, buflen);
4433 trace_nfs4_set_acl(inode, err);
4434 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004435 &exception);
4436 } while (exception.retry);
4437 return err;
4438}
4439
David Quigleyaa9c2662013-05-22 12:50:44 -04004440#ifdef CONFIG_NFS_V4_SECURITY_LABEL
4441static int _nfs4_get_security_label(struct inode *inode, void *buf,
4442 size_t buflen)
4443{
4444 struct nfs_server *server = NFS_SERVER(inode);
4445 struct nfs_fattr fattr;
4446 struct nfs4_label label = {0, 0, buflen, buf};
4447
4448 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4449 struct nfs4_getattr_arg args = {
4450 .fh = NFS_FH(inode),
4451 .bitmask = bitmask,
4452 };
4453 struct nfs4_getattr_res res = {
4454 .fattr = &fattr,
4455 .label = &label,
4456 .server = server,
4457 };
4458 struct rpc_message msg = {
4459 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4460 .rpc_argp = &args,
4461 .rpc_resp = &res,
4462 };
4463 int ret;
4464
4465 nfs_fattr_init(&fattr);
4466
4467 ret = rpc_call_sync(server->client, &msg, 0);
4468 if (ret)
4469 return ret;
4470 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4471 return -ENOENT;
4472 if (buflen < label.len)
4473 return -ERANGE;
4474 return 0;
4475}
4476
4477static int nfs4_get_security_label(struct inode *inode, void *buf,
4478 size_t buflen)
4479{
4480 struct nfs4_exception exception = { };
4481 int err;
4482
4483 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4484 return -EOPNOTSUPP;
4485
4486 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004487 err = _nfs4_get_security_label(inode, buf, buflen);
4488 trace_nfs4_get_security_label(inode, err);
4489 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004490 &exception);
4491 } while (exception.retry);
4492 return err;
4493}
4494
4495static int _nfs4_do_set_security_label(struct inode *inode,
4496 struct nfs4_label *ilabel,
4497 struct nfs_fattr *fattr,
4498 struct nfs4_label *olabel)
4499{
4500
4501 struct iattr sattr = {0};
4502 struct nfs_server *server = NFS_SERVER(inode);
4503 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4504 struct nfs_setattrargs args = {
4505 .fh = NFS_FH(inode),
4506 .iap = &sattr,
4507 .server = server,
4508 .bitmask = bitmask,
4509 .label = ilabel,
4510 };
4511 struct nfs_setattrres res = {
4512 .fattr = fattr,
4513 .label = olabel,
4514 .server = server,
4515 };
4516 struct rpc_message msg = {
4517 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
4518 .rpc_argp = &args,
4519 .rpc_resp = &res,
4520 };
4521 int status;
4522
4523 nfs4_stateid_copy(&args.stateid, &zero_stateid);
4524
4525 status = rpc_call_sync(server->client, &msg, 0);
4526 if (status)
4527 dprintk("%s failed: %d\n", __func__, status);
4528
4529 return status;
4530}
4531
4532static int nfs4_do_set_security_label(struct inode *inode,
4533 struct nfs4_label *ilabel,
4534 struct nfs_fattr *fattr,
4535 struct nfs4_label *olabel)
4536{
4537 struct nfs4_exception exception = { };
4538 int err;
4539
4540 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004541 err = _nfs4_do_set_security_label(inode, ilabel,
4542 fattr, olabel);
4543 trace_nfs4_set_security_label(inode, err);
4544 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004545 &exception);
4546 } while (exception.retry);
4547 return err;
4548}
4549
4550static int
4551nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4552{
4553 struct nfs4_label ilabel, *olabel = NULL;
4554 struct nfs_fattr fattr;
4555 struct rpc_cred *cred;
4556 struct inode *inode = dentry->d_inode;
4557 int status;
4558
4559 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4560 return -EOPNOTSUPP;
4561
4562 nfs_fattr_init(&fattr);
4563
4564 ilabel.pi = 0;
4565 ilabel.lfs = 0;
4566 ilabel.label = (char *)buf;
4567 ilabel.len = buflen;
4568
4569 cred = rpc_lookup_cred();
4570 if (IS_ERR(cred))
4571 return PTR_ERR(cred);
4572
4573 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4574 if (IS_ERR(olabel)) {
4575 status = -PTR_ERR(olabel);
4576 goto out;
4577 }
4578
4579 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4580 if (status == 0)
4581 nfs_setsecurity(inode, &fattr, olabel);
4582
4583 nfs4_label_free(olabel);
4584out:
4585 put_rpccred(cred);
4586 return status;
4587}
4588#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4589
4590
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004592nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004594 struct nfs_client *clp = server->nfs_client;
4595
4596 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 return 0;
4598 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004599 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004600 case -NFS4ERR_ADMIN_REVOKED:
4601 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004602 if (state == NULL)
4603 break;
4604 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004605 case -NFS4ERR_OPENMODE:
4606 if (state == NULL)
4607 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004608 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4609 goto stateid_invalid;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004610 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004611 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004612 if (state != NULL) {
4613 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4614 goto stateid_invalid;
4615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004617 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004618 nfs4_schedule_lease_recovery(clp);
4619 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004620#if defined(CONFIG_NFS_V4_1)
4621 case -NFS4ERR_BADSESSION:
4622 case -NFS4ERR_BADSLOT:
4623 case -NFS4ERR_BAD_HIGH_SLOT:
4624 case -NFS4ERR_DEADSESSION:
4625 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4626 case -NFS4ERR_SEQ_FALSE_RETRY:
4627 case -NFS4ERR_SEQ_MISORDERED:
4628 dprintk("%s ERROR %d, Reset session\n", __func__,
4629 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004630 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004631 task->tk_status = 0;
4632 return -EAGAIN;
4633#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004635 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004636 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4638 task->tk_status = 0;
4639 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004640 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 case -NFS4ERR_OLD_STATEID:
4642 task->tk_status = 0;
4643 return -EAGAIN;
4644 }
4645 task->tk_status = nfs4_map_errors(task->tk_status);
4646 return 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004647stateid_invalid:
4648 task->tk_status = -EIO;
4649 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004650wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004651 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004652 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4653 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4654 task->tk_status = 0;
4655 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656}
4657
Chuck Leverf0920752012-05-21 22:45:41 -04004658static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4659 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004660{
4661 __be32 verf[2];
4662
Chuck Lever2c820d92012-05-21 22:45:33 -04004663 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4664 /* An impossible timestamp guarantees this value
4665 * will never match a generated boot time. */
4666 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04004667 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04004668 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004669 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04004670 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
4671 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04004672 }
Chuck Levercd937102012-03-02 17:14:31 -05004673 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4674}
4675
Chuck Levere984a552012-09-14 17:24:21 -04004676static unsigned int
4677nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4678 char *buf, size_t len)
4679{
4680 unsigned int result;
4681
4682 rcu_read_lock();
4683 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4684 clp->cl_ipaddr,
4685 rpc_peeraddr2str(clp->cl_rpcclient,
4686 RPC_DISPLAY_ADDR),
4687 rpc_peeraddr2str(clp->cl_rpcclient,
4688 RPC_DISPLAY_PROTO));
4689 rcu_read_unlock();
4690 return result;
4691}
4692
4693static unsigned int
4694nfs4_init_uniform_client_string(const struct nfs_client *clp,
4695 char *buf, size_t len)
4696{
Trond Myklebust55b59292013-07-24 16:36:35 -04004697 const char *nodename = clp->cl_rpcclient->cl_nodename;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004698
4699 if (nfs4_client_id_uniquifier[0] != '\0')
Trond Myklebust55b59292013-07-24 16:36:35 -04004700 return scnprintf(buf, len, "Linux NFSv%u.%u %s/%s",
4701 clp->rpc_ops->version,
4702 clp->cl_minorversion,
4703 nfs4_client_id_uniquifier,
4704 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004705 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4706 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004707 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004708}
4709
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004710/**
4711 * nfs4_proc_setclientid - Negotiate client ID
4712 * @clp: state data structure
4713 * @program: RPC program for NFSv4 callback service
4714 * @port: IP port number for NFS4 callback service
4715 * @cred: RPC credential to use for this call
4716 * @res: where to place the result
4717 *
4718 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4719 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004720int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4721 unsigned short port, struct rpc_cred *cred,
4722 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723{
4724 nfs4_verifier sc_verifier;
4725 struct nfs4_setclientid setclientid = {
4726 .sc_verifier = &sc_verifier,
4727 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004728 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 };
4730 struct rpc_message msg = {
4731 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4732 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004733 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004734 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004736 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737
Chuck Leverde734832012-07-11 16:30:50 -04004738 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004739 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004740 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4741 setclientid.sc_name_len =
4742 nfs4_init_uniform_client_string(clp,
4743 setclientid.sc_name,
4744 sizeof(setclientid.sc_name));
4745 else
4746 setclientid.sc_name_len =
4747 nfs4_init_nonuniform_client_string(clp,
4748 setclientid.sc_name,
4749 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004750 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004751 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004752 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Djalal Harounife2d5392013-06-18 17:58:12 +01004753 sizeof(setclientid.sc_netid), "%s",
Chuck Leverd4d3c502007-12-10 14:57:09 -05004754 rpc_peeraddr2str(clp->cl_rpcclient,
4755 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004756 rcu_read_unlock();
4757 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004758 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 clp->cl_ipaddr, port >> 8, port & 255);
4760
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004761 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4762 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4763 setclientid.sc_name_len, setclientid.sc_name);
4764 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004765 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004766 dprintk("NFS reply setclientid: %d\n", status);
4767 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768}
4769
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004770/**
4771 * nfs4_proc_setclientid_confirm - Confirm client ID
4772 * @clp: state data structure
4773 * @res: result of a previous SETCLIENTID
4774 * @cred: RPC credential to use for this call
4775 *
4776 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4777 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004778int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004779 struct nfs4_setclientid_res *arg,
4780 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 struct rpc_message msg = {
4783 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004784 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004785 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 int status;
4788
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004789 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4790 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4791 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004792 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004793 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004794 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795 return status;
4796}
4797
Trond Myklebustfe650402006-01-03 09:55:18 +01004798struct nfs4_delegreturndata {
4799 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004800 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004801 struct nfs_fh fh;
4802 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004803 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004804 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004805 int rpc_status;
4806};
4807
Trond Myklebustfe650402006-01-03 09:55:18 +01004808static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4809{
4810 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004811
Trond Myklebust14516c32010-07-31 14:29:06 -04004812 if (!nfs4_sequence_done(task, &data->res.seq_res))
4813 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004814
Trond Myklebustca8acf82013-08-13 10:36:56 -04004815 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004816 switch (task->tk_status) {
4817 case -NFS4ERR_STALE_STATEID:
4818 case -NFS4ERR_EXPIRED:
4819 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004820 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004821 break;
4822 default:
4823 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4824 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004825 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004826 return;
4827 }
4828 }
4829 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004830}
4831
4832static void nfs4_delegreturn_release(void *calldata)
4833{
Trond Myklebustfe650402006-01-03 09:55:18 +01004834 kfree(calldata);
4835}
4836
Andy Adamson938e1012009-04-01 09:22:28 -04004837#if defined(CONFIG_NFS_V4_1)
4838static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4839{
4840 struct nfs4_delegreturndata *d_data;
4841
4842 d_data = (struct nfs4_delegreturndata *)data;
4843
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004844 nfs4_setup_sequence(d_data->res.server,
4845 &d_data->args.seq_args,
4846 &d_data->res.seq_res,
4847 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004848}
4849#endif /* CONFIG_NFS_V4_1 */
4850
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004851static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004852#if defined(CONFIG_NFS_V4_1)
4853 .rpc_call_prepare = nfs4_delegreturn_prepare,
4854#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004855 .rpc_call_done = nfs4_delegreturn_done,
4856 .rpc_release = nfs4_delegreturn_release,
4857};
4858
Trond Myklebuste6f81072008-01-24 18:14:34 -05004859static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004860{
4861 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004862 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004863 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004864 struct rpc_message msg = {
4865 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4866 .rpc_cred = cred,
4867 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004868 struct rpc_task_setup task_setup_data = {
4869 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004870 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004871 .callback_ops = &nfs4_delegreturn_ops,
4872 .flags = RPC_TASK_ASYNC,
4873 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004874 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004875
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004876 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004877 if (data == NULL)
4878 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004879 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004880 data->args.fhandle = &data->fh;
4881 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004882 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004883 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004884 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004885 data->res.fattr = &data->fattr;
4886 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004887 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004888 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004889 data->rpc_status = 0;
4890
Trond Myklebustc970aa82007-07-14 15:39:59 -04004891 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004892 msg.rpc_argp = &data->args;
4893 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004894 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004895 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004896 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004897 if (!issync)
4898 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004899 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004900 if (status != 0)
4901 goto out;
4902 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004903 if (status == 0)
4904 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4905 else
4906 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004907out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004908 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004909 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910}
4911
Trond Myklebuste6f81072008-01-24 18:14:34 -05004912int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913{
4914 struct nfs_server *server = NFS_SERVER(inode);
4915 struct nfs4_exception exception = { };
4916 int err;
4917 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004918 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Trond Myklebustca8acf82013-08-13 10:36:56 -04004919 trace_nfs4_delegreturn(inode, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920 switch (err) {
4921 case -NFS4ERR_STALE_STATEID:
4922 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 case 0:
4924 return 0;
4925 }
4926 err = nfs4_handle_exception(server, err, &exception);
4927 } while (exception.retry);
4928 return err;
4929}
4930
4931#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4932#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4933
4934/*
4935 * sleep, with exponential backoff, and retry the LOCK operation.
4936 */
4937static unsigned long
4938nfs4_set_lock_task_retry(unsigned long timeout)
4939{
Colin Cross416ad3c2013-05-06 23:50:06 +00004940 freezable_schedule_timeout_killable_unsafe(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 timeout <<= 1;
4942 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4943 return NFS4_LOCK_MAXTIMEOUT;
4944 return timeout;
4945}
4946
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4948{
4949 struct inode *inode = state->inode;
4950 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004951 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004952 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004954 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004956 struct nfs_lockt_res res = {
4957 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958 };
4959 struct rpc_message msg = {
4960 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4961 .rpc_argp = &arg,
4962 .rpc_resp = &res,
4963 .rpc_cred = state->owner->so_cred,
4964 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 struct nfs4_lock_state *lsp;
4966 int status;
4967
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004968 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004969 status = nfs4_set_lock_state(state, request);
4970 if (status != 0)
4971 goto out;
4972 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004973 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004974 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004975 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004976 switch (status) {
4977 case 0:
4978 request->fl_type = F_UNLCK;
4979 break;
4980 case -NFS4ERR_DENIED:
4981 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004983 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004984out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 return status;
4986}
4987
4988static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4989{
4990 struct nfs4_exception exception = { };
4991 int err;
4992
4993 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04004994 err = _nfs4_proc_getlk(state, cmd, request);
4995 trace_nfs4_get_lock(request, state, cmd, err);
4996 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 &exception);
4998 } while (exception.retry);
4999 return err;
5000}
5001
5002static int do_vfs_lock(struct file *file, struct file_lock *fl)
5003{
5004 int res = 0;
5005 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5006 case FL_POSIX:
5007 res = posix_lock_file_wait(file, fl);
5008 break;
5009 case FL_FLOCK:
5010 res = flock_lock_file_wait(file, fl);
5011 break;
5012 default:
5013 BUG();
5014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015 return res;
5016}
5017
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005018struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005019 struct nfs_locku_args arg;
5020 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005021 struct nfs4_lock_state *lsp;
5022 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005023 struct file_lock fl;
5024 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005025 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005026};
5027
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005028static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5029 struct nfs_open_context *ctx,
5030 struct nfs4_lock_state *lsp,
5031 struct nfs_seqid *seqid)
5032{
5033 struct nfs4_unlockdata *p;
5034 struct inode *inode = lsp->ls_state->inode;
5035
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005036 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005037 if (p == NULL)
5038 return NULL;
5039 p->arg.fh = NFS_FH(inode);
5040 p->arg.fl = &p->fl;
5041 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005042 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005043 p->arg.stateid = &lsp->ls_stateid;
5044 p->lsp = lsp;
5045 atomic_inc(&lsp->ls_count);
5046 /* Ensure we don't close file until we're done freeing locks! */
5047 p->ctx = get_nfs_open_context(ctx);
5048 memcpy(&p->fl, fl, sizeof(p->fl));
5049 p->server = NFS_SERVER(inode);
5050 return p;
5051}
5052
Trond Myklebust06f814a2006-01-03 09:55:07 +01005053static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005054{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005055 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005056 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005057 nfs4_put_lock_state(calldata->lsp);
5058 put_nfs_open_context(calldata->ctx);
5059 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005060}
5061
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005062static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005063{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005064 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005065
Trond Myklebust14516c32010-07-31 14:29:06 -04005066 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5067 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005068 switch (task->tk_status) {
5069 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05005070 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
5071 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005072 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005073 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005074 case -NFS4ERR_BAD_STATEID:
5075 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005076 case -NFS4ERR_STALE_STATEID:
5077 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005078 break;
5079 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005080 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005081 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005082 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005083 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005084}
5085
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005086static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005087{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005088 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005090 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005091 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04005092 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005093 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005094 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005095 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005096 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005097 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005098 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005099 &calldata->res.seq_res,
5100 task) != 0)
5101 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005102 return;
5103out_no_action:
5104 task->tk_action = NULL;
5105out_wait:
5106 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107}
5108
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005109static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005110 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005111 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005112 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005113};
5114
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005115static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5116 struct nfs_open_context *ctx,
5117 struct nfs4_lock_state *lsp,
5118 struct nfs_seqid *seqid)
5119{
5120 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005121 struct rpc_message msg = {
5122 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5123 .rpc_cred = ctx->cred,
5124 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005125 struct rpc_task_setup task_setup_data = {
5126 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005127 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005128 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005129 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005130 .flags = RPC_TASK_ASYNC,
5131 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005132
Frank Filz137d6ac2007-07-09 15:32:29 -07005133 /* Ensure this is an unlock - when canceling a lock, the
5134 * canceled lock is passed in, and it won't be an unlock.
5135 */
5136 fl->fl_type = F_UNLCK;
5137
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005138 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5139 if (data == NULL) {
5140 nfs_free_seqid(seqid);
5141 return ERR_PTR(-ENOMEM);
5142 }
5143
Trond Myklebust9d12b212012-01-17 22:04:25 -05005144 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005145 msg.rpc_argp = &data->arg;
5146 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005147 task_setup_data.callback_data = data;
5148 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005149}
5150
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5152{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005153 struct inode *inode = state->inode;
5154 struct nfs4_state_owner *sp = state->owner;
5155 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005156 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005157 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005158 struct rpc_task *task;
5159 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005160 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161
Trond Myklebust9b073572006-06-29 16:38:34 -04005162 status = nfs4_set_lock_state(state, request);
5163 /* Unlock _before_ we do the RPC call */
5164 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005165 /* Exclude nfs_delegation_claim_locks() */
5166 mutex_lock(&sp->so_delegreturn_mutex);
5167 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005168 down_read(&nfsi->rwsem);
5169 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
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 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005173 }
5174 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005175 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005176 if (status != 0)
5177 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005178 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005179 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005180 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5181 goto out;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005182 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005183 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005184 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04005185 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005186 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005187 status = PTR_ERR(task);
5188 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005189 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005190 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005191 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005192out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005193 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005194 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005195 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196}
5197
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005198struct nfs4_lockdata {
5199 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005200 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005201 struct nfs4_lock_state *lsp;
5202 struct nfs_open_context *ctx;
5203 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005204 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005205 int rpc_status;
5206 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005207 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005208};
5209
5210static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005211 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5212 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005213{
5214 struct nfs4_lockdata *p;
5215 struct inode *inode = lsp->ls_state->inode;
5216 struct nfs_server *server = NFS_SERVER(inode);
5217
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005218 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005219 if (p == NULL)
5220 return NULL;
5221
5222 p->arg.fh = NFS_FH(inode);
5223 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005224 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005225 if (p->arg.open_seqid == NULL)
5226 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005227 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005228 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005229 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005230 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04005231 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005232 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005233 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005234 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005235 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005236 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005237 atomic_inc(&lsp->ls_count);
5238 p->ctx = get_nfs_open_context(ctx);
5239 memcpy(&p->fl, fl, sizeof(p->fl));
5240 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005241out_free_seqid:
5242 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005243out_free:
5244 kfree(p);
5245 return NULL;
5246}
5247
5248static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5249{
5250 struct nfs4_lockdata *data = calldata;
5251 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252
Harvey Harrison3110ff82008-05-02 13:42:44 -07005253 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005254 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005255 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005256 /* Do we need to do an open_to_lock_owner? */
5257 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005258 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005259 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005260 }
Trond Myklebust8e472f32013-04-20 01:30:53 -04005261 data->arg.open_stateid = &state->open_stateid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005262 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005263 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005264 } else
5265 data->arg.new_lock_owner = 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04005266 if (!nfs4_valid_open_stateid(state)) {
5267 data->rpc_status = -EBADF;
5268 task->tk_action = NULL;
5269 goto out_release_open_seqid;
5270 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005271 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005272 if (nfs4_setup_sequence(data->server,
5273 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005274 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005275 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04005276 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04005277out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005278 nfs_release_seqid(data->arg.open_seqid);
5279out_release_lock_seqid:
5280 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005281out_wait:
5282 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005283 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005284}
5285
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005286static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5287{
5288 struct nfs4_lockdata *data = calldata;
5289
Harvey Harrison3110ff82008-05-02 13:42:44 -07005290 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005291
Trond Myklebust14516c32010-07-31 14:29:06 -04005292 if (!nfs4_sequence_done(task, &data->res.seq_res))
5293 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04005294
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005295 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005296 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005297 if (data->rpc_status == 0)
5298 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
5299 else
5300 goto out;
5301 }
5302 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05005303 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005304 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04005305 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005306 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005307out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07005308 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005309}
5310
5311static void nfs4_lock_release(void *calldata)
5312{
5313 struct nfs4_lockdata *data = calldata;
5314
Harvey Harrison3110ff82008-05-02 13:42:44 -07005315 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005316 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005317 if (data->cancelled != 0) {
5318 struct rpc_task *task;
5319 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5320 data->arg.lock_seqid);
5321 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08005322 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005323 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005324 } else
5325 nfs_free_seqid(data->arg.lock_seqid);
5326 nfs4_put_lock_state(data->lsp);
5327 put_nfs_open_context(data->ctx);
5328 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005329 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005330}
5331
5332static const struct rpc_call_ops nfs4_lock_ops = {
5333 .rpc_call_prepare = nfs4_lock_prepare,
5334 .rpc_call_done = nfs4_lock_done,
5335 .rpc_release = nfs4_lock_release,
5336};
5337
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005338static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5339{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005340 switch (error) {
5341 case -NFS4ERR_ADMIN_REVOKED:
5342 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05005343 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005344 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04005345 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05005346 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005347 break;
5348 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005349 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05005350 case -NFS4ERR_EXPIRED:
5351 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005352 };
5353}
5354
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005355static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005356{
5357 struct nfs4_lockdata *data;
5358 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005359 struct rpc_message msg = {
5360 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5361 .rpc_cred = state->owner->so_cred,
5362 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005363 struct rpc_task_setup task_setup_data = {
5364 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005365 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005366 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005367 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005368 .flags = RPC_TASK_ASYNC,
5369 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005370 int ret;
5371
Harvey Harrison3110ff82008-05-02 13:42:44 -07005372 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005373 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005374 fl->fl_u.nfs4_fl.owner,
5375 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005376 if (data == NULL)
5377 return -ENOMEM;
5378 if (IS_SETLKW(cmd))
5379 data->arg.block = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -05005380 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005381 msg.rpc_argp = &data->arg;
5382 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005383 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005384 if (recovery_type > NFS_LOCK_NEW) {
5385 if (recovery_type == NFS_LOCK_RECLAIM)
5386 data->arg.reclaim = NFS_LOCK_RECLAIM;
5387 nfs4_set_sequence_privileged(&data->arg.seq_args);
5388 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04005389 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005390 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005391 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005392 ret = nfs4_wait_for_completion_rpc_task(task);
5393 if (ret == 0) {
5394 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005395 if (ret)
5396 nfs4_handle_setlk_error(data->server, data->lsp,
5397 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005398 } else
5399 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005400 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005401 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005402 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403}
5404
5405static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5406{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005407 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005408 struct nfs4_exception exception = {
5409 .inode = state->inode,
5410 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005411 int err;
5412
5413 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005414 /* Cache the lock if possible... */
5415 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5416 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005417 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005418 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
Trond Myklebust168667c2010-10-19 19:47:49 -04005419 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00005420 break;
5421 nfs4_handle_exception(server, err, &exception);
5422 } while (exception.retry);
5423 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424}
5425
5426static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5427{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005428 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005429 struct nfs4_exception exception = {
5430 .inode = state->inode,
5431 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005432 int err;
5433
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005434 err = nfs4_set_lock_state(state, request);
5435 if (err != 0)
5436 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00005437 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005438 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5439 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005440 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005441 trace_nfs4_lock_expired(request, state, F_SETLK, err);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005442 switch (err) {
5443 default:
5444 goto out;
5445 case -NFS4ERR_GRACE:
5446 case -NFS4ERR_DELAY:
5447 nfs4_handle_exception(server, err, &exception);
5448 err = 0;
5449 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005450 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005451out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00005452 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453}
5454
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005455#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005456/**
5457 * nfs41_check_expired_locks - possibly free a lock stateid
5458 *
5459 * @state: NFSv4 state for an inode
5460 *
5461 * Returns NFS_OK if recovery for this stateid is now finished.
5462 * Otherwise a negative NFS4ERR value is returned.
5463 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005464static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005465{
Chuck Levereb64cf92012-07-11 16:30:05 -04005466 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005467 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005468 struct nfs_server *server = NFS_SERVER(state->inode);
5469
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005470 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04005471 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005472 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
5473
5474 status = nfs41_test_stateid(server,
5475 &lsp->ls_stateid,
5476 cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04005477 trace_nfs4_test_lock_stateid(state, lsp, status);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005478 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005479 /* Free the stateid unless the server
5480 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04005481 if (status != -NFS4ERR_BAD_STATEID)
5482 nfs41_free_stateid(server,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005483 &lsp->ls_stateid,
5484 cred);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005485 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005486 ret = status;
5487 }
5488 }
5489 };
5490
5491 return ret;
5492}
5493
5494static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5495{
5496 int status = NFS_OK;
5497
5498 if (test_bit(LK_STATE_IN_USE, &state->flags))
5499 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005500 if (status != NFS_OK)
5501 status = nfs4_lock_expired(state, request);
5502 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005503}
5504#endif
5505
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5507{
Trond Myklebust9a99af492013-02-04 20:17:49 -05005508 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005509 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005510 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005511 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005512 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005514 if ((fl_flags & FL_POSIX) &&
5515 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5516 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005517 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005518 status = nfs4_set_lock_state(state, request);
5519 if (status != 0)
5520 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005521 request->fl_flags |= FL_ACCESS;
5522 status = do_vfs_lock(request->fl_file, request);
5523 if (status < 0)
5524 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005525 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005526 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005527 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005528 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005529 request->fl_flags = fl_flags & ~FL_SLEEP;
5530 status = do_vfs_lock(request->fl_file, request);
5531 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005532 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05005533 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5534 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005535 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005536 if (status != 0)
Trond Myklebust9a99af492013-02-04 20:17:49 -05005537 goto out;
5538 down_read(&nfsi->rwsem);
5539 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5540 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005541 goto out_unlock;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005542 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005543 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005544 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005545 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005546 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5547 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005548out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005549 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005550out:
5551 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552 return status;
5553}
5554
5555static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5556{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005557 struct nfs4_exception exception = {
5558 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005559 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005560 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561 int err;
5562
5563 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005564 err = _nfs4_proc_setlk(state, cmd, request);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005565 trace_nfs4_set_lock(request, state, cmd, err);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005566 if (err == -NFS4ERR_DENIED)
5567 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005569 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 } while (exception.retry);
5571 return err;
5572}
5573
5574static int
5575nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5576{
5577 struct nfs_open_context *ctx;
5578 struct nfs4_state *state;
5579 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5580 int status;
5581
5582 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005583 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584 state = ctx->state;
5585
5586 if (request->fl_start < 0 || request->fl_end < 0)
5587 return -EINVAL;
5588
Trond Myklebustd9531262009-07-21 19:22:38 -04005589 if (IS_GETLK(cmd)) {
5590 if (state != NULL)
5591 return nfs4_proc_getlk(state, F_GETLK, request);
5592 return 0;
5593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594
5595 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5596 return -EINVAL;
5597
Trond Myklebustd9531262009-07-21 19:22:38 -04005598 if (request->fl_type == F_UNLCK) {
5599 if (state != NULL)
5600 return nfs4_proc_unlck(state, cmd, request);
5601 return 0;
5602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603
Trond Myklebustd9531262009-07-21 19:22:38 -04005604 if (state == NULL)
5605 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005606 /*
5607 * Don't rely on the VFS having checked the file open mode,
5608 * since it won't do this for flock() locks.
5609 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005610 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005611 case F_RDLCK:
5612 if (!(filp->f_mode & FMODE_READ))
5613 return -EBADF;
5614 break;
5615 case F_WRLCK:
5616 if (!(filp->f_mode & FMODE_WRITE))
5617 return -EBADF;
5618 }
5619
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620 do {
5621 status = nfs4_proc_setlk(state, cmd, request);
5622 if ((status != -EAGAIN) || IS_SETLK(cmd))
5623 break;
5624 timeout = nfs4_set_lock_task_retry(timeout);
5625 status = -ERESTARTSYS;
5626 if (signalled())
5627 break;
5628 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629 return status;
5630}
5631
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005632int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05005633{
5634 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05005635 int err;
5636
5637 err = nfs4_set_lock_state(state, fl);
5638 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005639 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04005640 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005641 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05005642}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005643
Trond Myklebustcf470c32012-03-07 13:49:12 -05005644struct nfs_release_lockowner_data {
5645 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005646 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005647 struct nfs_release_lockowner_args args;
5648};
5649
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005650static void nfs4_release_lockowner_release(void *calldata)
5651{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005652 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005653 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005654 kfree(calldata);
5655}
5656
Trond Myklebust17280172012-03-11 13:11:00 -04005657static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005658 .rpc_release = nfs4_release_lockowner_release,
5659};
5660
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04005661static int nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005662{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005663 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005664 struct rpc_message msg = {
5665 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5666 };
5667
5668 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005669 return -EINVAL;
5670 data = kmalloc(sizeof(*data), GFP_NOFS);
5671 if (!data)
5672 return -ENOMEM;
5673 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005674 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005675 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5676 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5677 data->args.lock_owner.s_dev = server->s_dev;
5678 msg.rpc_argp = &data->args;
5679 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5680 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005681}
5682
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005683#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5684
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005685static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5686 const void *buf, size_t buflen,
5687 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005688{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005689 if (strcmp(key, "") != 0)
5690 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005691
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005692 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005693}
5694
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005695static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5696 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005697{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005698 if (strcmp(key, "") != 0)
5699 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005700
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005701 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005702}
5703
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005704static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5705 size_t list_len, const char *name,
5706 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005707{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005708 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005709
J. Bruce Fields096455a2006-03-20 23:23:42 -05005710 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5711 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005712
5713 if (list && len <= list_len)
5714 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005715 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005716}
5717
David Quigleyc9bccef2013-05-22 12:50:45 -04005718#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5719static inline int nfs4_server_supports_labels(struct nfs_server *server)
5720{
5721 return server->caps & NFS_CAP_SECURITY_LABEL;
5722}
5723
5724static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
5725 const void *buf, size_t buflen,
5726 int flags, int type)
5727{
5728 if (security_ismaclabel(key))
5729 return nfs4_set_security_label(dentry, buf, buflen);
5730
5731 return -EOPNOTSUPP;
5732}
5733
5734static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
5735 void *buf, size_t buflen, int type)
5736{
5737 if (security_ismaclabel(key))
5738 return nfs4_get_security_label(dentry->d_inode, buf, buflen);
5739 return -EOPNOTSUPP;
5740}
5741
5742static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
5743 size_t list_len, const char *name,
5744 size_t name_len, int type)
5745{
5746 size_t len = 0;
5747
5748 if (nfs_server_capable(dentry->d_inode, NFS_CAP_SECURITY_LABEL)) {
5749 len = security_inode_listsecurity(dentry->d_inode, NULL, 0);
5750 if (list && len <= list_len)
5751 security_inode_listsecurity(dentry->d_inode, list, len);
5752 }
5753 return len;
5754}
5755
5756static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
5757 .prefix = XATTR_SECURITY_PREFIX,
5758 .list = nfs4_xattr_list_nfs4_label,
5759 .get = nfs4_xattr_get_nfs4_label,
5760 .set = nfs4_xattr_set_nfs4_label,
5761};
5762#endif
5763
5764
Andy Adamson533eb462011-06-13 18:25:56 -04005765/*
5766 * nfs_fhget will use either the mounted_on_fileid or the fileid
5767 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005768static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5769{
Andy Adamson533eb462011-06-13 18:25:56 -04005770 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5771 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5772 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005773 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005774 return;
5775
5776 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005777 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005778 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5779 fattr->nlink = 2;
5780}
5781
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005782static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5783 const struct qstr *name,
5784 struct nfs4_fs_locations *fs_locations,
5785 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005786{
5787 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04005788 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005789 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005790 };
5791 struct nfs4_fs_locations_arg args = {
5792 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005793 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005794 .page = page,
5795 .bitmask = bitmask,
5796 };
Benny Halevy22958462009-04-01 09:22:02 -04005797 struct nfs4_fs_locations_res res = {
5798 .fs_locations = fs_locations,
5799 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005800 struct rpc_message msg = {
5801 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5802 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005803 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005804 };
5805 int status;
5806
Harvey Harrison3110ff82008-05-02 13:42:44 -07005807 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005808
5809 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5810 * is not supported */
5811 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5812 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5813 else
5814 bitmask[0] |= FATTR4_WORD0_FILEID;
5815
Trond Myklebustc228fd32007-01-13 02:28:11 -05005816 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005817 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005818 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005819 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005820 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005821 return status;
5822}
5823
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005824int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5825 const struct qstr *name,
5826 struct nfs4_fs_locations *fs_locations,
5827 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005828{
5829 struct nfs4_exception exception = { };
5830 int err;
5831 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04005832 err = _nfs4_proc_fs_locations(client, dir, name,
5833 fs_locations, page);
5834 trace_nfs4_get_fs_locations(dir, name, err);
5835 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005836 &exception);
5837 } while (exception.retry);
5838 return err;
5839}
5840
Andy Adamson5ec16a82013-08-08 10:57:55 -04005841/**
5842 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
5843 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
5844 */
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005845static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5846{
5847 int status;
5848 struct nfs4_secinfo_arg args = {
5849 .dir_fh = NFS_FH(dir),
5850 .name = name,
5851 };
5852 struct nfs4_secinfo_res res = {
5853 .flavors = flavors,
5854 };
5855 struct rpc_message msg = {
5856 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5857 .rpc_argp = &args,
5858 .rpc_resp = &res,
5859 };
Andy Adamson5ec16a82013-08-08 10:57:55 -04005860 struct rpc_clnt *clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005861
5862 dprintk("NFS call secinfo %s\n", name->name);
Andy Adamson5ec16a82013-08-08 10:57:55 -04005863 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005864 dprintk("NFS reply secinfo: %d\n", status);
5865 return status;
5866}
5867
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005868int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5869 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005870{
5871 struct nfs4_exception exception = { };
5872 int err;
5873 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04005874 err = _nfs4_proc_secinfo(dir, name, flavors);
5875 trace_nfs4_secinfo(dir, name, err);
5876 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005877 &exception);
5878 } while (exception.retry);
5879 return err;
5880}
5881
Andy Adamson557134a2009-04-01 09:21:53 -04005882#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005883/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005884 * Check the exchange flags returned by the server for invalid flags, having
5885 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5886 * DS flags set.
5887 */
5888static int nfs4_check_cl_exchange_flags(u32 flags)
5889{
5890 if (flags & ~EXCHGID4_FLAG_MASK_R)
5891 goto out_inval;
5892 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5893 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5894 goto out_inval;
5895 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5896 goto out_inval;
5897 return NFS_OK;
5898out_inval:
5899 return -NFS4ERR_INVAL;
5900}
5901
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005902static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005903nfs41_same_server_scope(struct nfs41_server_scope *a,
5904 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005905{
5906 if (a->server_scope_sz == b->server_scope_sz &&
5907 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5908 return true;
5909
5910 return false;
5911}
5912
Andy Adamson357f54d2010-12-14 10:11:57 -05005913/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005914 * nfs4_proc_bind_conn_to_session()
5915 *
5916 * The 4.1 client currently uses the same TCP connection for the
5917 * fore and backchannel.
5918 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005919int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005920{
5921 int status;
5922 struct nfs41_bind_conn_to_session_res res;
5923 struct rpc_message msg = {
5924 .rpc_proc =
5925 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5926 .rpc_argp = clp,
5927 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005928 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005929 };
5930
5931 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005932
5933 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5934 if (unlikely(res.session == NULL)) {
5935 status = -ENOMEM;
5936 goto out;
5937 }
5938
5939 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005940 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005941 if (status == 0) {
5942 if (memcmp(res.session->sess_id.data,
5943 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5944 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5945 status = -EIO;
5946 goto out_session;
5947 }
5948 if (res.dir != NFS4_CDFS4_BOTH) {
5949 dprintk("NFS: %s: Unexpected direction from server\n",
5950 __func__);
5951 status = -EIO;
5952 goto out_session;
5953 }
5954 if (res.use_conn_in_rdma_mode) {
5955 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5956 __func__);
5957 status = -EIO;
5958 goto out_session;
5959 }
5960 }
5961out_session:
5962 kfree(res.session);
5963out:
5964 dprintk("<-- %s status= %d\n", __func__, status);
5965 return status;
5966}
5967
5968/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005969 * nfs4_proc_exchange_id()
5970 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005971 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5972 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005973 * Since the clientid has expired, all compounds using sessions
5974 * associated with the stale clientid will be returning
5975 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5976 * be in some phase of session reset.
5977 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005978int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005979{
5980 nfs4_verifier verifier;
5981 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005982 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005983 .client = clp,
Trond Myklebust4f0b4292013-05-20 12:24:03 -04005984 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
5985 EXCHGID4_FLAG_BIND_PRINC_STATEID,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005986 };
5987 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005988 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005989 };
5990 int status;
5991 struct rpc_message msg = {
5992 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5993 .rpc_argp = &args,
5994 .rpc_resp = &res,
5995 .rpc_cred = cred,
5996 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005997
Chuck Leverf0920752012-05-21 22:45:41 -04005998 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005999 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
6000 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006001 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
6002 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6003 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006004
Chuck Leveracdeb692012-05-21 22:46:16 -04006005 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006006 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04006007 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006008 status = -ENOMEM;
6009 goto out;
6010 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006011
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006012 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006013 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006014 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04006015 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04006016 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04006017 }
6018
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006019 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006020 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006021 status = -ENOMEM;
6022 goto out_server_scope;
6023 }
6024
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006025 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006026 trace_nfs4_exchange_id(clp, status);
Chuck Lever177313f2012-05-21 22:44:58 -04006027 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04006028 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006029
Chuck Lever177313f2012-05-21 22:44:58 -04006030 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04006031 clp->cl_clientid = res.clientid;
6032 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
6033 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
6034 clp->cl_seqid = res.seqid;
6035
Chuck Leveracdeb692012-05-21 22:46:16 -04006036 kfree(clp->cl_serverowner);
6037 clp->cl_serverowner = res.server_owner;
6038 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04006039
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006040 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04006041 kfree(clp->cl_implid);
6042 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006043
Chuck Lever177313f2012-05-21 22:44:58 -04006044 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006045 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006046 res.server_scope)) {
6047 dprintk("%s: server_scope mismatch detected\n",
6048 __func__);
6049 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006050 kfree(clp->cl_serverscope);
6051 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006052 }
6053
Chuck Lever177313f2012-05-21 22:44:58 -04006054 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006055 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006056 goto out;
6057 }
Trond Myklebust32b01312012-05-26 13:41:04 -04006058 } else
6059 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006060
Chuck Leveracdeb692012-05-21 22:46:16 -04006061out_server_owner:
6062 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006063out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006064 kfree(res.server_scope);
6065out:
Chuck Lever177313f2012-05-21 22:44:58 -04006066 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006067 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006068 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006069 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04006070 clp->cl_implid->date.seconds,
6071 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006072 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006073 return status;
6074}
6075
Trond Myklebust66245532012-05-25 17:18:09 -04006076static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
6077 struct rpc_cred *cred)
6078{
6079 struct rpc_message msg = {
6080 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
6081 .rpc_argp = clp,
6082 .rpc_cred = cred,
6083 };
6084 int status;
6085
6086 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006087 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04006088 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04006089 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04006090 "DESTROY_CLIENTID.", status, clp->cl_hostname);
6091 return status;
6092}
6093
6094static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
6095 struct rpc_cred *cred)
6096{
6097 unsigned int loop;
6098 int ret;
6099
6100 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
6101 ret = _nfs4_proc_destroy_clientid(clp, cred);
6102 switch (ret) {
6103 case -NFS4ERR_DELAY:
6104 case -NFS4ERR_CLIENTID_BUSY:
6105 ssleep(1);
6106 break;
6107 default:
6108 return ret;
6109 }
6110 }
6111 return 0;
6112}
6113
6114int nfs4_destroy_clientid(struct nfs_client *clp)
6115{
6116 struct rpc_cred *cred;
6117 int ret = 0;
6118
6119 if (clp->cl_mvops->minor_version < 1)
6120 goto out;
6121 if (clp->cl_exchange_flags == 0)
6122 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04006123 if (clp->cl_preserve_clid)
6124 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04006125 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04006126 ret = nfs4_proc_destroy_clientid(clp, cred);
6127 if (cred)
6128 put_rpccred(cred);
6129 switch (ret) {
6130 case 0:
6131 case -NFS4ERR_STALE_CLIENTID:
6132 clp->cl_exchange_flags = 0;
6133 }
6134out:
6135 return ret;
6136}
6137
Andy Adamson2050f0c2009-04-01 09:22:30 -04006138struct nfs4_get_lease_time_data {
6139 struct nfs4_get_lease_time_args *args;
6140 struct nfs4_get_lease_time_res *res;
6141 struct nfs_client *clp;
6142};
6143
6144static void nfs4_get_lease_time_prepare(struct rpc_task *task,
6145 void *calldata)
6146{
Andy Adamson2050f0c2009-04-01 09:22:30 -04006147 struct nfs4_get_lease_time_data *data =
6148 (struct nfs4_get_lease_time_data *)calldata;
6149
6150 dprintk("--> %s\n", __func__);
6151 /* just setup sequence, do not trigger session recovery
6152 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006153 nfs41_setup_sequence(data->clp->cl_session,
6154 &data->args->la_seq_args,
6155 &data->res->lr_seq_res,
6156 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006157 dprintk("<-- %s\n", __func__);
6158}
6159
6160/*
6161 * Called from nfs4_state_manager thread for session setup, so don't recover
6162 * from sequence operation or clientid errors.
6163 */
6164static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
6165{
6166 struct nfs4_get_lease_time_data *data =
6167 (struct nfs4_get_lease_time_data *)calldata;
6168
6169 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006170 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
6171 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04006172 switch (task->tk_status) {
6173 case -NFS4ERR_DELAY:
6174 case -NFS4ERR_GRACE:
6175 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
6176 rpc_delay(task, NFS4_POLL_RETRY_MIN);
6177 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006178 /* fall through */
6179 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006180 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006181 return;
6182 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04006183 dprintk("<-- %s\n", __func__);
6184}
6185
Trond Myklebust17280172012-03-11 13:11:00 -04006186static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04006187 .rpc_call_prepare = nfs4_get_lease_time_prepare,
6188 .rpc_call_done = nfs4_get_lease_time_done,
6189};
6190
6191int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
6192{
6193 struct rpc_task *task;
6194 struct nfs4_get_lease_time_args args;
6195 struct nfs4_get_lease_time_res res = {
6196 .lr_fsinfo = fsinfo,
6197 };
6198 struct nfs4_get_lease_time_data data = {
6199 .args = &args,
6200 .res = &res,
6201 .clp = clp,
6202 };
6203 struct rpc_message msg = {
6204 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
6205 .rpc_argp = &args,
6206 .rpc_resp = &res,
6207 };
6208 struct rpc_task_setup task_setup = {
6209 .rpc_client = clp->cl_rpcclient,
6210 .rpc_message = &msg,
6211 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006212 .callback_data = &data,
6213 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04006214 };
6215 int status;
6216
Trond Myklebust9d12b212012-01-17 22:04:25 -05006217 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006218 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006219 dprintk("--> %s\n", __func__);
6220 task = rpc_run_task(&task_setup);
6221
6222 if (IS_ERR(task))
6223 status = PTR_ERR(task);
6224 else {
6225 status = task->tk_status;
6226 rpc_put_task(task);
6227 }
6228 dprintk("<-- %s return %d\n", __func__, status);
6229
6230 return status;
6231}
6232
Andy Adamsonfc931582009-04-01 09:22:31 -04006233/*
6234 * Initialize the values to be used by the client in CREATE_SESSION
6235 * If nfs4_init_session set the fore channel request and response sizes,
6236 * use them.
6237 *
6238 * Set the back channel max_resp_sz_cached to zero to force the client to
6239 * always set csa_cachethis to FALSE because the current implementation
6240 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
6241 */
6242static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
6243{
Andy Adamson18aad3d2013-06-26 12:21:49 -04006244 unsigned int max_rqst_sz, max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006245
Andy Adamson18aad3d2013-06-26 12:21:49 -04006246 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
6247 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
6248
Andy Adamsonfc931582009-04-01 09:22:31 -04006249 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04006250 args->fc_attrs.max_rqst_sz = max_rqst_sz;
6251 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006252 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05006253 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04006254
6255 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05006256 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04006257 __func__,
6258 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05006259 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04006260
6261 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04006262 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
6263 args->bc_attrs.max_resp_sz = PAGE_SIZE;
6264 args->bc_attrs.max_resp_sz_cached = 0;
6265 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
6266 args->bc_attrs.max_reqs = 1;
6267
6268 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
6269 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
6270 __func__,
6271 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
6272 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
6273 args->bc_attrs.max_reqs);
6274}
6275
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006276static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04006277{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006278 struct nfs4_channel_attrs *sent = &args->fc_attrs;
6279 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
6280
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006281 if (rcvd->max_resp_sz > sent->max_resp_sz)
6282 return -EINVAL;
6283 /*
6284 * Our requested max_ops is the minimum we need; we're not
6285 * prepared to break up compounds into smaller pieces than that.
6286 * So, no point even trying to continue if the server won't
6287 * cooperate:
6288 */
6289 if (rcvd->max_ops < sent->max_ops)
6290 return -EINVAL;
6291 if (rcvd->max_reqs == 0)
6292 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006293 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
6294 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006295 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04006296}
6297
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006298static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
6299{
6300 struct nfs4_channel_attrs *sent = &args->bc_attrs;
6301 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04006302
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006303 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
6304 return -EINVAL;
6305 if (rcvd->max_resp_sz < sent->max_resp_sz)
6306 return -EINVAL;
6307 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
6308 return -EINVAL;
6309 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006310 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006311 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006312 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006313 return -EINVAL;
6314 return 0;
6315}
Andy Adamson8d353012009-04-01 09:22:32 -04006316
Andy Adamson8d353012009-04-01 09:22:32 -04006317static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
6318 struct nfs4_session *session)
6319{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006320 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04006321
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006322 ret = nfs4_verify_fore_channel_attrs(args, session);
6323 if (ret)
6324 return ret;
6325 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04006326}
6327
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006328static int _nfs4_proc_create_session(struct nfs_client *clp,
6329 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04006330{
6331 struct nfs4_session *session = clp->cl_session;
6332 struct nfs41_create_session_args args = {
6333 .client = clp,
6334 .cb_program = NFS4_CALLBACK,
6335 };
6336 struct nfs41_create_session_res res = {
6337 .client = clp,
6338 };
6339 struct rpc_message msg = {
6340 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
6341 .rpc_argp = &args,
6342 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006343 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04006344 };
6345 int status;
6346
6347 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04006348 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04006349
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006350 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006351 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04006352
Trond Myklebust43095d32012-11-20 11:13:12 -05006353 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04006354 /* Verify the session's negotiated channel_attrs values */
6355 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04006356 /* Increment the clientid slot sequence id */
6357 clp->cl_seqid++;
6358 }
6359
6360 return status;
6361}
6362
6363/*
6364 * Issues a CREATE_SESSION operation to the server.
6365 * It is the responsibility of the caller to verify the session is
6366 * expired before calling this routine.
6367 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006368int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04006369{
6370 int status;
6371 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04006372 struct nfs4_session *session = clp->cl_session;
6373
6374 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
6375
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006376 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04006377 if (status)
6378 goto out;
6379
Andy Adamsonaacd5532011-11-09 13:58:21 -05006380 /* Init or reset the session slot tables */
6381 status = nfs4_setup_session_slot_tables(session);
6382 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04006383 if (status)
6384 goto out;
6385
6386 ptr = (unsigned *)&session->sess_id.data[0];
6387 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
6388 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04006389out:
6390 dprintk("<-- %s\n", __func__);
6391 return status;
6392}
6393
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006394/*
6395 * Issue the over-the-wire RPC DESTROY_SESSION.
6396 * The caller must serialize access to this routine.
6397 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006398int nfs4_proc_destroy_session(struct nfs4_session *session,
6399 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006400{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006401 struct rpc_message msg = {
6402 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
6403 .rpc_argp = session,
6404 .rpc_cred = cred,
6405 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006406 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006407
6408 dprintk("--> nfs4_proc_destroy_session\n");
6409
6410 /* session is still being setup */
6411 if (session->clp->cl_cons_state != NFS_CS_READY)
6412 return status;
6413
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006414 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006415 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006416
6417 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04006418 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006419 "Session has been destroyed regardless...\n", status);
6420
6421 dprintk("<-- nfs4_proc_destroy_session\n");
6422 return status;
6423}
6424
Trond Myklebust7b38c362012-05-23 13:23:31 -04006425/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006426 * Renew the cl_session lease.
6427 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006428struct nfs4_sequence_data {
6429 struct nfs_client *clp;
6430 struct nfs4_sequence_args args;
6431 struct nfs4_sequence_res res;
6432};
6433
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006434static void nfs41_sequence_release(void *data)
6435{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006436 struct nfs4_sequence_data *calldata = data;
6437 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006438
Alexandros Batsakis71358402010-02-05 03:45:05 -08006439 if (atomic_read(&clp->cl_count) > 1)
6440 nfs4_schedule_state_renewal(clp);
6441 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006442 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006443}
6444
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006445static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6446{
6447 switch(task->tk_status) {
6448 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006449 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6450 return -EAGAIN;
6451 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006452 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006453 }
6454 return 0;
6455}
6456
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006457static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006458{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006459 struct nfs4_sequence_data *calldata = data;
6460 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006461
Trond Myklebust14516c32010-07-31 14:29:06 -04006462 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6463 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006464
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006465 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006466 if (task->tk_status < 0) {
6467 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006468 if (atomic_read(&clp->cl_count) == 1)
6469 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006470
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006471 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6472 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006473 return;
6474 }
6475 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006476 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006477out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006478 dprintk("<-- %s\n", __func__);
6479}
6480
6481static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6482{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006483 struct nfs4_sequence_data *calldata = data;
6484 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006485 struct nfs4_sequence_args *args;
6486 struct nfs4_sequence_res *res;
6487
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006488 args = task->tk_msg.rpc_argp;
6489 res = task->tk_msg.rpc_resp;
6490
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006491 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006492}
6493
6494static const struct rpc_call_ops nfs41_sequence_ops = {
6495 .rpc_call_done = nfs41_sequence_call_done,
6496 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006497 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006498};
6499
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006500static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
6501 struct rpc_cred *cred,
6502 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006503{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006504 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006505 struct rpc_message msg = {
6506 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6507 .rpc_cred = cred,
6508 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006509 struct rpc_task_setup task_setup_data = {
6510 .rpc_client = clp->cl_rpcclient,
6511 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006512 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04006513 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006514 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006515
Alexandros Batsakis71358402010-02-05 03:45:05 -08006516 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006517 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006518 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006519 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006520 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006521 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006522 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05006523 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006524 if (is_privileged)
6525 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006526 msg.rpc_argp = &calldata->args;
6527 msg.rpc_resp = &calldata->res;
6528 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006529 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006530
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006531 return rpc_run_task(&task_setup_data);
6532}
6533
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006534static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006535{
6536 struct rpc_task *task;
6537 int ret = 0;
6538
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006539 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6540 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006541 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006542 if (IS_ERR(task))
6543 ret = PTR_ERR(task);
6544 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006545 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006546 dprintk("<-- %s status=%d\n", __func__, ret);
6547 return ret;
6548}
6549
6550static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6551{
6552 struct rpc_task *task;
6553 int ret;
6554
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006555 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006556 if (IS_ERR(task)) {
6557 ret = PTR_ERR(task);
6558 goto out;
6559 }
6560 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006561 if (!ret) {
6562 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6563
6564 if (task->tk_status == 0)
6565 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006566 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006567 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006568 rpc_put_task(task);
6569out:
6570 dprintk("<-- %s status=%d\n", __func__, ret);
6571 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006572}
6573
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006574struct nfs4_reclaim_complete_data {
6575 struct nfs_client *clp;
6576 struct nfs41_reclaim_complete_args arg;
6577 struct nfs41_reclaim_complete_res res;
6578};
6579
6580static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6581{
6582 struct nfs4_reclaim_complete_data *calldata = data;
6583
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006584 nfs41_setup_sequence(calldata->clp->cl_session,
6585 &calldata->arg.seq_args,
6586 &calldata->res.seq_res,
6587 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006588}
6589
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006590static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6591{
6592 switch(task->tk_status) {
6593 case 0:
6594 case -NFS4ERR_COMPLETE_ALREADY:
6595 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6596 break;
6597 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006598 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006599 /* fall through */
6600 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006601 return -EAGAIN;
6602 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006603 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006604 }
6605 return 0;
6606}
6607
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006608static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6609{
6610 struct nfs4_reclaim_complete_data *calldata = data;
6611 struct nfs_client *clp = calldata->clp;
6612 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6613
6614 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006615 if (!nfs41_sequence_done(task, res))
6616 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006617
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006618 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006619 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6620 rpc_restart_call_prepare(task);
6621 return;
6622 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006623 dprintk("<-- %s\n", __func__);
6624}
6625
6626static void nfs4_free_reclaim_complete_data(void *data)
6627{
6628 struct nfs4_reclaim_complete_data *calldata = data;
6629
6630 kfree(calldata);
6631}
6632
6633static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6634 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6635 .rpc_call_done = nfs4_reclaim_complete_done,
6636 .rpc_release = nfs4_free_reclaim_complete_data,
6637};
6638
6639/*
6640 * Issue a global reclaim complete.
6641 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04006642static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
6643 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006644{
6645 struct nfs4_reclaim_complete_data *calldata;
6646 struct rpc_task *task;
6647 struct rpc_message msg = {
6648 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04006649 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006650 };
6651 struct rpc_task_setup task_setup_data = {
6652 .rpc_client = clp->cl_rpcclient,
6653 .rpc_message = &msg,
6654 .callback_ops = &nfs4_reclaim_complete_call_ops,
6655 .flags = RPC_TASK_ASYNC,
6656 };
6657 int status = -ENOMEM;
6658
6659 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006660 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006661 if (calldata == NULL)
6662 goto out;
6663 calldata->clp = clp;
6664 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006665
Trond Myklebust9d12b212012-01-17 22:04:25 -05006666 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006667 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006668 msg.rpc_argp = &calldata->arg;
6669 msg.rpc_resp = &calldata->res;
6670 task_setup_data.callback_data = calldata;
6671 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006672 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006673 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006674 goto out;
6675 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006676 status = nfs4_wait_for_completion_rpc_task(task);
6677 if (status == 0)
6678 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006679 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006680 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006681out:
6682 dprintk("<-- %s status=%d\n", __func__, status);
6683 return status;
6684}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006685
6686static void
6687nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6688{
6689 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006690 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006691 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006692
6693 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006694 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6695 * right now covering the LAYOUTGET we are about to send.
6696 * However, that is not so catastrophic, and there seems
6697 * to be no way to prevent it completely.
6698 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006699 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006700 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006701 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006702 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6703 NFS_I(lgp->args.inode)->layout,
6704 lgp->args.ctx->state)) {
6705 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00006706 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006707}
6708
6709static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6710{
6711 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006712 struct inode *inode = lgp->args.inode;
6713 struct nfs_server *server = NFS_SERVER(inode);
6714 struct pnfs_layout_hdr *lo;
6715 struct nfs4_state *state = NULL;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006716 unsigned long timeo, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006717
6718 dprintk("--> %s\n", __func__);
6719
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006720 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006721 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006722
6723 switch (task->tk_status) {
6724 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006725 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006726 case -NFS4ERR_LAYOUTTRYLATER:
6727 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05006728 timeo = rpc_get_timeout(task->tk_client);
6729 giveup = lgp->args.timestamp + timeo;
6730 if (time_after(giveup, jiffies))
6731 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006732 break;
6733 case -NFS4ERR_EXPIRED:
6734 case -NFS4ERR_BAD_STATEID:
6735 spin_lock(&inode->i_lock);
6736 lo = NFS_I(inode)->layout;
6737 if (!lo || list_empty(&lo->plh_segs)) {
6738 spin_unlock(&inode->i_lock);
6739 /* If the open stateid was bad, then recover it. */
6740 state = lgp->args.ctx->state;
6741 } else {
6742 LIST_HEAD(head);
6743
6744 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6745 spin_unlock(&inode->i_lock);
6746 /* Mark the bad layout state as invalid, then
6747 * retry using the open stateid. */
6748 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006749 }
6750 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006751 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6752 rpc_restart_call_prepare(task);
6753out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006754 dprintk("<-- %s\n", __func__);
6755}
6756
Idan Kedar85541162012-08-02 11:47:10 +03006757static size_t max_response_pages(struct nfs_server *server)
6758{
6759 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6760 return nfs_page_array_len(0, max_resp_sz);
6761}
6762
6763static void nfs4_free_pages(struct page **pages, size_t size)
6764{
6765 int i;
6766
6767 if (!pages)
6768 return;
6769
6770 for (i = 0; i < size; i++) {
6771 if (!pages[i])
6772 break;
6773 __free_page(pages[i]);
6774 }
6775 kfree(pages);
6776}
6777
6778static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6779{
6780 struct page **pages;
6781 int i;
6782
6783 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6784 if (!pages) {
6785 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6786 return NULL;
6787 }
6788
6789 for (i = 0; i < size; i++) {
6790 pages[i] = alloc_page(gfp_flags);
6791 if (!pages[i]) {
6792 dprintk("%s: failed to allocate page\n", __func__);
6793 nfs4_free_pages(pages, size);
6794 return NULL;
6795 }
6796 }
6797
6798 return pages;
6799}
6800
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006801static void nfs4_layoutget_release(void *calldata)
6802{
6803 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006804 struct inode *inode = lgp->args.inode;
6805 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006806 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006807
6808 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006809 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006810 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006811 put_nfs_open_context(lgp->args.ctx);
6812 kfree(calldata);
6813 dprintk("<-- %s\n", __func__);
6814}
6815
6816static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6817 .rpc_call_prepare = nfs4_layoutget_prepare,
6818 .rpc_call_done = nfs4_layoutget_done,
6819 .rpc_release = nfs4_layoutget_release,
6820};
6821
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006822struct pnfs_layout_segment *
6823nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006824{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006825 struct inode *inode = lgp->args.inode;
6826 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006827 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006828 struct rpc_task *task;
6829 struct rpc_message msg = {
6830 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6831 .rpc_argp = &lgp->args,
6832 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04006833 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006834 };
6835 struct rpc_task_setup task_setup_data = {
6836 .rpc_client = server->client,
6837 .rpc_message = &msg,
6838 .callback_ops = &nfs4_layoutget_call_ops,
6839 .callback_data = lgp,
6840 .flags = RPC_TASK_ASYNC,
6841 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006842 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006843 int status = 0;
6844
6845 dprintk("--> %s\n", __func__);
6846
Idan Kedar85541162012-08-02 11:47:10 +03006847 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6848 if (!lgp->args.layout.pages) {
6849 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006850 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006851 }
6852 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006853 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03006854
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006855 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006856 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006857 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006858
6859 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
6860 pnfs_get_layout_hdr(NFS_I(inode)->layout);
6861
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006862 task = rpc_run_task(&task_setup_data);
6863 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006864 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006865 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006866 if (status == 0)
6867 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04006868 trace_nfs4_layoutget(lgp->args.ctx,
6869 &lgp->args.range,
6870 &lgp->res.range,
6871 status);
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05006872 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
6873 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006874 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006875 rpc_put_task(task);
6876 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006877 if (status)
6878 return ERR_PTR(status);
6879 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006880}
6881
Benny Halevycbe82602011-05-22 19:52:37 +03006882static void
6883nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6884{
6885 struct nfs4_layoutreturn *lrp = calldata;
6886
6887 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006888 nfs41_setup_sequence(lrp->clp->cl_session,
6889 &lrp->args.seq_args,
6890 &lrp->res.seq_res,
6891 task);
Benny Halevycbe82602011-05-22 19:52:37 +03006892}
6893
6894static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6895{
6896 struct nfs4_layoutreturn *lrp = calldata;
6897 struct nfs_server *server;
6898
6899 dprintk("--> %s\n", __func__);
6900
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006901 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03006902 return;
6903
6904 server = NFS_SERVER(lrp->args.inode);
6905 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006906 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006907 return;
6908 }
Benny Halevycbe82602011-05-22 19:52:37 +03006909 dprintk("<-- %s\n", __func__);
6910}
6911
6912static void nfs4_layoutreturn_release(void *calldata)
6913{
6914 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006915 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006916
6917 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006918 spin_lock(&lo->plh_inode->i_lock);
6919 if (lrp->res.lrs_present)
6920 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6921 lo->plh_block_lgets--;
6922 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006923 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006924 kfree(calldata);
6925 dprintk("<-- %s\n", __func__);
6926}
6927
6928static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6929 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6930 .rpc_call_done = nfs4_layoutreturn_done,
6931 .rpc_release = nfs4_layoutreturn_release,
6932};
6933
6934int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6935{
6936 struct rpc_task *task;
6937 struct rpc_message msg = {
6938 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6939 .rpc_argp = &lrp->args,
6940 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04006941 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03006942 };
6943 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04006944 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03006945 .rpc_message = &msg,
6946 .callback_ops = &nfs4_layoutreturn_call_ops,
6947 .callback_data = lrp,
6948 };
6949 int status;
6950
6951 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006952 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006953 task = rpc_run_task(&task_setup_data);
6954 if (IS_ERR(task))
6955 return PTR_ERR(task);
6956 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04006957 trace_nfs4_layoutreturn(lrp->args.inode, status);
Benny Halevycbe82602011-05-22 19:52:37 +03006958 dprintk("<-- %s status=%d\n", __func__, status);
6959 rpc_put_task(task);
6960 return status;
6961}
6962
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006963/*
6964 * Retrieve the list of Data Server devices from the MDS.
6965 */
6966static int _nfs4_getdevicelist(struct nfs_server *server,
6967 const struct nfs_fh *fh,
6968 struct pnfs_devicelist *devlist)
6969{
6970 struct nfs4_getdevicelist_args args = {
6971 .fh = fh,
6972 .layoutclass = server->pnfs_curr_ld->id,
6973 };
6974 struct nfs4_getdevicelist_res res = {
6975 .devlist = devlist,
6976 };
6977 struct rpc_message msg = {
6978 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6979 .rpc_argp = &args,
6980 .rpc_resp = &res,
6981 };
6982 int status;
6983
6984 dprintk("--> %s\n", __func__);
6985 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6986 &res.seq_res, 0);
6987 dprintk("<-- %s status=%d\n", __func__, status);
6988 return status;
6989}
6990
6991int nfs4_proc_getdevicelist(struct nfs_server *server,
6992 const struct nfs_fh *fh,
6993 struct pnfs_devicelist *devlist)
6994{
6995 struct nfs4_exception exception = { };
6996 int err;
6997
6998 do {
6999 err = nfs4_handle_exception(server,
7000 _nfs4_getdevicelist(server, fh, devlist),
7001 &exception);
7002 } while (exception.retry);
7003
7004 dprintk("%s: err=%d, num_devs=%u\n", __func__,
7005 err, devlist->num_devs);
7006
7007 return err;
7008}
7009EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
7010
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007011static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007012_nfs4_proc_getdeviceinfo(struct nfs_server *server,
7013 struct pnfs_device *pdev,
7014 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007015{
7016 struct nfs4_getdeviceinfo_args args = {
7017 .pdev = pdev,
7018 };
7019 struct nfs4_getdeviceinfo_res res = {
7020 .pdev = pdev,
7021 };
7022 struct rpc_message msg = {
7023 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
7024 .rpc_argp = &args,
7025 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007026 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007027 };
7028 int status;
7029
7030 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00007031 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007032 dprintk("<-- %s status=%d\n", __func__, status);
7033
7034 return status;
7035}
7036
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007037int nfs4_proc_getdeviceinfo(struct nfs_server *server,
7038 struct pnfs_device *pdev,
7039 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007040{
7041 struct nfs4_exception exception = { };
7042 int err;
7043
7044 do {
7045 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007046 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007047 &exception);
7048 } while (exception.retry);
7049 return err;
7050}
7051EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
7052
Andy Adamson863a3c62011-03-23 13:27:54 +00007053static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
7054{
7055 struct nfs4_layoutcommit_data *data = calldata;
7056 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007057 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00007058
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007059 nfs41_setup_sequence(session,
7060 &data->args.seq_args,
7061 &data->res.seq_res,
7062 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00007063}
7064
7065static void
7066nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
7067{
7068 struct nfs4_layoutcommit_data *data = calldata;
7069 struct nfs_server *server = NFS_SERVER(data->args.inode);
7070
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007071 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00007072 return;
7073
7074 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007075 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
7076 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
7077 case -NFS4ERR_BADLAYOUT: /* no layout */
7078 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00007079 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007080 break;
7081 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00007082 nfs_post_op_update_inode_force_wcc(data->args.inode,
7083 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007084 break;
7085 default:
7086 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
7087 rpc_restart_call_prepare(task);
7088 return;
7089 }
7090 }
Andy Adamson863a3c62011-03-23 13:27:54 +00007091}
7092
7093static void nfs4_layoutcommit_release(void *calldata)
7094{
7095 struct nfs4_layoutcommit_data *data = calldata;
7096
Andy Adamsondb29c082011-07-30 20:52:38 -04007097 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00007098 put_rpccred(data->cred);
7099 kfree(data);
7100}
7101
7102static const struct rpc_call_ops nfs4_layoutcommit_ops = {
7103 .rpc_call_prepare = nfs4_layoutcommit_prepare,
7104 .rpc_call_done = nfs4_layoutcommit_done,
7105 .rpc_release = nfs4_layoutcommit_release,
7106};
7107
7108int
Andy Adamsonef311532011-03-12 02:58:10 -05007109nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00007110{
7111 struct rpc_message msg = {
7112 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
7113 .rpc_argp = &data->args,
7114 .rpc_resp = &data->res,
7115 .rpc_cred = data->cred,
7116 };
7117 struct rpc_task_setup task_setup_data = {
7118 .task = &data->task,
7119 .rpc_client = NFS_CLIENT(data->args.inode),
7120 .rpc_message = &msg,
7121 .callback_ops = &nfs4_layoutcommit_ops,
7122 .callback_data = data,
7123 .flags = RPC_TASK_ASYNC,
7124 };
7125 struct rpc_task *task;
7126 int status = 0;
7127
7128 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
7129 "lbw: %llu inode %lu\n",
7130 data->task.tk_pid, sync,
7131 data->args.lastbytewritten,
7132 data->args.inode->i_ino);
7133
Trond Myklebust9d12b212012-01-17 22:04:25 -05007134 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00007135 task = rpc_run_task(&task_setup_data);
7136 if (IS_ERR(task))
7137 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05007138 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00007139 goto out;
7140 status = nfs4_wait_for_completion_rpc_task(task);
7141 if (status != 0)
7142 goto out;
7143 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007144 trace_nfs4_layoutcommit(data->args.inode, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00007145out:
7146 dprintk("%s: status %d\n", __func__, status);
7147 rpc_put_task(task);
7148 return status;
7149}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007150
Andy Adamson97431202013-08-08 10:57:56 -04007151/**
7152 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
7153 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
7154 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007155static int
7156_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7157 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
7158{
7159 struct nfs41_secinfo_no_name_args args = {
7160 .style = SECINFO_STYLE_CURRENT_FH,
7161 };
7162 struct nfs4_secinfo_res res = {
7163 .flavors = flavors,
7164 };
7165 struct rpc_message msg = {
7166 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
7167 .rpc_argp = &args,
7168 .rpc_resp = &res,
7169 };
Andy Adamson97431202013-08-08 10:57:56 -04007170 return nfs4_call_sync(server->nfs_client->cl_rpcclient, server, &msg,
7171 &args.seq_args, &res.seq_res, 0);
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007172}
7173
7174static int
7175nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7176 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
7177{
7178 struct nfs4_exception exception = { };
7179 int err;
7180 do {
7181 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
7182 switch (err) {
7183 case 0:
7184 case -NFS4ERR_WRONGSEC:
7185 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007186 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007187 default:
7188 err = nfs4_handle_exception(server, err, &exception);
7189 }
7190 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007191out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007192 return err;
7193}
7194
7195static int
7196nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
7197 struct nfs_fsinfo *info)
7198{
7199 int err;
7200 struct page *page;
7201 rpc_authflavor_t flavor;
7202 struct nfs4_secinfo_flavors *flavors;
7203
7204 page = alloc_page(GFP_KERNEL);
7205 if (!page) {
7206 err = -ENOMEM;
7207 goto out;
7208 }
7209
7210 flavors = page_address(page);
7211 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
7212
7213 /*
7214 * Fall back on "guess and check" method if
7215 * the server doesn't support SECINFO_NO_NAME
7216 */
7217 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
7218 err = nfs4_find_root_sec(server, fhandle, info);
7219 goto out_freepage;
7220 }
7221 if (err)
7222 goto out_freepage;
7223
7224 flavor = nfs_find_best_sec(flavors);
7225 if (err == 0)
7226 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
7227
7228out_freepage:
7229 put_page(page);
7230 if (err == -EACCES)
7231 return -EPERM;
7232out:
7233 return err;
7234}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007235
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007236static int _nfs41_test_stateid(struct nfs_server *server,
7237 nfs4_stateid *stateid,
7238 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04007239{
7240 int status;
7241 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007242 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04007243 };
7244 struct nfs41_test_stateid_res res;
7245 struct rpc_message msg = {
7246 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
7247 .rpc_argp = &args,
7248 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007249 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04007250 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007251
Chuck Lever38527b12012-07-11 16:30:23 -04007252 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05007253 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007254 nfs4_set_sequence_privileged(&args.seq_args);
7255 status = nfs4_call_sync_sequence(server->client, server, &msg,
7256 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04007257 if (status != NFS_OK) {
7258 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04007259 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04007260 }
7261 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04007262 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04007263}
7264
Chuck Lever38527b12012-07-11 16:30:23 -04007265/**
7266 * nfs41_test_stateid - perform a TEST_STATEID operation
7267 *
7268 * @server: server / transport on which to perform the operation
7269 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007270 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04007271 *
7272 * Returns NFS_OK if the server recognizes that "stateid" is valid.
7273 * Otherwise a negative NFS4ERR value is returned if the operation
7274 * failed or the state ID is not currently valid.
7275 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007276static int nfs41_test_stateid(struct nfs_server *server,
7277 nfs4_stateid *stateid,
7278 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04007279{
7280 struct nfs4_exception exception = { };
7281 int err;
7282 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007283 err = _nfs41_test_stateid(server, stateid, cred);
Chuck Lever377e5072012-07-11 16:29:45 -04007284 if (err != -NFS4ERR_DELAY)
7285 break;
7286 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04007287 } while (exception.retry);
7288 return err;
7289}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007290
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007291struct nfs_free_stateid_data {
7292 struct nfs_server *server;
7293 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007294 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007295};
7296
7297static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
7298{
7299 struct nfs_free_stateid_data *data = calldata;
7300 nfs41_setup_sequence(nfs4_get_session(data->server),
7301 &data->args.seq_args,
7302 &data->res.seq_res,
7303 task);
7304}
7305
7306static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
7307{
7308 struct nfs_free_stateid_data *data = calldata;
7309
7310 nfs41_sequence_done(task, &data->res.seq_res);
7311
7312 switch (task->tk_status) {
7313 case -NFS4ERR_DELAY:
7314 if (nfs4_async_handle_error(task, data->server, NULL) == -EAGAIN)
7315 rpc_restart_call_prepare(task);
7316 }
7317}
7318
7319static void nfs41_free_stateid_release(void *calldata)
7320{
7321 kfree(calldata);
7322}
7323
Trond Myklebust17f26b12013-08-21 15:48:42 -04007324static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007325 .rpc_call_prepare = nfs41_free_stateid_prepare,
7326 .rpc_call_done = nfs41_free_stateid_done,
7327 .rpc_release = nfs41_free_stateid_release,
7328};
7329
7330static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
7331 nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007332 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007333 bool privileged)
7334{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007335 struct rpc_message msg = {
7336 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007337 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007338 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007339 struct rpc_task_setup task_setup = {
7340 .rpc_client = server->client,
7341 .rpc_message = &msg,
7342 .callback_ops = &nfs41_free_stateid_ops,
7343 .flags = RPC_TASK_ASYNC,
7344 };
7345 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007346
Chuck Lever38527b12012-07-11 16:30:23 -04007347 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007348 data = kmalloc(sizeof(*data), GFP_NOFS);
7349 if (!data)
7350 return ERR_PTR(-ENOMEM);
7351 data->server = server;
7352 nfs4_stateid_copy(&data->args.stateid, stateid);
7353
7354 task_setup.callback_data = data;
7355
7356 msg.rpc_argp = &data->args;
7357 msg.rpc_resp = &data->res;
7358 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
7359 if (privileged)
7360 nfs4_set_sequence_privileged(&data->args.seq_args);
7361
7362 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007363}
7364
Chuck Lever38527b12012-07-11 16:30:23 -04007365/**
7366 * nfs41_free_stateid - perform a FREE_STATEID operation
7367 *
7368 * @server: server / transport on which to perform the operation
7369 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007370 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04007371 *
7372 * Returns NFS_OK if the server freed "stateid". Otherwise a
7373 * negative NFS4ERR value is returned.
7374 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007375static int nfs41_free_stateid(struct nfs_server *server,
7376 nfs4_stateid *stateid,
7377 struct rpc_cred *cred)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007378{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007379 struct rpc_task *task;
7380 int ret;
7381
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007382 task = _nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007383 if (IS_ERR(task))
7384 return PTR_ERR(task);
7385 ret = rpc_wait_for_completion_task(task);
7386 if (!ret)
7387 ret = task->tk_status;
7388 rpc_put_task(task);
7389 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007390}
Trond Myklebust36281ca2012-03-04 18:13:56 -05007391
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007392static int nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
7393{
7394 struct rpc_task *task;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007395 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007396
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007397 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007398 nfs4_free_lock_state(server, lsp);
7399 if (IS_ERR(task))
7400 return PTR_ERR(task);
7401 rpc_put_task(task);
7402 return 0;
7403}
7404
Trond Myklebust36281ca2012-03-04 18:13:56 -05007405static bool nfs41_match_stateid(const nfs4_stateid *s1,
7406 const nfs4_stateid *s2)
7407{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007408 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007409 return false;
7410
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007411 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007412 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007413 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007414 return true;
7415
7416 return false;
7417}
7418
Andy Adamson557134a2009-04-01 09:21:53 -04007419#endif /* CONFIG_NFS_V4_1 */
7420
Trond Myklebust36281ca2012-03-04 18:13:56 -05007421static bool nfs4_match_stateid(const nfs4_stateid *s1,
7422 const nfs4_stateid *s2)
7423{
Trond Myklebustf597c532012-03-04 18:13:56 -05007424 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05007425}
7426
7427
Trond Myklebust17280172012-03-11 13:11:00 -04007428static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007429 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007430 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007431 .recover_open = nfs4_open_reclaim,
7432 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04007433 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04007434 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007435};
7436
Andy Adamson591d71c2009-04-01 09:22:47 -04007437#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007438static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007439 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
7440 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
7441 .recover_open = nfs4_open_reclaim,
7442 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05007443 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007444 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04007445 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04007446};
7447#endif /* CONFIG_NFS_V4_1 */
7448
Trond Myklebust17280172012-03-11 13:11:00 -04007449static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007450 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007451 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007452 .recover_open = nfs4_open_expired,
7453 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04007454 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007455};
7456
Andy Adamson591d71c2009-04-01 09:22:47 -04007457#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007458static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007459 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
7460 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007461 .recover_open = nfs41_open_expired,
7462 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05007463 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04007464};
7465#endif /* CONFIG_NFS_V4_1 */
7466
Trond Myklebust17280172012-03-11 13:11:00 -04007467static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007468 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04007469 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007470 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04007471};
7472
7473#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007474static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007475 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04007476 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007477 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04007478};
7479#endif
7480
Trond Myklebust97dc1352010-06-16 09:52:26 -04007481static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
7482 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04007483 .init_caps = NFS_CAP_READDIRPLUS
7484 | NFS_CAP_ATOMIC_OPEN
7485 | NFS_CAP_CHANGE_ATTR
7486 | NFS_CAP_POSIX_LOCK,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007487 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007488 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007489 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007490 .free_lock_state = nfs4_release_lockowner,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007491 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
7492 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
7493 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007494};
7495
7496#if defined(CONFIG_NFS_V4_1)
7497static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
7498 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04007499 .init_caps = NFS_CAP_READDIRPLUS
7500 | NFS_CAP_ATOMIC_OPEN
7501 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04007502 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04007503 | NFS_CAP_STATEID_NFSV41
7504 | NFS_CAP_ATOMIC_OPEN_V1,
Bryan Schumaker104287c2012-11-12 14:13:13 -05007505 .call_sync = nfs4_call_sync_sequence,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007506 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007507 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007508 .free_lock_state = nfs41_free_lock_state,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007509 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7510 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7511 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007512};
7513#endif
7514
Steve Dickson42c2c422013-05-22 12:50:38 -04007515#if defined(CONFIG_NFS_V4_2)
7516static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
7517 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04007518 .init_caps = NFS_CAP_READDIRPLUS
7519 | NFS_CAP_ATOMIC_OPEN
7520 | NFS_CAP_CHANGE_ATTR
7521 | NFS_CAP_POSIX_LOCK
7522 | NFS_CAP_STATEID_NFSV41
7523 | NFS_CAP_ATOMIC_OPEN_V1,
Steve Dickson42c2c422013-05-22 12:50:38 -04007524 .call_sync = nfs4_call_sync_sequence,
7525 .match_stateid = nfs41_match_stateid,
7526 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04007527 .free_lock_state = nfs41_free_lock_state,
Steve Dickson42c2c422013-05-22 12:50:38 -04007528 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7529 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7530 .state_renewal_ops = &nfs41_state_renewal_ops,
7531};
7532#endif
7533
Trond Myklebust97dc1352010-06-16 09:52:26 -04007534const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
7535 [0] = &nfs_v4_0_minor_ops,
7536#if defined(CONFIG_NFS_V4_1)
7537 [1] = &nfs_v4_1_minor_ops,
7538#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04007539#if defined(CONFIG_NFS_V4_2)
7540 [2] = &nfs_v4_2_minor_ops,
7541#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04007542};
7543
Trond Myklebust17f26b12013-08-21 15:48:42 -04007544static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04007545 .create = nfs_create,
7546 .lookup = nfs_lookup,
7547 .atomic_open = nfs_atomic_open,
7548 .link = nfs_link,
7549 .unlink = nfs_unlink,
7550 .symlink = nfs_symlink,
7551 .mkdir = nfs_mkdir,
7552 .rmdir = nfs_rmdir,
7553 .mknod = nfs_mknod,
7554 .rename = nfs_rename,
7555 .permission = nfs_permission,
7556 .getattr = nfs_getattr,
7557 .setattr = nfs_setattr,
7558 .getxattr = generic_getxattr,
7559 .setxattr = generic_setxattr,
7560 .listxattr = generic_listxattr,
7561 .removexattr = generic_removexattr,
7562};
7563
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08007564static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007565 .permission = nfs_permission,
7566 .getattr = nfs_getattr,
7567 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007568 .getxattr = generic_getxattr,
7569 .setxattr = generic_setxattr,
7570 .listxattr = generic_listxattr,
7571 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007572};
7573
David Howells509de812006-08-22 20:06:11 -04007574const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007575 .version = 4, /* protocol version */
7576 .dentry_ops = &nfs4_dentry_operations,
7577 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007578 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04007579 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007580 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04007581 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04007582 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007583 .getattr = nfs4_proc_getattr,
7584 .setattr = nfs4_proc_setattr,
7585 .lookup = nfs4_proc_lookup,
7586 .access = nfs4_proc_access,
7587 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007588 .create = nfs4_proc_create,
7589 .remove = nfs4_proc_remove,
7590 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04007591 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007592 .unlink_done = nfs4_proc_unlink_done,
7593 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04007594 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04007595 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04007596 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007597 .link = nfs4_proc_link,
7598 .symlink = nfs4_proc_symlink,
7599 .mkdir = nfs4_proc_mkdir,
7600 .rmdir = nfs4_proc_remove,
7601 .readdir = nfs4_proc_readdir,
7602 .mknod = nfs4_proc_mknod,
7603 .statfs = nfs4_proc_statfs,
7604 .fsinfo = nfs4_proc_fsinfo,
7605 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04007606 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007607 .decode_dirent = nfs4_decode_dirent,
7608 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04007609 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04007610 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05007611 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007612 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04007613 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04007614 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007615 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007616 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04007617 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007618 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007619 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00007620 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04007621 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04007622 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04007623 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04007624 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04007625 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00007626 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04007627 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04007628 .create_server = nfs4_create_server,
7629 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007630};
7631
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007632static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7633 .prefix = XATTR_NAME_NFSV4_ACL,
7634 .list = nfs4_xattr_list_nfs4_acl,
7635 .get = nfs4_xattr_get_nfs4_acl,
7636 .set = nfs4_xattr_set_nfs4_acl,
7637};
7638
7639const struct xattr_handler *nfs4_xattr_handlers[] = {
7640 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04007641#ifdef CONFIG_NFS_V4_SECURITY_LABEL
7642 &nfs4_xattr_nfs4_label_handler,
7643#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007644 NULL
7645};
7646
Linus Torvalds1da177e2005-04-16 15:20:36 -07007647/*
7648 * Local variables:
7649 * c-basic-offset: 8
7650 * End:
7651 */