blob: 1eb694e0f305b98a0e612dbf21db9a1fff3f1295 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Trond Myklebust6926afd2012-01-07 13:22:46 -050054#include <linux/nfs_idmap.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000055#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050056#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010057#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050066#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000067#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Trond Myklebustc6d01c62013-08-09 11:51:26 -040069#include "nfs4trace.h"
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define NFSDBG_FACILITY NFSDBG_PROC
72
Trond Myklebust2066fe82006-09-15 08:30:46 -040073#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define NFS4_POLL_RETRY_MAX (15*HZ)
75
Trond Myklebustcdd4e682006-01-03 09:55:12 +010076struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010077static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080078static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050080static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Chuck Lever81934dd2012-03-01 17:01:57 -050081static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040082static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
83static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040084static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
85 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -040086 struct nfs4_state *state, struct nfs4_label *ilabel,
87 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040088#ifdef CONFIG_NFS_V4_1
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040089static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
90 struct rpc_cred *);
91static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *,
92 struct rpc_cred *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040093#endif
David Quigleyaa9c2662013-05-22 12:50:44 -040094
95#ifdef CONFIG_NFS_V4_SECURITY_LABEL
96static inline struct nfs4_label *
97nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
98 struct iattr *sattr, struct nfs4_label *label)
99{
100 int err;
101
102 if (label == NULL)
103 return NULL;
104
105 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
106 return NULL;
107
108 if (NFS_SERVER(dir)->nfs_client->cl_minorversion < 2)
109 return NULL;
110
111 err = security_dentry_init_security(dentry, sattr->ia_mode,
112 &dentry->d_name, (void **)&label->label, &label->len);
113 if (err == 0)
114 return label;
115
116 return NULL;
117}
118static inline void
119nfs4_label_release_security(struct nfs4_label *label)
120{
121 if (label)
122 security_release_secctx(label->label, label->len);
123}
124static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
125{
126 if (label)
127 return server->attr_bitmask;
128
129 return server->attr_bitmask_nl;
130}
131#else
132static inline struct nfs4_label *
133nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
134 struct iattr *sattr, struct nfs4_label *l)
135{ return NULL; }
136static inline void
137nfs4_label_release_security(struct nfs4_label *label)
138{ return; }
139static inline u32 *
140nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
141{ return server->attr_bitmask; }
142#endif
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500145static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Trond Myklebust52567b02009-10-23 14:46:42 -0400147 if (err >= -1000)
148 return err;
149 switch (err) {
150 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500151 case -NFS4ERR_LAYOUTTRYLATER:
152 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400153 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000154 case -NFS4ERR_WRONGSEC:
155 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800156 case -NFS4ERR_BADOWNER:
157 case -NFS4ERR_BADNAME:
158 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400159 case -NFS4ERR_SHARE_DENIED:
160 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400161 case -NFS4ERR_MINOR_VERS_MISMATCH:
162 return -EPROTONOSUPPORT;
Weston Andros Adamson6168f622012-09-10 14:00:46 -0400163 case -NFS4ERR_ACCESS:
164 return -EACCES;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400165 case -NFS4ERR_FILE_OPEN:
166 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400167 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700169 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400170 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400172 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
175/*
176 * This is our standard bitmap for GETATTR requests.
177 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400178const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 FATTR4_WORD0_TYPE
180 | FATTR4_WORD0_CHANGE
181 | FATTR4_WORD0_SIZE
182 | FATTR4_WORD0_FSID
183 | FATTR4_WORD0_FILEID,
184 FATTR4_WORD1_MODE
185 | FATTR4_WORD1_NUMLINKS
186 | FATTR4_WORD1_OWNER
187 | FATTR4_WORD1_OWNER_GROUP
188 | FATTR4_WORD1_RAWDEV
189 | FATTR4_WORD1_SPACE_USED
190 | FATTR4_WORD1_TIME_ACCESS
191 | FATTR4_WORD1_TIME_METADATA
David Quigleyaa9c2662013-05-22 12:50:44 -0400192 | FATTR4_WORD1_TIME_MODIFY,
193#ifdef CONFIG_NFS_V4_SECURITY_LABEL
194 FATTR4_WORD2_SECURITY_LABEL
195#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196};
197
Trond Myklebust1549210f2012-06-05 09:16:47 -0400198static const u32 nfs4_pnfs_open_bitmap[3] = {
199 FATTR4_WORD0_TYPE
200 | FATTR4_WORD0_CHANGE
201 | FATTR4_WORD0_SIZE
202 | FATTR4_WORD0_FSID
203 | FATTR4_WORD0_FILEID,
204 FATTR4_WORD1_MODE
205 | FATTR4_WORD1_NUMLINKS
206 | FATTR4_WORD1_OWNER
207 | FATTR4_WORD1_OWNER_GROUP
208 | FATTR4_WORD1_RAWDEV
209 | FATTR4_WORD1_SPACE_USED
210 | FATTR4_WORD1_TIME_ACCESS
211 | FATTR4_WORD1_TIME_METADATA
212 | FATTR4_WORD1_TIME_MODIFY,
213 FATTR4_WORD2_MDSTHRESHOLD
214};
215
Andy Adamsone23008e2012-10-02 21:07:32 -0400216static const u32 nfs4_open_noattr_bitmap[3] = {
217 FATTR4_WORD0_TYPE
218 | FATTR4_WORD0_CHANGE
219 | FATTR4_WORD0_FILEID,
220};
221
David Quigleya09df2c2013-05-22 12:50:41 -0400222const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 FATTR4_WORD0_FILES_AVAIL
224 | FATTR4_WORD0_FILES_FREE
225 | FATTR4_WORD0_FILES_TOTAL,
226 FATTR4_WORD1_SPACE_AVAIL
227 | FATTR4_WORD1_SPACE_FREE
228 | FATTR4_WORD1_SPACE_TOTAL
229};
230
David Quigleya09df2c2013-05-22 12:50:41 -0400231const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 FATTR4_WORD0_MAXLINK
233 | FATTR4_WORD0_MAXNAME,
234 0
235};
236
Fred Isamandae100c2011-07-30 20:52:37 -0400237const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 | FATTR4_WORD0_MAXREAD
239 | FATTR4_WORD0_MAXWRITE
240 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700241 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400242 | FATTR4_WORD1_FS_LAYOUT_TYPES,
243 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244};
245
David Quigleya09df2c2013-05-22 12:50:41 -0400246const u32 nfs4_fs_locations_bitmap[3] = {
Manoj Naik830b8e32006-06-09 09:34:25 -0400247 FATTR4_WORD0_TYPE
248 | FATTR4_WORD0_CHANGE
249 | FATTR4_WORD0_SIZE
250 | FATTR4_WORD0_FSID
251 | FATTR4_WORD0_FILEID
252 | FATTR4_WORD0_FS_LOCATIONS,
253 FATTR4_WORD1_MODE
254 | FATTR4_WORD1_NUMLINKS
255 | FATTR4_WORD1_OWNER
256 | FATTR4_WORD1_OWNER_GROUP
257 | FATTR4_WORD1_RAWDEV
258 | FATTR4_WORD1_SPACE_USED
259 | FATTR4_WORD1_TIME_ACCESS
260 | FATTR4_WORD1_TIME_METADATA
261 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400262 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400263};
264
Al Virobc4785c2006-10-19 23:28:51 -0700265static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 struct nfs4_readdir_arg *readdir)
267{
Al Viro0dbb4c62006-10-19 23:28:49 -0700268 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000271 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
273 return;
274 }
275
276 readdir->cookie = 0;
277 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
278 if (cookie == 2)
279 return;
280
281 /*
282 * NFSv4 servers do not return entries for '.' and '..'
283 * Therefore, we fake these entries here. We let '.'
284 * have cookie 0 and '..' have cookie 1. Note that
285 * when talking to the server, we always send cookie 0
286 * instead of 1 or 2.
287 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800288 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 if (cookie == 0) {
291 *p++ = xdr_one; /* next */
292 *p++ = xdr_zero; /* cookie, first word */
293 *p++ = xdr_one; /* cookie, second word */
294 *p++ = xdr_one; /* entry len */
295 memcpy(p, ".\0\0\0", 4); /* entry */
296 p++;
297 *p++ = xdr_one; /* bitmap length */
298 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
299 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400300 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302
303 *p++ = xdr_one; /* next */
304 *p++ = xdr_zero; /* cookie, first word */
305 *p++ = xdr_two; /* cookie, second word */
306 *p++ = xdr_two; /* entry len */
307 memcpy(p, "..\0\0", 4); /* entry */
308 p++;
309 *p++ = xdr_one; /* bitmap length */
310 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
311 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400312 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 readdir->pgbase = (char *)p - (char *)start;
315 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800316 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
Trond Myklebust65de8722008-12-23 15:21:44 -0500319static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
320{
321 int res = 0;
322
323 might_sleep();
324
325 if (*timeout <= 0)
326 *timeout = NFS4_POLL_RETRY_MIN;
327 if (*timeout > NFS4_POLL_RETRY_MAX)
328 *timeout = NFS4_POLL_RETRY_MAX;
Colin Cross416ad3c2013-05-06 23:50:06 +0000329 freezable_schedule_timeout_killable_unsafe(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500330 if (fatal_signal_pending(current))
331 res = -ERESTARTSYS;
332 *timeout <<= 1;
333 return res;
334}
335
336/* This is the error handling routine for processes that are allowed
337 * to sleep.
338 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800339static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500340{
341 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500342 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500343 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500344 int ret = errorcode;
345
346 exception->retry = 0;
347 switch(errorcode) {
348 case 0:
349 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500350 case -NFS4ERR_OPENMODE:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400351 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400352 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500353 exception->retry = 1;
354 return 0;
355 }
356 if (state == NULL)
357 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400358 ret = nfs4_schedule_stateid_recovery(server, state);
359 if (ret < 0)
360 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500361 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500362 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500363 case -NFS4ERR_ADMIN_REVOKED:
364 case -NFS4ERR_BAD_STATEID:
Trond Myklebustb570a972013-04-12 15:04:51 -0400365 if (inode != NULL && nfs4_have_delegation(inode, FMODE_READ)) {
366 nfs_remove_bad_delegation(inode);
367 exception->retry = 1;
368 break;
369 }
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500370 if (state == NULL)
371 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400372 ret = nfs4_schedule_stateid_recovery(server, state);
373 if (ret < 0)
374 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500375 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400376 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -0400377 if (state != NULL) {
378 ret = nfs4_schedule_stateid_recovery(server, state);
379 if (ret < 0)
380 break;
381 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500382 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500383 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500384 nfs4_schedule_lease_recovery(clp);
385 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500386#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400387 case -NFS4ERR_BADSESSION:
388 case -NFS4ERR_BADSLOT:
389 case -NFS4ERR_BAD_HIGH_SLOT:
390 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
391 case -NFS4ERR_DEADSESSION:
392 case -NFS4ERR_SEQ_FALSE_RETRY:
393 case -NFS4ERR_SEQ_MISORDERED:
394 dprintk("%s ERROR: %d Reset session\n", __func__,
395 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400396 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400397 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500398#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500399 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500400 if (exception->timeout > HZ) {
401 /* We have retried a decent amount, time to
402 * fail
403 */
404 ret = -EBUSY;
405 break;
406 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500407 case -NFS4ERR_GRACE:
408 case -NFS4ERR_DELAY:
409 ret = nfs4_delay(server->client, &exception->timeout);
410 if (ret != 0)
411 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400412 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500413 case -NFS4ERR_OLD_STATEID:
414 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800415 break;
416 case -NFS4ERR_BADOWNER:
417 /* The following works around a Linux server bug! */
418 case -NFS4ERR_BADNAME:
419 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
420 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
421 exception->retry = 1;
422 printk(KERN_WARNING "NFS: v4 server %s "
423 "does not accept raw "
424 "uid/gids. "
425 "Reenabling the idmapper.\n",
426 server->nfs_client->cl_hostname);
427 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500428 }
429 /* We failed to handle the error */
430 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500431wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500432 ret = nfs4_wait_clnt_recover(clp);
433 if (ret == 0)
434 exception->retry = 1;
435 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500436}
437
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400438/*
439 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
440 * or 'false' otherwise.
441 */
442static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
443{
444 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
445
446 if (flavor == RPC_AUTH_GSS_KRB5I ||
447 flavor == RPC_AUTH_GSS_KRB5P)
448 return true;
449
450 return false;
451}
Trond Myklebust65de8722008-12-23 15:21:44 -0500452
Trond Myklebust452e9352010-07-31 14:29:06 -0400453static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 spin_lock(&clp->cl_lock);
456 if (time_before(clp->cl_last_renewal,timestamp))
457 clp->cl_last_renewal = timestamp;
458 spin_unlock(&clp->cl_lock);
459}
460
Trond Myklebust452e9352010-07-31 14:29:06 -0400461static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
462{
463 do_renew_lease(server->nfs_client, timestamp);
464}
465
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400466struct nfs4_call_sync_data {
467 const struct nfs_server *seq_server;
468 struct nfs4_sequence_args *seq_args;
469 struct nfs4_sequence_res *seq_res;
470};
471
Chuck Levera9c92d62013-08-09 12:48:18 -0400472static void nfs4_init_sequence(struct nfs4_sequence_args *args,
473 struct nfs4_sequence_res *res, int cache_reply)
474{
475 args->sa_slot = NULL;
476 args->sa_cache_this = cache_reply;
477 args->sa_privileged = 0;
478
479 res->sr_slot = NULL;
480}
481
482static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
483{
484 args->sa_privileged = 1;
485}
486
Chuck Lever3bd23842013-08-09 12:49:19 -0400487static int nfs40_setup_sequence(const struct nfs_server *server,
488 struct nfs4_sequence_args *args,
489 struct nfs4_sequence_res *res,
490 struct rpc_task *task)
491{
492 struct nfs4_slot_table *tbl = server->nfs_client->cl_slot_tbl;
493 struct nfs4_slot *slot;
494
495 /* slot already allocated? */
496 if (res->sr_slot != NULL)
497 goto out_start;
498
499 spin_lock(&tbl->slot_tbl_lock);
500 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
501 goto out_sleep;
502
503 slot = nfs4_alloc_slot(tbl);
504 if (IS_ERR(slot)) {
505 if (slot == ERR_PTR(-ENOMEM))
506 task->tk_timeout = HZ >> 2;
507 goto out_sleep;
508 }
509 spin_unlock(&tbl->slot_tbl_lock);
510
511 args->sa_slot = slot;
512 res->sr_slot = slot;
513
514out_start:
515 rpc_call_start(task);
516 return 0;
517
518out_sleep:
519 if (args->sa_privileged)
520 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
521 NULL, RPC_PRIORITY_PRIVILEGED);
522 else
523 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
524 spin_unlock(&tbl->slot_tbl_lock);
525 return -EAGAIN;
526}
527
528static int nfs40_sequence_done(struct rpc_task *task,
529 struct nfs4_sequence_res *res)
530{
531 struct nfs4_slot *slot = res->sr_slot;
532 struct nfs4_slot_table *tbl;
533
534 if (!RPC_WAS_SENT(task))
535 goto out;
536
537 tbl = slot->table;
538 spin_lock(&tbl->slot_tbl_lock);
539 if (!nfs41_wake_and_assign_slot(tbl, slot))
540 nfs4_free_slot(tbl, slot);
541 spin_unlock(&tbl->slot_tbl_lock);
542
543 res->sr_slot = NULL;
544out:
545 return 1;
546}
547
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400548#if defined(CONFIG_NFS_V4_1)
549
Trond Myklebustd185a332010-06-16 09:52:25 -0400550static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400551{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500552 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400553 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500554 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400555
Benny Halevydfb4f3092010-09-24 09:17:01 -0400556 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400557 /* just wake up the next guy waiting since
558 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500559 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500560 return;
Andy Adamson13615872009-04-01 09:22:17 -0400561 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500562 tbl = res->sr_slot->table;
563 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500564
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500565 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500566 /* Be nice to the server: try to ensure that the last transmitted
567 * value for highest_user_slotid <= target_highest_slotid
568 */
569 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
570 send_new_highest_used_slotid = true;
571
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500572 if (nfs41_wake_and_assign_slot(tbl, res->sr_slot)) {
573 send_new_highest_used_slotid = false;
574 goto out_unlock;
575 }
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500576 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500577
578 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
579 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500580out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500581 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400582 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500583 if (send_new_highest_used_slotid)
584 nfs41_server_notify_highest_slotid_update(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400585}
586
Trond Myklebust14516c32010-07-31 14:29:06 -0400587static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400588{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500589 struct nfs4_session *session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500590 struct nfs4_slot *slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400591 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500592 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500593 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400594
Bryan Schumaker468f8612011-04-18 15:57:32 -0400595 /* don't increment the sequence number if the task wasn't sent */
596 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400597 goto out;
598
Trond Myklebust933602e2012-11-16 12:12:38 -0500599 slot = res->sr_slot;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500600 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500601
Trond Myklebustac20d162012-12-15 15:36:07 -0500602 if (slot->interrupted) {
603 slot->interrupted = 0;
604 interrupted = true;
605 }
606
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400607 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500608 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400609 switch (res->sr_status) {
610 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400611 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500612 ++slot->seq_nr;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500613 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500614 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500615 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500616 if (res->sr_status_flags != 0)
617 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500618 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400619 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500620 case 1:
621 /*
622 * sr_status remains 1 if an RPC level error occurred.
623 * The server may or may not have processed the sequence
624 * operation..
625 * Mark the slot as having hosted an interrupted RPC call.
626 */
627 slot->interrupted = 1;
628 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400629 case -NFS4ERR_DELAY:
630 /* The server detected a resend of the RPC call and
631 * returned NFS4ERR_DELAY as per Section 2.10.6.2
632 * of RFC5661.
633 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500634 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400635 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500636 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500637 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400638 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500639 case -NFS4ERR_BADSLOT:
640 /*
641 * The slot id we used was probably retired. Try again
642 * using a different slot id.
643 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500644 goto retry_nowait;
645 case -NFS4ERR_SEQ_MISORDERED:
646 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500647 * Was the last operation on this sequence interrupted?
648 * If so, retry after bumping the sequence number.
649 */
650 if (interrupted) {
651 ++slot->seq_nr;
652 goto retry_nowait;
653 }
654 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500655 * Could this slot have been previously retired?
656 * If so, then the server may be expecting seq_nr = 1!
657 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500658 if (slot->seq_nr != 1) {
659 slot->seq_nr = 1;
660 goto retry_nowait;
661 }
662 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500663 case -NFS4ERR_SEQ_FALSE_RETRY:
664 ++slot->seq_nr;
665 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400666 default:
667 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500668 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400669 }
670out:
671 /* The session may be reset by one of the error handlers. */
672 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400673 nfs41_sequence_free_slot(res);
Trond Myklebust85563072012-12-11 10:31:12 -0500674 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500675retry_nowait:
676 if (rpc_restart_call_prepare(task)) {
677 task->tk_status = 0;
678 ret = 0;
679 }
680 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400681out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400682 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400683 goto out;
684 rpc_delay(task, NFS4_POLL_RETRY_MAX);
685 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400686}
687
Trond Myklebust14516c32010-07-31 14:29:06 -0400688static int nfs4_sequence_done(struct rpc_task *task,
689 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400690{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500691 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400692 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400693 if (!res->sr_slot->table->session)
694 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400695 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400696}
697
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000698int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400699 struct nfs4_sequence_args *args,
700 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400701 struct rpc_task *task)
702{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400703 struct nfs4_slot *slot;
704 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400705
706 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400707 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400708 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400709 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400710
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400711 tbl = &session->fc_slot_table;
712
Trond Myklebust69d206b2012-11-22 13:21:02 -0500713 task->tk_timeout = 0;
714
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400715 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400716 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400717 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500718 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500719 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400720 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400721 }
722
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500723 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500724 if (IS_ERR(slot)) {
725 /* If out of memory, try again in 1/4 second */
726 if (slot == ERR_PTR(-ENOMEM))
727 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400728 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400729 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400730 }
731 spin_unlock(&tbl->slot_tbl_lock);
732
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500733 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400734
Chuck Levere8d92382013-08-09 12:47:51 -0400735 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500736 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400737
Benny Halevydfb4f3092010-09-24 09:17:01 -0400738 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500739 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400740 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400741 /*
742 * sr_status is only set in decode_sequence, and so will remain
743 * set to 1 if an rpc level failure occurs.
744 */
745 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400746 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400747out_success:
748 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400749 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400750out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400751 /* Privileged tasks are queued with top priority */
752 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400753 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
754 NULL, RPC_PRIORITY_PRIVILEGED);
755 else
756 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400757 spin_unlock(&tbl->slot_tbl_lock);
758 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400759}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000760EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400761
Chuck Lever5a580e02013-08-09 12:48:09 -0400762static int nfs4_setup_sequence(const struct nfs_server *server,
763 struct nfs4_sequence_args *args,
764 struct nfs4_sequence_res *res,
765 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400766{
Trond Myklebust035168a2010-06-16 09:52:26 -0400767 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400768 int ret = 0;
769
Chuck Lever3bd23842013-08-09 12:49:19 -0400770 if (!session)
771 return nfs40_setup_sequence(server, args, res, task);
Trond Myklebust035168a2010-06-16 09:52:26 -0400772
Chuck Levere8d92382013-08-09 12:47:51 -0400773 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400774 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400775 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168a2010-06-16 09:52:26 -0400776
Trond Myklebust9d12b212012-01-17 22:04:25 -0500777 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400778
Andy Adamsonce5039c2009-04-01 09:22:13 -0400779 dprintk("<-- %s status=%d\n", __func__, ret);
780 return ret;
781}
782
Andy Adamsonce5039c2009-04-01 09:22:13 -0400783static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
784{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400785 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400786 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400787
Trond Myklebust035168a2010-06-16 09:52:26 -0400788 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
789
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400790 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400791}
792
Andy Adamson69ab40c2009-04-01 09:22:19 -0400793static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
794{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400795 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400796
Trond Myklebust14516c32010-07-31 14:29:06 -0400797 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400798}
799
Trond Myklebust17280172012-03-11 13:11:00 -0400800static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400801 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400802 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400803};
804
Chuck Lever3bd23842013-08-09 12:49:19 -0400805#else /* !CONFIG_NFS_V4_1 */
806
Chuck Lever5a580e02013-08-09 12:48:09 -0400807static int nfs4_setup_sequence(const struct nfs_server *server,
808 struct nfs4_sequence_args *args,
809 struct nfs4_sequence_res *res,
810 struct rpc_task *task)
811{
Chuck Lever3bd23842013-08-09 12:49:19 -0400812 return nfs40_setup_sequence(server, args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -0400813}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400814
Trond Myklebust14516c32010-07-31 14:29:06 -0400815static int nfs4_sequence_done(struct rpc_task *task,
816 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400817{
Chuck Lever3bd23842013-08-09 12:49:19 -0400818 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400819}
Chuck Lever3bd23842013-08-09 12:49:19 -0400820
821#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400822
Chuck Lever9915ea72013-08-09 12:48:27 -0400823static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
824{
825 struct nfs4_call_sync_data *data = calldata;
826 nfs4_setup_sequence(data->seq_server,
827 data->seq_args, data->seq_res, task);
828}
829
830static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
831{
832 struct nfs4_call_sync_data *data = calldata;
833 nfs4_sequence_done(task, data->seq_res);
834}
835
836static const struct rpc_call_ops nfs40_call_sync_ops = {
837 .rpc_call_prepare = nfs40_call_sync_prepare,
838 .rpc_call_done = nfs40_call_sync_done,
839};
840
841static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
842 struct nfs_server *server,
843 struct rpc_message *msg,
844 struct nfs4_sequence_args *args,
845 struct nfs4_sequence_res *res)
846{
847 int ret;
848 struct rpc_task *task;
849 struct nfs_client *clp = server->nfs_client;
850 struct nfs4_call_sync_data data = {
851 .seq_server = server,
852 .seq_args = args,
853 .seq_res = res,
854 };
855 struct rpc_task_setup task_setup = {
856 .rpc_client = clnt,
857 .rpc_message = msg,
858 .callback_ops = clp->cl_mvops->call_sync_ops,
859 .callback_data = &data
860 };
861
862 task = rpc_run_task(&task_setup);
863 if (IS_ERR(task))
864 ret = PTR_ERR(task);
865 else {
866 ret = task->tk_status;
867 rpc_put_task(task);
868 }
869 return ret;
870}
871
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400872static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000873int nfs4_call_sync(struct rpc_clnt *clnt,
874 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000875 struct rpc_message *msg,
876 struct nfs4_sequence_args *args,
877 struct nfs4_sequence_res *res,
878 int cache_reply)
879{
Chuck Levera9c92d62013-08-09 12:48:18 -0400880 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -0400881 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000882}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400883
Trond Myklebust38478b22006-05-25 01:40:57 -0400884static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
Trond Myklebust38478b22006-05-25 01:40:57 -0400886 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Trond Myklebust38478b22006-05-25 01:40:57 -0400888 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400889 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700890 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400891 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700892 dir->i_version = cinfo->after;
David Howellsde242c02012-12-20 21:52:38 +0000893 nfs_fscache_invalidate(dir);
Trond Myklebust38478b22006-05-25 01:40:57 -0400894 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895}
896
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100897struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400898 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100899 struct nfs_openargs o_arg;
900 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100901 struct nfs_open_confirmargs c_arg;
902 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500903 struct nfs4_string owner_name;
904 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100905 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400906 struct nfs4_label *f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100907 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400908 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100909 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400910 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100911 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100912 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400913 unsigned int rpc_done : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -0400914 unsigned int is_recover : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100915 int rpc_status;
916 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100917};
918
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400919static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
920 int err, struct nfs4_exception *exception)
921{
922 if (err != -EINVAL)
923 return false;
924 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
925 return false;
926 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
927 exception->retry = 1;
928 return true;
929}
930
931static enum open_claim_type4
932nfs4_map_atomic_open_claim(struct nfs_server *server,
933 enum open_claim_type4 claim)
934{
935 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
936 return claim;
937 switch (claim) {
938 default:
939 return claim;
940 case NFS4_OPEN_CLAIM_FH:
941 return NFS4_OPEN_CLAIM_NULL;
942 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
943 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
944 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
945 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
946 }
947}
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400948
949static void nfs4_init_opendata_res(struct nfs4_opendata *p)
950{
951 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400952 p->o_res.f_label = p->f_label;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400953 p->o_res.seqid = p->o_arg.seqid;
954 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400955 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400956 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400957 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500958 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400959}
960
Al Viro82a2c1b2011-06-22 18:30:55 -0400961static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500962 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400963 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -0400964 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400965 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400966 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100967{
Al Viro82a2c1b2011-06-22 18:30:55 -0400968 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100969 struct inode *dir = parent->d_inode;
970 struct nfs_server *server = NFS_SERVER(dir);
971 struct nfs4_opendata *p;
972
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400973 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100974 if (p == NULL)
975 goto err;
David Quigley14c43f72013-05-22 12:50:43 -0400976
977 p->f_label = nfs4_label_alloc(server, gfp_mask);
978 if (IS_ERR(p->f_label))
979 goto err_free_p;
980
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400981 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100982 if (p->o_arg.seqid == NULL)
David Quigley14c43f72013-05-22 12:50:43 -0400983 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -0400984 nfs_sb_active(dentry->d_sb);
985 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100986 p->dir = parent;
987 p->owner = sp;
988 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500989 p->o_arg.open_flags = flags;
990 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700991 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
992 * will return permission denied for all bits until close */
993 if (!(flags & O_EXCL)) {
994 /* ask server to check for all possible rights as results
995 * are cached */
996 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
997 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700998 }
David Howells7539bba2006-08-22 20:06:09 -0400999 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001000 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1001 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001002 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001003 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001004 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001005 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
David Quigley1775fd32013-05-22 12:50:42 -04001006 p->o_arg.label = label;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001007 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1008 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001009 case NFS4_OPEN_CLAIM_NULL:
1010 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1011 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1012 p->o_arg.fh = NFS_FH(dir);
1013 break;
1014 case NFS4_OPEN_CLAIM_PREVIOUS:
1015 case NFS4_OPEN_CLAIM_FH:
1016 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1017 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1018 p->o_arg.fh = NFS_FH(dentry->d_inode);
1019 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001020 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001021 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001022
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001023 p->o_arg.u.attrs = &p->attrs;
1024 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001025
1026 verf[0] = jiffies;
1027 verf[1] = current->pid;
1028 memcpy(p->o_arg.u.verifier.data, verf,
1029 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001030 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001031 p->c_arg.fh = &p->o_res.fh;
1032 p->c_arg.stateid = &p->o_res.stateid;
1033 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001034 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001035 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001036 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001037
1038err_free_label:
1039 nfs4_label_free(p->f_label);
1040err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001041 kfree(p);
1042err:
1043 dput(parent);
1044 return NULL;
1045}
1046
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001047static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001048{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001049 struct nfs4_opendata *p = container_of(kref,
1050 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001051 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001052
1053 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001054 if (p->state != NULL)
1055 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001056 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001057
1058 nfs4_label_free(p->f_label);
1059
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001060 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001061 dput(p->dentry);
1062 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001063 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001064 kfree(p);
1065}
1066
1067static void nfs4_opendata_put(struct nfs4_opendata *p)
1068{
1069 if (p != NULL)
1070 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001071}
1072
Trond Myklebust06f814a2006-01-03 09:55:07 +01001073static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1074{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001075 int ret;
1076
Trond Myklebust06f814a2006-01-03 09:55:07 +01001077 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001078 return ret;
1079}
1080
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001081static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001082{
1083 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001084
Trond Myklebust536e43d2012-01-17 22:04:26 -05001085 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001086 goto out;
1087 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001088 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001089 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1090 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001091 break;
1092 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001093 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1094 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001095 break;
1096 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001097 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1098 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001099 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001100out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001101 return ret;
1102}
1103
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001104static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001105{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001106 if (delegation == NULL)
1107 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001108 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001109 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -05001110 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -04001111 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001112 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1113 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -05001114 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001115 return 1;
1116}
1117
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001118static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001119{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001120 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001121 case FMODE_WRITE:
1122 state->n_wronly++;
1123 break;
1124 case FMODE_READ:
1125 state->n_rdonly++;
1126 break;
1127 case FMODE_READ|FMODE_WRITE:
1128 state->n_rdwr++;
1129 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001130 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001131}
1132
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001133static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001134{
1135 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001136 nfs4_stateid_copy(&state->stateid, stateid);
1137 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust92b40e92013-04-20 01:25:45 -04001138 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001139 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001140 case FMODE_READ:
1141 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1142 break;
1143 case FMODE_WRITE:
1144 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1145 break;
1146 case FMODE_READ|FMODE_WRITE:
1147 set_bit(NFS_O_RDWR_STATE, &state->flags);
1148 }
1149}
1150
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001151static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001152{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001153 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001154 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001155 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001156}
1157
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001158static 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 -07001159{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001160 /*
1161 * Protect the call to nfs4_state_set_mode_locked and
1162 * serialise the stateid update
1163 */
1164 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001165 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001166 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001167 set_bit(NFS_DELEGATED_STATE, &state->flags);
1168 }
1169 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001170 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001171 write_sequnlock(&state->seqlock);
1172 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001173 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001174 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175}
1176
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001177static 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 -05001178{
1179 struct nfs_inode *nfsi = NFS_I(state->inode);
1180 struct nfs_delegation *deleg_cur;
1181 int ret = 0;
1182
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001183 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001184
1185 rcu_read_lock();
1186 deleg_cur = rcu_dereference(nfsi->delegation);
1187 if (deleg_cur == NULL)
1188 goto no_delegation;
1189
1190 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001191 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001192 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001193 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001194 goto no_delegation_unlock;
1195
1196 if (delegation == NULL)
1197 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001198 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001199 goto no_delegation_unlock;
1200
Trond Myklebustb7391f42008-12-23 15:21:52 -05001201 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001202 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001203 ret = 1;
1204no_delegation_unlock:
1205 spin_unlock(&deleg_cur->lock);
1206no_delegation:
1207 rcu_read_unlock();
1208
1209 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001210 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001211 ret = 1;
1212 }
1213
1214 return ret;
1215}
1216
1217
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001218static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001219{
1220 struct nfs_delegation *delegation;
1221
1222 rcu_read_lock();
1223 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001224 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001225 rcu_read_unlock();
1226 return;
1227 }
1228 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001229 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001230}
1231
Trond Myklebust6ee41262007-07-08 14:11:36 -04001232static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001233{
1234 struct nfs4_state *state = opendata->state;
1235 struct nfs_inode *nfsi = NFS_I(state->inode);
1236 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001237 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001238 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001239 nfs4_stateid stateid;
1240 int ret = -EAGAIN;
1241
Trond Myklebustaac00a82007-07-05 19:02:21 -04001242 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001243 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001244 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001245 if (can_open_cached(state, fmode, open_mode)) {
1246 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001247 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001248 goto out_return_state;
1249 }
1250 spin_unlock(&state->owner->so_lock);
1251 }
Trond Myklebust34310432008-12-23 15:21:38 -05001252 rcu_read_lock();
1253 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001254 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001255 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001256 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001257 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001258 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001259 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001260 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001261 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001262 if (!opendata->is_recover) {
1263 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1264 if (ret != 0)
1265 goto out;
1266 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001267 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001268
1269 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001270 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001271 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001272 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001273out:
1274 return ERR_PTR(ret);
1275out_return_state:
1276 atomic_inc(&state->count);
1277 return state;
1278}
1279
Andy Adamsone23008e2012-10-02 21:07:32 -04001280static void
1281nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1282{
1283 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1284 struct nfs_delegation *delegation;
1285 int delegation_flags = 0;
1286
1287 rcu_read_lock();
1288 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1289 if (delegation)
1290 delegation_flags = delegation->flags;
1291 rcu_read_unlock();
1292 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1293 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1294 "returning a delegation for "
1295 "OPEN(CLAIM_DELEGATE_CUR)\n",
1296 clp->cl_hostname);
1297 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1298 nfs_inode_set_delegation(state->inode,
1299 data->owner->so_cred,
1300 &data->o_res);
1301 else
1302 nfs_inode_reclaim_delegation(state->inode,
1303 data->owner->so_cred,
1304 &data->o_res);
1305}
1306
1307/*
1308 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1309 * and update the nfs4_state.
1310 */
1311static struct nfs4_state *
1312_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1313{
1314 struct inode *inode = data->state->inode;
1315 struct nfs4_state *state = data->state;
1316 int ret;
1317
1318 if (!data->rpc_done) {
1319 ret = data->rpc_status;
1320 goto err;
1321 }
1322
1323 ret = -ESTALE;
1324 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1325 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1326 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1327 goto err;
1328
1329 ret = -ENOMEM;
1330 state = nfs4_get_open_state(inode, data->owner);
1331 if (state == NULL)
1332 goto err;
1333
1334 ret = nfs_refresh_inode(inode, &data->f_attr);
1335 if (ret)
1336 goto err;
1337
David Quigleyaa9c2662013-05-22 12:50:44 -04001338 nfs_setsecurity(inode, &data->f_attr, data->f_label);
1339
Andy Adamsone23008e2012-10-02 21:07:32 -04001340 if (data->o_res.delegation_type != 0)
1341 nfs4_opendata_check_deleg(data, state);
1342 update_open_stateid(state, &data->o_res.stateid, NULL,
1343 data->o_arg.fmode);
1344
1345 return state;
1346err:
1347 return ERR_PTR(ret);
1348
1349}
1350
1351static struct nfs4_state *
1352_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001353{
1354 struct inode *inode;
1355 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001356 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001357
Trond Myklebustaac00a82007-07-05 19:02:21 -04001358 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001359 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001360 goto out;
1361 }
1362
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001363 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001364 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001365 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001366 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001367 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001368 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001369 goto err;
1370 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001371 state = nfs4_get_open_state(inode, data->owner);
1372 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001373 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001374 if (data->o_res.delegation_type != 0)
1375 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001376 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001377 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001378 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001379out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001380 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001381 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001382err_put_inode:
1383 iput(inode);
1384err:
1385 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001386}
1387
Andy Adamsone23008e2012-10-02 21:07:32 -04001388static struct nfs4_state *
1389nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1390{
1391 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1392 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1393 return _nfs4_opendata_to_nfs4_state(data);
1394}
1395
Trond Myklebust864472e2006-01-03 09:55:15 +01001396static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1397{
1398 struct nfs_inode *nfsi = NFS_I(state->inode);
1399 struct nfs_open_context *ctx;
1400
1401 spin_lock(&state->inode->i_lock);
1402 list_for_each_entry(ctx, &nfsi->open_files, list) {
1403 if (ctx->state != state)
1404 continue;
1405 get_nfs_open_context(ctx);
1406 spin_unlock(&state->inode->i_lock);
1407 return ctx;
1408 }
1409 spin_unlock(&state->inode->i_lock);
1410 return ERR_PTR(-ENOENT);
1411}
1412
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001413static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1414 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001415{
1416 struct nfs4_opendata *opendata;
1417
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001418 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001419 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001420 if (opendata == NULL)
1421 return ERR_PTR(-ENOMEM);
1422 opendata->state = state;
1423 atomic_inc(&state->count);
1424 return opendata;
1425}
1426
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001427static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001428{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001429 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001430 int ret;
1431
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001432 opendata->o_arg.open_flags = 0;
1433 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001434 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1435 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1436 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001437 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001438 if (ret != 0)
1439 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001440 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001441 if (IS_ERR(newstate))
1442 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001443 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001444 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001445 return 0;
1446}
1447
1448static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1449{
Trond Myklebust864472e2006-01-03 09:55:15 +01001450 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001451 int ret;
1452
1453 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001454 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001455 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001456 smp_rmb();
1457 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001458 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001459 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001460 if (ret != 0)
1461 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001462 if (newstate != state)
1463 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001464 }
1465 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001466 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001467 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001468 if (ret != 0)
1469 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001470 if (newstate != state)
1471 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001472 }
1473 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001474 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001475 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001476 if (ret != 0)
1477 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001478 if (newstate != state)
1479 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001480 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001481 /*
1482 * We may have performed cached opens for all three recoveries.
1483 * Check if we need to update the current stateid.
1484 */
1485 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001486 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001487 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001488 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001489 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001490 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001491 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001492 return 0;
1493}
1494
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495/*
1496 * OPEN_RECLAIM:
1497 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001499static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001501 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001502 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001503 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 int status;
1505
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001506 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1507 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001508 if (IS_ERR(opendata))
1509 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001510 rcu_read_lock();
1511 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001512 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001513 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001514 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001515 opendata->o_arg.u.delegation_type = delegation_type;
1516 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001517 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 return status;
1519}
1520
Trond Myklebust539cd032007-06-05 11:46:42 -04001521static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522{
1523 struct nfs_server *server = NFS_SERVER(state->inode);
1524 struct nfs4_exception exception = { };
1525 int err;
1526 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001527 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001528 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001529 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1530 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001531 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001532 break;
1533 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 } while (exception.retry);
1535 return err;
1536}
1537
Trond Myklebust864472e2006-01-03 09:55:15 +01001538static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1539{
1540 struct nfs_open_context *ctx;
1541 int ret;
1542
1543 ctx = nfs4_state_find_open_context(state);
1544 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001545 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001546 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001547 put_nfs_open_context(ctx);
1548 return ret;
1549}
1550
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001551static 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 -07001552{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001553 switch (err) {
1554 default:
1555 printk(KERN_ERR "NFS: %s: unhandled error "
1556 "%d.\n", __func__, err);
1557 case 0:
1558 case -ENOENT:
1559 case -ESTALE:
1560 break;
1561 case -NFS4ERR_BADSESSION:
1562 case -NFS4ERR_BADSLOT:
1563 case -NFS4ERR_BAD_HIGH_SLOT:
1564 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1565 case -NFS4ERR_DEADSESSION:
1566 set_bit(NFS_DELEGATED_STATE, &state->flags);
1567 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1568 return -EAGAIN;
1569 case -NFS4ERR_STALE_CLIENTID:
1570 case -NFS4ERR_STALE_STATEID:
1571 set_bit(NFS_DELEGATED_STATE, &state->flags);
1572 case -NFS4ERR_EXPIRED:
1573 /* Don't recall a delegation if it was lost */
1574 nfs4_schedule_lease_recovery(server->nfs_client);
1575 return -EAGAIN;
1576 case -NFS4ERR_DELEG_REVOKED:
1577 case -NFS4ERR_ADMIN_REVOKED:
1578 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001579 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001580 nfs_inode_find_state_and_recover(state->inode,
1581 stateid);
1582 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001583 return 0;
1584 case -NFS4ERR_DELAY:
1585 case -NFS4ERR_GRACE:
1586 set_bit(NFS_DELEGATED_STATE, &state->flags);
1587 ssleep(1);
1588 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001589 case -ENOMEM:
1590 case -NFS4ERR_DENIED:
1591 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1592 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 return err;
1595}
1596
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001597int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1598{
1599 struct nfs_server *server = NFS_SERVER(state->inode);
1600 struct nfs4_opendata *opendata;
1601 int err;
1602
1603 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1604 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1605 if (IS_ERR(opendata))
1606 return PTR_ERR(opendata);
1607 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1608 err = nfs4_open_recover(opendata, state);
1609 nfs4_opendata_put(opendata);
1610 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1611}
1612
Chuck Leverbe05c862013-08-09 12:49:47 -04001613static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1614{
1615 struct nfs4_opendata *data = calldata;
1616
1617 nfs40_setup_sequence(data->o_arg.server, &data->o_arg.seq_args,
1618 &data->o_res.seq_res, task);
1619}
1620
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001621static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1622{
1623 struct nfs4_opendata *data = calldata;
1624
Chuck Leverbe05c862013-08-09 12:49:47 -04001625 nfs40_sequence_done(task, &data->o_res.seq_res);
1626
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001627 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001628 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001629 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001630 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001631 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001632 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001633 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001634}
1635
1636static void nfs4_open_confirm_release(void *calldata)
1637{
1638 struct nfs4_opendata *data = calldata;
1639 struct nfs4_state *state = NULL;
1640
1641 /* If this request hasn't been cancelled, do nothing */
1642 if (data->cancelled == 0)
1643 goto out_free;
1644 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001645 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001646 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001647 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001648 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001649 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001650out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001651 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001652}
1653
1654static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04001655 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001656 .rpc_call_done = nfs4_open_confirm_done,
1657 .rpc_release = nfs4_open_confirm_release,
1658};
1659
1660/*
1661 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1662 */
1663static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1664{
1665 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1666 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001667 struct rpc_message msg = {
1668 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1669 .rpc_argp = &data->c_arg,
1670 .rpc_resp = &data->c_res,
1671 .rpc_cred = data->owner->so_cred,
1672 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001673 struct rpc_task_setup task_setup_data = {
1674 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001675 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001676 .callback_ops = &nfs4_open_confirm_ops,
1677 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001678 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001679 .flags = RPC_TASK_ASYNC,
1680 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 int status;
1682
Chuck Leverbe05c862013-08-09 12:49:47 -04001683 nfs4_init_sequence(&data->o_arg.seq_args, &data->o_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001684 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001685 data->rpc_done = 0;
1686 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001687 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001688 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001689 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001690 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001691 status = nfs4_wait_for_completion_rpc_task(task);
1692 if (status != 0) {
1693 data->cancelled = 1;
1694 smp_wmb();
1695 } else
1696 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001697 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 return status;
1699}
1700
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001701static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001703 struct nfs4_opendata *data = calldata;
1704 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001705 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001706
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001707 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001708 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001709 /*
1710 * Check if we still need to send an OPEN call, or if we can use
1711 * a delegation instead.
1712 */
1713 if (data->state != NULL) {
1714 struct nfs_delegation *delegation;
1715
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001716 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001717 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001718 rcu_read_lock();
1719 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001720 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
Trond Myklebustcd4c9be2013-04-23 14:46:25 -04001721 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
Trond Myklebust652f89f2011-12-09 19:05:58 -05001722 can_open_delegated(delegation, data->o_arg.fmode))
1723 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001724 rcu_read_unlock();
1725 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001726 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04001727 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust8188df12013-04-23 14:31:19 -04001728 switch (data->o_arg.claim) {
1729 case NFS4_OPEN_CLAIM_PREVIOUS:
1730 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1731 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001732 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04001733 case NFS4_OPEN_CLAIM_FH:
1734 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001735 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1736 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001737 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001738 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001739 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001740 &data->o_res.seq_res,
1741 task) != 0)
1742 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04001743
1744 /* Set the create mode (note dependency on the session type) */
1745 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1746 if (data->o_arg.open_flags & O_EXCL) {
1747 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1748 if (nfs4_has_persistent_session(clp))
1749 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1750 else if (clp->cl_mvops->minor_version > 0)
1751 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1752 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04001753 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001754unlock_no_action:
1755 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001756out_no_action:
1757 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001758out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001759 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001760}
1761
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001762static void nfs4_open_done(struct rpc_task *task, void *calldata)
1763{
1764 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001766 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001767
Trond Myklebust14516c32010-07-31 14:29:06 -04001768 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1769 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001770
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001771 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001772 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1773 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001774 case S_IFREG:
1775 break;
1776 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001777 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001778 break;
1779 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001780 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001781 break;
1782 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001783 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001784 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001785 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001786 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001787 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1788 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001789 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001790 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001791}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001792
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001793static void nfs4_open_release(void *calldata)
1794{
1795 struct nfs4_opendata *data = calldata;
1796 struct nfs4_state *state = NULL;
1797
1798 /* If this request hasn't been cancelled, do nothing */
1799 if (data->cancelled == 0)
1800 goto out_free;
1801 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001802 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001803 goto out_free;
1804 /* In case we need an open_confirm, no cleanup! */
1805 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1806 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001807 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001808 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001809 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001810out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001811 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001812}
1813
1814static const struct rpc_call_ops nfs4_open_ops = {
1815 .rpc_call_prepare = nfs4_open_prepare,
1816 .rpc_call_done = nfs4_open_done,
1817 .rpc_release = nfs4_open_release,
1818};
1819
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001820static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001821{
1822 struct inode *dir = data->dir->d_inode;
1823 struct nfs_server *server = NFS_SERVER(dir);
1824 struct nfs_openargs *o_arg = &data->o_arg;
1825 struct nfs_openres *o_res = &data->o_res;
1826 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001827 struct rpc_message msg = {
1828 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1829 .rpc_argp = o_arg,
1830 .rpc_resp = o_res,
1831 .rpc_cred = data->owner->so_cred,
1832 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001833 struct rpc_task_setup task_setup_data = {
1834 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001835 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001836 .callback_ops = &nfs4_open_ops,
1837 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001838 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001839 .flags = RPC_TASK_ASYNC,
1840 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001841 int status;
1842
Chuck Levera9c92d62013-08-09 12:48:18 -04001843 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001844 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001845 data->rpc_done = 0;
1846 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001847 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001848 data->is_recover = 0;
1849 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001850 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001851 data->is_recover = 1;
1852 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04001853 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001854 if (IS_ERR(task))
1855 return PTR_ERR(task);
1856 status = nfs4_wait_for_completion_rpc_task(task);
1857 if (status != 0) {
1858 data->cancelled = 1;
1859 smp_wmb();
1860 } else
1861 status = data->rpc_status;
1862 rpc_put_task(task);
1863
1864 return status;
1865}
1866
1867static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1868{
1869 struct inode *dir = data->dir->d_inode;
1870 struct nfs_openres *o_res = &data->o_res;
1871 int status;
1872
1873 status = nfs4_run_open_task(data, 1);
1874 if (status != 0 || !data->rpc_done)
1875 return status;
1876
Trond Myklebust6926afd2012-01-07 13:22:46 -05001877 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1878
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001879 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1880 status = _nfs4_proc_open_confirm(data);
1881 if (status != 0)
1882 return status;
1883 }
1884
1885 return status;
1886}
1887
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001888static int nfs4_opendata_access(struct rpc_cred *cred,
1889 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001890 struct nfs4_state *state, fmode_t fmode,
1891 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001892{
1893 struct nfs_access_entry cache;
1894 u32 mask;
1895
1896 /* access call failed or for some reason the server doesn't
1897 * support any access modes -- defer access call until later */
1898 if (opendata->o_res.access_supported == 0)
1899 return 0;
1900
1901 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001902 /* don't check MAY_WRITE - a newly created file may not have
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001903 * write mode bits, but POSIX allows the creating process to write.
1904 * use openflags to check for exec, because fmode won't
1905 * always have FMODE_EXEC set when file open for exec. */
1906 if (openflags & __FMODE_EXEC) {
1907 /* ONLY check for exec rights */
1908 mask = MAY_EXEC;
1909 } else if (fmode & FMODE_READ)
1910 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001911
1912 cache.cred = cred;
1913 cache.jiffies = jiffies;
1914 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1915 nfs_access_add_cache(state->inode, &cache);
1916
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001917 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001918 return 0;
1919
1920 /* even though OPEN succeeded, access is denied. Close the file */
1921 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001922 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001923}
1924
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001925/*
1926 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1927 */
1928static int _nfs4_proc_open(struct nfs4_opendata *data)
1929{
1930 struct inode *dir = data->dir->d_inode;
1931 struct nfs_server *server = NFS_SERVER(dir);
1932 struct nfs_openargs *o_arg = &data->o_arg;
1933 struct nfs_openres *o_res = &data->o_res;
1934 int status;
1935
1936 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001937 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001938 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001939 if (status != 0) {
1940 if (status == -NFS4ERR_BADNAME &&
1941 !(o_arg->open_flags & O_CREAT))
1942 return -ENOENT;
1943 return status;
1944 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001945
Trond Myklebust6926afd2012-01-07 13:22:46 -05001946 nfs_fattr_map_and_free_names(server, &data->f_attr);
1947
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001948 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001949 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001950 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1951 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001953 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001955 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 }
1957 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
David Quigley1775fd32013-05-22 12:50:42 -04001958 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001959 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960}
1961
Andy Adamsond83217c2011-03-01 01:34:17 +00001962static int nfs4_recover_expired_lease(struct nfs_server *server)
1963{
1964 return nfs4_client_recover_expired_lease(server->nfs_client);
1965}
1966
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967/*
1968 * OPEN_EXPIRED:
1969 * reclaim state on the server after a network partition.
1970 * Assumes caller holds the appropriate lock
1971 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001972static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001974 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001975 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001977 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001978 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001979 if (IS_ERR(opendata))
1980 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001981 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001982 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001983 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001984 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001985 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986}
1987
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001988static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001989{
Trond Myklebust539cd032007-06-05 11:46:42 -04001990 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001991 struct nfs4_exception exception = { };
1992 int err;
1993
1994 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001995 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001996 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001997 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1998 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001999 switch (err) {
2000 default:
2001 goto out;
2002 case -NFS4ERR_GRACE:
2003 case -NFS4ERR_DELAY:
2004 nfs4_handle_exception(server, err, &exception);
2005 err = 0;
2006 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002007 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002008out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002009 return err;
2010}
2011
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2013{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002015 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
Trond Myklebust864472e2006-01-03 09:55:15 +01002017 ctx = nfs4_state_find_open_context(state);
2018 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002019 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002020 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002021 put_nfs_open_context(ctx);
2022 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023}
2024
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002025#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002026static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002027{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002028 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002029 nfs4_stateid *stateid = &state->stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002030 struct nfs_delegation *delegation;
2031 struct rpc_cred *cred = NULL;
2032 int status = -NFS4ERR_BAD_STATEID;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002033
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002034 /* If a state reset has been done, test_stateid is unneeded */
2035 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2036 return;
2037
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002038 /* Get the delegation credential for use by test/free_stateid */
2039 rcu_read_lock();
2040 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2041 if (delegation != NULL &&
2042 nfs4_stateid_match(&delegation->stateid, stateid)) {
2043 cred = get_rpccred(delegation->cred);
2044 rcu_read_unlock();
2045 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002046 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002047 } else
2048 rcu_read_unlock();
2049
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002050 if (status != NFS_OK) {
2051 /* Free the stateid unless the server explicitly
2052 * informs us the stateid is unrecognized. */
2053 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002054 nfs41_free_stateid(server, stateid, cred);
Bryan Schumaker69388672012-09-26 15:25:52 -04002055 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002056
Bryan Schumaker69388672012-09-26 15:25:52 -04002057 write_seqlock(&state->seqlock);
2058 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2059 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002060 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2061 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002062
2063 if (cred != NULL)
2064 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002065}
2066
2067/**
2068 * nfs41_check_open_stateid - possibly free an open stateid
2069 *
2070 * @state: NFSv4 state for an inode
2071 *
2072 * Returns NFS_OK if recovery for this stateid is now finished.
2073 * Otherwise a negative NFS4ERR value is returned.
2074 */
2075static int nfs41_check_open_stateid(struct nfs4_state *state)
2076{
2077 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002078 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002079 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002080 int status;
2081
2082 /* If a state reset has been done, test_stateid is unneeded */
2083 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2084 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2085 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2086 return -NFS4ERR_BAD_STATEID;
2087
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002088 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002089 trace_nfs4_test_open_stateid(state, NULL, status);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002090 if (status != NFS_OK) {
2091 /* Free the stateid unless the server explicitly
2092 * informs us the stateid is unrecognized. */
2093 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002094 nfs41_free_stateid(server, stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002095
2096 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2097 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2098 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002099 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002100 }
2101 return status;
2102}
2103
2104static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2105{
Chuck Levereb64cf92012-07-11 16:30:05 -04002106 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002107
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002108 nfs41_clear_delegation_stateid(state);
2109 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002110 if (status != NFS_OK)
2111 status = nfs4_open_expired(sp, state);
2112 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002113}
2114#endif
2115
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002117 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2118 * fields corresponding to attributes that were used to store the verifier.
2119 * Make sure we clobber those fields in the later setattr call
2120 */
2121static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2122{
2123 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2124 !(sattr->ia_valid & ATTR_ATIME_SET))
2125 sattr->ia_valid |= ATTR_ATIME;
2126
2127 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2128 !(sattr->ia_valid & ATTR_MTIME_SET))
2129 sattr->ia_valid |= ATTR_MTIME;
2130}
2131
Trond Myklebustc21443c2013-02-07 14:26:21 -05002132static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2133 fmode_t fmode,
2134 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002135 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002136{
2137 struct nfs4_state_owner *sp = opendata->owner;
2138 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002139 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002140 struct nfs4_state *state;
2141 unsigned int seq;
2142 int ret;
2143
2144 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2145
2146 ret = _nfs4_proc_open(opendata);
2147 if (ret != 0)
2148 goto out;
2149
2150 state = nfs4_opendata_to_nfs4_state(opendata);
2151 ret = PTR_ERR(state);
2152 if (IS_ERR(state))
2153 goto out;
2154 if (server->caps & NFS_CAP_POSIX_LOCK)
2155 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2156
Trond Myklebust275bb302013-05-29 13:11:28 -04002157 dentry = opendata->dentry;
2158 if (dentry->d_inode == NULL) {
2159 /* FIXME: Is this d_drop() ever needed? */
2160 d_drop(dentry);
2161 dentry = d_add_unique(dentry, igrab(state->inode));
2162 if (dentry == NULL) {
2163 dentry = opendata->dentry;
2164 } else if (dentry != ctx->dentry) {
2165 dput(ctx->dentry);
2166 ctx->dentry = dget(dentry);
2167 }
2168 nfs_set_verifier(dentry,
2169 nfs_save_change_attribute(opendata->dir->d_inode));
2170 }
2171
Trond Myklebustc21443c2013-02-07 14:26:21 -05002172 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2173 if (ret != 0)
2174 goto out;
2175
Trond Myklebust3efb9722013-05-29 13:17:04 -04002176 ctx->state = state;
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002177 if (dentry->d_inode == state->inode) {
2178 nfs_inode_attach_open_context(ctx);
2179 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2180 nfs4_schedule_stateid_recovery(server, state);
2181 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002182out:
2183 return ret;
2184}
2185
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002186/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002187 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 */
Andy Adamson82be4172012-05-23 05:02:35 -04002189static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002190 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002191 int flags,
2192 struct iattr *sattr,
Trond Myklebust959d9212013-06-28 16:29:51 -04002193 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194{
2195 struct nfs4_state_owner *sp;
2196 struct nfs4_state *state = NULL;
2197 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002198 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002199 struct dentry *dentry = ctx->dentry;
2200 struct rpc_cred *cred = ctx->cred;
2201 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2202 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002203 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002204 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002205 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
2207 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002209 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2210 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2212 goto out_err;
2213 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002214 status = nfs4_recover_expired_lease(server);
2215 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002216 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04002217 if (dentry->d_inode != NULL)
2218 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002219 status = -ENOMEM;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002220 if (dentry->d_inode)
2221 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002222 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002223 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002224 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002225 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
David Quigley14c43f72013-05-22 12:50:43 -04002227 if (label) {
2228 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2229 if (IS_ERR(olabel)) {
2230 status = PTR_ERR(olabel);
2231 goto err_opendata_put;
2232 }
2233 }
2234
Andy Adamson82be4172012-05-23 05:02:35 -04002235 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2236 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2237 if (!opendata->f_attr.mdsthreshold)
David Quigley14c43f72013-05-22 12:50:43 -04002238 goto err_free_label;
Trond Myklebust1549210f2012-06-05 09:16:47 -04002239 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002240 }
Al Viro82a2c1b2011-06-22 18:30:55 -04002241 if (dentry->d_inode != NULL)
2242 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002243
Trond Myklebust3efb9722013-05-29 13:17:04 -04002244 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002245 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002246 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002247 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002248
Trond Myklebust549b19c2013-04-16 18:42:34 -04002249 if ((opendata->o_arg.open_flags & O_EXCL) &&
2250 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002251 nfs4_exclusive_attrset(opendata, sattr);
2252
2253 nfs_fattr_init(opendata->o_res.f_attr);
2254 status = nfs4_do_setattr(state->inode, cred,
2255 opendata->o_res.f_attr, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002256 state, label, olabel);
2257 if (status == 0) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002258 nfs_setattr_update_inode(state->inode, sattr);
David Quigley1775fd32013-05-22 12:50:42 -04002259 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
David Quigleyaa9c2662013-05-22 12:50:44 -04002260 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
David Quigley1775fd32013-05-22 12:50:42 -04002261 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002262 }
Andy Adamson82be4172012-05-23 05:02:35 -04002263
2264 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2265 *ctx_th = opendata->f_attr.mdsthreshold;
2266 else
2267 kfree(opendata->f_attr.mdsthreshold);
2268 opendata->f_attr.mdsthreshold = NULL;
2269
David Quigley14c43f72013-05-22 12:50:43 -04002270 nfs4_label_free(olabel);
2271
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002272 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002275err_free_label:
2276 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002277err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002278 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002279 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002280err_put_state_owner:
2281 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 return status;
2284}
2285
2286
Andy Adamson82be4172012-05-23 05:02:35 -04002287static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002288 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002289 int flags,
2290 struct iattr *sattr,
Trond Myklebust959d9212013-06-28 16:29:51 -04002291 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002293 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 struct nfs4_exception exception = { };
2295 struct nfs4_state *res;
2296 int status;
2297
2298 do {
Trond Myklebust959d9212013-06-28 16:29:51 -04002299 status = _nfs4_do_open(dir, ctx, flags, sattr, label);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002300 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002301 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 if (status == 0)
2303 break;
2304 /* NOTE: BAD_SEQID means the server and client disagree about the
2305 * book-keeping w.r.t. state-changing operations
2306 * (OPEN/CLOSE/LOCK/LOCKU...)
2307 * It is actually a sign of a bug on the client or on the server.
2308 *
2309 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002310 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 * have unhashed the old state_owner for us, and that we can
2312 * therefore safely retry using a new one. We should still warn
2313 * the user though...
2314 */
2315 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002316 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002317 " returned a bad sequence-id error!\n",
2318 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 exception.retry = 1;
2320 continue;
2321 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002322 /*
2323 * BAD_STATEID on OPEN means that the server cancelled our
2324 * state before it received the OPEN_CONFIRM.
2325 * Recover by retrying the request as per the discussion
2326 * on Page 181 of RFC3530.
2327 */
2328 if (status == -NFS4ERR_BAD_STATEID) {
2329 exception.retry = 1;
2330 continue;
2331 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002332 if (status == -EAGAIN) {
2333 /* We must have found a delegation */
2334 exception.retry = 1;
2335 continue;
2336 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002337 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2338 continue;
2339 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 status, &exception));
2341 } while (exception.retry);
2342 return res;
2343}
2344
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002345static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2346 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002347 struct nfs4_state *state, struct nfs4_label *ilabel,
2348 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002350 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002352 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 .iap = sattr,
2354 .server = server,
2355 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002356 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 };
2358 struct nfs_setattrres res = {
2359 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002360 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 .server = server,
2362 };
2363 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002364 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2365 .rpc_argp = &arg,
2366 .rpc_resp = &res,
2367 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002369 unsigned long timestamp = jiffies;
Trond Myklebustee3ae842013-04-29 10:35:36 -04002370 fmode_t fmode;
2371 bool truncate;
Trond Myklebust65e43082005-08-16 11:49:44 -04002372 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373
David Quigleyaa9c2662013-05-22 12:50:44 -04002374 arg.bitmask = nfs4_bitmask(server, ilabel);
2375 if (ilabel)
2376 arg.bitmask = nfs4_bitmask(server, olabel);
2377
Trond Myklebust0e574af2005-10-27 22:12:38 -04002378 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
Trond Myklebustee3ae842013-04-29 10:35:36 -04002380 /* Servers should only apply open mode checks for file size changes */
2381 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2382 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2383
2384 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2385 /* Use that stateid */
2386 } else if (truncate && state != NULL && nfs4_valid_open_stateid(state)) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002387 struct nfs_lockowner lockowner = {
2388 .l_owner = current->files,
2389 .l_pid = current->tgid,
2390 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002391 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002392 &lockowner);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002393 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002394 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
Bryan Schumaker7c513052011-03-24 17:12:24 +00002396 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002397 if (status == 0 && state != NULL)
2398 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002399 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400}
2401
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002402static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2403 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002404 struct nfs4_state *state, struct nfs4_label *ilabel,
2405 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002407 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002408 struct nfs4_exception exception = {
2409 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002410 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002411 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 int err;
2413 do {
David Quigley1775fd32013-05-22 12:50:42 -04002414 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
Trond Myklebustc1578b72013-08-12 16:58:42 -04002415 trace_nfs4_setattr(inode, err);
Trond Myklebust451146b2012-04-18 16:29:11 -04002416 switch (err) {
2417 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002418 if (!(sattr->ia_valid & ATTR_SIZE)) {
2419 pr_warn_once("NFSv4: server %s is incorrectly "
2420 "applying open mode checks to "
2421 "a SETATTR that is not "
2422 "changing file size.\n",
2423 server->nfs_client->cl_hostname);
2424 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002425 if (state && !(state->state & FMODE_WRITE)) {
2426 err = -EBADF;
2427 if (sattr->ia_valid & ATTR_OPEN)
2428 err = -EACCES;
2429 goto out;
2430 }
2431 }
2432 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002434out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 return err;
2436}
2437
2438struct nfs4_closedata {
2439 struct inode *inode;
2440 struct nfs4_state *state;
2441 struct nfs_closeargs arg;
2442 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002443 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002444 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002445 bool roc;
2446 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447};
2448
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002449static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002450{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002451 struct nfs4_closedata *calldata = data;
2452 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002453 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002454
Fred Isamanf7e89172011-01-06 11:36:32 +00002455 if (calldata->roc)
2456 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002457 nfs4_put_open_state(calldata->state);
2458 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002459 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002460 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002461 kfree(calldata);
2462}
2463
Trond Myklebust88069f72009-12-08 08:33:16 -05002464static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2465 fmode_t fmode)
2466{
2467 spin_lock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002468 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002469 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
2470 case FMODE_WRITE:
2471 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2472 break;
2473 case FMODE_READ:
2474 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2475 break;
2476 case 0:
2477 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2478 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2479 clear_bit(NFS_OPEN_STATE, &state->flags);
2480 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002481 spin_unlock(&state->owner->so_lock);
2482}
2483
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002484static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002486 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 struct nfs_server *server = NFS_SERVER(calldata->inode);
2489
Chuck Levera3ca5652012-03-01 17:00:40 -05002490 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002491 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2492 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002493 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 /* hmm. we are done with the inode, and in the process of freeing
2495 * the state_owner. we keep this around to process errors
2496 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 switch (task->tk_status) {
2498 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002499 if (calldata->roc)
2500 pnfs_roc_set_barrier(state->inode,
2501 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002502 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002503 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002504 nfs4_close_clear_stateid_flags(state,
2505 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 break;
2507 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002508 case -NFS4ERR_OLD_STATEID:
2509 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002511 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002512 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002514 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2515 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002517 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002518 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002519 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520}
2521
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002522static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002524 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002525 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002526 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002527 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002528
Chuck Levera3ca5652012-03-01 17:00:40 -05002529 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002530 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002531 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002532
Trond Myklebust88069f72009-12-08 08:33:16 -05002533 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2534 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002535 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002536 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002537 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002538 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002539 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2540 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2541 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002542 }
2543 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002544 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2545 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2546 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002547 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002548 }
Trond Myklebust5d422302013-03-14 16:57:48 -04002549 if (!nfs4_valid_open_stateid(state))
2550 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002551 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002552
2553 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002554 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002555 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002556 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002557
Fred Isamanf7e89172011-01-06 11:36:32 +00002558 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002559 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002560 if (calldata->roc &&
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002561 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2562 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002563 goto out_wait;
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002564 }
Fred Isamanf7e89172011-01-06 11:36:32 +00002565 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002566
Trond Myklebust516a6af2005-10-27 22:12:41 -04002567 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002568 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002569 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002570 &calldata->arg.seq_args,
2571 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002572 task) != 0)
2573 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002574 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002575 return;
2576out_no_action:
2577 task->tk_action = NULL;
2578out_wait:
2579 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580}
2581
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002582static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002583 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002584 .rpc_call_done = nfs4_close_done,
2585 .rpc_release = nfs4_free_closedata,
2586};
2587
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588/*
2589 * It is possible for data to be read/written from a mem-mapped file
2590 * after the sys_close call (which hits the vfs layer as a flush).
2591 * This means that we can't safely call nfsv4 close on a file until
2592 * the inode is cleared. This in turn means that we are not good
2593 * NFSv4 citizens - we do not indicate to the server to update the file's
2594 * share state even when we are done with one of the three share
2595 * stateid's in the inode.
2596 *
2597 * NOTE: Caller must be holding the sp->so_owner semaphore!
2598 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002599int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002601 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002603 struct nfs4_state_owner *sp = state->owner;
2604 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002605 struct rpc_message msg = {
2606 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2607 .rpc_cred = state->owner->so_cred,
2608 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002609 struct rpc_task_setup task_setup_data = {
2610 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002611 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002612 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002613 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002614 .flags = RPC_TASK_ASYNC,
2615 };
Trond Myklebust95121352005-10-18 14:20:12 -07002616 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002618 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002620 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04002621 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002622 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002624 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002625 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002627 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002628 if (calldata->arg.seqid == NULL)
2629 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002630 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002631 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002632 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002633 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002634 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002635 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002636 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002637
Trond Myklebust1174dd12010-12-21 10:52:24 -05002638 msg.rpc_argp = &calldata->arg;
2639 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002640 task_setup_data.callback_data = calldata;
2641 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002642 if (IS_ERR(task))
2643 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002644 status = 0;
2645 if (wait)
2646 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002647 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002648 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002649out_free_calldata:
2650 kfree(calldata);
2651out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002652 nfs4_put_open_state(state);
2653 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002654 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655}
2656
Trond Myklebust2b484292010-09-17 10:56:51 -04002657static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002658nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04002661 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2662
2663 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
Trond Myklebust565277f2007-10-15 18:17:53 -04002665 /* Protect against concurrent sillydeletes */
Trond Myklebust959d9212013-06-28 16:29:51 -04002666 state = nfs4_do_open(dir, ctx, open_flags, attr, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04002667
2668 nfs4_label_release_security(label);
2669
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002670 if (IS_ERR(state))
2671 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04002672 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673}
2674
Trond Myklebust1185a552009-12-03 15:54:02 -05002675static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002676{
2677 if (ctx->state == NULL)
2678 return;
2679 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002680 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002681 else
Al Viro643168c2011-06-22 18:20:23 -04002682 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002683}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684
2685static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2686{
Benny Halevy43652ad2009-04-01 09:21:54 -04002687 struct nfs4_server_caps_arg args = {
2688 .fhandle = fhandle,
2689 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 struct nfs4_server_caps_res res = {};
2691 struct rpc_message msg = {
2692 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002693 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 .rpc_resp = &res,
2695 };
2696 int status;
2697
Bryan Schumaker7c513052011-03-24 17:12:24 +00002698 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 if (status == 0) {
2700 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002701 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2702 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2703 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2704 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2705 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2707 server->caps |= NFS_CAP_ACLS;
2708 if (res.has_links != 0)
2709 server->caps |= NFS_CAP_HARDLINKS;
2710 if (res.has_symlinks != 0)
2711 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002712 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2713 server->caps |= NFS_CAP_FILEID;
2714 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2715 server->caps |= NFS_CAP_MODE;
2716 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2717 server->caps |= NFS_CAP_NLINK;
2718 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2719 server->caps |= NFS_CAP_OWNER;
2720 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2721 server->caps |= NFS_CAP_OWNER_GROUP;
2722 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2723 server->caps |= NFS_CAP_ATIME;
2724 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2725 server->caps |= NFS_CAP_CTIME;
2726 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2727 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04002728#ifdef CONFIG_NFS_V4_SECURITY_LABEL
2729 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
2730 server->caps |= NFS_CAP_SECURITY_LABEL;
2731#endif
2732 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
2733 sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002734
David Quigleyaa9c2662013-05-22 12:50:44 -04002735 if (server->caps & NFS_CAP_SECURITY_LABEL) {
2736 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2737 res.attr_bitmask[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2738 }
Trond Myklebusta65318b2009-03-11 14:10:28 -04002739 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2740 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2741 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002743 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002745
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 return status;
2747}
2748
Trond Myklebust55a97592006-06-09 09:34:19 -04002749int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750{
2751 struct nfs4_exception exception = { };
2752 int err;
2753 do {
2754 err = nfs4_handle_exception(server,
2755 _nfs4_server_capabilities(server, fhandle),
2756 &exception);
2757 } while (exception.retry);
2758 return err;
2759}
2760
2761static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2762 struct nfs_fsinfo *info)
2763{
David Quigleyaa9c2662013-05-22 12:50:44 -04002764 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04002766 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 };
2768 struct nfs4_lookup_res res = {
2769 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002770 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 .fh = fhandle,
2772 };
2773 struct rpc_message msg = {
2774 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2775 .rpc_argp = &args,
2776 .rpc_resp = &res,
2777 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002778
David Quigleyaa9c2662013-05-22 12:50:44 -04002779 bitmask[0] = nfs4_fattr_bitmap[0];
2780 bitmask[1] = nfs4_fattr_bitmap[1];
2781 /*
2782 * Process the label in the upcoming getfattr
2783 */
2784 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
2785
Trond Myklebust0e574af2005-10-27 22:12:38 -04002786 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002787 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788}
2789
2790static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2791 struct nfs_fsinfo *info)
2792{
2793 struct nfs4_exception exception = { };
2794 int err;
2795 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002796 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04002797 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002798 switch (err) {
2799 case 0:
2800 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002801 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002802 default:
2803 err = nfs4_handle_exception(server, err, &exception);
2804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002806out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 return err;
2808}
2809
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002810static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2811 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2812{
Trond Myklebustc2190662013-08-26 19:23:04 -04002813 struct rpc_auth_create_args auth_args = {
2814 .pseudoflavor = flavor,
2815 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002816 struct rpc_auth *auth;
2817 int ret;
2818
Trond Myklebustc2190662013-08-26 19:23:04 -04002819 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002820 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04002821 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002822 goto out;
2823 }
2824 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002825out:
2826 return ret;
2827}
2828
Chuck Lever9a744ba2013-03-16 15:56:02 -04002829/*
2830 * Retry pseudoroot lookup with various security flavors. We do this when:
2831 *
2832 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2833 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2834 *
2835 * Returns zero on success, or a negative NFS4ERR value, or a
2836 * negative errno value.
2837 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002838static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002839 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840{
Chuck Lever9a744ba2013-03-16 15:56:02 -04002841 /* Per 3530bis 15.33.5 */
2842 static const rpc_authflavor_t flav_array[] = {
2843 RPC_AUTH_GSS_KRB5P,
2844 RPC_AUTH_GSS_KRB5I,
2845 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04002846 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04002847 RPC_AUTH_NULL,
2848 };
2849 int status = -EPERM;
2850 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851
Chuck Lever9a744ba2013-03-16 15:56:02 -04002852 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002853 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002854 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002855 continue;
2856 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002857 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04002858
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002859 /*
2860 * -EACCESS could mean that the user doesn't have correct permissions
2861 * to access the mount. It could also mean that we tried to mount
2862 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2863 * existing mount programs don't handle -EACCES very well so it should
2864 * be mapped to -EPERM instead.
2865 */
2866 if (status == -EACCES)
2867 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002868 return status;
2869}
2870
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002871static int nfs4_do_find_root_sec(struct nfs_server *server,
2872 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
2873{
2874 int mv = server->nfs_client->cl_minorversion;
2875 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
2876}
2877
2878/**
2879 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
2880 * @server: initialized nfs_server handle
2881 * @fhandle: we fill in the pseudo-fs root file handle
2882 * @info: we fill in an FSINFO struct
2883 *
2884 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002885 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002886int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2887 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002888{
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002889 int status;
2890
2891 status = nfs4_lookup_root(server, fhandle, info);
2892 if ((status == -NFS4ERR_WRONGSEC) &&
2893 !(server->flags & NFS_MOUNT_SECFLAVOUR))
2894 status = nfs4_do_find_root_sec(server, fhandle, info);
2895
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 if (status == 0)
2897 status = nfs4_server_capabilities(server, fhandle);
2898 if (status == 0)
2899 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002900
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002901 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902}
2903
Bryan Schumakerbae36242012-05-10 15:07:31 -04002904static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2905 struct nfs_fsinfo *info)
2906{
2907 int error;
2908 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04002909 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04002910
2911 error = nfs4_server_capabilities(server, mntfh);
2912 if (error < 0) {
2913 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2914 return error;
2915 }
2916
David Quigley14c43f72013-05-22 12:50:43 -04002917 label = nfs4_label_alloc(server, GFP_KERNEL);
2918 if (IS_ERR(label))
2919 return PTR_ERR(label);
2920
David Quigley1775fd32013-05-22 12:50:42 -04002921 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04002922 if (error < 0) {
2923 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04002924 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04002925 }
2926
2927 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2928 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2929 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2930
David Quigley14c43f72013-05-22 12:50:43 -04002931err_free_label:
2932 nfs4_label_free(label);
2933
Bryan Schumakerbae36242012-05-10 15:07:31 -04002934 return error;
2935}
2936
Manoj Naik6b97fd32006-06-09 09:34:29 -04002937/*
2938 * Get locations and (maybe) other attributes of a referral.
2939 * Note that we'll actually follow the referral later when
2940 * we detect fsid mismatch in inode revalidation
2941 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002942static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2943 const struct qstr *name, struct nfs_fattr *fattr,
2944 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002945{
2946 int status = -ENOMEM;
2947 struct page *page = NULL;
2948 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002949
2950 page = alloc_page(GFP_KERNEL);
2951 if (page == NULL)
2952 goto out;
2953 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2954 if (locations == NULL)
2955 goto out;
2956
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002957 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002958 if (status != 0)
2959 goto out;
2960 /* Make sure server returned a different fsid for the referral */
2961 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002962 dprintk("%s: server did not return a different fsid for"
2963 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002964 status = -EIO;
2965 goto out;
2966 }
Andy Adamson533eb462011-06-13 18:25:56 -04002967 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2968 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002969
Andy Adamson533eb462011-06-13 18:25:56 -04002970 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002971 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002972 memset(fhandle, 0, sizeof(struct nfs_fh));
2973out:
2974 if (page)
2975 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002976 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002977 return status;
2978}
2979
David Quigley1775fd32013-05-22 12:50:42 -04002980static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
2981 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982{
2983 struct nfs4_getattr_arg args = {
2984 .fh = fhandle,
2985 .bitmask = server->attr_bitmask,
2986 };
2987 struct nfs4_getattr_res res = {
2988 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002989 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 .server = server,
2991 };
2992 struct rpc_message msg = {
2993 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2994 .rpc_argp = &args,
2995 .rpc_resp = &res,
2996 };
David Quigleyaa9c2662013-05-22 12:50:44 -04002997
2998 args.bitmask = nfs4_bitmask(server, label);
2999
Trond Myklebust0e574af2005-10-27 22:12:38 -04003000 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003001 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002}
3003
David Quigley1775fd32013-05-22 12:50:42 -04003004static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3005 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006{
3007 struct nfs4_exception exception = { };
3008 int err;
3009 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003010 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3011 trace_nfs4_getattr(server, fhandle, fattr, err);
3012 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 &exception);
3014 } while (exception.retry);
3015 return err;
3016}
3017
3018/*
3019 * The file is not closed if it is opened due to the a request to change
3020 * the size of the file. The open call will not be needed once the
3021 * VFS layer lookup-intents are implemented.
3022 *
3023 * Close is called when the inode is destroyed.
3024 * If we haven't opened the file for O_WRONLY, we
3025 * need to in the size_change case to obtain a stateid.
3026 *
3027 * Got race?
3028 * Because OPEN is always done by name in nfsv4, it is
3029 * possible that we opened a different file by the same
3030 * name. We can recognize this race condition, but we
3031 * can't do anything about it besides returning an error.
3032 *
3033 * This will be fixed with VFS changes (lookup-intent).
3034 */
3035static int
3036nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3037 struct iattr *sattr)
3038{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003039 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003040 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003041 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003042 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 int status;
3044
Benny Halevy8a1636c2010-07-14 15:43:57 -04003045 if (pnfs_ld_layoutret_on_setattr(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003046 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003047
Trond Myklebust0e574af2005-10-27 22:12:38 -04003048 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049
Andy Adamson26699402012-05-30 16:12:24 -04003050 /* Deal with open(O_TRUNC) */
3051 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003052 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003053
3054 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003055 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003056 return 0;
3057
Trond Myklebustd5308382005-11-04 15:33:38 -05003058 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003059 if (sattr->ia_valid & ATTR_FILE) {
3060 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003061
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003062 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003063 if (ctx) {
3064 cred = ctx->cred;
3065 state = ctx->state;
3066 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003067 }
3068
David Quigley14c43f72013-05-22 12:50:43 -04003069 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3070 if (IS_ERR(label))
3071 return PTR_ERR(label);
3072
3073 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003074 if (status == 0) {
Trond Myklebust65e43082005-08-16 11:49:44 -04003075 nfs_setattr_update_inode(inode, sattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003076 nfs_setsecurity(inode, fattr, label);
3077 }
David Quigley14c43f72013-05-22 12:50:43 -04003078 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 return status;
3080}
3081
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003082static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3083 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003084 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003085{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003086 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003087 int status;
3088 struct nfs4_lookup_arg args = {
3089 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003090 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003091 .name = name,
3092 };
3093 struct nfs4_lookup_res res = {
3094 .server = server,
3095 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003096 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003097 .fh = fhandle,
3098 };
3099 struct rpc_message msg = {
3100 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3101 .rpc_argp = &args,
3102 .rpc_resp = &res,
3103 };
3104
David Quigleyaa9c2662013-05-22 12:50:44 -04003105 args.bitmask = nfs4_bitmask(server, label);
3106
David Howells2b3de442006-08-22 20:06:09 -04003107 nfs_fattr_init(fattr);
3108
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003110 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 dprintk("NFS reply lookup: %d\n", status);
3112 return status;
3113}
3114
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003115static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003116{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003117 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003118 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003119 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3120 fattr->nlink = 2;
3121}
3122
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003123static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3124 struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003125 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003126{
3127 struct nfs4_exception exception = { };
3128 struct rpc_clnt *client = *clnt;
3129 int err;
3130 do {
David Quigley1775fd32013-05-22 12:50:42 -04003131 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003132 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003133 switch (err) {
3134 case -NFS4ERR_BADNAME:
3135 err = -ENOENT;
3136 goto out;
3137 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003138 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003139 goto out;
3140 case -NFS4ERR_WRONGSEC:
3141 err = -EPERM;
3142 if (client != *clnt)
3143 goto out;
3144
3145 client = nfs4_create_sec_client(client, dir, name);
3146 if (IS_ERR(client))
3147 return PTR_ERR(client);
3148
3149 exception.retry = 1;
3150 break;
3151 default:
3152 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3153 }
3154 } while (exception.retry);
3155
3156out:
3157 if (err == 0)
3158 *clnt = client;
3159 else if (client != *clnt)
3160 rpc_shutdown_client(client);
3161
3162 return err;
3163}
3164
Bryan Schumaker80a16b22012-04-27 13:27:46 -04003165static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003166 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3167 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003169 int status;
3170 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003171
David Quigley1775fd32013-05-22 12:50:42 -04003172 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003173 if (client != NFS_CLIENT(dir)) {
3174 rpc_shutdown_client(client);
3175 nfs_fixup_secinfo_attributes(fattr);
3176 }
3177 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178}
3179
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003180struct rpc_clnt *
3181nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3182 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3183{
3184 int status;
3185 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
3186
David Quigley1775fd32013-05-22 12:50:42 -04003187 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003188 if (status < 0) {
3189 rpc_shutdown_client(client);
3190 return ERR_PTR(status);
3191 }
3192 return client;
3193}
3194
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3196{
Trond Myklebust76b32992007-08-10 17:45:11 -04003197 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 struct nfs4_accessargs args = {
3199 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003200 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003202 struct nfs4_accessres res = {
3203 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003204 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 struct rpc_message msg = {
3206 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3207 .rpc_argp = &args,
3208 .rpc_resp = &res,
3209 .rpc_cred = entry->cred,
3210 };
3211 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003212 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213
3214 /*
3215 * Determine which access bits we want to ask for...
3216 */
3217 if (mode & MAY_READ)
3218 args.access |= NFS4_ACCESS_READ;
3219 if (S_ISDIR(inode->i_mode)) {
3220 if (mode & MAY_WRITE)
3221 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3222 if (mode & MAY_EXEC)
3223 args.access |= NFS4_ACCESS_LOOKUP;
3224 } else {
3225 if (mode & MAY_WRITE)
3226 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3227 if (mode & MAY_EXEC)
3228 args.access |= NFS4_ACCESS_EXECUTE;
3229 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003230
3231 res.fattr = nfs_alloc_fattr();
3232 if (res.fattr == NULL)
3233 return -ENOMEM;
3234
Bryan Schumaker7c513052011-03-24 17:12:24 +00003235 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003237 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003238 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003240 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 return status;
3242}
3243
3244static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3245{
3246 struct nfs4_exception exception = { };
3247 int err;
3248 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003249 err = _nfs4_proc_access(inode, entry);
3250 trace_nfs4_access(inode, err);
3251 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 &exception);
3253 } while (exception.retry);
3254 return err;
3255}
3256
3257/*
3258 * TODO: For the time being, we don't try to get any attributes
3259 * along with any of the zero-copy operations READ, READDIR,
3260 * READLINK, WRITE.
3261 *
3262 * In the case of the first three, we want to put the GETATTR
3263 * after the read-type operation -- this is because it is hard
3264 * to predict the length of a GETATTR response in v4, and thus
3265 * align the READ data correctly. This means that the GETATTR
3266 * may end up partially falling into the page cache, and we should
3267 * shift it into the 'tail' of the xdr_buf before processing.
3268 * To do this efficiently, we need to know the total length
3269 * of data received, which doesn't seem to be available outside
3270 * of the RPC layer.
3271 *
3272 * In the case of WRITE, we also want to put the GETATTR after
3273 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003274 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 *
3276 * Both of these changes to the XDR layer would in fact be quite
3277 * minor, but I decided to leave them for a subsequent patch.
3278 */
3279static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3280 unsigned int pgbase, unsigned int pglen)
3281{
3282 struct nfs4_readlink args = {
3283 .fh = NFS_FH(inode),
3284 .pgbase = pgbase,
3285 .pglen = pglen,
3286 .pages = &page,
3287 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003288 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 struct rpc_message msg = {
3290 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3291 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003292 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 };
3294
Bryan Schumaker7c513052011-03-24 17:12:24 +00003295 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 -07003296}
3297
3298static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3299 unsigned int pgbase, unsigned int pglen)
3300{
3301 struct nfs4_exception exception = { };
3302 int err;
3303 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003304 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3305 trace_nfs4_readlink(inode, err);
3306 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 &exception);
3308 } while (exception.retry);
3309 return err;
3310}
3311
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003313 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315static int
3316nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003317 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318{
David Quigleyaa9c2662013-05-22 12:50:44 -04003319 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003320 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 int status = 0;
3323
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003324 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3325 if (IS_ERR(ctx))
3326 return PTR_ERR(ctx);
3327
David Quigleyaa9c2662013-05-22 12:50:44 -04003328 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3329
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003330 sattr->ia_mode &= ~current_umask();
Trond Myklebust959d9212013-06-28 16:29:51 -04003331 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 if (IS_ERR(state)) {
3333 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003334 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003337 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003338 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 return status;
3340}
3341
3342static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3343{
Trond Myklebust16e42952005-10-27 22:12:44 -04003344 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003345 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003347 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003349 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003350 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003351 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003353 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3354 .rpc_argp = &args,
3355 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003357 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003358
Bryan Schumaker7c513052011-03-24 17:12:24 +00003359 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003360 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003361 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 return status;
3363}
3364
3365static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3366{
3367 struct nfs4_exception exception = { };
3368 int err;
3369 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003370 err = _nfs4_proc_remove(dir, name);
3371 trace_nfs4_remove(dir, name, err);
3372 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 &exception);
3374 } while (exception.retry);
3375 return err;
3376}
3377
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003378static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003380 struct nfs_server *server = NFS_SERVER(dir);
3381 struct nfs_removeargs *args = msg->rpc_argp;
3382 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003384 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04003386 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003387
3388 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389}
3390
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003391static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3392{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003393 nfs4_setup_sequence(NFS_SERVER(data->dir),
3394 &data->args.seq_args,
3395 &data->res.seq_res,
3396 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397}
3398
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003399static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003401 struct nfs_unlinkdata *data = task->tk_calldata;
3402 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003403
Trond Myklebust14516c32010-07-31 14:29:06 -04003404 if (!nfs4_sequence_done(task, &res->seq_res))
3405 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003406 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003407 return 0;
3408 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003409 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410}
3411
Jeff Laytond3d41522010-09-17 17:31:57 -04003412static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3413{
3414 struct nfs_server *server = NFS_SERVER(dir);
3415 struct nfs_renameargs *arg = msg->rpc_argp;
3416 struct nfs_renameres *res = msg->rpc_resp;
3417
3418 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003419 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04003420 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003421}
3422
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003423static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3424{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003425 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3426 &data->args.seq_args,
3427 &data->res.seq_res,
3428 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003429}
3430
3431static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3432 struct inode *new_dir)
3433{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003434 struct nfs_renamedata *data = task->tk_calldata;
3435 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04003436
3437 if (!nfs4_sequence_done(task, &res->seq_res))
3438 return 0;
3439 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3440 return 0;
3441
3442 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003443 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003444 return 1;
3445}
3446
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3448 struct inode *new_dir, struct qstr *new_name)
3449{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003450 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003451 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 .old_dir = NFS_FH(old_dir),
3453 .new_dir = NFS_FH(new_dir),
3454 .old_name = old_name,
3455 .new_name = new_name,
3456 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003457 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003458 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003459 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 struct rpc_message msg = {
3461 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3462 .rpc_argp = &arg,
3463 .rpc_resp = &res,
3464 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003465 int status = -ENOMEM;
David Quigley14c43f72013-05-22 12:50:43 -04003466
Bryan Schumaker7c513052011-03-24 17:12:24 +00003467 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 if (!status) {
3469 update_changeattr(old_dir, &res.old_cinfo);
3470 update_changeattr(new_dir, &res.new_cinfo);
3471 }
3472 return status;
3473}
3474
3475static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3476 struct inode *new_dir, struct qstr *new_name)
3477{
3478 struct nfs4_exception exception = { };
3479 int err;
3480 do {
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003481 err = _nfs4_proc_rename(old_dir, old_name,
3482 new_dir, new_name);
3483 trace_nfs4_rename(old_dir, old_name, new_dir, new_name, err);
3484 err = nfs4_handle_exception(NFS_SERVER(old_dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 &exception);
3486 } while (exception.retry);
3487 return err;
3488}
3489
3490static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3491{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003492 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 struct nfs4_link_arg arg = {
3494 .fh = NFS_FH(inode),
3495 .dir_fh = NFS_FH(dir),
3496 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003497 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003499 struct nfs4_link_res res = {
3500 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003501 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003502 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 struct rpc_message msg = {
3504 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3505 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003506 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003508 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509
Trond Myklebust136f2622010-04-16 16:22:49 -04003510 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003511 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003512 goto out;
3513
David Quigley14c43f72013-05-22 12:50:43 -04003514 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3515 if (IS_ERR(res.label)) {
3516 status = PTR_ERR(res.label);
3517 goto out;
3518 }
David Quigleyaa9c2662013-05-22 12:50:44 -04003519 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04003520
Bryan Schumaker7c513052011-03-24 17:12:24 +00003521 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003522 if (!status) {
3523 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04003524 status = nfs_post_op_update_inode(inode, res.fattr);
3525 if (!status)
3526 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003527 }
David Quigley14c43f72013-05-22 12:50:43 -04003528
3529
3530 nfs4_label_free(res.label);
3531
Trond Myklebust136f2622010-04-16 16:22:49 -04003532out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003533 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 return status;
3535}
3536
3537static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3538{
3539 struct nfs4_exception exception = { };
3540 int err;
3541 do {
3542 err = nfs4_handle_exception(NFS_SERVER(inode),
3543 _nfs4_proc_link(inode, dir, name),
3544 &exception);
3545 } while (exception.retry);
3546 return err;
3547}
3548
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003549struct nfs4_createdata {
3550 struct rpc_message msg;
3551 struct nfs4_create_arg arg;
3552 struct nfs4_create_res res;
3553 struct nfs_fh fh;
3554 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003555 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003556};
3557
3558static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3559 struct qstr *name, struct iattr *sattr, u32 ftype)
3560{
3561 struct nfs4_createdata *data;
3562
3563 data = kzalloc(sizeof(*data), GFP_KERNEL);
3564 if (data != NULL) {
3565 struct nfs_server *server = NFS_SERVER(dir);
3566
David Quigley14c43f72013-05-22 12:50:43 -04003567 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3568 if (IS_ERR(data->label))
3569 goto out_free;
3570
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003571 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3572 data->msg.rpc_argp = &data->arg;
3573 data->msg.rpc_resp = &data->res;
3574 data->arg.dir_fh = NFS_FH(dir);
3575 data->arg.server = server;
3576 data->arg.name = name;
3577 data->arg.attrs = sattr;
3578 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04003579 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003580 data->res.server = server;
3581 data->res.fh = &data->fh;
3582 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003583 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003584 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003585 }
3586 return data;
David Quigley14c43f72013-05-22 12:50:43 -04003587out_free:
3588 kfree(data);
3589 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003590}
3591
3592static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3593{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003594 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003595 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003596 if (status == 0) {
3597 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04003598 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003599 }
3600 return status;
3601}
3602
3603static void nfs4_free_createdata(struct nfs4_createdata *data)
3604{
David Quigley14c43f72013-05-22 12:50:43 -04003605 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003606 kfree(data);
3607}
3608
Chuck Lever4f390c12006-08-22 20:06:22 -04003609static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003610 struct page *page, unsigned int len, struct iattr *sattr,
3611 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003613 struct nfs4_createdata *data;
3614 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615
Chuck Lever94a6d752006-08-22 20:06:23 -04003616 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003617 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003618
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003619 status = -ENOMEM;
3620 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3621 if (data == NULL)
3622 goto out;
3623
3624 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3625 data->arg.u.symlink.pages = &page;
3626 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04003627 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003629 status = nfs4_do_create(dir, dentry, data);
3630
3631 nfs4_free_createdata(data);
3632out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 return status;
3634}
3635
Chuck Lever4f390c12006-08-22 20:06:22 -04003636static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003637 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638{
3639 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003640 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04003642
3643 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3644
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003646 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3647 trace_nfs4_symlink(dir, &dentry->d_name, err);
3648 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 &exception);
3650 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003651
3652 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 return err;
3654}
3655
3656static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003657 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003659 struct nfs4_createdata *data;
3660 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003662 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3663 if (data == NULL)
3664 goto out;
3665
David Quigley1775fd32013-05-22 12:50:42 -04003666 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003667 status = nfs4_do_create(dir, dentry, data);
3668
3669 nfs4_free_createdata(data);
3670out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 return status;
3672}
3673
3674static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3675 struct iattr *sattr)
3676{
3677 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003678 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003680
David Quigleyaa9c2662013-05-22 12:50:44 -04003681 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3682
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003683 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003685 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3686 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3687 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 &exception);
3689 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003690 nfs4_label_release_security(label);
3691
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 return err;
3693}
3694
3695static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003696 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697{
3698 struct inode *dir = dentry->d_inode;
3699 struct nfs4_readdir_arg args = {
3700 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003701 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 .pgbase = 0,
3703 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003704 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003705 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 };
3707 struct nfs4_readdir_res res;
3708 struct rpc_message msg = {
3709 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3710 .rpc_argp = &args,
3711 .rpc_resp = &res,
3712 .rpc_cred = cred,
3713 };
3714 int status;
3715
Harvey Harrison3110ff82008-05-02 13:42:44 -07003716 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003717 dentry->d_parent->d_name.name,
3718 dentry->d_name.name,
3719 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003720 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003722 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 -05003723 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003724 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003725 status += args.pgbase;
3726 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003727
3728 nfs_invalidate_atime(dir);
3729
Harvey Harrison3110ff82008-05-02 13:42:44 -07003730 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 return status;
3732}
3733
3734static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003735 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736{
3737 struct nfs4_exception exception = { };
3738 int err;
3739 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003740 err = _nfs4_proc_readdir(dentry, cred, cookie,
3741 pages, count, plus);
3742 trace_nfs4_readdir(dentry->d_inode, err);
3743 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 &exception);
3745 } while (exception.retry);
3746 return err;
3747}
3748
3749static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04003750 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003752 struct nfs4_createdata *data;
3753 int mode = sattr->ia_mode;
3754 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003756 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3757 if (data == NULL)
3758 goto out;
3759
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003761 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003763 data->arg.ftype = NF4BLK;
3764 data->arg.u.device.specdata1 = MAJOR(rdev);
3765 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 }
3767 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003768 data->arg.ftype = NF4CHR;
3769 data->arg.u.device.specdata1 = MAJOR(rdev);
3770 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003771 } else if (!S_ISSOCK(mode)) {
3772 status = -EINVAL;
3773 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 }
David Quigley1775fd32013-05-22 12:50:42 -04003775
David Quigleyaa9c2662013-05-22 12:50:44 -04003776 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003777 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003778out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003779 nfs4_free_createdata(data);
3780out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 return status;
3782}
3783
3784static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3785 struct iattr *sattr, dev_t rdev)
3786{
3787 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003788 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003790
David Quigleyaa9c2662013-05-22 12:50:44 -04003791 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3792
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003793 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003795 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
3796 trace_nfs4_mknod(dir, &dentry->d_name, err);
3797 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 &exception);
3799 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003800
3801 nfs4_label_release_security(label);
3802
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 return err;
3804}
3805
3806static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3807 struct nfs_fsstat *fsstat)
3808{
3809 struct nfs4_statfs_arg args = {
3810 .fh = fhandle,
3811 .bitmask = server->attr_bitmask,
3812 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003813 struct nfs4_statfs_res res = {
3814 .fsstat = fsstat,
3815 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 struct rpc_message msg = {
3817 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3818 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003819 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 };
3821
Trond Myklebust0e574af2005-10-27 22:12:38 -04003822 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003823 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824}
3825
3826static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3827{
3828 struct nfs4_exception exception = { };
3829 int err;
3830 do {
3831 err = nfs4_handle_exception(server,
3832 _nfs4_proc_statfs(server, fhandle, fsstat),
3833 &exception);
3834 } while (exception.retry);
3835 return err;
3836}
3837
3838static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3839 struct nfs_fsinfo *fsinfo)
3840{
3841 struct nfs4_fsinfo_arg args = {
3842 .fh = fhandle,
3843 .bitmask = server->attr_bitmask,
3844 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003845 struct nfs4_fsinfo_res res = {
3846 .fsinfo = fsinfo,
3847 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 struct rpc_message msg = {
3849 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3850 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003851 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 };
3853
Bryan Schumaker7c513052011-03-24 17:12:24 +00003854 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855}
3856
3857static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3858{
3859 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04003860 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 int err;
3862
3863 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04003864 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003865 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04003866 if (err == 0) {
3867 struct nfs_client *clp = server->nfs_client;
3868
3869 spin_lock(&clp->cl_lock);
3870 clp->cl_lease_time = fsinfo->lease_time * HZ;
3871 clp->cl_last_renewal = now;
3872 spin_unlock(&clp->cl_lock);
3873 break;
3874 }
3875 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 } while (exception.retry);
3877 return err;
3878}
3879
3880static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3881{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003882 int error;
3883
Trond Myklebust0e574af2005-10-27 22:12:38 -04003884 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003885 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003886 if (error == 0) {
3887 /* block layout checks this! */
3888 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003889 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003890 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003891
3892 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893}
3894
3895static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3896 struct nfs_pathconf *pathconf)
3897{
3898 struct nfs4_pathconf_arg args = {
3899 .fh = fhandle,
3900 .bitmask = server->attr_bitmask,
3901 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003902 struct nfs4_pathconf_res res = {
3903 .pathconf = pathconf,
3904 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 struct rpc_message msg = {
3906 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3907 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003908 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 };
3910
3911 /* None of the pathconf attributes are mandatory to implement */
3912 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3913 memset(pathconf, 0, sizeof(*pathconf));
3914 return 0;
3915 }
3916
Trond Myklebust0e574af2005-10-27 22:12:38 -04003917 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003918 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919}
3920
3921static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3922 struct nfs_pathconf *pathconf)
3923{
3924 struct nfs4_exception exception = { };
3925 int err;
3926
3927 do {
3928 err = nfs4_handle_exception(server,
3929 _nfs4_proc_pathconf(server, fhandle, pathconf),
3930 &exception);
3931 } while (exception.retry);
3932 return err;
3933}
3934
Trond Myklebust5521abf2013-03-16 20:54:34 -04003935int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04003936 const struct nfs_open_context *ctx,
3937 const struct nfs_lock_context *l_ctx,
3938 fmode_t fmode)
3939{
3940 const struct nfs_lockowner *lockowner = NULL;
3941
3942 if (l_ctx != NULL)
3943 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003944 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04003945}
3946EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
3947
Trond Myklebust5521abf2013-03-16 20:54:34 -04003948static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
3949 const struct nfs_open_context *ctx,
3950 const struct nfs_lock_context *l_ctx,
3951 fmode_t fmode)
3952{
3953 nfs4_stateid current_stateid;
3954
3955 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode))
3956 return false;
3957 return nfs4_stateid_match(stateid, &current_stateid);
3958}
3959
3960static bool nfs4_error_stateid_expired(int err)
3961{
3962 switch (err) {
3963 case -NFS4ERR_DELEG_REVOKED:
3964 case -NFS4ERR_ADMIN_REVOKED:
3965 case -NFS4ERR_BAD_STATEID:
3966 case -NFS4ERR_STALE_STATEID:
3967 case -NFS4ERR_OLD_STATEID:
3968 case -NFS4ERR_OPENMODE:
3969 case -NFS4ERR_EXPIRED:
3970 return true;
3971 }
3972 return false;
3973}
3974
Benny Halevyd20581a2011-05-22 19:52:03 +03003975void __nfs4_read_done_cb(struct nfs_read_data *data)
3976{
Fred Isamancd841602012-04-20 14:47:44 -04003977 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003978}
3979
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003980static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981{
Fred Isamancd841602012-04-20 14:47:44 -04003982 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983
Trond Myklebustcc668ab2013-08-14 15:31:28 -04003984 trace_nfs4_read(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003985 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003986 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003987 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003989
Benny Halevyd20581a2011-05-22 19:52:03 +03003990 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003992 renew_lease(server, data->timestamp);
3993 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994}
3995
Trond Myklebust5521abf2013-03-16 20:54:34 -04003996static bool nfs4_read_stateid_changed(struct rpc_task *task,
3997 struct nfs_readargs *args)
3998{
3999
4000 if (!nfs4_error_stateid_expired(task->tk_status) ||
4001 nfs4_stateid_is_current(&args->stateid,
4002 args->context,
4003 args->lock_context,
4004 FMODE_READ))
4005 return false;
4006 rpc_restart_call_prepare(task);
4007 return true;
4008}
4009
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004010static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
4011{
4012
4013 dprintk("--> %s\n", __func__);
4014
4015 if (!nfs4_sequence_done(task, &data->res.seq_res))
4016 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004017 if (nfs4_read_stateid_changed(task, &data->args))
4018 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03004019 return data->read_done_cb ? data->read_done_cb(task, data) :
4020 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004021}
4022
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004023static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004026 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004027 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Chuck Levera9c92d62013-08-09 12:48:18 -04004028 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029}
4030
NeilBrownef1820f2013-09-04 17:04:49 +10004031static int nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004032{
Trond Myklebust9b206142013-03-17 15:52:00 -04004033 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004034 &data->args.seq_args,
4035 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004036 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004037 return 0;
4038 if (nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4039 data->args.lock_context, FMODE_READ) == -EIO)
4040 return -EIO;
4041 if (unlikely(test_bit(NFS_CONTEXT_BAD, &data->args.context->flags)))
4042 return -EIO;
4043 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044}
4045
Fred Isamanb029bc92011-03-03 15:13:42 +00004046static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047{
Fred Isamancd841602012-04-20 14:47:44 -04004048 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004050 trace_nfs4_write(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004051 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004052 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004053 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004055 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04004057 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004058 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004059 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060}
4061
Trond Myklebust5521abf2013-03-16 20:54:34 -04004062static bool nfs4_write_stateid_changed(struct rpc_task *task,
4063 struct nfs_writeargs *args)
4064{
4065
4066 if (!nfs4_error_stateid_expired(task->tk_status) ||
4067 nfs4_stateid_is_current(&args->stateid,
4068 args->context,
4069 args->lock_context,
4070 FMODE_WRITE))
4071 return false;
4072 rpc_restart_call_prepare(task);
4073 return true;
4074}
4075
Fred Isamanb029bc92011-03-03 15:13:42 +00004076static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
4077{
4078 if (!nfs4_sequence_done(task, &data->res.seq_res))
4079 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004080 if (nfs4_write_stateid_changed(task, &data->args))
4081 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03004082 return data->write_done_cb ? data->write_done_cb(task, data) :
4083 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00004084}
4085
Trond Myklebust5a37f852012-04-28 14:55:16 -04004086static
4087bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00004088{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004089 const struct nfs_pgio_header *hdr = data->header;
4090
4091 /* Don't request attributes for pNFS or O_DIRECT writes */
4092 if (data->ds_clp != NULL || hdr->dreq != NULL)
4093 return false;
4094 /* Otherwise, request attributes if and only if we don't hold
4095 * a delegation
4096 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004097 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004098}
Fred Isamana69aef12011-03-03 15:13:47 +00004099
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004100static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101{
Fred Isamancd841602012-04-20 14:47:44 -04004102 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004103
Trond Myklebust5a37f852012-04-28 14:55:16 -04004104 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00004105 data->args.bitmask = NULL;
4106 data->res.fattr = NULL;
4107 } else
4108 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004109
Fred Isamanb029bc92011-03-03 15:13:42 +00004110 if (!data->write_done_cb)
4111 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004112 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 data->timestamp = jiffies;
4114
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004115 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004116 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117}
4118
NeilBrownef1820f2013-09-04 17:04:49 +10004119static int nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04004120{
Trond Myklebust9b206142013-03-17 15:52:00 -04004121 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004122 &data->args.seq_args,
4123 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004124 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004125 return 0;
4126 if (nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4127 data->args.lock_context, FMODE_WRITE) == -EIO)
4128 return -EIO;
4129 if (unlikely(test_bit(NFS_CONTEXT_BAD, &data->args.context->flags)))
4130 return -EIO;
4131 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132}
4133
Fred Isaman0b7c0152012-04-20 14:47:39 -04004134static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4135{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004136 nfs4_setup_sequence(NFS_SERVER(data->inode),
4137 &data->args.seq_args,
4138 &data->res.seq_res,
4139 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140}
4141
Fred Isaman0b7c0152012-04-20 14:47:39 -04004142static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004145
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004146 trace_nfs4_commit(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004147 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004148 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004149 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004151 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152}
4153
Fred Isaman0b7c0152012-04-20 14:47:39 -04004154static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004155{
4156 if (!nfs4_sequence_done(task, &data->res.seq_res))
4157 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004158 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004159}
4160
Fred Isaman0b7c0152012-04-20 14:47:39 -04004161static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004163 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004164
Fred Isaman0b7c0152012-04-20 14:47:39 -04004165 if (data->commit_done_cb == NULL)
4166 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004167 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004168 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004169 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170}
4171
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004172struct nfs4_renewdata {
4173 struct nfs_client *client;
4174 unsigned long timestamp;
4175};
4176
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177/*
4178 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4179 * standalone procedure for queueing an asynchronous RENEW.
4180 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004181static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004182{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004183 struct nfs4_renewdata *data = calldata;
4184 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004185
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004186 if (atomic_read(&clp->cl_count) > 1)
4187 nfs4_schedule_state_renewal(clp);
4188 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004189 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004190}
4191
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004192static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004194 struct nfs4_renewdata *data = calldata;
4195 struct nfs_client *clp = data->client;
4196 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004198 trace_nfs4_renew_async(clp, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04004200 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004201 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4202 return;
4203 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004204 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004205 return;
4206 }
4207 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004209 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210}
4211
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004212static const struct rpc_call_ops nfs4_renew_ops = {
4213 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004214 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004215};
4216
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004217static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218{
4219 struct rpc_message msg = {
4220 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4221 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004222 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004224 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004226 if (renew_flags == 0)
4227 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004228 if (!atomic_inc_not_zero(&clp->cl_count))
4229 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004230 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004231 if (data == NULL)
4232 return -ENOMEM;
4233 data->client = clp;
4234 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004235 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004236 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237}
4238
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004239static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240{
4241 struct rpc_message msg = {
4242 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4243 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004244 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 };
4246 unsigned long now = jiffies;
4247 int status;
4248
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004249 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 if (status < 0)
4251 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004252 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 return 0;
4254}
4255
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004256static inline int nfs4_server_supports_acls(struct nfs_server *server)
4257{
4258 return (server->caps & NFS_CAP_ACLS)
4259 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
4260 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
4261}
4262
Trond Myklebust21f498c2012-08-24 10:59:25 -04004263/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4264 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004265 * the stack.
4266 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004267#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004268
Neil Hormane9e3d722011-03-04 19:26:03 -05004269static int buf_to_pages_noslab(const void *buf, size_t buflen,
4270 struct page **pages, unsigned int *pgbase)
4271{
4272 struct page *newpage, **spages;
4273 int rc = 0;
4274 size_t len;
4275 spages = pages;
4276
4277 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004278 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004279 newpage = alloc_page(GFP_KERNEL);
4280
4281 if (newpage == NULL)
4282 goto unwind;
4283 memcpy(page_address(newpage), buf, len);
4284 buf += len;
4285 buflen -= len;
4286 *pages++ = newpage;
4287 rc++;
4288 } while (buflen != 0);
4289
4290 return rc;
4291
4292unwind:
4293 for(; rc > 0; rc--)
4294 __free_page(spages[rc-1]);
4295 return -ENOMEM;
4296}
4297
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004298struct nfs4_cached_acl {
4299 int cached;
4300 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004301 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004302};
4303
4304static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004305{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004306 struct nfs_inode *nfsi = NFS_I(inode);
4307
4308 spin_lock(&inode->i_lock);
4309 kfree(nfsi->nfs4_acl);
4310 nfsi->nfs4_acl = acl;
4311 spin_unlock(&inode->i_lock);
4312}
4313
4314static void nfs4_zap_acl_attr(struct inode *inode)
4315{
4316 nfs4_set_cached_acl(inode, NULL);
4317}
4318
4319static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4320{
4321 struct nfs_inode *nfsi = NFS_I(inode);
4322 struct nfs4_cached_acl *acl;
4323 int ret = -ENOENT;
4324
4325 spin_lock(&inode->i_lock);
4326 acl = nfsi->nfs4_acl;
4327 if (acl == NULL)
4328 goto out;
4329 if (buf == NULL) /* user is just asking for length */
4330 goto out_len;
4331 if (acl->cached == 0)
4332 goto out;
4333 ret = -ERANGE; /* see getxattr(2) man page */
4334 if (acl->len > buflen)
4335 goto out;
4336 memcpy(buf, acl->data, acl->len);
4337out_len:
4338 ret = acl->len;
4339out:
4340 spin_unlock(&inode->i_lock);
4341 return ret;
4342}
4343
Sachin Prabhu5794d212012-04-17 14:36:40 +01004344static 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 +00004345{
4346 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004347 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004348
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004349 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004350 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004351 if (acl == NULL)
4352 goto out;
4353 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004354 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004355 } else {
4356 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4357 if (acl == NULL)
4358 goto out;
4359 acl->cached = 0;
4360 }
4361 acl->len = acl_len;
4362out:
4363 nfs4_set_cached_acl(inode, acl);
4364}
4365
Andy Adamsonbf118a32011-12-07 11:55:27 -05004366/*
4367 * The getxattr API returns the required buffer length when called with a
4368 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4369 * the required buf. On a NULL buf, we send a page of data to the server
4370 * guessing that the ACL request can be serviced by a page. If so, we cache
4371 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4372 * the cache. If not so, we throw away the page, and cache the required
4373 * length. The next getxattr call will then produce another round trip to
4374 * the server, this time with the input buf of the required size.
4375 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004376static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004377{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004378 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004379 struct nfs_getaclargs args = {
4380 .fh = NFS_FH(inode),
4381 .acl_pages = pages,
4382 .acl_len = buflen,
4383 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004384 struct nfs_getaclres res = {
4385 .acl_len = buflen,
4386 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004387 struct rpc_message msg = {
4388 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4389 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004390 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004391 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004392 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4393 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004394
Andy Adamsonbf118a32011-12-07 11:55:27 -05004395 /* As long as we're doing a round trip to the server anyway,
4396 * let's be prepared for a page of acl data. */
4397 if (npages == 0)
4398 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004399 if (npages > ARRAY_SIZE(pages))
4400 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004401
Andy Adamsonbf118a32011-12-07 11:55:27 -05004402 for (i = 0; i < npages; i++) {
4403 pages[i] = alloc_page(GFP_KERNEL);
4404 if (!pages[i])
4405 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004406 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004407
4408 /* for decoding across pages */
4409 res.acl_scratch = alloc_page(GFP_KERNEL);
4410 if (!res.acl_scratch)
4411 goto out_free;
4412
Andy Adamsonbf118a32011-12-07 11:55:27 -05004413 args.acl_len = npages * PAGE_SIZE;
4414 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004415
Peng Taode040be2012-01-10 22:42:47 +08004416 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004417 __func__, buf, buflen, npages, args.acl_len);
4418 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4419 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004420 if (ret)
4421 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004422
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004423 /* Handle the case where the passed-in buffer is too short */
4424 if (res.acl_flags & NFS4_ACL_TRUNC) {
4425 /* Did the user only issue a request for the acl length? */
4426 if (buf == NULL)
4427 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004428 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004429 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004430 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004431 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004432 if (buf) {
4433 if (res.acl_len > buflen) {
4434 ret = -ERANGE;
4435 goto out_free;
4436 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004437 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004438 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004439out_ok:
4440 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004441out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004442 for (i = 0; i < npages; i++)
4443 if (pages[i])
4444 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004445 if (res.acl_scratch)
4446 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004447 return ret;
4448}
4449
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004450static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4451{
4452 struct nfs4_exception exception = { };
4453 ssize_t ret;
4454 do {
4455 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04004456 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004457 if (ret >= 0)
4458 break;
4459 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4460 } while (exception.retry);
4461 return ret;
4462}
4463
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004464static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4465{
4466 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004467 int ret;
4468
4469 if (!nfs4_server_supports_acls(server))
4470 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004471 ret = nfs_revalidate_inode(server, inode);
4472 if (ret < 0)
4473 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004474 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4475 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004476 ret = nfs4_read_cached_acl(inode, buf, buflen);
4477 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004478 /* -ENOENT is returned if there is no ACL or if there is an ACL
4479 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004480 return ret;
4481 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004482}
4483
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004484static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004485{
4486 struct nfs_server *server = NFS_SERVER(inode);
4487 struct page *pages[NFS4ACL_MAXPAGES];
4488 struct nfs_setaclargs arg = {
4489 .fh = NFS_FH(inode),
4490 .acl_pages = pages,
4491 .acl_len = buflen,
4492 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004493 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004494 struct rpc_message msg = {
4495 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4496 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004497 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004498 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004499 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004500 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004501
4502 if (!nfs4_server_supports_acls(server))
4503 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004504 if (npages > ARRAY_SIZE(pages))
4505 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004506 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4507 if (i < 0)
4508 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004509 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004510 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004511
4512 /*
4513 * Free each page after tx, so the only ref left is
4514 * held by the network stack
4515 */
4516 for (; i > 0; i--)
4517 put_page(pages[i-1]);
4518
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004519 /*
4520 * Acl update can result in inode attribute update.
4521 * so mark the attribute cache invalid.
4522 */
4523 spin_lock(&inode->i_lock);
4524 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4525 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004526 nfs_access_zap_cache(inode);
4527 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004528 return ret;
4529}
4530
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004531static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4532{
4533 struct nfs4_exception exception = { };
4534 int err;
4535 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004536 err = __nfs4_proc_set_acl(inode, buf, buflen);
4537 trace_nfs4_set_acl(inode, err);
4538 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004539 &exception);
4540 } while (exception.retry);
4541 return err;
4542}
4543
David Quigleyaa9c2662013-05-22 12:50:44 -04004544#ifdef CONFIG_NFS_V4_SECURITY_LABEL
4545static int _nfs4_get_security_label(struct inode *inode, void *buf,
4546 size_t buflen)
4547{
4548 struct nfs_server *server = NFS_SERVER(inode);
4549 struct nfs_fattr fattr;
4550 struct nfs4_label label = {0, 0, buflen, buf};
4551
4552 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4553 struct nfs4_getattr_arg args = {
4554 .fh = NFS_FH(inode),
4555 .bitmask = bitmask,
4556 };
4557 struct nfs4_getattr_res res = {
4558 .fattr = &fattr,
4559 .label = &label,
4560 .server = server,
4561 };
4562 struct rpc_message msg = {
4563 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4564 .rpc_argp = &args,
4565 .rpc_resp = &res,
4566 };
4567 int ret;
4568
4569 nfs_fattr_init(&fattr);
4570
4571 ret = rpc_call_sync(server->client, &msg, 0);
4572 if (ret)
4573 return ret;
4574 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4575 return -ENOENT;
4576 if (buflen < label.len)
4577 return -ERANGE;
4578 return 0;
4579}
4580
4581static int nfs4_get_security_label(struct inode *inode, void *buf,
4582 size_t buflen)
4583{
4584 struct nfs4_exception exception = { };
4585 int err;
4586
4587 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4588 return -EOPNOTSUPP;
4589
4590 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004591 err = _nfs4_get_security_label(inode, buf, buflen);
4592 trace_nfs4_get_security_label(inode, err);
4593 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004594 &exception);
4595 } while (exception.retry);
4596 return err;
4597}
4598
4599static int _nfs4_do_set_security_label(struct inode *inode,
4600 struct nfs4_label *ilabel,
4601 struct nfs_fattr *fattr,
4602 struct nfs4_label *olabel)
4603{
4604
4605 struct iattr sattr = {0};
4606 struct nfs_server *server = NFS_SERVER(inode);
4607 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4608 struct nfs_setattrargs args = {
4609 .fh = NFS_FH(inode),
4610 .iap = &sattr,
4611 .server = server,
4612 .bitmask = bitmask,
4613 .label = ilabel,
4614 };
4615 struct nfs_setattrres res = {
4616 .fattr = fattr,
4617 .label = olabel,
4618 .server = server,
4619 };
4620 struct rpc_message msg = {
4621 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
4622 .rpc_argp = &args,
4623 .rpc_resp = &res,
4624 };
4625 int status;
4626
4627 nfs4_stateid_copy(&args.stateid, &zero_stateid);
4628
4629 status = rpc_call_sync(server->client, &msg, 0);
4630 if (status)
4631 dprintk("%s failed: %d\n", __func__, status);
4632
4633 return status;
4634}
4635
4636static int nfs4_do_set_security_label(struct inode *inode,
4637 struct nfs4_label *ilabel,
4638 struct nfs_fattr *fattr,
4639 struct nfs4_label *olabel)
4640{
4641 struct nfs4_exception exception = { };
4642 int err;
4643
4644 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004645 err = _nfs4_do_set_security_label(inode, ilabel,
4646 fattr, olabel);
4647 trace_nfs4_set_security_label(inode, err);
4648 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004649 &exception);
4650 } while (exception.retry);
4651 return err;
4652}
4653
4654static int
4655nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4656{
4657 struct nfs4_label ilabel, *olabel = NULL;
4658 struct nfs_fattr fattr;
4659 struct rpc_cred *cred;
4660 struct inode *inode = dentry->d_inode;
4661 int status;
4662
4663 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4664 return -EOPNOTSUPP;
4665
4666 nfs_fattr_init(&fattr);
4667
4668 ilabel.pi = 0;
4669 ilabel.lfs = 0;
4670 ilabel.label = (char *)buf;
4671 ilabel.len = buflen;
4672
4673 cred = rpc_lookup_cred();
4674 if (IS_ERR(cred))
4675 return PTR_ERR(cred);
4676
4677 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4678 if (IS_ERR(olabel)) {
4679 status = -PTR_ERR(olabel);
4680 goto out;
4681 }
4682
4683 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4684 if (status == 0)
4685 nfs_setsecurity(inode, &fattr, olabel);
4686
4687 nfs4_label_free(olabel);
4688out:
4689 put_rpccred(cred);
4690 return status;
4691}
4692#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4693
4694
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004696nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004698 struct nfs_client *clp = server->nfs_client;
4699
4700 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 return 0;
4702 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004703 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004704 case -NFS4ERR_ADMIN_REVOKED:
4705 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004706 if (state == NULL)
4707 break;
4708 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004709 case -NFS4ERR_OPENMODE:
4710 if (state == NULL)
4711 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004712 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4713 goto stateid_invalid;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004714 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004715 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004716 if (state != NULL) {
4717 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4718 goto stateid_invalid;
4719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004721 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004722 nfs4_schedule_lease_recovery(clp);
4723 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004724#if defined(CONFIG_NFS_V4_1)
4725 case -NFS4ERR_BADSESSION:
4726 case -NFS4ERR_BADSLOT:
4727 case -NFS4ERR_BAD_HIGH_SLOT:
4728 case -NFS4ERR_DEADSESSION:
4729 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4730 case -NFS4ERR_SEQ_FALSE_RETRY:
4731 case -NFS4ERR_SEQ_MISORDERED:
4732 dprintk("%s ERROR %d, Reset session\n", __func__,
4733 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004734 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004735 task->tk_status = 0;
4736 return -EAGAIN;
4737#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004739 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004740 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4742 task->tk_status = 0;
4743 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004744 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745 case -NFS4ERR_OLD_STATEID:
4746 task->tk_status = 0;
4747 return -EAGAIN;
4748 }
4749 task->tk_status = nfs4_map_errors(task->tk_status);
4750 return 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004751stateid_invalid:
4752 task->tk_status = -EIO;
4753 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004754wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004755 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004756 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4757 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4758 task->tk_status = 0;
4759 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760}
4761
Chuck Leverf0920752012-05-21 22:45:41 -04004762static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4763 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004764{
4765 __be32 verf[2];
4766
Chuck Lever2c820d92012-05-21 22:45:33 -04004767 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4768 /* An impossible timestamp guarantees this value
4769 * will never match a generated boot time. */
4770 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04004771 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04004772 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004773 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04004774 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
4775 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04004776 }
Chuck Levercd937102012-03-02 17:14:31 -05004777 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4778}
4779
Chuck Levere984a552012-09-14 17:24:21 -04004780static unsigned int
4781nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4782 char *buf, size_t len)
4783{
4784 unsigned int result;
4785
4786 rcu_read_lock();
4787 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4788 clp->cl_ipaddr,
4789 rpc_peeraddr2str(clp->cl_rpcclient,
4790 RPC_DISPLAY_ADDR),
4791 rpc_peeraddr2str(clp->cl_rpcclient,
4792 RPC_DISPLAY_PROTO));
4793 rcu_read_unlock();
4794 return result;
4795}
4796
4797static unsigned int
4798nfs4_init_uniform_client_string(const struct nfs_client *clp,
4799 char *buf, size_t len)
4800{
Trond Myklebust55b59292013-07-24 16:36:35 -04004801 const char *nodename = clp->cl_rpcclient->cl_nodename;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004802
4803 if (nfs4_client_id_uniquifier[0] != '\0')
Trond Myklebust55b59292013-07-24 16:36:35 -04004804 return scnprintf(buf, len, "Linux NFSv%u.%u %s/%s",
4805 clp->rpc_ops->version,
4806 clp->cl_minorversion,
4807 nfs4_client_id_uniquifier,
4808 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004809 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4810 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004811 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004812}
4813
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004814/**
4815 * nfs4_proc_setclientid - Negotiate client ID
4816 * @clp: state data structure
4817 * @program: RPC program for NFSv4 callback service
4818 * @port: IP port number for NFS4 callback service
4819 * @cred: RPC credential to use for this call
4820 * @res: where to place the result
4821 *
4822 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4823 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004824int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4825 unsigned short port, struct rpc_cred *cred,
4826 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827{
4828 nfs4_verifier sc_verifier;
4829 struct nfs4_setclientid setclientid = {
4830 .sc_verifier = &sc_verifier,
4831 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004832 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833 };
4834 struct rpc_message msg = {
4835 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4836 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004837 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004838 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004840 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841
Chuck Leverde734832012-07-11 16:30:50 -04004842 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004843 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004844 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4845 setclientid.sc_name_len =
4846 nfs4_init_uniform_client_string(clp,
4847 setclientid.sc_name,
4848 sizeof(setclientid.sc_name));
4849 else
4850 setclientid.sc_name_len =
4851 nfs4_init_nonuniform_client_string(clp,
4852 setclientid.sc_name,
4853 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004854 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004855 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004856 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Djalal Harounife2d5392013-06-18 17:58:12 +01004857 sizeof(setclientid.sc_netid), "%s",
Chuck Leverd4d3c502007-12-10 14:57:09 -05004858 rpc_peeraddr2str(clp->cl_rpcclient,
4859 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004860 rcu_read_unlock();
4861 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004862 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 clp->cl_ipaddr, port >> 8, port & 255);
4864
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004865 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4866 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4867 setclientid.sc_name_len, setclientid.sc_name);
4868 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004869 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004870 dprintk("NFS reply setclientid: %d\n", status);
4871 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872}
4873
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004874/**
4875 * nfs4_proc_setclientid_confirm - Confirm client ID
4876 * @clp: state data structure
4877 * @res: result of a previous SETCLIENTID
4878 * @cred: RPC credential to use for this call
4879 *
4880 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4881 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004882int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004883 struct nfs4_setclientid_res *arg,
4884 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 struct rpc_message msg = {
4887 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004888 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004889 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891 int status;
4892
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004893 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4894 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4895 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004896 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004897 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004898 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899 return status;
4900}
4901
Trond Myklebustfe650402006-01-03 09:55:18 +01004902struct nfs4_delegreturndata {
4903 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004904 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004905 struct nfs_fh fh;
4906 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004907 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004908 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004909 int rpc_status;
4910};
4911
Trond Myklebustfe650402006-01-03 09:55:18 +01004912static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4913{
4914 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004915
Trond Myklebust14516c32010-07-31 14:29:06 -04004916 if (!nfs4_sequence_done(task, &data->res.seq_res))
4917 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004918
Trond Myklebustca8acf82013-08-13 10:36:56 -04004919 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004920 switch (task->tk_status) {
4921 case -NFS4ERR_STALE_STATEID:
4922 case -NFS4ERR_EXPIRED:
4923 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004924 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004925 break;
4926 default:
4927 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4928 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004929 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004930 return;
4931 }
4932 }
4933 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004934}
4935
4936static void nfs4_delegreturn_release(void *calldata)
4937{
Trond Myklebustfe650402006-01-03 09:55:18 +01004938 kfree(calldata);
4939}
4940
Andy Adamson938e1012009-04-01 09:22:28 -04004941static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4942{
4943 struct nfs4_delegreturndata *d_data;
4944
4945 d_data = (struct nfs4_delegreturndata *)data;
4946
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004947 nfs4_setup_sequence(d_data->res.server,
4948 &d_data->args.seq_args,
4949 &d_data->res.seq_res,
4950 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004951}
Andy Adamson938e1012009-04-01 09:22:28 -04004952
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004953static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004954 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01004955 .rpc_call_done = nfs4_delegreturn_done,
4956 .rpc_release = nfs4_delegreturn_release,
4957};
4958
Trond Myklebuste6f81072008-01-24 18:14:34 -05004959static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004960{
4961 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004962 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004963 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004964 struct rpc_message msg = {
4965 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4966 .rpc_cred = cred,
4967 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004968 struct rpc_task_setup task_setup_data = {
4969 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004970 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004971 .callback_ops = &nfs4_delegreturn_ops,
4972 .flags = RPC_TASK_ASYNC,
4973 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004974 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004975
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004976 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004977 if (data == NULL)
4978 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04004979 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004980 data->args.fhandle = &data->fh;
4981 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004982 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004983 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004984 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004985 data->res.fattr = &data->fattr;
4986 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004987 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004988 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004989 data->rpc_status = 0;
4990
Trond Myklebustc970aa82007-07-14 15:39:59 -04004991 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004992 msg.rpc_argp = &data->args;
4993 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004994 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004995 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004996 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004997 if (!issync)
4998 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004999 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005000 if (status != 0)
5001 goto out;
5002 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005003 if (status == 0)
5004 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5005 else
5006 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005007out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005008 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005009 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010}
5011
Trond Myklebuste6f81072008-01-24 18:14:34 -05005012int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013{
5014 struct nfs_server *server = NFS_SERVER(inode);
5015 struct nfs4_exception exception = { };
5016 int err;
5017 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005018 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Trond Myklebustca8acf82013-08-13 10:36:56 -04005019 trace_nfs4_delegreturn(inode, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020 switch (err) {
5021 case -NFS4ERR_STALE_STATEID:
5022 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 case 0:
5024 return 0;
5025 }
5026 err = nfs4_handle_exception(server, err, &exception);
5027 } while (exception.retry);
5028 return err;
5029}
5030
5031#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5032#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5033
5034/*
5035 * sleep, with exponential backoff, and retry the LOCK operation.
5036 */
5037static unsigned long
5038nfs4_set_lock_task_retry(unsigned long timeout)
5039{
Colin Cross416ad3c2013-05-06 23:50:06 +00005040 freezable_schedule_timeout_killable_unsafe(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041 timeout <<= 1;
5042 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5043 return NFS4_LOCK_MAXTIMEOUT;
5044 return timeout;
5045}
5046
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5048{
5049 struct inode *inode = state->inode;
5050 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005051 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005052 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005054 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005056 struct nfs_lockt_res res = {
5057 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058 };
5059 struct rpc_message msg = {
5060 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5061 .rpc_argp = &arg,
5062 .rpc_resp = &res,
5063 .rpc_cred = state->owner->so_cred,
5064 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065 struct nfs4_lock_state *lsp;
5066 int status;
5067
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005068 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005069 status = nfs4_set_lock_state(state, request);
5070 if (status != 0)
5071 goto out;
5072 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005073 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005074 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005075 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005076 switch (status) {
5077 case 0:
5078 request->fl_type = F_UNLCK;
5079 break;
5080 case -NFS4ERR_DENIED:
5081 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005083 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005084out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085 return status;
5086}
5087
5088static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5089{
5090 struct nfs4_exception exception = { };
5091 int err;
5092
5093 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005094 err = _nfs4_proc_getlk(state, cmd, request);
5095 trace_nfs4_get_lock(request, state, cmd, err);
5096 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097 &exception);
5098 } while (exception.retry);
5099 return err;
5100}
5101
5102static int do_vfs_lock(struct file *file, struct file_lock *fl)
5103{
5104 int res = 0;
5105 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5106 case FL_POSIX:
5107 res = posix_lock_file_wait(file, fl);
5108 break;
5109 case FL_FLOCK:
5110 res = flock_lock_file_wait(file, fl);
5111 break;
5112 default:
5113 BUG();
5114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115 return res;
5116}
5117
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005118struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005119 struct nfs_locku_args arg;
5120 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005121 struct nfs4_lock_state *lsp;
5122 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005123 struct file_lock fl;
5124 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005125 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005126};
5127
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005128static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5129 struct nfs_open_context *ctx,
5130 struct nfs4_lock_state *lsp,
5131 struct nfs_seqid *seqid)
5132{
5133 struct nfs4_unlockdata *p;
5134 struct inode *inode = lsp->ls_state->inode;
5135
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005136 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005137 if (p == NULL)
5138 return NULL;
5139 p->arg.fh = NFS_FH(inode);
5140 p->arg.fl = &p->fl;
5141 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005142 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005143 p->arg.stateid = &lsp->ls_stateid;
5144 p->lsp = lsp;
5145 atomic_inc(&lsp->ls_count);
5146 /* Ensure we don't close file until we're done freeing locks! */
5147 p->ctx = get_nfs_open_context(ctx);
5148 memcpy(&p->fl, fl, sizeof(p->fl));
5149 p->server = NFS_SERVER(inode);
5150 return p;
5151}
5152
Trond Myklebust06f814a2006-01-03 09:55:07 +01005153static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005154{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005155 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005156 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005157 nfs4_put_lock_state(calldata->lsp);
5158 put_nfs_open_context(calldata->ctx);
5159 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005160}
5161
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005162static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005163{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005164 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005165
Trond Myklebust14516c32010-07-31 14:29:06 -04005166 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5167 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005168 switch (task->tk_status) {
5169 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05005170 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
5171 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005172 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005173 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005174 case -NFS4ERR_BAD_STATEID:
5175 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005176 case -NFS4ERR_STALE_STATEID:
5177 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005178 break;
5179 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005180 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005181 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005182 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005183 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005184}
5185
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005186static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005187{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005188 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005190 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005191 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04005192 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005193 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005194 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005195 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005196 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005197 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005198 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005199 &calldata->res.seq_res,
5200 task) != 0)
5201 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005202 return;
5203out_no_action:
5204 task->tk_action = NULL;
5205out_wait:
5206 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207}
5208
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005209static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005210 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005211 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005212 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005213};
5214
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005215static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5216 struct nfs_open_context *ctx,
5217 struct nfs4_lock_state *lsp,
5218 struct nfs_seqid *seqid)
5219{
5220 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005221 struct rpc_message msg = {
5222 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5223 .rpc_cred = ctx->cred,
5224 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005225 struct rpc_task_setup task_setup_data = {
5226 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005227 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005228 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005229 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005230 .flags = RPC_TASK_ASYNC,
5231 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005232
Frank Filz137d6ac2007-07-09 15:32:29 -07005233 /* Ensure this is an unlock - when canceling a lock, the
5234 * canceled lock is passed in, and it won't be an unlock.
5235 */
5236 fl->fl_type = F_UNLCK;
5237
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005238 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5239 if (data == NULL) {
5240 nfs_free_seqid(seqid);
5241 return ERR_PTR(-ENOMEM);
5242 }
5243
Chuck Levera9c92d62013-08-09 12:48:18 -04005244 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005245 msg.rpc_argp = &data->arg;
5246 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005247 task_setup_data.callback_data = data;
5248 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005249}
5250
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5252{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005253 struct inode *inode = state->inode;
5254 struct nfs4_state_owner *sp = state->owner;
5255 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005256 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005257 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005258 struct rpc_task *task;
5259 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005260 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261
Trond Myklebust9b073572006-06-29 16:38:34 -04005262 status = nfs4_set_lock_state(state, request);
5263 /* Unlock _before_ we do the RPC call */
5264 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005265 /* Exclude nfs_delegation_claim_locks() */
5266 mutex_lock(&sp->so_delegreturn_mutex);
5267 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005268 down_read(&nfsi->rwsem);
5269 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
5270 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005271 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005272 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005273 }
5274 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005275 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005276 if (status != 0)
5277 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005278 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005279 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005280 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5281 goto out;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005282 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005283 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005284 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04005285 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005286 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005287 status = PTR_ERR(task);
5288 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005289 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005290 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005291 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005292out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005293 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005294 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005295 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296}
5297
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005298struct nfs4_lockdata {
5299 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005300 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005301 struct nfs4_lock_state *lsp;
5302 struct nfs_open_context *ctx;
5303 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005304 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005305 int rpc_status;
5306 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005307 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005308};
5309
5310static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005311 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5312 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005313{
5314 struct nfs4_lockdata *p;
5315 struct inode *inode = lsp->ls_state->inode;
5316 struct nfs_server *server = NFS_SERVER(inode);
5317
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005318 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005319 if (p == NULL)
5320 return NULL;
5321
5322 p->arg.fh = NFS_FH(inode);
5323 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005324 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005325 if (p->arg.open_seqid == NULL)
5326 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005327 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005328 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005329 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005330 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04005331 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005332 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005333 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005334 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005335 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005336 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005337 atomic_inc(&lsp->ls_count);
5338 p->ctx = get_nfs_open_context(ctx);
5339 memcpy(&p->fl, fl, sizeof(p->fl));
5340 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005341out_free_seqid:
5342 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005343out_free:
5344 kfree(p);
5345 return NULL;
5346}
5347
5348static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5349{
5350 struct nfs4_lockdata *data = calldata;
5351 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352
Harvey Harrison3110ff82008-05-02 13:42:44 -07005353 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005354 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005355 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005356 /* Do we need to do an open_to_lock_owner? */
5357 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005358 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005359 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005360 }
Trond Myklebust8e472f32013-04-20 01:30:53 -04005361 data->arg.open_stateid = &state->open_stateid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005362 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005363 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005364 } else
5365 data->arg.new_lock_owner = 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04005366 if (!nfs4_valid_open_stateid(state)) {
5367 data->rpc_status = -EBADF;
5368 task->tk_action = NULL;
5369 goto out_release_open_seqid;
5370 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005371 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005372 if (nfs4_setup_sequence(data->server,
5373 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005374 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005375 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04005376 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04005377out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005378 nfs_release_seqid(data->arg.open_seqid);
5379out_release_lock_seqid:
5380 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005381out_wait:
5382 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005383 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005384}
5385
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005386static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5387{
5388 struct nfs4_lockdata *data = calldata;
5389
Harvey Harrison3110ff82008-05-02 13:42:44 -07005390 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005391
Trond Myklebust14516c32010-07-31 14:29:06 -04005392 if (!nfs4_sequence_done(task, &data->res.seq_res))
5393 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04005394
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005395 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005396 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005397 if (data->rpc_status == 0)
5398 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
5399 else
5400 goto out;
5401 }
5402 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05005403 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005404 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04005405 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005406 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005407out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07005408 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005409}
5410
5411static void nfs4_lock_release(void *calldata)
5412{
5413 struct nfs4_lockdata *data = calldata;
5414
Harvey Harrison3110ff82008-05-02 13:42:44 -07005415 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005416 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005417 if (data->cancelled != 0) {
5418 struct rpc_task *task;
5419 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5420 data->arg.lock_seqid);
5421 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08005422 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005423 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005424 } else
5425 nfs_free_seqid(data->arg.lock_seqid);
5426 nfs4_put_lock_state(data->lsp);
5427 put_nfs_open_context(data->ctx);
5428 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005429 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005430}
5431
5432static const struct rpc_call_ops nfs4_lock_ops = {
5433 .rpc_call_prepare = nfs4_lock_prepare,
5434 .rpc_call_done = nfs4_lock_done,
5435 .rpc_release = nfs4_lock_release,
5436};
5437
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005438static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5439{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005440 switch (error) {
5441 case -NFS4ERR_ADMIN_REVOKED:
5442 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05005443 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005444 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04005445 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05005446 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005447 break;
5448 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005449 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05005450 case -NFS4ERR_EXPIRED:
5451 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005452 };
5453}
5454
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005455static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005456{
5457 struct nfs4_lockdata *data;
5458 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005459 struct rpc_message msg = {
5460 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5461 .rpc_cred = state->owner->so_cred,
5462 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005463 struct rpc_task_setup task_setup_data = {
5464 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005465 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005466 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005467 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005468 .flags = RPC_TASK_ASYNC,
5469 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005470 int ret;
5471
Harvey Harrison3110ff82008-05-02 13:42:44 -07005472 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005473 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005474 fl->fl_u.nfs4_fl.owner,
5475 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005476 if (data == NULL)
5477 return -ENOMEM;
5478 if (IS_SETLKW(cmd))
5479 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04005480 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005481 msg.rpc_argp = &data->arg;
5482 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005483 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005484 if (recovery_type > NFS_LOCK_NEW) {
5485 if (recovery_type == NFS_LOCK_RECLAIM)
5486 data->arg.reclaim = NFS_LOCK_RECLAIM;
5487 nfs4_set_sequence_privileged(&data->arg.seq_args);
5488 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04005489 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005490 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005491 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005492 ret = nfs4_wait_for_completion_rpc_task(task);
5493 if (ret == 0) {
5494 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005495 if (ret)
5496 nfs4_handle_setlk_error(data->server, data->lsp,
5497 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005498 } else
5499 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005500 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005501 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005502 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503}
5504
5505static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5506{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005507 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005508 struct nfs4_exception exception = {
5509 .inode = state->inode,
5510 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005511 int err;
5512
5513 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005514 /* Cache the lock if possible... */
5515 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5516 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005517 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005518 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
Trond Myklebust168667c2010-10-19 19:47:49 -04005519 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00005520 break;
5521 nfs4_handle_exception(server, err, &exception);
5522 } while (exception.retry);
5523 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524}
5525
NeilBrownef1820f2013-09-04 17:04:49 +10005526bool recover_locks = true;
5527module_param(recover_locks, bool, 0644);
5528MODULE_PARM_DESC(recover_locks,
5529 "If the server reports that a lock might be lost, "
5530 "try to recovery it risking corruption.");
5531
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5533{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005534 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005535 struct nfs4_exception exception = {
5536 .inode = state->inode,
5537 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005538 int err;
5539
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005540 err = nfs4_set_lock_state(state, request);
5541 if (err != 0)
5542 return err;
NeilBrownef1820f2013-09-04 17:04:49 +10005543 if (!recover_locks) {
5544 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
5545 return 0;
5546 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005547 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005548 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5549 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005550 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005551 trace_nfs4_lock_expired(request, state, F_SETLK, err);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005552 switch (err) {
5553 default:
5554 goto out;
5555 case -NFS4ERR_GRACE:
5556 case -NFS4ERR_DELAY:
5557 nfs4_handle_exception(server, err, &exception);
5558 err = 0;
5559 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005560 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005561out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00005562 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563}
5564
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005565#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005566/**
5567 * nfs41_check_expired_locks - possibly free a lock stateid
5568 *
5569 * @state: NFSv4 state for an inode
5570 *
5571 * Returns NFS_OK if recovery for this stateid is now finished.
5572 * Otherwise a negative NFS4ERR value is returned.
5573 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005574static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005575{
Chuck Levereb64cf92012-07-11 16:30:05 -04005576 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005577 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005578 struct nfs_server *server = NFS_SERVER(state->inode);
5579
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005580 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04005581 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005582 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
5583
5584 status = nfs41_test_stateid(server,
5585 &lsp->ls_stateid,
5586 cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04005587 trace_nfs4_test_lock_stateid(state, lsp, status);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005588 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005589 /* Free the stateid unless the server
5590 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04005591 if (status != -NFS4ERR_BAD_STATEID)
5592 nfs41_free_stateid(server,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005593 &lsp->ls_stateid,
5594 cred);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005595 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005596 ret = status;
5597 }
5598 }
5599 };
5600
5601 return ret;
5602}
5603
5604static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5605{
5606 int status = NFS_OK;
5607
5608 if (test_bit(LK_STATE_IN_USE, &state->flags))
5609 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005610 if (status != NFS_OK)
5611 status = nfs4_lock_expired(state, request);
5612 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005613}
5614#endif
5615
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5617{
Trond Myklebust9a99af492013-02-04 20:17:49 -05005618 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005619 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005620 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005621 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005622 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005624 if ((fl_flags & FL_POSIX) &&
5625 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5626 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005627 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005628 status = nfs4_set_lock_state(state, request);
5629 if (status != 0)
5630 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005631 request->fl_flags |= FL_ACCESS;
5632 status = do_vfs_lock(request->fl_file, request);
5633 if (status < 0)
5634 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005635 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005636 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005637 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005638 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005639 request->fl_flags = fl_flags & ~FL_SLEEP;
5640 status = do_vfs_lock(request->fl_file, request);
5641 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005642 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05005643 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5644 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005645 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005646 if (status != 0)
Trond Myklebust9a99af492013-02-04 20:17:49 -05005647 goto out;
5648 down_read(&nfsi->rwsem);
5649 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5650 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005651 goto out_unlock;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005652 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005653 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005654 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005655 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005656 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5657 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005658out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005659 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005660out:
5661 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662 return status;
5663}
5664
5665static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5666{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005667 struct nfs4_exception exception = {
5668 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005669 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005670 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671 int err;
5672
5673 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005674 err = _nfs4_proc_setlk(state, cmd, request);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005675 trace_nfs4_set_lock(request, state, cmd, err);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005676 if (err == -NFS4ERR_DENIED)
5677 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005679 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680 } while (exception.retry);
5681 return err;
5682}
5683
5684static int
5685nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5686{
5687 struct nfs_open_context *ctx;
5688 struct nfs4_state *state;
5689 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5690 int status;
5691
5692 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005693 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694 state = ctx->state;
5695
5696 if (request->fl_start < 0 || request->fl_end < 0)
5697 return -EINVAL;
5698
Trond Myklebustd9531262009-07-21 19:22:38 -04005699 if (IS_GETLK(cmd)) {
5700 if (state != NULL)
5701 return nfs4_proc_getlk(state, F_GETLK, request);
5702 return 0;
5703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704
5705 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5706 return -EINVAL;
5707
Trond Myklebustd9531262009-07-21 19:22:38 -04005708 if (request->fl_type == F_UNLCK) {
5709 if (state != NULL)
5710 return nfs4_proc_unlck(state, cmd, request);
5711 return 0;
5712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713
Trond Myklebustd9531262009-07-21 19:22:38 -04005714 if (state == NULL)
5715 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005716 /*
5717 * Don't rely on the VFS having checked the file open mode,
5718 * since it won't do this for flock() locks.
5719 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005720 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005721 case F_RDLCK:
5722 if (!(filp->f_mode & FMODE_READ))
5723 return -EBADF;
5724 break;
5725 case F_WRLCK:
5726 if (!(filp->f_mode & FMODE_WRITE))
5727 return -EBADF;
5728 }
5729
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 do {
5731 status = nfs4_proc_setlk(state, cmd, request);
5732 if ((status != -EAGAIN) || IS_SETLK(cmd))
5733 break;
5734 timeout = nfs4_set_lock_task_retry(timeout);
5735 status = -ERESTARTSYS;
5736 if (signalled())
5737 break;
5738 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739 return status;
5740}
5741
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005742int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05005743{
5744 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05005745 int err;
5746
5747 err = nfs4_set_lock_state(state, fl);
5748 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005749 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04005750 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005751 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05005752}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005753
Trond Myklebustcf470c32012-03-07 13:49:12 -05005754struct nfs_release_lockowner_data {
5755 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005756 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005757 struct nfs_release_lockowner_args args;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005758 struct nfs4_sequence_args seq_args;
5759 struct nfs4_sequence_res seq_res;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005760};
5761
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005762static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
5763{
5764 struct nfs_release_lockowner_data *data = calldata;
5765 nfs40_setup_sequence(data->server,
5766 &data->seq_args, &data->seq_res, task);
5767}
5768
5769static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
5770{
5771 struct nfs_release_lockowner_data *data = calldata;
5772 nfs40_sequence_done(task, &data->seq_res);
5773}
5774
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005775static void nfs4_release_lockowner_release(void *calldata)
5776{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005777 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005778 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005779 kfree(calldata);
5780}
5781
Trond Myklebust17280172012-03-11 13:11:00 -04005782static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005783 .rpc_call_prepare = nfs4_release_lockowner_prepare,
5784 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005785 .rpc_release = nfs4_release_lockowner_release,
5786};
5787
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04005788static int nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005789{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005790 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005791 struct rpc_message msg = {
5792 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5793 };
5794
5795 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005796 return -EINVAL;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005797
Trond Myklebustcf470c32012-03-07 13:49:12 -05005798 data = kmalloc(sizeof(*data), GFP_NOFS);
5799 if (!data)
5800 return -ENOMEM;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005801 nfs4_init_sequence(&data->seq_args, &data->seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05005802 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005803 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005804 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5805 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5806 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005807
Trond Myklebustcf470c32012-03-07 13:49:12 -05005808 msg.rpc_argp = &data->args;
5809 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5810 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005811}
5812
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005813#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5814
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005815static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5816 const void *buf, size_t buflen,
5817 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005818{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005819 if (strcmp(key, "") != 0)
5820 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005821
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005822 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005823}
5824
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005825static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5826 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005827{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005828 if (strcmp(key, "") != 0)
5829 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005830
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005831 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005832}
5833
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005834static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5835 size_t list_len, const char *name,
5836 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005837{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005838 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005839
J. Bruce Fields096455a2006-03-20 23:23:42 -05005840 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5841 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005842
5843 if (list && len <= list_len)
5844 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005845 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005846}
5847
David Quigleyc9bccef2013-05-22 12:50:45 -04005848#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5849static inline int nfs4_server_supports_labels(struct nfs_server *server)
5850{
5851 return server->caps & NFS_CAP_SECURITY_LABEL;
5852}
5853
5854static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
5855 const void *buf, size_t buflen,
5856 int flags, int type)
5857{
5858 if (security_ismaclabel(key))
5859 return nfs4_set_security_label(dentry, buf, buflen);
5860
5861 return -EOPNOTSUPP;
5862}
5863
5864static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
5865 void *buf, size_t buflen, int type)
5866{
5867 if (security_ismaclabel(key))
5868 return nfs4_get_security_label(dentry->d_inode, buf, buflen);
5869 return -EOPNOTSUPP;
5870}
5871
5872static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
5873 size_t list_len, const char *name,
5874 size_t name_len, int type)
5875{
5876 size_t len = 0;
5877
5878 if (nfs_server_capable(dentry->d_inode, NFS_CAP_SECURITY_LABEL)) {
5879 len = security_inode_listsecurity(dentry->d_inode, NULL, 0);
5880 if (list && len <= list_len)
5881 security_inode_listsecurity(dentry->d_inode, list, len);
5882 }
5883 return len;
5884}
5885
5886static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
5887 .prefix = XATTR_SECURITY_PREFIX,
5888 .list = nfs4_xattr_list_nfs4_label,
5889 .get = nfs4_xattr_get_nfs4_label,
5890 .set = nfs4_xattr_set_nfs4_label,
5891};
5892#endif
5893
5894
Andy Adamson533eb462011-06-13 18:25:56 -04005895/*
5896 * nfs_fhget will use either the mounted_on_fileid or the fileid
5897 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005898static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5899{
Andy Adamson533eb462011-06-13 18:25:56 -04005900 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5901 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5902 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005903 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005904 return;
5905
5906 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005907 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005908 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5909 fattr->nlink = 2;
5910}
5911
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005912static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5913 const struct qstr *name,
5914 struct nfs4_fs_locations *fs_locations,
5915 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005916{
5917 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04005918 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005919 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005920 };
5921 struct nfs4_fs_locations_arg args = {
5922 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005923 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005924 .page = page,
5925 .bitmask = bitmask,
5926 };
Benny Halevy22958462009-04-01 09:22:02 -04005927 struct nfs4_fs_locations_res res = {
5928 .fs_locations = fs_locations,
5929 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005930 struct rpc_message msg = {
5931 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5932 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005933 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005934 };
5935 int status;
5936
Harvey Harrison3110ff82008-05-02 13:42:44 -07005937 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005938
5939 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5940 * is not supported */
5941 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5942 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5943 else
5944 bitmask[0] |= FATTR4_WORD0_FILEID;
5945
Trond Myklebustc228fd32007-01-13 02:28:11 -05005946 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005947 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005948 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005949 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005950 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005951 return status;
5952}
5953
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005954int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5955 const struct qstr *name,
5956 struct nfs4_fs_locations *fs_locations,
5957 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005958{
5959 struct nfs4_exception exception = { };
5960 int err;
5961 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04005962 err = _nfs4_proc_fs_locations(client, dir, name,
5963 fs_locations, page);
5964 trace_nfs4_get_fs_locations(dir, name, err);
5965 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005966 &exception);
5967 } while (exception.retry);
5968 return err;
5969}
5970
Andy Adamson5ec16a82013-08-08 10:57:55 -04005971/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04005972 * If 'use_integrity' is true and the state managment nfs_client
5973 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
5974 * and the machine credential as per RFC3530bis and RFC5661 Security
5975 * Considerations sections. Otherwise, just use the user cred with the
5976 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04005977 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04005978static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005979{
5980 int status;
5981 struct nfs4_secinfo_arg args = {
5982 .dir_fh = NFS_FH(dir),
5983 .name = name,
5984 };
5985 struct nfs4_secinfo_res res = {
5986 .flavors = flavors,
5987 };
5988 struct rpc_message msg = {
5989 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5990 .rpc_argp = &args,
5991 .rpc_resp = &res,
5992 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04005993 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
5994
5995 if (use_integrity) {
5996 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
5997 msg.rpc_cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
5998 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005999
6000 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006001 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6002 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006003 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006004
6005 if (msg.rpc_cred)
6006 put_rpccred(msg.rpc_cred);
6007
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006008 return status;
6009}
6010
Bryan Schumaker72de53e2012-04-27 13:27:40 -04006011int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6012 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006013{
6014 struct nfs4_exception exception = { };
6015 int err;
6016 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006017 err = -NFS4ERR_WRONGSEC;
6018
6019 /* try to use integrity protection with machine cred */
6020 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6021 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6022
6023 /*
6024 * if unable to use integrity protection, or SECINFO with
6025 * integrity protection returns NFS4ERR_WRONGSEC (which is
6026 * disallowed by spec, but exists in deployed servers) use
6027 * the current filesystem's rpc_client and the user cred.
6028 */
6029 if (err == -NFS4ERR_WRONGSEC)
6030 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6031
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006032 trace_nfs4_secinfo(dir, name, err);
6033 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006034 &exception);
6035 } while (exception.retry);
6036 return err;
6037}
6038
Andy Adamson557134a2009-04-01 09:21:53 -04006039#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04006040/*
Andy Adamson357f54d2010-12-14 10:11:57 -05006041 * Check the exchange flags returned by the server for invalid flags, having
6042 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6043 * DS flags set.
6044 */
6045static int nfs4_check_cl_exchange_flags(u32 flags)
6046{
6047 if (flags & ~EXCHGID4_FLAG_MASK_R)
6048 goto out_inval;
6049 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6050 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6051 goto out_inval;
6052 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6053 goto out_inval;
6054 return NFS_OK;
6055out_inval:
6056 return -NFS4ERR_INVAL;
6057}
6058
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006059static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006060nfs41_same_server_scope(struct nfs41_server_scope *a,
6061 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006062{
6063 if (a->server_scope_sz == b->server_scope_sz &&
6064 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6065 return true;
6066
6067 return false;
6068}
6069
Andy Adamson357f54d2010-12-14 10:11:57 -05006070/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006071 * nfs4_proc_bind_conn_to_session()
6072 *
6073 * The 4.1 client currently uses the same TCP connection for the
6074 * fore and backchannel.
6075 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006076int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006077{
6078 int status;
6079 struct nfs41_bind_conn_to_session_res res;
6080 struct rpc_message msg = {
6081 .rpc_proc =
6082 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
6083 .rpc_argp = clp,
6084 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006085 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006086 };
6087
6088 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006089
6090 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
6091 if (unlikely(res.session == NULL)) {
6092 status = -ENOMEM;
6093 goto out;
6094 }
6095
6096 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006097 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006098 if (status == 0) {
6099 if (memcmp(res.session->sess_id.data,
6100 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6101 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6102 status = -EIO;
6103 goto out_session;
6104 }
6105 if (res.dir != NFS4_CDFS4_BOTH) {
6106 dprintk("NFS: %s: Unexpected direction from server\n",
6107 __func__);
6108 status = -EIO;
6109 goto out_session;
6110 }
6111 if (res.use_conn_in_rdma_mode) {
6112 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6113 __func__);
6114 status = -EIO;
6115 goto out_session;
6116 }
6117 }
6118out_session:
6119 kfree(res.session);
6120out:
6121 dprintk("<-- %s status= %d\n", __func__, status);
6122 return status;
6123}
6124
6125/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04006126 * nfs4_proc_exchange_id()
6127 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006128 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6129 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04006130 * Since the clientid has expired, all compounds using sessions
6131 * associated with the stale clientid will be returning
6132 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
6133 * be in some phase of session reset.
6134 */
Andy Adamson4d643d12009-12-04 15:52:24 -05006135int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04006136{
6137 nfs4_verifier verifier;
6138 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05006139 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04006140 .client = clp,
Trond Myklebust4f0b4292013-05-20 12:24:03 -04006141 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6142 EXCHGID4_FLAG_BIND_PRINC_STATEID,
Benny Halevy99fe60d2009-04-01 09:22:29 -04006143 };
6144 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04006145 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04006146 };
6147 int status;
6148 struct rpc_message msg = {
6149 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
6150 .rpc_argp = &args,
6151 .rpc_resp = &res,
6152 .rpc_cred = cred,
6153 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04006154
Chuck Leverf0920752012-05-21 22:45:41 -04006155 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04006156 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
6157 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006158 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
6159 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6160 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006161
Chuck Leveracdeb692012-05-21 22:46:16 -04006162 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006163 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04006164 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006165 status = -ENOMEM;
6166 goto out;
6167 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006168
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006169 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006170 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006171 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04006172 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04006173 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04006174 }
6175
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006176 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006177 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006178 status = -ENOMEM;
6179 goto out_server_scope;
6180 }
6181
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006182 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006183 trace_nfs4_exchange_id(clp, status);
Chuck Lever177313f2012-05-21 22:44:58 -04006184 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04006185 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006186
Chuck Lever177313f2012-05-21 22:44:58 -04006187 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04006188 clp->cl_clientid = res.clientid;
6189 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
6190 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
6191 clp->cl_seqid = res.seqid;
6192
Chuck Leveracdeb692012-05-21 22:46:16 -04006193 kfree(clp->cl_serverowner);
6194 clp->cl_serverowner = res.server_owner;
6195 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04006196
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006197 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04006198 kfree(clp->cl_implid);
6199 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006200
Chuck Lever177313f2012-05-21 22:44:58 -04006201 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006202 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006203 res.server_scope)) {
6204 dprintk("%s: server_scope mismatch detected\n",
6205 __func__);
6206 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006207 kfree(clp->cl_serverscope);
6208 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006209 }
6210
Chuck Lever177313f2012-05-21 22:44:58 -04006211 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006212 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006213 goto out;
6214 }
Trond Myklebust32b01312012-05-26 13:41:04 -04006215 } else
6216 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006217
Chuck Leveracdeb692012-05-21 22:46:16 -04006218out_server_owner:
6219 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006220out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006221 kfree(res.server_scope);
6222out:
Chuck Lever177313f2012-05-21 22:44:58 -04006223 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006224 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006225 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006226 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04006227 clp->cl_implid->date.seconds,
6228 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006229 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006230 return status;
6231}
6232
Trond Myklebust66245532012-05-25 17:18:09 -04006233static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
6234 struct rpc_cred *cred)
6235{
6236 struct rpc_message msg = {
6237 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
6238 .rpc_argp = clp,
6239 .rpc_cred = cred,
6240 };
6241 int status;
6242
6243 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006244 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04006245 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04006246 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04006247 "DESTROY_CLIENTID.", status, clp->cl_hostname);
6248 return status;
6249}
6250
6251static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
6252 struct rpc_cred *cred)
6253{
6254 unsigned int loop;
6255 int ret;
6256
6257 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
6258 ret = _nfs4_proc_destroy_clientid(clp, cred);
6259 switch (ret) {
6260 case -NFS4ERR_DELAY:
6261 case -NFS4ERR_CLIENTID_BUSY:
6262 ssleep(1);
6263 break;
6264 default:
6265 return ret;
6266 }
6267 }
6268 return 0;
6269}
6270
6271int nfs4_destroy_clientid(struct nfs_client *clp)
6272{
6273 struct rpc_cred *cred;
6274 int ret = 0;
6275
6276 if (clp->cl_mvops->minor_version < 1)
6277 goto out;
6278 if (clp->cl_exchange_flags == 0)
6279 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04006280 if (clp->cl_preserve_clid)
6281 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04006282 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04006283 ret = nfs4_proc_destroy_clientid(clp, cred);
6284 if (cred)
6285 put_rpccred(cred);
6286 switch (ret) {
6287 case 0:
6288 case -NFS4ERR_STALE_CLIENTID:
6289 clp->cl_exchange_flags = 0;
6290 }
6291out:
6292 return ret;
6293}
6294
Andy Adamson2050f0c2009-04-01 09:22:30 -04006295struct nfs4_get_lease_time_data {
6296 struct nfs4_get_lease_time_args *args;
6297 struct nfs4_get_lease_time_res *res;
6298 struct nfs_client *clp;
6299};
6300
6301static void nfs4_get_lease_time_prepare(struct rpc_task *task,
6302 void *calldata)
6303{
Andy Adamson2050f0c2009-04-01 09:22:30 -04006304 struct nfs4_get_lease_time_data *data =
6305 (struct nfs4_get_lease_time_data *)calldata;
6306
6307 dprintk("--> %s\n", __func__);
6308 /* just setup sequence, do not trigger session recovery
6309 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006310 nfs41_setup_sequence(data->clp->cl_session,
6311 &data->args->la_seq_args,
6312 &data->res->lr_seq_res,
6313 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006314 dprintk("<-- %s\n", __func__);
6315}
6316
6317/*
6318 * Called from nfs4_state_manager thread for session setup, so don't recover
6319 * from sequence operation or clientid errors.
6320 */
6321static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
6322{
6323 struct nfs4_get_lease_time_data *data =
6324 (struct nfs4_get_lease_time_data *)calldata;
6325
6326 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006327 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
6328 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04006329 switch (task->tk_status) {
6330 case -NFS4ERR_DELAY:
6331 case -NFS4ERR_GRACE:
6332 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
6333 rpc_delay(task, NFS4_POLL_RETRY_MIN);
6334 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006335 /* fall through */
6336 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006337 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006338 return;
6339 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04006340 dprintk("<-- %s\n", __func__);
6341}
6342
Trond Myklebust17280172012-03-11 13:11:00 -04006343static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04006344 .rpc_call_prepare = nfs4_get_lease_time_prepare,
6345 .rpc_call_done = nfs4_get_lease_time_done,
6346};
6347
6348int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
6349{
6350 struct rpc_task *task;
6351 struct nfs4_get_lease_time_args args;
6352 struct nfs4_get_lease_time_res res = {
6353 .lr_fsinfo = fsinfo,
6354 };
6355 struct nfs4_get_lease_time_data data = {
6356 .args = &args,
6357 .res = &res,
6358 .clp = clp,
6359 };
6360 struct rpc_message msg = {
6361 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
6362 .rpc_argp = &args,
6363 .rpc_resp = &res,
6364 };
6365 struct rpc_task_setup task_setup = {
6366 .rpc_client = clp->cl_rpcclient,
6367 .rpc_message = &msg,
6368 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006369 .callback_data = &data,
6370 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04006371 };
6372 int status;
6373
Chuck Levera9c92d62013-08-09 12:48:18 -04006374 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006375 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006376 dprintk("--> %s\n", __func__);
6377 task = rpc_run_task(&task_setup);
6378
6379 if (IS_ERR(task))
6380 status = PTR_ERR(task);
6381 else {
6382 status = task->tk_status;
6383 rpc_put_task(task);
6384 }
6385 dprintk("<-- %s return %d\n", __func__, status);
6386
6387 return status;
6388}
6389
Andy Adamsonfc931582009-04-01 09:22:31 -04006390/*
6391 * Initialize the values to be used by the client in CREATE_SESSION
6392 * If nfs4_init_session set the fore channel request and response sizes,
6393 * use them.
6394 *
6395 * Set the back channel max_resp_sz_cached to zero to force the client to
6396 * always set csa_cachethis to FALSE because the current implementation
6397 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
6398 */
6399static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
6400{
Andy Adamson18aad3d2013-06-26 12:21:49 -04006401 unsigned int max_rqst_sz, max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006402
Andy Adamson18aad3d2013-06-26 12:21:49 -04006403 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
6404 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
6405
Andy Adamsonfc931582009-04-01 09:22:31 -04006406 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04006407 args->fc_attrs.max_rqst_sz = max_rqst_sz;
6408 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006409 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05006410 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04006411
6412 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05006413 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04006414 __func__,
6415 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05006416 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04006417
6418 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04006419 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
6420 args->bc_attrs.max_resp_sz = PAGE_SIZE;
6421 args->bc_attrs.max_resp_sz_cached = 0;
6422 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
6423 args->bc_attrs.max_reqs = 1;
6424
6425 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
6426 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
6427 __func__,
6428 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
6429 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
6430 args->bc_attrs.max_reqs);
6431}
6432
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006433static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04006434{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006435 struct nfs4_channel_attrs *sent = &args->fc_attrs;
6436 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
6437
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006438 if (rcvd->max_resp_sz > sent->max_resp_sz)
6439 return -EINVAL;
6440 /*
6441 * Our requested max_ops is the minimum we need; we're not
6442 * prepared to break up compounds into smaller pieces than that.
6443 * So, no point even trying to continue if the server won't
6444 * cooperate:
6445 */
6446 if (rcvd->max_ops < sent->max_ops)
6447 return -EINVAL;
6448 if (rcvd->max_reqs == 0)
6449 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006450 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
6451 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006452 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04006453}
6454
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006455static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
6456{
6457 struct nfs4_channel_attrs *sent = &args->bc_attrs;
6458 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04006459
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006460 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
6461 return -EINVAL;
6462 if (rcvd->max_resp_sz < sent->max_resp_sz)
6463 return -EINVAL;
6464 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
6465 return -EINVAL;
6466 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006467 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006468 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006469 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006470 return -EINVAL;
6471 return 0;
6472}
Andy Adamson8d353012009-04-01 09:22:32 -04006473
Andy Adamson8d353012009-04-01 09:22:32 -04006474static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
6475 struct nfs4_session *session)
6476{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006477 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04006478
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006479 ret = nfs4_verify_fore_channel_attrs(args, session);
6480 if (ret)
6481 return ret;
6482 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04006483}
6484
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006485static int _nfs4_proc_create_session(struct nfs_client *clp,
6486 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04006487{
6488 struct nfs4_session *session = clp->cl_session;
6489 struct nfs41_create_session_args args = {
6490 .client = clp,
6491 .cb_program = NFS4_CALLBACK,
6492 };
6493 struct nfs41_create_session_res res = {
6494 .client = clp,
6495 };
6496 struct rpc_message msg = {
6497 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
6498 .rpc_argp = &args,
6499 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006500 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04006501 };
6502 int status;
6503
6504 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04006505 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04006506
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006507 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006508 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04006509
Trond Myklebust43095d32012-11-20 11:13:12 -05006510 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04006511 /* Verify the session's negotiated channel_attrs values */
6512 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04006513 /* Increment the clientid slot sequence id */
6514 clp->cl_seqid++;
6515 }
6516
6517 return status;
6518}
6519
6520/*
6521 * Issues a CREATE_SESSION operation to the server.
6522 * It is the responsibility of the caller to verify the session is
6523 * expired before calling this routine.
6524 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006525int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04006526{
6527 int status;
6528 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04006529 struct nfs4_session *session = clp->cl_session;
6530
6531 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
6532
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006533 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04006534 if (status)
6535 goto out;
6536
Andy Adamsonaacd5532011-11-09 13:58:21 -05006537 /* Init or reset the session slot tables */
6538 status = nfs4_setup_session_slot_tables(session);
6539 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04006540 if (status)
6541 goto out;
6542
6543 ptr = (unsigned *)&session->sess_id.data[0];
6544 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
6545 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04006546out:
6547 dprintk("<-- %s\n", __func__);
6548 return status;
6549}
6550
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006551/*
6552 * Issue the over-the-wire RPC DESTROY_SESSION.
6553 * The caller must serialize access to this routine.
6554 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006555int nfs4_proc_destroy_session(struct nfs4_session *session,
6556 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006557{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006558 struct rpc_message msg = {
6559 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
6560 .rpc_argp = session,
6561 .rpc_cred = cred,
6562 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006563 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006564
6565 dprintk("--> nfs4_proc_destroy_session\n");
6566
6567 /* session is still being setup */
6568 if (session->clp->cl_cons_state != NFS_CS_READY)
6569 return status;
6570
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006571 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006572 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006573
6574 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04006575 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006576 "Session has been destroyed regardless...\n", status);
6577
6578 dprintk("<-- nfs4_proc_destroy_session\n");
6579 return status;
6580}
6581
Trond Myklebust7b38c362012-05-23 13:23:31 -04006582/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006583 * Renew the cl_session lease.
6584 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006585struct nfs4_sequence_data {
6586 struct nfs_client *clp;
6587 struct nfs4_sequence_args args;
6588 struct nfs4_sequence_res res;
6589};
6590
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006591static void nfs41_sequence_release(void *data)
6592{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006593 struct nfs4_sequence_data *calldata = data;
6594 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006595
Alexandros Batsakis71358402010-02-05 03:45:05 -08006596 if (atomic_read(&clp->cl_count) > 1)
6597 nfs4_schedule_state_renewal(clp);
6598 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006599 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006600}
6601
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006602static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6603{
6604 switch(task->tk_status) {
6605 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006606 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6607 return -EAGAIN;
6608 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006609 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006610 }
6611 return 0;
6612}
6613
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006614static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006615{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006616 struct nfs4_sequence_data *calldata = data;
6617 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006618
Trond Myklebust14516c32010-07-31 14:29:06 -04006619 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6620 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006621
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006622 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006623 if (task->tk_status < 0) {
6624 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006625 if (atomic_read(&clp->cl_count) == 1)
6626 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006627
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006628 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6629 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006630 return;
6631 }
6632 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006633 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006634out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006635 dprintk("<-- %s\n", __func__);
6636}
6637
6638static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6639{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006640 struct nfs4_sequence_data *calldata = data;
6641 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006642 struct nfs4_sequence_args *args;
6643 struct nfs4_sequence_res *res;
6644
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006645 args = task->tk_msg.rpc_argp;
6646 res = task->tk_msg.rpc_resp;
6647
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006648 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006649}
6650
6651static const struct rpc_call_ops nfs41_sequence_ops = {
6652 .rpc_call_done = nfs41_sequence_call_done,
6653 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006654 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006655};
6656
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006657static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
6658 struct rpc_cred *cred,
6659 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006660{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006661 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006662 struct rpc_message msg = {
6663 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6664 .rpc_cred = cred,
6665 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006666 struct rpc_task_setup task_setup_data = {
6667 .rpc_client = clp->cl_rpcclient,
6668 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006669 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04006670 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006671 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006672
Alexandros Batsakis71358402010-02-05 03:45:05 -08006673 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006674 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006675 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006676 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006677 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006678 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006679 }
Chuck Levera9c92d62013-08-09 12:48:18 -04006680 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006681 if (is_privileged)
6682 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006683 msg.rpc_argp = &calldata->args;
6684 msg.rpc_resp = &calldata->res;
6685 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006686 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006687
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006688 return rpc_run_task(&task_setup_data);
6689}
6690
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006691static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006692{
6693 struct rpc_task *task;
6694 int ret = 0;
6695
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006696 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6697 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006698 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006699 if (IS_ERR(task))
6700 ret = PTR_ERR(task);
6701 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006702 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006703 dprintk("<-- %s status=%d\n", __func__, ret);
6704 return ret;
6705}
6706
6707static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6708{
6709 struct rpc_task *task;
6710 int ret;
6711
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006712 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006713 if (IS_ERR(task)) {
6714 ret = PTR_ERR(task);
6715 goto out;
6716 }
6717 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006718 if (!ret) {
6719 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6720
6721 if (task->tk_status == 0)
6722 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006723 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006724 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006725 rpc_put_task(task);
6726out:
6727 dprintk("<-- %s status=%d\n", __func__, ret);
6728 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006729}
6730
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006731struct nfs4_reclaim_complete_data {
6732 struct nfs_client *clp;
6733 struct nfs41_reclaim_complete_args arg;
6734 struct nfs41_reclaim_complete_res res;
6735};
6736
6737static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6738{
6739 struct nfs4_reclaim_complete_data *calldata = data;
6740
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006741 nfs41_setup_sequence(calldata->clp->cl_session,
6742 &calldata->arg.seq_args,
6743 &calldata->res.seq_res,
6744 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006745}
6746
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006747static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6748{
6749 switch(task->tk_status) {
6750 case 0:
6751 case -NFS4ERR_COMPLETE_ALREADY:
6752 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6753 break;
6754 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006755 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006756 /* fall through */
6757 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006758 return -EAGAIN;
6759 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006760 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006761 }
6762 return 0;
6763}
6764
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006765static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6766{
6767 struct nfs4_reclaim_complete_data *calldata = data;
6768 struct nfs_client *clp = calldata->clp;
6769 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6770
6771 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006772 if (!nfs41_sequence_done(task, res))
6773 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006774
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006775 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006776 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6777 rpc_restart_call_prepare(task);
6778 return;
6779 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006780 dprintk("<-- %s\n", __func__);
6781}
6782
6783static void nfs4_free_reclaim_complete_data(void *data)
6784{
6785 struct nfs4_reclaim_complete_data *calldata = data;
6786
6787 kfree(calldata);
6788}
6789
6790static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6791 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6792 .rpc_call_done = nfs4_reclaim_complete_done,
6793 .rpc_release = nfs4_free_reclaim_complete_data,
6794};
6795
6796/*
6797 * Issue a global reclaim complete.
6798 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04006799static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
6800 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006801{
6802 struct nfs4_reclaim_complete_data *calldata;
6803 struct rpc_task *task;
6804 struct rpc_message msg = {
6805 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04006806 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006807 };
6808 struct rpc_task_setup task_setup_data = {
6809 .rpc_client = clp->cl_rpcclient,
6810 .rpc_message = &msg,
6811 .callback_ops = &nfs4_reclaim_complete_call_ops,
6812 .flags = RPC_TASK_ASYNC,
6813 };
6814 int status = -ENOMEM;
6815
6816 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006817 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006818 if (calldata == NULL)
6819 goto out;
6820 calldata->clp = clp;
6821 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006822
Chuck Levera9c92d62013-08-09 12:48:18 -04006823 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006824 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006825 msg.rpc_argp = &calldata->arg;
6826 msg.rpc_resp = &calldata->res;
6827 task_setup_data.callback_data = calldata;
6828 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006829 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006830 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006831 goto out;
6832 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006833 status = nfs4_wait_for_completion_rpc_task(task);
6834 if (status == 0)
6835 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006836 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006837 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006838out:
6839 dprintk("<-- %s status=%d\n", __func__, status);
6840 return status;
6841}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006842
6843static void
6844nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6845{
6846 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006847 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006848 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006849
6850 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006851 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6852 * right now covering the LAYOUTGET we are about to send.
6853 * However, that is not so catastrophic, and there seems
6854 * to be no way to prevent it completely.
6855 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006856 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006857 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006858 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006859 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6860 NFS_I(lgp->args.inode)->layout,
6861 lgp->args.ctx->state)) {
6862 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00006863 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006864}
6865
6866static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6867{
6868 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006869 struct inode *inode = lgp->args.inode;
6870 struct nfs_server *server = NFS_SERVER(inode);
6871 struct pnfs_layout_hdr *lo;
6872 struct nfs4_state *state = NULL;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006873 unsigned long timeo, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006874
6875 dprintk("--> %s\n", __func__);
6876
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006877 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006878 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006879
6880 switch (task->tk_status) {
6881 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006882 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006883 case -NFS4ERR_LAYOUTTRYLATER:
6884 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05006885 timeo = rpc_get_timeout(task->tk_client);
6886 giveup = lgp->args.timestamp + timeo;
6887 if (time_after(giveup, jiffies))
6888 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006889 break;
6890 case -NFS4ERR_EXPIRED:
6891 case -NFS4ERR_BAD_STATEID:
6892 spin_lock(&inode->i_lock);
6893 lo = NFS_I(inode)->layout;
6894 if (!lo || list_empty(&lo->plh_segs)) {
6895 spin_unlock(&inode->i_lock);
6896 /* If the open stateid was bad, then recover it. */
6897 state = lgp->args.ctx->state;
6898 } else {
6899 LIST_HEAD(head);
6900
6901 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6902 spin_unlock(&inode->i_lock);
6903 /* Mark the bad layout state as invalid, then
6904 * retry using the open stateid. */
6905 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006906 }
6907 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006908 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6909 rpc_restart_call_prepare(task);
6910out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006911 dprintk("<-- %s\n", __func__);
6912}
6913
Idan Kedar85541162012-08-02 11:47:10 +03006914static size_t max_response_pages(struct nfs_server *server)
6915{
6916 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6917 return nfs_page_array_len(0, max_resp_sz);
6918}
6919
6920static void nfs4_free_pages(struct page **pages, size_t size)
6921{
6922 int i;
6923
6924 if (!pages)
6925 return;
6926
6927 for (i = 0; i < size; i++) {
6928 if (!pages[i])
6929 break;
6930 __free_page(pages[i]);
6931 }
6932 kfree(pages);
6933}
6934
6935static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6936{
6937 struct page **pages;
6938 int i;
6939
6940 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6941 if (!pages) {
6942 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6943 return NULL;
6944 }
6945
6946 for (i = 0; i < size; i++) {
6947 pages[i] = alloc_page(gfp_flags);
6948 if (!pages[i]) {
6949 dprintk("%s: failed to allocate page\n", __func__);
6950 nfs4_free_pages(pages, size);
6951 return NULL;
6952 }
6953 }
6954
6955 return pages;
6956}
6957
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006958static void nfs4_layoutget_release(void *calldata)
6959{
6960 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006961 struct inode *inode = lgp->args.inode;
6962 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006963 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006964
6965 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006966 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006967 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006968 put_nfs_open_context(lgp->args.ctx);
6969 kfree(calldata);
6970 dprintk("<-- %s\n", __func__);
6971}
6972
6973static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6974 .rpc_call_prepare = nfs4_layoutget_prepare,
6975 .rpc_call_done = nfs4_layoutget_done,
6976 .rpc_release = nfs4_layoutget_release,
6977};
6978
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006979struct pnfs_layout_segment *
6980nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006981{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006982 struct inode *inode = lgp->args.inode;
6983 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006984 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006985 struct rpc_task *task;
6986 struct rpc_message msg = {
6987 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6988 .rpc_argp = &lgp->args,
6989 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04006990 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006991 };
6992 struct rpc_task_setup task_setup_data = {
6993 .rpc_client = server->client,
6994 .rpc_message = &msg,
6995 .callback_ops = &nfs4_layoutget_call_ops,
6996 .callback_data = lgp,
6997 .flags = RPC_TASK_ASYNC,
6998 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006999 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007000 int status = 0;
7001
7002 dprintk("--> %s\n", __func__);
7003
Idan Kedar85541162012-08-02 11:47:10 +03007004 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
7005 if (!lgp->args.layout.pages) {
7006 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007007 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03007008 }
7009 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05007010 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03007011
Weston Andros Adamson35124a02011-03-24 16:48:21 -04007012 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007013 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04007014 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007015
7016 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
7017 pnfs_get_layout_hdr(NFS_I(inode)->layout);
7018
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007019 task = rpc_run_task(&task_setup_data);
7020 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007021 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007022 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00007023 if (status == 0)
7024 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007025 trace_nfs4_layoutget(lgp->args.ctx,
7026 &lgp->args.range,
7027 &lgp->res.range,
7028 status);
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05007029 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
7030 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007031 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007032 rpc_put_task(task);
7033 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007034 if (status)
7035 return ERR_PTR(status);
7036 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007037}
7038
Benny Halevycbe82602011-05-22 19:52:37 +03007039static void
7040nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
7041{
7042 struct nfs4_layoutreturn *lrp = calldata;
7043
7044 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007045 nfs41_setup_sequence(lrp->clp->cl_session,
7046 &lrp->args.seq_args,
7047 &lrp->res.seq_res,
7048 task);
Benny Halevycbe82602011-05-22 19:52:37 +03007049}
7050
7051static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
7052{
7053 struct nfs4_layoutreturn *lrp = calldata;
7054 struct nfs_server *server;
7055
7056 dprintk("--> %s\n", __func__);
7057
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007058 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03007059 return;
7060
7061 server = NFS_SERVER(lrp->args.inode);
7062 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007063 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03007064 return;
7065 }
Benny Halevycbe82602011-05-22 19:52:37 +03007066 dprintk("<-- %s\n", __func__);
7067}
7068
7069static void nfs4_layoutreturn_release(void *calldata)
7070{
7071 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04007072 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03007073
7074 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04007075 spin_lock(&lo->plh_inode->i_lock);
7076 if (lrp->res.lrs_present)
7077 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
7078 lo->plh_block_lgets--;
7079 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04007080 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03007081 kfree(calldata);
7082 dprintk("<-- %s\n", __func__);
7083}
7084
7085static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
7086 .rpc_call_prepare = nfs4_layoutreturn_prepare,
7087 .rpc_call_done = nfs4_layoutreturn_done,
7088 .rpc_release = nfs4_layoutreturn_release,
7089};
7090
7091int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
7092{
7093 struct rpc_task *task;
7094 struct rpc_message msg = {
7095 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
7096 .rpc_argp = &lrp->args,
7097 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04007098 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03007099 };
7100 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04007101 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03007102 .rpc_message = &msg,
7103 .callback_ops = &nfs4_layoutreturn_call_ops,
7104 .callback_data = lrp,
7105 };
7106 int status;
7107
7108 dprintk("--> %s\n", __func__);
Chuck Levera9c92d62013-08-09 12:48:18 -04007109 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03007110 task = rpc_run_task(&task_setup_data);
7111 if (IS_ERR(task))
7112 return PTR_ERR(task);
7113 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007114 trace_nfs4_layoutreturn(lrp->args.inode, status);
Benny Halevycbe82602011-05-22 19:52:37 +03007115 dprintk("<-- %s status=%d\n", __func__, status);
7116 rpc_put_task(task);
7117 return status;
7118}
7119
Andy Adamson7f11d8d2011-07-30 20:52:35 -04007120/*
7121 * Retrieve the list of Data Server devices from the MDS.
7122 */
7123static int _nfs4_getdevicelist(struct nfs_server *server,
7124 const struct nfs_fh *fh,
7125 struct pnfs_devicelist *devlist)
7126{
7127 struct nfs4_getdevicelist_args args = {
7128 .fh = fh,
7129 .layoutclass = server->pnfs_curr_ld->id,
7130 };
7131 struct nfs4_getdevicelist_res res = {
7132 .devlist = devlist,
7133 };
7134 struct rpc_message msg = {
7135 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
7136 .rpc_argp = &args,
7137 .rpc_resp = &res,
7138 };
7139 int status;
7140
7141 dprintk("--> %s\n", __func__);
7142 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
7143 &res.seq_res, 0);
7144 dprintk("<-- %s status=%d\n", __func__, status);
7145 return status;
7146}
7147
7148int nfs4_proc_getdevicelist(struct nfs_server *server,
7149 const struct nfs_fh *fh,
7150 struct pnfs_devicelist *devlist)
7151{
7152 struct nfs4_exception exception = { };
7153 int err;
7154
7155 do {
7156 err = nfs4_handle_exception(server,
7157 _nfs4_getdevicelist(server, fh, devlist),
7158 &exception);
7159 } while (exception.retry);
7160
7161 dprintk("%s: err=%d, num_devs=%u\n", __func__,
7162 err, devlist->num_devs);
7163
7164 return err;
7165}
7166EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
7167
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007168static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007169_nfs4_proc_getdeviceinfo(struct nfs_server *server,
7170 struct pnfs_device *pdev,
7171 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007172{
7173 struct nfs4_getdeviceinfo_args args = {
7174 .pdev = pdev,
7175 };
7176 struct nfs4_getdeviceinfo_res res = {
7177 .pdev = pdev,
7178 };
7179 struct rpc_message msg = {
7180 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
7181 .rpc_argp = &args,
7182 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007183 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007184 };
7185 int status;
7186
7187 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00007188 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007189 dprintk("<-- %s status=%d\n", __func__, status);
7190
7191 return status;
7192}
7193
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007194int nfs4_proc_getdeviceinfo(struct nfs_server *server,
7195 struct pnfs_device *pdev,
7196 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007197{
7198 struct nfs4_exception exception = { };
7199 int err;
7200
7201 do {
7202 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007203 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007204 &exception);
7205 } while (exception.retry);
7206 return err;
7207}
7208EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
7209
Andy Adamson863a3c62011-03-23 13:27:54 +00007210static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
7211{
7212 struct nfs4_layoutcommit_data *data = calldata;
7213 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007214 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00007215
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007216 nfs41_setup_sequence(session,
7217 &data->args.seq_args,
7218 &data->res.seq_res,
7219 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00007220}
7221
7222static void
7223nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
7224{
7225 struct nfs4_layoutcommit_data *data = calldata;
7226 struct nfs_server *server = NFS_SERVER(data->args.inode);
7227
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007228 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00007229 return;
7230
7231 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007232 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
7233 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
7234 case -NFS4ERR_BADLAYOUT: /* no layout */
7235 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00007236 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007237 break;
7238 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00007239 nfs_post_op_update_inode_force_wcc(data->args.inode,
7240 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007241 break;
7242 default:
7243 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
7244 rpc_restart_call_prepare(task);
7245 return;
7246 }
7247 }
Andy Adamson863a3c62011-03-23 13:27:54 +00007248}
7249
7250static void nfs4_layoutcommit_release(void *calldata)
7251{
7252 struct nfs4_layoutcommit_data *data = calldata;
7253
Andy Adamsondb29c082011-07-30 20:52:38 -04007254 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00007255 put_rpccred(data->cred);
7256 kfree(data);
7257}
7258
7259static const struct rpc_call_ops nfs4_layoutcommit_ops = {
7260 .rpc_call_prepare = nfs4_layoutcommit_prepare,
7261 .rpc_call_done = nfs4_layoutcommit_done,
7262 .rpc_release = nfs4_layoutcommit_release,
7263};
7264
7265int
Andy Adamsonef311532011-03-12 02:58:10 -05007266nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00007267{
7268 struct rpc_message msg = {
7269 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
7270 .rpc_argp = &data->args,
7271 .rpc_resp = &data->res,
7272 .rpc_cred = data->cred,
7273 };
7274 struct rpc_task_setup task_setup_data = {
7275 .task = &data->task,
7276 .rpc_client = NFS_CLIENT(data->args.inode),
7277 .rpc_message = &msg,
7278 .callback_ops = &nfs4_layoutcommit_ops,
7279 .callback_data = data,
7280 .flags = RPC_TASK_ASYNC,
7281 };
7282 struct rpc_task *task;
7283 int status = 0;
7284
7285 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
7286 "lbw: %llu inode %lu\n",
7287 data->task.tk_pid, sync,
7288 data->args.lastbytewritten,
7289 data->args.inode->i_ino);
7290
Chuck Levera9c92d62013-08-09 12:48:18 -04007291 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00007292 task = rpc_run_task(&task_setup_data);
7293 if (IS_ERR(task))
7294 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05007295 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00007296 goto out;
7297 status = nfs4_wait_for_completion_rpc_task(task);
7298 if (status != 0)
7299 goto out;
7300 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007301 trace_nfs4_layoutcommit(data->args.inode, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00007302out:
7303 dprintk("%s: status %d\n", __func__, status);
7304 rpc_put_task(task);
7305 return status;
7306}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007307
Andy Adamson97431202013-08-08 10:57:56 -04007308/**
7309 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
7310 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
7311 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007312static int
7313_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7314 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
7315{
7316 struct nfs41_secinfo_no_name_args args = {
7317 .style = SECINFO_STYLE_CURRENT_FH,
7318 };
7319 struct nfs4_secinfo_res res = {
7320 .flavors = flavors,
7321 };
7322 struct rpc_message msg = {
7323 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
7324 .rpc_argp = &args,
7325 .rpc_resp = &res,
7326 };
Andy Adamson97431202013-08-08 10:57:56 -04007327 return nfs4_call_sync(server->nfs_client->cl_rpcclient, server, &msg,
7328 &args.seq_args, &res.seq_res, 0);
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007329}
7330
7331static int
7332nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7333 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
7334{
7335 struct nfs4_exception exception = { };
7336 int err;
7337 do {
7338 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
7339 switch (err) {
7340 case 0:
7341 case -NFS4ERR_WRONGSEC:
7342 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007343 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007344 default:
7345 err = nfs4_handle_exception(server, err, &exception);
7346 }
7347 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007348out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007349 return err;
7350}
7351
7352static int
7353nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
7354 struct nfs_fsinfo *info)
7355{
7356 int err;
7357 struct page *page;
7358 rpc_authflavor_t flavor;
7359 struct nfs4_secinfo_flavors *flavors;
7360
7361 page = alloc_page(GFP_KERNEL);
7362 if (!page) {
7363 err = -ENOMEM;
7364 goto out;
7365 }
7366
7367 flavors = page_address(page);
7368 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
7369
7370 /*
7371 * Fall back on "guess and check" method if
7372 * the server doesn't support SECINFO_NO_NAME
7373 */
7374 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
7375 err = nfs4_find_root_sec(server, fhandle, info);
7376 goto out_freepage;
7377 }
7378 if (err)
7379 goto out_freepage;
7380
7381 flavor = nfs_find_best_sec(flavors);
7382 if (err == 0)
7383 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
7384
7385out_freepage:
7386 put_page(page);
7387 if (err == -EACCES)
7388 return -EPERM;
7389out:
7390 return err;
7391}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007392
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007393static int _nfs41_test_stateid(struct nfs_server *server,
7394 nfs4_stateid *stateid,
7395 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04007396{
7397 int status;
7398 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007399 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04007400 };
7401 struct nfs41_test_stateid_res res;
7402 struct rpc_message msg = {
7403 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
7404 .rpc_argp = &args,
7405 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007406 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04007407 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007408
Chuck Lever38527b12012-07-11 16:30:23 -04007409 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04007410 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007411 nfs4_set_sequence_privileged(&args.seq_args);
7412 status = nfs4_call_sync_sequence(server->client, server, &msg,
7413 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04007414 if (status != NFS_OK) {
7415 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04007416 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04007417 }
7418 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04007419 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04007420}
7421
Chuck Lever38527b12012-07-11 16:30:23 -04007422/**
7423 * nfs41_test_stateid - perform a TEST_STATEID operation
7424 *
7425 * @server: server / transport on which to perform the operation
7426 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007427 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04007428 *
7429 * Returns NFS_OK if the server recognizes that "stateid" is valid.
7430 * Otherwise a negative NFS4ERR value is returned if the operation
7431 * failed or the state ID is not currently valid.
7432 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007433static int nfs41_test_stateid(struct nfs_server *server,
7434 nfs4_stateid *stateid,
7435 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04007436{
7437 struct nfs4_exception exception = { };
7438 int err;
7439 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007440 err = _nfs41_test_stateid(server, stateid, cred);
Chuck Lever377e5072012-07-11 16:29:45 -04007441 if (err != -NFS4ERR_DELAY)
7442 break;
7443 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04007444 } while (exception.retry);
7445 return err;
7446}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007447
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007448struct nfs_free_stateid_data {
7449 struct nfs_server *server;
7450 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007451 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007452};
7453
7454static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
7455{
7456 struct nfs_free_stateid_data *data = calldata;
7457 nfs41_setup_sequence(nfs4_get_session(data->server),
7458 &data->args.seq_args,
7459 &data->res.seq_res,
7460 task);
7461}
7462
7463static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
7464{
7465 struct nfs_free_stateid_data *data = calldata;
7466
7467 nfs41_sequence_done(task, &data->res.seq_res);
7468
7469 switch (task->tk_status) {
7470 case -NFS4ERR_DELAY:
7471 if (nfs4_async_handle_error(task, data->server, NULL) == -EAGAIN)
7472 rpc_restart_call_prepare(task);
7473 }
7474}
7475
7476static void nfs41_free_stateid_release(void *calldata)
7477{
7478 kfree(calldata);
7479}
7480
Trond Myklebust17f26b12013-08-21 15:48:42 -04007481static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007482 .rpc_call_prepare = nfs41_free_stateid_prepare,
7483 .rpc_call_done = nfs41_free_stateid_done,
7484 .rpc_release = nfs41_free_stateid_release,
7485};
7486
7487static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
7488 nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007489 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007490 bool privileged)
7491{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007492 struct rpc_message msg = {
7493 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007494 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007495 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007496 struct rpc_task_setup task_setup = {
7497 .rpc_client = server->client,
7498 .rpc_message = &msg,
7499 .callback_ops = &nfs41_free_stateid_ops,
7500 .flags = RPC_TASK_ASYNC,
7501 };
7502 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007503
Chuck Lever38527b12012-07-11 16:30:23 -04007504 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007505 data = kmalloc(sizeof(*data), GFP_NOFS);
7506 if (!data)
7507 return ERR_PTR(-ENOMEM);
7508 data->server = server;
7509 nfs4_stateid_copy(&data->args.stateid, stateid);
7510
7511 task_setup.callback_data = data;
7512
7513 msg.rpc_argp = &data->args;
7514 msg.rpc_resp = &data->res;
Chuck Levera9c92d62013-08-09 12:48:18 -04007515 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007516 if (privileged)
7517 nfs4_set_sequence_privileged(&data->args.seq_args);
7518
7519 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007520}
7521
Chuck Lever38527b12012-07-11 16:30:23 -04007522/**
7523 * nfs41_free_stateid - perform a FREE_STATEID operation
7524 *
7525 * @server: server / transport on which to perform the operation
7526 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007527 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04007528 *
7529 * Returns NFS_OK if the server freed "stateid". Otherwise a
7530 * negative NFS4ERR value is returned.
7531 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007532static int nfs41_free_stateid(struct nfs_server *server,
7533 nfs4_stateid *stateid,
7534 struct rpc_cred *cred)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007535{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007536 struct rpc_task *task;
7537 int ret;
7538
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007539 task = _nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007540 if (IS_ERR(task))
7541 return PTR_ERR(task);
7542 ret = rpc_wait_for_completion_task(task);
7543 if (!ret)
7544 ret = task->tk_status;
7545 rpc_put_task(task);
7546 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007547}
Trond Myklebust36281ca2012-03-04 18:13:56 -05007548
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007549static int nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
7550{
7551 struct rpc_task *task;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007552 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007553
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007554 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007555 nfs4_free_lock_state(server, lsp);
7556 if (IS_ERR(task))
7557 return PTR_ERR(task);
7558 rpc_put_task(task);
7559 return 0;
7560}
7561
Trond Myklebust36281ca2012-03-04 18:13:56 -05007562static bool nfs41_match_stateid(const nfs4_stateid *s1,
7563 const nfs4_stateid *s2)
7564{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007565 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007566 return false;
7567
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007568 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007569 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007570 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007571 return true;
7572
7573 return false;
7574}
7575
Andy Adamson557134a2009-04-01 09:21:53 -04007576#endif /* CONFIG_NFS_V4_1 */
7577
Trond Myklebust36281ca2012-03-04 18:13:56 -05007578static bool nfs4_match_stateid(const nfs4_stateid *s1,
7579 const nfs4_stateid *s2)
7580{
Trond Myklebustf597c532012-03-04 18:13:56 -05007581 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05007582}
7583
7584
Trond Myklebust17280172012-03-11 13:11:00 -04007585static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007586 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007587 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007588 .recover_open = nfs4_open_reclaim,
7589 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04007590 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04007591 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007592};
7593
Andy Adamson591d71c2009-04-01 09:22:47 -04007594#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007595static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007596 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
7597 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
7598 .recover_open = nfs4_open_reclaim,
7599 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05007600 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007601 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04007602 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04007603};
7604#endif /* CONFIG_NFS_V4_1 */
7605
Trond Myklebust17280172012-03-11 13:11:00 -04007606static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007607 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007608 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007609 .recover_open = nfs4_open_expired,
7610 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04007611 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007612};
7613
Andy Adamson591d71c2009-04-01 09:22:47 -04007614#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007615static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007616 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
7617 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007618 .recover_open = nfs41_open_expired,
7619 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05007620 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04007621};
7622#endif /* CONFIG_NFS_V4_1 */
7623
Trond Myklebust17280172012-03-11 13:11:00 -04007624static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007625 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04007626 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007627 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04007628};
7629
7630#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007631static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007632 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04007633 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007634 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04007635};
7636#endif
7637
Trond Myklebust97dc1352010-06-16 09:52:26 -04007638static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
7639 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04007640 .init_caps = NFS_CAP_READDIRPLUS
7641 | NFS_CAP_ATOMIC_OPEN
7642 | NFS_CAP_CHANGE_ATTR
7643 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04007644 .init_client = nfs40_init_client,
7645 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007646 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007647 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007648 .free_lock_state = nfs4_release_lockowner,
Chuck Lever9915ea72013-08-09 12:48:27 -04007649 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007650 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
7651 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
7652 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007653};
7654
7655#if defined(CONFIG_NFS_V4_1)
7656static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
7657 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04007658 .init_caps = NFS_CAP_READDIRPLUS
7659 | NFS_CAP_ATOMIC_OPEN
7660 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04007661 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04007662 | NFS_CAP_STATEID_NFSV41
7663 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04007664 .init_client = nfs41_init_client,
7665 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007666 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007667 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007668 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04007669 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007670 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7671 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7672 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007673};
7674#endif
7675
Steve Dickson42c2c422013-05-22 12:50:38 -04007676#if defined(CONFIG_NFS_V4_2)
7677static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
7678 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04007679 .init_caps = NFS_CAP_READDIRPLUS
7680 | NFS_CAP_ATOMIC_OPEN
7681 | NFS_CAP_CHANGE_ATTR
7682 | NFS_CAP_POSIX_LOCK
7683 | NFS_CAP_STATEID_NFSV41
7684 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04007685 .init_client = nfs41_init_client,
7686 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04007687 .match_stateid = nfs41_match_stateid,
7688 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04007689 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04007690 .call_sync_ops = &nfs41_call_sync_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04007691 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7692 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7693 .state_renewal_ops = &nfs41_state_renewal_ops,
7694};
7695#endif
7696
Trond Myklebust97dc1352010-06-16 09:52:26 -04007697const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
7698 [0] = &nfs_v4_0_minor_ops,
7699#if defined(CONFIG_NFS_V4_1)
7700 [1] = &nfs_v4_1_minor_ops,
7701#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04007702#if defined(CONFIG_NFS_V4_2)
7703 [2] = &nfs_v4_2_minor_ops,
7704#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04007705};
7706
Trond Myklebust17f26b12013-08-21 15:48:42 -04007707static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04007708 .create = nfs_create,
7709 .lookup = nfs_lookup,
7710 .atomic_open = nfs_atomic_open,
7711 .link = nfs_link,
7712 .unlink = nfs_unlink,
7713 .symlink = nfs_symlink,
7714 .mkdir = nfs_mkdir,
7715 .rmdir = nfs_rmdir,
7716 .mknod = nfs_mknod,
7717 .rename = nfs_rename,
7718 .permission = nfs_permission,
7719 .getattr = nfs_getattr,
7720 .setattr = nfs_setattr,
7721 .getxattr = generic_getxattr,
7722 .setxattr = generic_setxattr,
7723 .listxattr = generic_listxattr,
7724 .removexattr = generic_removexattr,
7725};
7726
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08007727static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007728 .permission = nfs_permission,
7729 .getattr = nfs_getattr,
7730 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007731 .getxattr = generic_getxattr,
7732 .setxattr = generic_setxattr,
7733 .listxattr = generic_listxattr,
7734 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007735};
7736
David Howells509de812006-08-22 20:06:11 -04007737const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007738 .version = 4, /* protocol version */
7739 .dentry_ops = &nfs4_dentry_operations,
7740 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007741 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04007742 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007743 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04007744 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04007745 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007746 .getattr = nfs4_proc_getattr,
7747 .setattr = nfs4_proc_setattr,
7748 .lookup = nfs4_proc_lookup,
7749 .access = nfs4_proc_access,
7750 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007751 .create = nfs4_proc_create,
7752 .remove = nfs4_proc_remove,
7753 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04007754 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007755 .unlink_done = nfs4_proc_unlink_done,
7756 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04007757 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04007758 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04007759 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007760 .link = nfs4_proc_link,
7761 .symlink = nfs4_proc_symlink,
7762 .mkdir = nfs4_proc_mkdir,
7763 .rmdir = nfs4_proc_remove,
7764 .readdir = nfs4_proc_readdir,
7765 .mknod = nfs4_proc_mknod,
7766 .statfs = nfs4_proc_statfs,
7767 .fsinfo = nfs4_proc_fsinfo,
7768 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04007769 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007770 .decode_dirent = nfs4_decode_dirent,
7771 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04007772 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04007773 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05007774 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007775 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04007776 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04007777 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007778 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007779 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04007780 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007781 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007782 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00007783 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04007784 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04007785 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04007786 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04007787 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04007788 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00007789 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04007790 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04007791 .create_server = nfs4_create_server,
7792 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007793};
7794
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007795static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7796 .prefix = XATTR_NAME_NFSV4_ACL,
7797 .list = nfs4_xattr_list_nfs4_acl,
7798 .get = nfs4_xattr_get_nfs4_acl,
7799 .set = nfs4_xattr_set_nfs4_acl,
7800};
7801
7802const struct xattr_handler *nfs4_xattr_handlers[] = {
7803 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04007804#ifdef CONFIG_NFS_V4_SECURITY_LABEL
7805 &nfs4_xattr_nfs4_label_handler,
7806#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007807 NULL
7808};
7809
Linus Torvalds1da177e2005-04-16 15:20:36 -07007810/*
7811 * Local variables:
7812 * c-basic-offset: 8
7813 * End:
7814 */