blob: fa87f81527b3e48374bb14ca2a82f3765060b51a [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:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400155 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000156 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800157 case -NFS4ERR_BADOWNER:
158 case -NFS4ERR_BADNAME:
159 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400160 case -NFS4ERR_SHARE_DENIED:
161 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400162 case -NFS4ERR_MINOR_VERS_MISMATCH:
163 return -EPROTONOSUPPORT;
Weston Andros Adamson6168f622012-09-10 14:00:46 -0400164 case -NFS4ERR_ACCESS:
165 return -EACCES;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400166 case -NFS4ERR_FILE_OPEN:
167 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400168 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700170 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400171 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400173 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
175
176/*
177 * This is our standard bitmap for GETATTR requests.
178 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400179const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 FATTR4_WORD0_TYPE
181 | FATTR4_WORD0_CHANGE
182 | FATTR4_WORD0_SIZE
183 | FATTR4_WORD0_FSID
184 | FATTR4_WORD0_FILEID,
185 FATTR4_WORD1_MODE
186 | FATTR4_WORD1_NUMLINKS
187 | FATTR4_WORD1_OWNER
188 | FATTR4_WORD1_OWNER_GROUP
189 | FATTR4_WORD1_RAWDEV
190 | FATTR4_WORD1_SPACE_USED
191 | FATTR4_WORD1_TIME_ACCESS
192 | FATTR4_WORD1_TIME_METADATA
David Quigleyaa9c2662013-05-22 12:50:44 -0400193 | FATTR4_WORD1_TIME_MODIFY,
194#ifdef CONFIG_NFS_V4_SECURITY_LABEL
195 FATTR4_WORD2_SECURITY_LABEL
196#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197};
198
Trond Myklebust1549210f2012-06-05 09:16:47 -0400199static const u32 nfs4_pnfs_open_bitmap[3] = {
200 FATTR4_WORD0_TYPE
201 | FATTR4_WORD0_CHANGE
202 | FATTR4_WORD0_SIZE
203 | FATTR4_WORD0_FSID
204 | FATTR4_WORD0_FILEID,
205 FATTR4_WORD1_MODE
206 | FATTR4_WORD1_NUMLINKS
207 | FATTR4_WORD1_OWNER
208 | FATTR4_WORD1_OWNER_GROUP
209 | FATTR4_WORD1_RAWDEV
210 | FATTR4_WORD1_SPACE_USED
211 | FATTR4_WORD1_TIME_ACCESS
212 | FATTR4_WORD1_TIME_METADATA
213 | FATTR4_WORD1_TIME_MODIFY,
214 FATTR4_WORD2_MDSTHRESHOLD
215};
216
Andy Adamsone23008e2012-10-02 21:07:32 -0400217static const u32 nfs4_open_noattr_bitmap[3] = {
218 FATTR4_WORD0_TYPE
219 | FATTR4_WORD0_CHANGE
220 | FATTR4_WORD0_FILEID,
221};
222
David Quigleya09df2c2013-05-22 12:50:41 -0400223const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 FATTR4_WORD0_FILES_AVAIL
225 | FATTR4_WORD0_FILES_FREE
226 | FATTR4_WORD0_FILES_TOTAL,
227 FATTR4_WORD1_SPACE_AVAIL
228 | FATTR4_WORD1_SPACE_FREE
229 | FATTR4_WORD1_SPACE_TOTAL
230};
231
David Quigleya09df2c2013-05-22 12:50:41 -0400232const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 FATTR4_WORD0_MAXLINK
234 | FATTR4_WORD0_MAXNAME,
235 0
236};
237
Fred Isamandae100c2011-07-30 20:52:37 -0400238const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 | FATTR4_WORD0_MAXREAD
240 | FATTR4_WORD0_MAXWRITE
241 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700242 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400243 | FATTR4_WORD1_FS_LAYOUT_TYPES,
244 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245};
246
David Quigleya09df2c2013-05-22 12:50:41 -0400247const u32 nfs4_fs_locations_bitmap[3] = {
Manoj Naik830b8e32006-06-09 09:34:25 -0400248 FATTR4_WORD0_TYPE
249 | FATTR4_WORD0_CHANGE
250 | FATTR4_WORD0_SIZE
251 | FATTR4_WORD0_FSID
252 | FATTR4_WORD0_FILEID
253 | FATTR4_WORD0_FS_LOCATIONS,
254 FATTR4_WORD1_MODE
255 | FATTR4_WORD1_NUMLINKS
256 | FATTR4_WORD1_OWNER
257 | FATTR4_WORD1_OWNER_GROUP
258 | FATTR4_WORD1_RAWDEV
259 | FATTR4_WORD1_SPACE_USED
260 | FATTR4_WORD1_TIME_ACCESS
261 | FATTR4_WORD1_TIME_METADATA
262 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400263 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400264};
265
Al Virobc4785c2006-10-19 23:28:51 -0700266static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 struct nfs4_readdir_arg *readdir)
268{
Al Viro0dbb4c62006-10-19 23:28:49 -0700269 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000272 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
274 return;
275 }
276
277 readdir->cookie = 0;
278 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
279 if (cookie == 2)
280 return;
281
282 /*
283 * NFSv4 servers do not return entries for '.' and '..'
284 * Therefore, we fake these entries here. We let '.'
285 * have cookie 0 and '..' have cookie 1. Note that
286 * when talking to the server, we always send cookie 0
287 * instead of 1 or 2.
288 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800289 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 if (cookie == 0) {
292 *p++ = xdr_one; /* next */
293 *p++ = xdr_zero; /* cookie, first word */
294 *p++ = xdr_one; /* cookie, second word */
295 *p++ = xdr_one; /* entry len */
296 memcpy(p, ".\0\0\0", 4); /* entry */
297 p++;
298 *p++ = xdr_one; /* bitmap length */
299 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
300 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400301 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
303
304 *p++ = xdr_one; /* next */
305 *p++ = xdr_zero; /* cookie, first word */
306 *p++ = xdr_two; /* cookie, second word */
307 *p++ = xdr_two; /* entry len */
308 memcpy(p, "..\0\0", 4); /* entry */
309 p++;
310 *p++ = xdr_one; /* bitmap length */
311 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
312 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400313 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 readdir->pgbase = (char *)p - (char *)start;
316 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800317 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
319
Trond Myklebust65de8722008-12-23 15:21:44 -0500320static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
321{
322 int res = 0;
323
324 might_sleep();
325
326 if (*timeout <= 0)
327 *timeout = NFS4_POLL_RETRY_MIN;
328 if (*timeout > NFS4_POLL_RETRY_MAX)
329 *timeout = NFS4_POLL_RETRY_MAX;
Colin Cross416ad3c2013-05-06 23:50:06 +0000330 freezable_schedule_timeout_killable_unsafe(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500331 if (fatal_signal_pending(current))
332 res = -ERESTARTSYS;
333 *timeout <<= 1;
334 return res;
335}
336
337/* This is the error handling routine for processes that are allowed
338 * to sleep.
339 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800340static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500341{
342 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500343 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500344 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500345 int ret = errorcode;
346
347 exception->retry = 0;
348 switch(errorcode) {
349 case 0:
350 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500351 case -NFS4ERR_OPENMODE:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400352 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400353 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500354 exception->retry = 1;
355 return 0;
356 }
357 if (state == NULL)
358 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400359 ret = nfs4_schedule_stateid_recovery(server, state);
360 if (ret < 0)
361 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500362 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500363 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500364 case -NFS4ERR_ADMIN_REVOKED:
365 case -NFS4ERR_BAD_STATEID:
Trond Myklebustb570a972013-04-12 15:04:51 -0400366 if (inode != NULL && nfs4_have_delegation(inode, FMODE_READ)) {
367 nfs_remove_bad_delegation(inode);
368 exception->retry = 1;
369 break;
370 }
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500371 if (state == NULL)
372 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400373 ret = nfs4_schedule_stateid_recovery(server, state);
374 if (ret < 0)
375 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500376 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400377 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -0400378 if (state != NULL) {
379 ret = nfs4_schedule_stateid_recovery(server, state);
380 if (ret < 0)
381 break;
382 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500383 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500384 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500385 nfs4_schedule_lease_recovery(clp);
386 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500387#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400388 case -NFS4ERR_BADSESSION:
389 case -NFS4ERR_BADSLOT:
390 case -NFS4ERR_BAD_HIGH_SLOT:
391 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
392 case -NFS4ERR_DEADSESSION:
393 case -NFS4ERR_SEQ_FALSE_RETRY:
394 case -NFS4ERR_SEQ_MISORDERED:
395 dprintk("%s ERROR: %d Reset session\n", __func__,
396 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400397 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400398 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500399#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500400 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500401 if (exception->timeout > HZ) {
402 /* We have retried a decent amount, time to
403 * fail
404 */
405 ret = -EBUSY;
406 break;
407 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500408 case -NFS4ERR_GRACE:
409 case -NFS4ERR_DELAY:
410 ret = nfs4_delay(server->client, &exception->timeout);
411 if (ret != 0)
412 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400413 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500414 case -NFS4ERR_OLD_STATEID:
415 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800416 break;
417 case -NFS4ERR_BADOWNER:
418 /* The following works around a Linux server bug! */
419 case -NFS4ERR_BADNAME:
420 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
421 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
422 exception->retry = 1;
423 printk(KERN_WARNING "NFS: v4 server %s "
424 "does not accept raw "
425 "uid/gids. "
426 "Reenabling the idmapper.\n",
427 server->nfs_client->cl_hostname);
428 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500429 }
430 /* We failed to handle the error */
431 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500432wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500433 ret = nfs4_wait_clnt_recover(clp);
434 if (ret == 0)
435 exception->retry = 1;
436 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500437}
438
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400439/*
440 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
441 * or 'false' otherwise.
442 */
443static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
444{
445 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
446
447 if (flavor == RPC_AUTH_GSS_KRB5I ||
448 flavor == RPC_AUTH_GSS_KRB5P)
449 return true;
450
451 return false;
452}
Trond Myklebust65de8722008-12-23 15:21:44 -0500453
Trond Myklebust452e9352010-07-31 14:29:06 -0400454static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 spin_lock(&clp->cl_lock);
457 if (time_before(clp->cl_last_renewal,timestamp))
458 clp->cl_last_renewal = timestamp;
459 spin_unlock(&clp->cl_lock);
460}
461
Trond Myklebust452e9352010-07-31 14:29:06 -0400462static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
463{
464 do_renew_lease(server->nfs_client, timestamp);
465}
466
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400467struct nfs4_call_sync_data {
468 const struct nfs_server *seq_server;
469 struct nfs4_sequence_args *seq_args;
470 struct nfs4_sequence_res *seq_res;
471};
472
Chuck Levera9c92d62013-08-09 12:48:18 -0400473static void nfs4_init_sequence(struct nfs4_sequence_args *args,
474 struct nfs4_sequence_res *res, int cache_reply)
475{
476 args->sa_slot = NULL;
477 args->sa_cache_this = cache_reply;
478 args->sa_privileged = 0;
479
480 res->sr_slot = NULL;
481}
482
483static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
484{
485 args->sa_privileged = 1;
486}
487
Chuck Lever3bd23842013-08-09 12:49:19 -0400488static int nfs40_setup_sequence(const struct nfs_server *server,
489 struct nfs4_sequence_args *args,
490 struct nfs4_sequence_res *res,
491 struct rpc_task *task)
492{
493 struct nfs4_slot_table *tbl = server->nfs_client->cl_slot_tbl;
494 struct nfs4_slot *slot;
495
496 /* slot already allocated? */
497 if (res->sr_slot != NULL)
498 goto out_start;
499
500 spin_lock(&tbl->slot_tbl_lock);
501 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
502 goto out_sleep;
503
504 slot = nfs4_alloc_slot(tbl);
505 if (IS_ERR(slot)) {
506 if (slot == ERR_PTR(-ENOMEM))
507 task->tk_timeout = HZ >> 2;
508 goto out_sleep;
509 }
510 spin_unlock(&tbl->slot_tbl_lock);
511
512 args->sa_slot = slot;
513 res->sr_slot = slot;
514
515out_start:
516 rpc_call_start(task);
517 return 0;
518
519out_sleep:
520 if (args->sa_privileged)
521 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
522 NULL, RPC_PRIORITY_PRIVILEGED);
523 else
524 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
525 spin_unlock(&tbl->slot_tbl_lock);
526 return -EAGAIN;
527}
528
529static int nfs40_sequence_done(struct rpc_task *task,
530 struct nfs4_sequence_res *res)
531{
532 struct nfs4_slot *slot = res->sr_slot;
533 struct nfs4_slot_table *tbl;
534
535 if (!RPC_WAS_SENT(task))
536 goto out;
537
538 tbl = slot->table;
539 spin_lock(&tbl->slot_tbl_lock);
540 if (!nfs41_wake_and_assign_slot(tbl, slot))
541 nfs4_free_slot(tbl, slot);
542 spin_unlock(&tbl->slot_tbl_lock);
543
544 res->sr_slot = NULL;
545out:
546 return 1;
547}
548
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400549#if defined(CONFIG_NFS_V4_1)
550
Trond Myklebustd185a332010-06-16 09:52:25 -0400551static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400552{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500553 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400554 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500555 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400556
Benny Halevydfb4f3092010-09-24 09:17:01 -0400557 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400558 /* just wake up the next guy waiting since
559 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500560 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500561 return;
Andy Adamson13615872009-04-01 09:22:17 -0400562 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500563 tbl = res->sr_slot->table;
564 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500565
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500566 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500567 /* Be nice to the server: try to ensure that the last transmitted
568 * value for highest_user_slotid <= target_highest_slotid
569 */
570 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
571 send_new_highest_used_slotid = true;
572
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500573 if (nfs41_wake_and_assign_slot(tbl, res->sr_slot)) {
574 send_new_highest_used_slotid = false;
575 goto out_unlock;
576 }
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500577 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500578
579 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
580 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500581out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500582 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400583 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500584 if (send_new_highest_used_slotid)
585 nfs41_server_notify_highest_slotid_update(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400586}
587
Trond Myklebust14516c32010-07-31 14:29:06 -0400588static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400589{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500590 struct nfs4_session *session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500591 struct nfs4_slot *slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400592 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500593 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500594 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400595
Bryan Schumaker468f8612011-04-18 15:57:32 -0400596 /* don't increment the sequence number if the task wasn't sent */
597 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400598 goto out;
599
Trond Myklebust933602e2012-11-16 12:12:38 -0500600 slot = res->sr_slot;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500601 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500602
Trond Myklebustac20d162012-12-15 15:36:07 -0500603 if (slot->interrupted) {
604 slot->interrupted = 0;
605 interrupted = true;
606 }
607
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400608 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500609 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400610 switch (res->sr_status) {
611 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400612 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500613 ++slot->seq_nr;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500614 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500615 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500616 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500617 if (res->sr_status_flags != 0)
618 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500619 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400620 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500621 case 1:
622 /*
623 * sr_status remains 1 if an RPC level error occurred.
624 * The server may or may not have processed the sequence
625 * operation..
626 * Mark the slot as having hosted an interrupted RPC call.
627 */
628 slot->interrupted = 1;
629 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400630 case -NFS4ERR_DELAY:
631 /* The server detected a resend of the RPC call and
632 * returned NFS4ERR_DELAY as per Section 2.10.6.2
633 * of RFC5661.
634 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500635 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400636 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500637 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500638 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400639 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500640 case -NFS4ERR_BADSLOT:
641 /*
642 * The slot id we used was probably retired. Try again
643 * using a different slot id.
644 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500645 goto retry_nowait;
646 case -NFS4ERR_SEQ_MISORDERED:
647 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500648 * Was the last operation on this sequence interrupted?
649 * If so, retry after bumping the sequence number.
650 */
651 if (interrupted) {
652 ++slot->seq_nr;
653 goto retry_nowait;
654 }
655 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500656 * Could this slot have been previously retired?
657 * If so, then the server may be expecting seq_nr = 1!
658 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500659 if (slot->seq_nr != 1) {
660 slot->seq_nr = 1;
661 goto retry_nowait;
662 }
663 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500664 case -NFS4ERR_SEQ_FALSE_RETRY:
665 ++slot->seq_nr;
666 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400667 default:
668 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500669 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400670 }
671out:
672 /* The session may be reset by one of the error handlers. */
673 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400674 nfs41_sequence_free_slot(res);
Trond Myklebust85563072012-12-11 10:31:12 -0500675 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500676retry_nowait:
677 if (rpc_restart_call_prepare(task)) {
678 task->tk_status = 0;
679 ret = 0;
680 }
681 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400682out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400683 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400684 goto out;
685 rpc_delay(task, NFS4_POLL_RETRY_MAX);
686 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400687}
688
Trond Myklebust14516c32010-07-31 14:29:06 -0400689static int nfs4_sequence_done(struct rpc_task *task,
690 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400691{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500692 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400693 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400694 if (!res->sr_slot->table->session)
695 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400696 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400697}
698
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000699int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400700 struct nfs4_sequence_args *args,
701 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400702 struct rpc_task *task)
703{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400704 struct nfs4_slot *slot;
705 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400706
707 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400708 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400709 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400710 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400711
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400712 tbl = &session->fc_slot_table;
713
Trond Myklebust69d206b2012-11-22 13:21:02 -0500714 task->tk_timeout = 0;
715
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400716 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400717 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400718 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500719 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500720 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400721 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400722 }
723
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500724 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500725 if (IS_ERR(slot)) {
726 /* If out of memory, try again in 1/4 second */
727 if (slot == ERR_PTR(-ENOMEM))
728 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400729 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400730 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400731 }
732 spin_unlock(&tbl->slot_tbl_lock);
733
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500734 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400735
Chuck Levere8d92382013-08-09 12:47:51 -0400736 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500737 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400738
Benny Halevydfb4f3092010-09-24 09:17:01 -0400739 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500740 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400741 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400742 /*
743 * sr_status is only set in decode_sequence, and so will remain
744 * set to 1 if an rpc level failure occurs.
745 */
746 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400747 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400748out_success:
749 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400750 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400751out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400752 /* Privileged tasks are queued with top priority */
753 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400754 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
755 NULL, RPC_PRIORITY_PRIVILEGED);
756 else
757 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400758 spin_unlock(&tbl->slot_tbl_lock);
759 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400760}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000761EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400762
Chuck Lever5a580e02013-08-09 12:48:09 -0400763static int nfs4_setup_sequence(const struct nfs_server *server,
764 struct nfs4_sequence_args *args,
765 struct nfs4_sequence_res *res,
766 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400767{
Trond Myklebust035168a2010-06-16 09:52:26 -0400768 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400769 int ret = 0;
770
Chuck Lever3bd23842013-08-09 12:49:19 -0400771 if (!session)
772 return nfs40_setup_sequence(server, args, res, task);
Trond Myklebust035168a2010-06-16 09:52:26 -0400773
Chuck Levere8d92382013-08-09 12:47:51 -0400774 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400775 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400776 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168a2010-06-16 09:52:26 -0400777
Trond Myklebust9d12b212012-01-17 22:04:25 -0500778 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400779
Andy Adamsonce5039c2009-04-01 09:22:13 -0400780 dprintk("<-- %s status=%d\n", __func__, ret);
781 return ret;
782}
783
Andy Adamsonce5039c2009-04-01 09:22:13 -0400784static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
785{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400786 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400787 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400788
Trond Myklebust035168a2010-06-16 09:52:26 -0400789 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
790
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400791 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400792}
793
Andy Adamson69ab40c2009-04-01 09:22:19 -0400794static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
795{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400796 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400797
Trond Myklebust14516c32010-07-31 14:29:06 -0400798 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400799}
800
Trond Myklebust17280172012-03-11 13:11:00 -0400801static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400802 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400803 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400804};
805
Chuck Lever3bd23842013-08-09 12:49:19 -0400806#else /* !CONFIG_NFS_V4_1 */
807
Chuck Lever5a580e02013-08-09 12:48:09 -0400808static int nfs4_setup_sequence(const struct nfs_server *server,
809 struct nfs4_sequence_args *args,
810 struct nfs4_sequence_res *res,
811 struct rpc_task *task)
812{
Chuck Lever3bd23842013-08-09 12:49:19 -0400813 return nfs40_setup_sequence(server, args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -0400814}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400815
Trond Myklebust14516c32010-07-31 14:29:06 -0400816static int nfs4_sequence_done(struct rpc_task *task,
817 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400818{
Chuck Lever3bd23842013-08-09 12:49:19 -0400819 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400820}
Chuck Lever3bd23842013-08-09 12:49:19 -0400821
822#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400823
Chuck Lever9915ea72013-08-09 12:48:27 -0400824static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
825{
826 struct nfs4_call_sync_data *data = calldata;
827 nfs4_setup_sequence(data->seq_server,
828 data->seq_args, data->seq_res, task);
829}
830
831static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
832{
833 struct nfs4_call_sync_data *data = calldata;
834 nfs4_sequence_done(task, data->seq_res);
835}
836
837static const struct rpc_call_ops nfs40_call_sync_ops = {
838 .rpc_call_prepare = nfs40_call_sync_prepare,
839 .rpc_call_done = nfs40_call_sync_done,
840};
841
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400842static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -0400843 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500844 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100845 struct nfs4_sequence_args *args,
846 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -0400847{
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100848 int ret;
849 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -0400850 struct nfs_client *clp = server->nfs_client;
851 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100852 .seq_server = server,
853 .seq_args = args,
854 .seq_res = res,
855 };
856 struct rpc_task_setup task_setup = {
857 .rpc_client = clnt,
858 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -0400859 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100860 .callback_data = &data
861 };
862
863 task = rpc_run_task(&task_setup);
864 if (IS_ERR(task))
865 ret = PTR_ERR(task);
866 else {
867 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -0400868 rpc_put_task(task);
869 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100870 return ret;
871}
872
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400873static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000874int nfs4_call_sync(struct rpc_clnt *clnt,
875 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000876 struct rpc_message *msg,
877 struct nfs4_sequence_args *args,
878 struct nfs4_sequence_res *res,
879 int cache_reply)
880{
Chuck Levera9c92d62013-08-09 12:48:18 -0400881 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -0400882 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000883}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
886{
887 struct nfs_inode *nfsi = NFS_I(dir);
888
889 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400890 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700891 if (!cinfo->atomic || cinfo->before != dir->i_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700893 dir->i_version = cinfo->after;
David Howellsde242c02012-12-20 21:52:38 +0000894 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 spin_unlock(&dir->i_lock);
896}
897
898struct nfs4_opendata {
899 struct kref kref;
900 struct nfs_openargs o_arg;
901 struct nfs_openres o_res;
902 struct nfs_open_confirmargs c_arg;
903 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500904 struct nfs4_string owner_name;
905 struct nfs4_string group_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400907 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400909 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 struct nfs4_state_owner *owner;
911 struct nfs4_state *state;
912 struct iattr attrs;
913 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400914 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -0400915 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -0400916 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400917 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 int cancelled;
919};
Trond Myklebustdecf4912005-10-27 22:12:39 -0400920
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400921static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
922 int err, struct nfs4_exception *exception)
923{
924 if (err != -EINVAL)
925 return false;
926 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
927 return false;
928 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
929 exception->retry = 1;
930 return true;
931}
932
933static enum open_claim_type4
934nfs4_map_atomic_open_claim(struct nfs_server *server,
935 enum open_claim_type4 claim)
936{
937 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
938 return claim;
939 switch (claim) {
940 default:
941 return claim;
942 case NFS4_OPEN_CLAIM_FH:
943 return NFS4_OPEN_CLAIM_NULL;
944 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
945 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
946 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
947 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
948 }
949}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100952{
953 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400954 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100955 p->o_res.seqid = p->o_arg.seqid;
956 p->c_res.seqid = p->c_arg.seqid;
957 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400958 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100959 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500960 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100961}
962
Al Viro82a2c1b2011-06-22 18:30:55 -0400963static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100964 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400965 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -0400966 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400967 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400968 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100969{
Al Viro82a2c1b2011-06-22 18:30:55 -0400970 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100971 struct inode *dir = parent->d_inode;
972 struct nfs_server *server = NFS_SERVER(dir);
973 struct nfs4_opendata *p;
974
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400975 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100976 if (p == NULL)
977 goto err;
David Quigley14c43f72013-05-22 12:50:43 -0400978
979 p->f_label = nfs4_label_alloc(server, gfp_mask);
980 if (IS_ERR(p->f_label))
981 goto err_free_p;
982
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400983 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100984 if (p->o_arg.seqid == NULL)
David Quigley14c43f72013-05-22 12:50:43 -0400985 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -0400986 nfs_sb_active(dentry->d_sb);
987 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100988 p->dir = parent;
989 p->owner = sp;
990 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500991 p->o_arg.open_flags = flags;
992 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700993 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
994 * will return permission denied for all bits until close */
995 if (!(flags & O_EXCL)) {
996 /* ask server to check for all possible rights as results
997 * are cached */
998 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
999 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001000 }
David Howells7539bba2006-08-22 20:06:09 -04001001 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001002 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1003 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001004 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001005 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001006 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001007 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
David Quigley1775fd32013-05-22 12:50:42 -04001008 p->o_arg.label = label;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001009 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1010 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001011 case NFS4_OPEN_CLAIM_NULL:
1012 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1013 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1014 p->o_arg.fh = NFS_FH(dir);
1015 break;
1016 case NFS4_OPEN_CLAIM_PREVIOUS:
1017 case NFS4_OPEN_CLAIM_FH:
1018 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1019 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1020 p->o_arg.fh = NFS_FH(dentry->d_inode);
1021 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001022 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001023 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001024
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001025 p->o_arg.u.attrs = &p->attrs;
1026 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001027
1028 verf[0] = jiffies;
1029 verf[1] = current->pid;
1030 memcpy(p->o_arg.u.verifier.data, verf,
1031 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001032 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001033 p->c_arg.fh = &p->o_res.fh;
1034 p->c_arg.stateid = &p->o_res.stateid;
1035 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001036 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001037 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001038 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001039
1040err_free_label:
1041 nfs4_label_free(p->f_label);
1042err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001043 kfree(p);
1044err:
1045 dput(parent);
1046 return NULL;
1047}
1048
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001049static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001050{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001051 struct nfs4_opendata *p = container_of(kref,
1052 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001053 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001054
1055 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001056 if (p->state != NULL)
1057 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001058 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001059
1060 nfs4_label_free(p->f_label);
1061
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001062 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001063 dput(p->dentry);
1064 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001065 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001066 kfree(p);
1067}
1068
1069static void nfs4_opendata_put(struct nfs4_opendata *p)
1070{
1071 if (p != NULL)
1072 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001073}
1074
Trond Myklebust06f814a2006-01-03 09:55:07 +01001075static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1076{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001077 int ret;
1078
Trond Myklebust06f814a2006-01-03 09:55:07 +01001079 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001080 return ret;
1081}
1082
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001083static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001084{
1085 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001086
Trond Myklebust536e43d2012-01-17 22:04:26 -05001087 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001088 goto out;
1089 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001090 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001091 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1092 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001093 break;
1094 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001095 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1096 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001097 break;
1098 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001099 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1100 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001101 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001102out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001103 return ret;
1104}
1105
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001106static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001107{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001108 if (delegation == NULL)
1109 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001110 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001111 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -05001112 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -04001113 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001114 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1115 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -05001116 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001117 return 1;
1118}
1119
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001120static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001121{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001122 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001123 case FMODE_WRITE:
1124 state->n_wronly++;
1125 break;
1126 case FMODE_READ:
1127 state->n_rdonly++;
1128 break;
1129 case FMODE_READ|FMODE_WRITE:
1130 state->n_rdwr++;
1131 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001132 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001133}
1134
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001135static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001136{
1137 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001138 nfs4_stateid_copy(&state->stateid, stateid);
1139 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust92b40e92013-04-20 01:25:45 -04001140 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001141 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001142 case FMODE_READ:
1143 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1144 break;
1145 case FMODE_WRITE:
1146 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1147 break;
1148 case FMODE_READ|FMODE_WRITE:
1149 set_bit(NFS_O_RDWR_STATE, &state->flags);
1150 }
1151}
1152
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001153static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001154{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001155 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001156 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001157 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001158}
1159
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001160static 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 -07001161{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001162 /*
1163 * Protect the call to nfs4_state_set_mode_locked and
1164 * serialise the stateid update
1165 */
1166 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001167 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001168 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001169 set_bit(NFS_DELEGATED_STATE, &state->flags);
1170 }
1171 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001172 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001173 write_sequnlock(&state->seqlock);
1174 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001175 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001176 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177}
1178
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001179static 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 -05001180{
1181 struct nfs_inode *nfsi = NFS_I(state->inode);
1182 struct nfs_delegation *deleg_cur;
1183 int ret = 0;
1184
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001185 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001186
1187 rcu_read_lock();
1188 deleg_cur = rcu_dereference(nfsi->delegation);
1189 if (deleg_cur == NULL)
1190 goto no_delegation;
1191
1192 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001193 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001194 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001195 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001196 goto no_delegation_unlock;
1197
1198 if (delegation == NULL)
1199 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001200 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001201 goto no_delegation_unlock;
1202
Trond Myklebustb7391f42008-12-23 15:21:52 -05001203 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001204 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001205 ret = 1;
1206no_delegation_unlock:
1207 spin_unlock(&deleg_cur->lock);
1208no_delegation:
1209 rcu_read_unlock();
1210
1211 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001212 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001213 ret = 1;
1214 }
1215
1216 return ret;
1217}
1218
1219
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001220static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001221{
1222 struct nfs_delegation *delegation;
1223
1224 rcu_read_lock();
1225 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001226 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001227 rcu_read_unlock();
1228 return;
1229 }
1230 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001231 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001232}
1233
Trond Myklebust6ee41262007-07-08 14:11:36 -04001234static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001235{
1236 struct nfs4_state *state = opendata->state;
1237 struct nfs_inode *nfsi = NFS_I(state->inode);
1238 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001239 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001240 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001241 nfs4_stateid stateid;
1242 int ret = -EAGAIN;
1243
Trond Myklebustaac00a82007-07-05 19:02:21 -04001244 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001245 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001246 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001247 if (can_open_cached(state, fmode, open_mode)) {
1248 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001249 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001250 goto out_return_state;
1251 }
1252 spin_unlock(&state->owner->so_lock);
1253 }
Trond Myklebust34310432008-12-23 15:21:38 -05001254 rcu_read_lock();
1255 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001256 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001257 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001258 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001259 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001260 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001261 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001262 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001263 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001264 if (!opendata->is_recover) {
1265 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1266 if (ret != 0)
1267 goto out;
1268 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001269 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001270
1271 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001272 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001273 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001274 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001275out:
1276 return ERR_PTR(ret);
1277out_return_state:
1278 atomic_inc(&state->count);
1279 return state;
1280}
1281
Andy Adamsone23008e2012-10-02 21:07:32 -04001282static void
1283nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1284{
1285 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1286 struct nfs_delegation *delegation;
1287 int delegation_flags = 0;
1288
1289 rcu_read_lock();
1290 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1291 if (delegation)
1292 delegation_flags = delegation->flags;
1293 rcu_read_unlock();
1294 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1295 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1296 "returning a delegation for "
1297 "OPEN(CLAIM_DELEGATE_CUR)\n",
1298 clp->cl_hostname);
1299 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1300 nfs_inode_set_delegation(state->inode,
1301 data->owner->so_cred,
1302 &data->o_res);
1303 else
1304 nfs_inode_reclaim_delegation(state->inode,
1305 data->owner->so_cred,
1306 &data->o_res);
1307}
1308
1309/*
1310 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1311 * and update the nfs4_state.
1312 */
1313static struct nfs4_state *
1314_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1315{
1316 struct inode *inode = data->state->inode;
1317 struct nfs4_state *state = data->state;
1318 int ret;
1319
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001320 if (!data->rpc_done) {
1321 if (data->rpc_status) {
1322 ret = data->rpc_status;
1323 goto err;
1324 }
1325 /* cached opens have already been processed */
1326 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001327 }
1328
Andy Adamsone23008e2012-10-02 21:07:32 -04001329 ret = nfs_refresh_inode(inode, &data->f_attr);
1330 if (ret)
1331 goto err;
1332
1333 if (data->o_res.delegation_type != 0)
1334 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001335update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001336 update_open_stateid(state, &data->o_res.stateid, NULL,
1337 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001338 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001339
1340 return state;
1341err:
1342 return ERR_PTR(ret);
1343
1344}
1345
1346static struct nfs4_state *
1347_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001348{
1349 struct inode *inode;
1350 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001351 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001352
Trond Myklebustaac00a82007-07-05 19:02:21 -04001353 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001354 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001355 goto out;
1356 }
1357
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001358 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001359 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001360 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001361 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001362 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001363 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001364 goto err;
1365 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001366 state = nfs4_get_open_state(inode, data->owner);
1367 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001368 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001369 if (data->o_res.delegation_type != 0)
1370 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001371 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001372 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001373 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001374out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001375 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001376 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001377err_put_inode:
1378 iput(inode);
1379err:
1380 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001381}
1382
Andy Adamsone23008e2012-10-02 21:07:32 -04001383static struct nfs4_state *
1384nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1385{
1386 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1387 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1388 return _nfs4_opendata_to_nfs4_state(data);
1389}
1390
Trond Myklebust864472e2006-01-03 09:55:15 +01001391static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1392{
1393 struct nfs_inode *nfsi = NFS_I(state->inode);
1394 struct nfs_open_context *ctx;
1395
1396 spin_lock(&state->inode->i_lock);
1397 list_for_each_entry(ctx, &nfsi->open_files, list) {
1398 if (ctx->state != state)
1399 continue;
1400 get_nfs_open_context(ctx);
1401 spin_unlock(&state->inode->i_lock);
1402 return ctx;
1403 }
1404 spin_unlock(&state->inode->i_lock);
1405 return ERR_PTR(-ENOENT);
1406}
1407
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001408static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1409 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001410{
1411 struct nfs4_opendata *opendata;
1412
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001413 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001414 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001415 if (opendata == NULL)
1416 return ERR_PTR(-ENOMEM);
1417 opendata->state = state;
1418 atomic_inc(&state->count);
1419 return opendata;
1420}
1421
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001422static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001423{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001424 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001425 int ret;
1426
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001427 opendata->o_arg.open_flags = 0;
1428 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001429 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1430 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1431 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001432 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001433 if (ret != 0)
1434 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001435 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001436 if (IS_ERR(newstate))
1437 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001438 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001439 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001440 return 0;
1441}
1442
1443static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1444{
Trond Myklebust864472e2006-01-03 09:55:15 +01001445 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001446 int ret;
1447
1448 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001449 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001450 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001451 smp_rmb();
1452 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001453 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001454 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001455 if (ret != 0)
1456 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001457 if (newstate != state)
1458 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001459 }
1460 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001461 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001462 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001463 if (ret != 0)
1464 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001465 if (newstate != state)
1466 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001467 }
1468 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001469 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001470 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001471 if (ret != 0)
1472 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001473 if (newstate != state)
1474 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001475 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001476 /*
1477 * We may have performed cached opens for all three recoveries.
1478 * Check if we need to update the current stateid.
1479 */
1480 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001481 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001482 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001483 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001484 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001485 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001486 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001487 return 0;
1488}
1489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490/*
1491 * OPEN_RECLAIM:
1492 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001494static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001496 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001497 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001498 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 int status;
1500
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001501 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1502 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001503 if (IS_ERR(opendata))
1504 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001505 rcu_read_lock();
1506 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001507 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001508 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001509 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001510 opendata->o_arg.u.delegation_type = delegation_type;
1511 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001512 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 return status;
1514}
1515
Trond Myklebust539cd032007-06-05 11:46:42 -04001516static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517{
1518 struct nfs_server *server = NFS_SERVER(state->inode);
1519 struct nfs4_exception exception = { };
1520 int err;
1521 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001522 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001523 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001524 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1525 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001526 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001527 break;
1528 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 } while (exception.retry);
1530 return err;
1531}
1532
Trond Myklebust864472e2006-01-03 09:55:15 +01001533static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1534{
1535 struct nfs_open_context *ctx;
1536 int ret;
1537
1538 ctx = nfs4_state_find_open_context(state);
1539 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001540 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001541 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001542 put_nfs_open_context(ctx);
1543 return ret;
1544}
1545
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001546static 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 -07001547{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001548 switch (err) {
1549 default:
1550 printk(KERN_ERR "NFS: %s: unhandled error "
1551 "%d.\n", __func__, err);
1552 case 0:
1553 case -ENOENT:
1554 case -ESTALE:
1555 break;
1556 case -NFS4ERR_BADSESSION:
1557 case -NFS4ERR_BADSLOT:
1558 case -NFS4ERR_BAD_HIGH_SLOT:
1559 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1560 case -NFS4ERR_DEADSESSION:
1561 set_bit(NFS_DELEGATED_STATE, &state->flags);
1562 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1563 return -EAGAIN;
1564 case -NFS4ERR_STALE_CLIENTID:
1565 case -NFS4ERR_STALE_STATEID:
1566 set_bit(NFS_DELEGATED_STATE, &state->flags);
1567 case -NFS4ERR_EXPIRED:
1568 /* Don't recall a delegation if it was lost */
1569 nfs4_schedule_lease_recovery(server->nfs_client);
1570 return -EAGAIN;
1571 case -NFS4ERR_DELEG_REVOKED:
1572 case -NFS4ERR_ADMIN_REVOKED:
1573 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001574 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001575 nfs_inode_find_state_and_recover(state->inode,
1576 stateid);
1577 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001578 return 0;
1579 case -NFS4ERR_DELAY:
1580 case -NFS4ERR_GRACE:
1581 set_bit(NFS_DELEGATED_STATE, &state->flags);
1582 ssleep(1);
1583 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001584 case -ENOMEM:
1585 case -NFS4ERR_DENIED:
1586 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1587 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 return err;
1590}
1591
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001592int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1593{
1594 struct nfs_server *server = NFS_SERVER(state->inode);
1595 struct nfs4_opendata *opendata;
1596 int err;
1597
1598 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1599 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1600 if (IS_ERR(opendata))
1601 return PTR_ERR(opendata);
1602 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1603 err = nfs4_open_recover(opendata, state);
1604 nfs4_opendata_put(opendata);
1605 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1606}
1607
Chuck Leverbe05c862013-08-09 12:49:47 -04001608static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1609{
1610 struct nfs4_opendata *data = calldata;
1611
1612 nfs40_setup_sequence(data->o_arg.server, &data->o_arg.seq_args,
1613 &data->o_res.seq_res, task);
1614}
1615
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001616static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1617{
1618 struct nfs4_opendata *data = calldata;
1619
Chuck Leverbe05c862013-08-09 12:49:47 -04001620 nfs40_sequence_done(task, &data->o_res.seq_res);
1621
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001622 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001623 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001624 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001625 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001626 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001627 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001628 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001629}
1630
1631static void nfs4_open_confirm_release(void *calldata)
1632{
1633 struct nfs4_opendata *data = calldata;
1634 struct nfs4_state *state = NULL;
1635
1636 /* If this request hasn't been cancelled, do nothing */
1637 if (data->cancelled == 0)
1638 goto out_free;
1639 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001640 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001641 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001642 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001643 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001644 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001645out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001646 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001647}
1648
1649static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04001650 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001651 .rpc_call_done = nfs4_open_confirm_done,
1652 .rpc_release = nfs4_open_confirm_release,
1653};
1654
1655/*
1656 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1657 */
1658static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1659{
1660 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1661 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001662 struct rpc_message msg = {
1663 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1664 .rpc_argp = &data->c_arg,
1665 .rpc_resp = &data->c_res,
1666 .rpc_cred = data->owner->so_cred,
1667 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001668 struct rpc_task_setup task_setup_data = {
1669 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001670 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001671 .callback_ops = &nfs4_open_confirm_ops,
1672 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001673 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001674 .flags = RPC_TASK_ASYNC,
1675 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 int status;
1677
Chuck Leverbe05c862013-08-09 12:49:47 -04001678 nfs4_init_sequence(&data->o_arg.seq_args, &data->o_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001679 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001680 data->rpc_done = 0;
1681 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001682 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001683 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001684 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001685 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001686 status = nfs4_wait_for_completion_rpc_task(task);
1687 if (status != 0) {
1688 data->cancelled = 1;
1689 smp_wmb();
1690 } else
1691 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001692 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 return status;
1694}
1695
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001696static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001698 struct nfs4_opendata *data = calldata;
1699 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001700 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001701
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001702 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001703 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001704 /*
1705 * Check if we still need to send an OPEN call, or if we can use
1706 * a delegation instead.
1707 */
1708 if (data->state != NULL) {
1709 struct nfs_delegation *delegation;
1710
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001711 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001712 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001713 rcu_read_lock();
1714 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001715 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
Trond Myklebustcd4c9be2013-04-23 14:46:25 -04001716 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
Trond Myklebust652f89f2011-12-09 19:05:58 -05001717 can_open_delegated(delegation, data->o_arg.fmode))
1718 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001719 rcu_read_unlock();
1720 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001721 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04001722 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust8188df12013-04-23 14:31:19 -04001723 switch (data->o_arg.claim) {
1724 case NFS4_OPEN_CLAIM_PREVIOUS:
1725 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1726 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001727 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04001728 case NFS4_OPEN_CLAIM_FH:
1729 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001730 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1731 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001732 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001733 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001734 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001735 &data->o_res.seq_res,
1736 task) != 0)
1737 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04001738
1739 /* Set the create mode (note dependency on the session type) */
1740 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1741 if (data->o_arg.open_flags & O_EXCL) {
1742 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1743 if (nfs4_has_persistent_session(clp))
1744 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1745 else if (clp->cl_mvops->minor_version > 0)
1746 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1747 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04001748 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001749unlock_no_action:
1750 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001751out_no_action:
1752 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001753out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001754 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001755}
1756
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001757static void nfs4_open_done(struct rpc_task *task, void *calldata)
1758{
1759 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001761 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001762
Trond Myklebust14516c32010-07-31 14:29:06 -04001763 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1764 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001765
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001766 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001767 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1768 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001769 case S_IFREG:
1770 break;
1771 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001772 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001773 break;
1774 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001775 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001776 break;
1777 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001778 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001779 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001780 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001781 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001782 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1783 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001784 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001785 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001786}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001787
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001788static void nfs4_open_release(void *calldata)
1789{
1790 struct nfs4_opendata *data = calldata;
1791 struct nfs4_state *state = NULL;
1792
1793 /* If this request hasn't been cancelled, do nothing */
1794 if (data->cancelled == 0)
1795 goto out_free;
1796 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001797 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001798 goto out_free;
1799 /* In case we need an open_confirm, no cleanup! */
1800 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1801 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001802 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001803 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001804 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001805out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001806 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001807}
1808
1809static const struct rpc_call_ops nfs4_open_ops = {
1810 .rpc_call_prepare = nfs4_open_prepare,
1811 .rpc_call_done = nfs4_open_done,
1812 .rpc_release = nfs4_open_release,
1813};
1814
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001815static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001816{
1817 struct inode *dir = data->dir->d_inode;
1818 struct nfs_server *server = NFS_SERVER(dir);
1819 struct nfs_openargs *o_arg = &data->o_arg;
1820 struct nfs_openres *o_res = &data->o_res;
1821 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001822 struct rpc_message msg = {
1823 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1824 .rpc_argp = o_arg,
1825 .rpc_resp = o_res,
1826 .rpc_cred = data->owner->so_cred,
1827 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001828 struct rpc_task_setup task_setup_data = {
1829 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001830 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001831 .callback_ops = &nfs4_open_ops,
1832 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001833 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001834 .flags = RPC_TASK_ASYNC,
1835 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001836 int status;
1837
Chuck Levera9c92d62013-08-09 12:48:18 -04001838 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001839 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001840 data->rpc_done = 0;
1841 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001842 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001843 data->is_recover = 0;
1844 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001845 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001846 data->is_recover = 1;
1847 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04001848 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001849 if (IS_ERR(task))
1850 return PTR_ERR(task);
1851 status = nfs4_wait_for_completion_rpc_task(task);
1852 if (status != 0) {
1853 data->cancelled = 1;
1854 smp_wmb();
1855 } else
1856 status = data->rpc_status;
1857 rpc_put_task(task);
1858
1859 return status;
1860}
1861
1862static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1863{
1864 struct inode *dir = data->dir->d_inode;
1865 struct nfs_openres *o_res = &data->o_res;
1866 int status;
1867
1868 status = nfs4_run_open_task(data, 1);
1869 if (status != 0 || !data->rpc_done)
1870 return status;
1871
Trond Myklebust6926afd2012-01-07 13:22:46 -05001872 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1873
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001874 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1875 status = _nfs4_proc_open_confirm(data);
1876 if (status != 0)
1877 return status;
1878 }
1879
1880 return status;
1881}
1882
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001883static int nfs4_opendata_access(struct rpc_cred *cred,
1884 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001885 struct nfs4_state *state, fmode_t fmode,
1886 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001887{
1888 struct nfs_access_entry cache;
1889 u32 mask;
1890
1891 /* access call failed or for some reason the server doesn't
1892 * support any access modes -- defer access call until later */
1893 if (opendata->o_res.access_supported == 0)
1894 return 0;
1895
1896 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001897 /* don't check MAY_WRITE - a newly created file may not have
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001898 * write mode bits, but POSIX allows the creating process to write.
1899 * use openflags to check for exec, because fmode won't
1900 * always have FMODE_EXEC set when file open for exec. */
1901 if (openflags & __FMODE_EXEC) {
1902 /* ONLY check for exec rights */
1903 mask = MAY_EXEC;
1904 } else if (fmode & FMODE_READ)
1905 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001906
1907 cache.cred = cred;
1908 cache.jiffies = jiffies;
1909 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1910 nfs_access_add_cache(state->inode, &cache);
1911
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001912 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001913 return 0;
1914
1915 /* even though OPEN succeeded, access is denied. Close the file */
1916 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001917 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001918}
1919
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001920/*
1921 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1922 */
1923static int _nfs4_proc_open(struct nfs4_opendata *data)
1924{
1925 struct inode *dir = data->dir->d_inode;
1926 struct nfs_server *server = NFS_SERVER(dir);
1927 struct nfs_openargs *o_arg = &data->o_arg;
1928 struct nfs_openres *o_res = &data->o_res;
1929 int status;
1930
1931 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001932 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001933 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001934 if (status != 0) {
1935 if (status == -NFS4ERR_BADNAME &&
1936 !(o_arg->open_flags & O_CREAT))
1937 return -ENOENT;
1938 return status;
1939 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001940
Trond Myklebust6926afd2012-01-07 13:22:46 -05001941 nfs_fattr_map_and_free_names(server, &data->f_attr);
1942
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001943 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001944 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001945 if (o_arg->open_flags & O_EXCL)
1946 data->file_created = 1;
1947 else if (o_res->cinfo.before != o_res->cinfo.after)
1948 data->file_created = 1;
1949 }
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 Myklebust5bc2afc2013-09-23 18:01:28 -04002193 struct nfs4_label *label,
2194 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195{
2196 struct nfs4_state_owner *sp;
2197 struct nfs4_state *state = NULL;
2198 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002199 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002200 struct dentry *dentry = ctx->dentry;
2201 struct rpc_cred *cred = ctx->cred;
2202 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2203 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002204 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002205 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002206 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
2208 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002210 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2211 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2213 goto out_err;
2214 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002215 status = nfs4_recover_expired_lease(server);
2216 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002217 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04002218 if (dentry->d_inode != NULL)
2219 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002220 status = -ENOMEM;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002221 if (dentry->d_inode)
2222 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002223 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002224 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002225 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002226 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
David Quigley14c43f72013-05-22 12:50:43 -04002228 if (label) {
2229 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2230 if (IS_ERR(olabel)) {
2231 status = PTR_ERR(olabel);
2232 goto err_opendata_put;
2233 }
2234 }
2235
Andy Adamson82be4172012-05-23 05:02:35 -04002236 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2237 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2238 if (!opendata->f_attr.mdsthreshold)
David Quigley14c43f72013-05-22 12:50:43 -04002239 goto err_free_label;
Trond Myklebust1549210f2012-06-05 09:16:47 -04002240 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002241 }
Al Viro82a2c1b2011-06-22 18:30:55 -04002242 if (dentry->d_inode != NULL)
2243 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002244
Trond Myklebust3efb9722013-05-29 13:17:04 -04002245 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002246 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002247 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002248 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002249
Trond Myklebust549b19c2013-04-16 18:42:34 -04002250 if ((opendata->o_arg.open_flags & O_EXCL) &&
2251 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002252 nfs4_exclusive_attrset(opendata, sattr);
2253
2254 nfs_fattr_init(opendata->o_res.f_attr);
2255 status = nfs4_do_setattr(state->inode, cred,
2256 opendata->o_res.f_attr, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002257 state, label, olabel);
2258 if (status == 0) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002259 nfs_setattr_update_inode(state->inode, sattr);
David Quigley1775fd32013-05-22 12:50:42 -04002260 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
David Quigleyaa9c2662013-05-22 12:50:44 -04002261 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
David Quigley1775fd32013-05-22 12:50:42 -04002262 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002263 }
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002264 if (opendata->file_created)
2265 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002266
2267 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2268 *ctx_th = opendata->f_attr.mdsthreshold;
2269 else
2270 kfree(opendata->f_attr.mdsthreshold);
2271 opendata->f_attr.mdsthreshold = NULL;
2272
David Quigley14c43f72013-05-22 12:50:43 -04002273 nfs4_label_free(olabel);
2274
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002275 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002278err_free_label:
2279 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002280err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002281 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002282 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002283err_put_state_owner:
2284 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 return status;
2287}
2288
2289
Andy Adamson82be4172012-05-23 05:02:35 -04002290static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002291 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002292 int flags,
2293 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002294 struct nfs4_label *label,
2295 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002297 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 struct nfs4_exception exception = { };
2299 struct nfs4_state *res;
2300 int status;
2301
2302 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002303 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002304 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002305 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 if (status == 0)
2307 break;
2308 /* NOTE: BAD_SEQID means the server and client disagree about the
2309 * book-keeping w.r.t. state-changing operations
2310 * (OPEN/CLOSE/LOCK/LOCKU...)
2311 * It is actually a sign of a bug on the client or on the server.
2312 *
2313 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002314 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 * have unhashed the old state_owner for us, and that we can
2316 * therefore safely retry using a new one. We should still warn
2317 * the user though...
2318 */
2319 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002320 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002321 " returned a bad sequence-id error!\n",
2322 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 exception.retry = 1;
2324 continue;
2325 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002326 /*
2327 * BAD_STATEID on OPEN means that the server cancelled our
2328 * state before it received the OPEN_CONFIRM.
2329 * Recover by retrying the request as per the discussion
2330 * on Page 181 of RFC3530.
2331 */
2332 if (status == -NFS4ERR_BAD_STATEID) {
2333 exception.retry = 1;
2334 continue;
2335 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002336 if (status == -EAGAIN) {
2337 /* We must have found a delegation */
2338 exception.retry = 1;
2339 continue;
2340 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002341 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2342 continue;
2343 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 status, &exception));
2345 } while (exception.retry);
2346 return res;
2347}
2348
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002349static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2350 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002351 struct nfs4_state *state, struct nfs4_label *ilabel,
2352 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002354 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002356 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 .iap = sattr,
2358 .server = server,
2359 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002360 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 };
2362 struct nfs_setattrres res = {
2363 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002364 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 .server = server,
2366 };
2367 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002368 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2369 .rpc_argp = &arg,
2370 .rpc_resp = &res,
2371 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002373 unsigned long timestamp = jiffies;
Trond Myklebustee3ae842013-04-29 10:35:36 -04002374 fmode_t fmode;
2375 bool truncate;
Trond Myklebust65e43082005-08-16 11:49:44 -04002376 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
David Quigleyaa9c2662013-05-22 12:50:44 -04002378 arg.bitmask = nfs4_bitmask(server, ilabel);
2379 if (ilabel)
2380 arg.bitmask = nfs4_bitmask(server, olabel);
2381
Trond Myklebust0e574af2005-10-27 22:12:38 -04002382 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
Trond Myklebustee3ae842013-04-29 10:35:36 -04002384 /* Servers should only apply open mode checks for file size changes */
2385 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2386 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2387
2388 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2389 /* Use that stateid */
2390 } else if (truncate && state != NULL && nfs4_valid_open_stateid(state)) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002391 struct nfs_lockowner lockowner = {
2392 .l_owner = current->files,
2393 .l_pid = current->tgid,
2394 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002395 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002396 &lockowner);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002397 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002398 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
Bryan Schumaker7c513052011-03-24 17:12:24 +00002400 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002401 if (status == 0 && state != NULL)
2402 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002403 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404}
2405
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002406static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2407 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002408 struct nfs4_state *state, struct nfs4_label *ilabel,
2409 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002411 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002412 struct nfs4_exception exception = {
2413 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002414 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002415 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 int err;
2417 do {
David Quigley1775fd32013-05-22 12:50:42 -04002418 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
Trond Myklebustc1578b72013-08-12 16:58:42 -04002419 trace_nfs4_setattr(inode, err);
Trond Myklebust451146b2012-04-18 16:29:11 -04002420 switch (err) {
2421 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002422 if (!(sattr->ia_valid & ATTR_SIZE)) {
2423 pr_warn_once("NFSv4: server %s is incorrectly "
2424 "applying open mode checks to "
2425 "a SETATTR that is not "
2426 "changing file size.\n",
2427 server->nfs_client->cl_hostname);
2428 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002429 if (state && !(state->state & FMODE_WRITE)) {
2430 err = -EBADF;
2431 if (sattr->ia_valid & ATTR_OPEN)
2432 err = -EACCES;
2433 goto out;
2434 }
2435 }
2436 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002438out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 return err;
2440}
2441
2442struct nfs4_closedata {
2443 struct inode *inode;
2444 struct nfs4_state *state;
2445 struct nfs_closeargs arg;
2446 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002447 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002448 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002449 bool roc;
2450 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451};
2452
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002453static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002454{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002455 struct nfs4_closedata *calldata = data;
2456 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002457 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002458
Fred Isamanf7e89172011-01-06 11:36:32 +00002459 if (calldata->roc)
2460 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002461 nfs4_put_open_state(calldata->state);
2462 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002463 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002464 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002465 kfree(calldata);
2466}
2467
Trond Myklebust88069f72009-12-08 08:33:16 -05002468static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2469 fmode_t fmode)
2470{
2471 spin_lock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002472 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002473 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
2474 case FMODE_WRITE:
2475 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2476 break;
2477 case FMODE_READ:
2478 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2479 break;
2480 case 0:
2481 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2482 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2483 clear_bit(NFS_OPEN_STATE, &state->flags);
2484 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002485 spin_unlock(&state->owner->so_lock);
2486}
2487
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002488static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002490 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 struct nfs_server *server = NFS_SERVER(calldata->inode);
2493
Chuck Levera3ca5652012-03-01 17:00:40 -05002494 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002495 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2496 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002497 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 /* hmm. we are done with the inode, and in the process of freeing
2499 * the state_owner. we keep this around to process errors
2500 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 switch (task->tk_status) {
2502 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002503 if (calldata->roc)
2504 pnfs_roc_set_barrier(state->inode,
2505 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002506 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002507 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002508 nfs4_close_clear_stateid_flags(state,
2509 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 break;
2511 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002512 case -NFS4ERR_OLD_STATEID:
2513 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002515 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002516 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002518 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2519 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002521 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002522 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002523 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524}
2525
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002526static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002528 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002529 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002530 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002531 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002532
Chuck Levera3ca5652012-03-01 17:00:40 -05002533 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002534 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002535 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002536
Trond Myklebust88069f72009-12-08 08:33:16 -05002537 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2538 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002539 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002540 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002541 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002542 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002543 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2544 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2545 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002546 }
2547 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002548 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2549 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2550 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002551 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002552 }
Trond Myklebust5d422302013-03-14 16:57:48 -04002553 if (!nfs4_valid_open_stateid(state))
2554 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002555 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002556
2557 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002558 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002559 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002560 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002561
Fred Isamanf7e89172011-01-06 11:36:32 +00002562 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002563 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002564 if (calldata->roc &&
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002565 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2566 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002567 goto out_wait;
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002568 }
Fred Isamanf7e89172011-01-06 11:36:32 +00002569 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002570
Trond Myklebust516a6af2005-10-27 22:12:41 -04002571 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002572 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002573 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002574 &calldata->arg.seq_args,
2575 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002576 task) != 0)
2577 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002578 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002579 return;
2580out_no_action:
2581 task->tk_action = NULL;
2582out_wait:
2583 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584}
2585
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002586static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002587 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002588 .rpc_call_done = nfs4_close_done,
2589 .rpc_release = nfs4_free_closedata,
2590};
2591
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592/*
2593 * It is possible for data to be read/written from a mem-mapped file
2594 * after the sys_close call (which hits the vfs layer as a flush).
2595 * This means that we can't safely call nfsv4 close on a file until
2596 * the inode is cleared. This in turn means that we are not good
2597 * NFSv4 citizens - we do not indicate to the server to update the file's
2598 * share state even when we are done with one of the three share
2599 * stateid's in the inode.
2600 *
2601 * NOTE: Caller must be holding the sp->so_owner semaphore!
2602 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002603int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002605 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002607 struct nfs4_state_owner *sp = state->owner;
2608 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002609 struct rpc_message msg = {
2610 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2611 .rpc_cred = state->owner->so_cred,
2612 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002613 struct rpc_task_setup task_setup_data = {
2614 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002615 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002616 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002617 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002618 .flags = RPC_TASK_ASYNC,
2619 };
Trond Myklebust95121352005-10-18 14:20:12 -07002620 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04002622 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
2623 &task_setup_data.rpc_client, &msg);
2624
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002625 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002627 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04002628 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002629 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002631 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002632 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002634 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002635 if (calldata->arg.seqid == NULL)
2636 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002637 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002638 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002639 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002640 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002641 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002642 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002643 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002644
Trond Myklebust1174dd12010-12-21 10:52:24 -05002645 msg.rpc_argp = &calldata->arg;
2646 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002647 task_setup_data.callback_data = calldata;
2648 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002649 if (IS_ERR(task))
2650 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002651 status = 0;
2652 if (wait)
2653 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002654 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002655 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002656out_free_calldata:
2657 kfree(calldata);
2658out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002659 nfs4_put_open_state(state);
2660 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002661 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662}
2663
Trond Myklebust2b484292010-09-17 10:56:51 -04002664static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002665nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
2666 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04002669 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2670
2671 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672
Trond Myklebust565277f2007-10-15 18:17:53 -04002673 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002674 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04002675
2676 nfs4_label_release_security(label);
2677
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002678 if (IS_ERR(state))
2679 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04002680 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681}
2682
Trond Myklebust1185a552009-12-03 15:54:02 -05002683static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002684{
2685 if (ctx->state == NULL)
2686 return;
2687 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002688 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002689 else
Al Viro643168c2011-06-22 18:20:23 -04002690 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002691}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692
2693static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2694{
Benny Halevy43652ad2009-04-01 09:21:54 -04002695 struct nfs4_server_caps_arg args = {
2696 .fhandle = fhandle,
2697 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 struct nfs4_server_caps_res res = {};
2699 struct rpc_message msg = {
2700 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002701 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 .rpc_resp = &res,
2703 };
2704 int status;
2705
Bryan Schumaker7c513052011-03-24 17:12:24 +00002706 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 if (status == 0) {
2708 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002709 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2710 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2711 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2712 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2713 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2715 server->caps |= NFS_CAP_ACLS;
2716 if (res.has_links != 0)
2717 server->caps |= NFS_CAP_HARDLINKS;
2718 if (res.has_symlinks != 0)
2719 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002720 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2721 server->caps |= NFS_CAP_FILEID;
2722 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2723 server->caps |= NFS_CAP_MODE;
2724 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2725 server->caps |= NFS_CAP_NLINK;
2726 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2727 server->caps |= NFS_CAP_OWNER;
2728 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2729 server->caps |= NFS_CAP_OWNER_GROUP;
2730 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2731 server->caps |= NFS_CAP_ATIME;
2732 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2733 server->caps |= NFS_CAP_CTIME;
2734 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2735 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04002736#ifdef CONFIG_NFS_V4_SECURITY_LABEL
2737 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
2738 server->caps |= NFS_CAP_SECURITY_LABEL;
2739#endif
2740 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
2741 sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002742
David Quigleyaa9c2662013-05-22 12:50:44 -04002743 if (server->caps & NFS_CAP_SECURITY_LABEL) {
2744 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2745 res.attr_bitmask[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2746 }
Trond Myklebusta65318b2009-03-11 14:10:28 -04002747 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2748 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2749 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002751 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002753
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 return status;
2755}
2756
Trond Myklebust55a97592006-06-09 09:34:19 -04002757int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758{
2759 struct nfs4_exception exception = { };
2760 int err;
2761 do {
2762 err = nfs4_handle_exception(server,
2763 _nfs4_server_capabilities(server, fhandle),
2764 &exception);
2765 } while (exception.retry);
2766 return err;
2767}
2768
2769static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2770 struct nfs_fsinfo *info)
2771{
David Quigleyaa9c2662013-05-22 12:50:44 -04002772 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04002774 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 };
2776 struct nfs4_lookup_res res = {
2777 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002778 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 .fh = fhandle,
2780 };
2781 struct rpc_message msg = {
2782 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2783 .rpc_argp = &args,
2784 .rpc_resp = &res,
2785 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002786
David Quigleyaa9c2662013-05-22 12:50:44 -04002787 bitmask[0] = nfs4_fattr_bitmap[0];
2788 bitmask[1] = nfs4_fattr_bitmap[1];
2789 /*
2790 * Process the label in the upcoming getfattr
2791 */
2792 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
2793
Trond Myklebust0e574af2005-10-27 22:12:38 -04002794 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002795 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796}
2797
2798static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2799 struct nfs_fsinfo *info)
2800{
2801 struct nfs4_exception exception = { };
2802 int err;
2803 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002804 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04002805 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002806 switch (err) {
2807 case 0:
2808 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002809 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002810 default:
2811 err = nfs4_handle_exception(server, err, &exception);
2812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002814out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 return err;
2816}
2817
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002818static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2819 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2820{
Trond Myklebustc2190662013-08-26 19:23:04 -04002821 struct rpc_auth_create_args auth_args = {
2822 .pseudoflavor = flavor,
2823 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002824 struct rpc_auth *auth;
2825 int ret;
2826
Trond Myklebustc2190662013-08-26 19:23:04 -04002827 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002828 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04002829 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002830 goto out;
2831 }
2832 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002833out:
2834 return ret;
2835}
2836
Chuck Lever9a744ba2013-03-16 15:56:02 -04002837/*
2838 * Retry pseudoroot lookup with various security flavors. We do this when:
2839 *
2840 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2841 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2842 *
2843 * Returns zero on success, or a negative NFS4ERR value, or a
2844 * negative errno value.
2845 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002846static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002847 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848{
Chuck Lever9a744ba2013-03-16 15:56:02 -04002849 /* Per 3530bis 15.33.5 */
2850 static const rpc_authflavor_t flav_array[] = {
2851 RPC_AUTH_GSS_KRB5P,
2852 RPC_AUTH_GSS_KRB5I,
2853 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04002854 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04002855 RPC_AUTH_NULL,
2856 };
2857 int status = -EPERM;
2858 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859
Chuck Lever9a744ba2013-03-16 15:56:02 -04002860 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002861 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002862 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002863 continue;
2864 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002865 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04002866
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002867 /*
2868 * -EACCESS could mean that the user doesn't have correct permissions
2869 * to access the mount. It could also mean that we tried to mount
2870 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2871 * existing mount programs don't handle -EACCES very well so it should
2872 * be mapped to -EPERM instead.
2873 */
2874 if (status == -EACCES)
2875 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002876 return status;
2877}
2878
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002879static int nfs4_do_find_root_sec(struct nfs_server *server,
2880 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
2881{
2882 int mv = server->nfs_client->cl_minorversion;
2883 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
2884}
2885
2886/**
2887 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
2888 * @server: initialized nfs_server handle
2889 * @fhandle: we fill in the pseudo-fs root file handle
2890 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002891 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002892 *
2893 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002894 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002895int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002896 struct nfs_fsinfo *info,
2897 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002898{
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002899 int status;
2900
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002901 switch (auth_probe) {
2902 case false:
2903 status = nfs4_lookup_root(server, fhandle, info);
2904 if (status != -NFS4ERR_WRONGSEC)
2905 break;
2906 /* Did user force a 'sec=' mount option? */
2907 if (server->flags & NFS_MOUNT_SECFLAVOUR)
2908 break;
2909 default:
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002910 status = nfs4_do_find_root_sec(server, fhandle, info);
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002911 }
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002912
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 if (status == 0)
2914 status = nfs4_server_capabilities(server, fhandle);
2915 if (status == 0)
2916 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002917
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002918 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919}
2920
Bryan Schumakerbae36242012-05-10 15:07:31 -04002921static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2922 struct nfs_fsinfo *info)
2923{
2924 int error;
2925 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04002926 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04002927
2928 error = nfs4_server_capabilities(server, mntfh);
2929 if (error < 0) {
2930 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2931 return error;
2932 }
2933
David Quigley14c43f72013-05-22 12:50:43 -04002934 label = nfs4_label_alloc(server, GFP_KERNEL);
2935 if (IS_ERR(label))
2936 return PTR_ERR(label);
2937
David Quigley1775fd32013-05-22 12:50:42 -04002938 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04002939 if (error < 0) {
2940 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04002941 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04002942 }
2943
2944 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2945 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2946 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2947
David Quigley14c43f72013-05-22 12:50:43 -04002948err_free_label:
2949 nfs4_label_free(label);
2950
Bryan Schumakerbae36242012-05-10 15:07:31 -04002951 return error;
2952}
2953
Manoj Naik6b97fd32006-06-09 09:34:29 -04002954/*
2955 * Get locations and (maybe) other attributes of a referral.
2956 * Note that we'll actually follow the referral later when
2957 * we detect fsid mismatch in inode revalidation
2958 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002959static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2960 const struct qstr *name, struct nfs_fattr *fattr,
2961 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002962{
2963 int status = -ENOMEM;
2964 struct page *page = NULL;
2965 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002966
2967 page = alloc_page(GFP_KERNEL);
2968 if (page == NULL)
2969 goto out;
2970 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2971 if (locations == NULL)
2972 goto out;
2973
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002974 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002975 if (status != 0)
2976 goto out;
2977 /* Make sure server returned a different fsid for the referral */
2978 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002979 dprintk("%s: server did not return a different fsid for"
2980 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002981 status = -EIO;
2982 goto out;
2983 }
Andy Adamson533eb462011-06-13 18:25:56 -04002984 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2985 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002986
Andy Adamson533eb462011-06-13 18:25:56 -04002987 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002988 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002989 memset(fhandle, 0, sizeof(struct nfs_fh));
2990out:
2991 if (page)
2992 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002993 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002994 return status;
2995}
2996
David Quigley1775fd32013-05-22 12:50:42 -04002997static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
2998 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999{
3000 struct nfs4_getattr_arg args = {
3001 .fh = fhandle,
3002 .bitmask = server->attr_bitmask,
3003 };
3004 struct nfs4_getattr_res res = {
3005 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003006 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 .server = server,
3008 };
3009 struct rpc_message msg = {
3010 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3011 .rpc_argp = &args,
3012 .rpc_resp = &res,
3013 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003014
3015 args.bitmask = nfs4_bitmask(server, label);
3016
Trond Myklebust0e574af2005-10-27 22:12:38 -04003017 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003018 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019}
3020
David Quigley1775fd32013-05-22 12:50:42 -04003021static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3022 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023{
3024 struct nfs4_exception exception = { };
3025 int err;
3026 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003027 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3028 trace_nfs4_getattr(server, fhandle, fattr, err);
3029 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 &exception);
3031 } while (exception.retry);
3032 return err;
3033}
3034
3035/*
3036 * The file is not closed if it is opened due to the a request to change
3037 * the size of the file. The open call will not be needed once the
3038 * VFS layer lookup-intents are implemented.
3039 *
3040 * Close is called when the inode is destroyed.
3041 * If we haven't opened the file for O_WRONLY, we
3042 * need to in the size_change case to obtain a stateid.
3043 *
3044 * Got race?
3045 * Because OPEN is always done by name in nfsv4, it is
3046 * possible that we opened a different file by the same
3047 * name. We can recognize this race condition, but we
3048 * can't do anything about it besides returning an error.
3049 *
3050 * This will be fixed with VFS changes (lookup-intent).
3051 */
3052static int
3053nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3054 struct iattr *sattr)
3055{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003056 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003057 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003058 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003059 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 int status;
3061
Benny Halevy8a1636c2010-07-14 15:43:57 -04003062 if (pnfs_ld_layoutret_on_setattr(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003063 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003064
Trond Myklebust0e574af2005-10-27 22:12:38 -04003065 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066
Andy Adamson26699402012-05-30 16:12:24 -04003067 /* Deal with open(O_TRUNC) */
3068 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003069 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003070
3071 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003072 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003073 return 0;
3074
Trond Myklebustd5308382005-11-04 15:33:38 -05003075 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003076 if (sattr->ia_valid & ATTR_FILE) {
3077 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003078
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003079 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003080 if (ctx) {
3081 cred = ctx->cred;
3082 state = ctx->state;
3083 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003084 }
3085
David Quigley14c43f72013-05-22 12:50:43 -04003086 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3087 if (IS_ERR(label))
3088 return PTR_ERR(label);
3089
3090 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003091 if (status == 0) {
Trond Myklebust65e43082005-08-16 11:49:44 -04003092 nfs_setattr_update_inode(inode, sattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003093 nfs_setsecurity(inode, fattr, label);
3094 }
David Quigley14c43f72013-05-22 12:50:43 -04003095 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 return status;
3097}
3098
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003099static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3100 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003101 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003102{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003103 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003104 int status;
3105 struct nfs4_lookup_arg args = {
3106 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003107 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003108 .name = name,
3109 };
3110 struct nfs4_lookup_res res = {
3111 .server = server,
3112 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003113 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003114 .fh = fhandle,
3115 };
3116 struct rpc_message msg = {
3117 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3118 .rpc_argp = &args,
3119 .rpc_resp = &res,
3120 };
3121
David Quigleyaa9c2662013-05-22 12:50:44 -04003122 args.bitmask = nfs4_bitmask(server, label);
3123
David Howells2b3de442006-08-22 20:06:09 -04003124 nfs_fattr_init(fattr);
3125
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003127 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 dprintk("NFS reply lookup: %d\n", status);
3129 return status;
3130}
3131
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003132static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003133{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003134 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003135 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003136 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3137 fattr->nlink = 2;
3138}
3139
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003140static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3141 struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003142 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003143{
3144 struct nfs4_exception exception = { };
3145 struct rpc_clnt *client = *clnt;
3146 int err;
3147 do {
David Quigley1775fd32013-05-22 12:50:42 -04003148 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003149 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003150 switch (err) {
3151 case -NFS4ERR_BADNAME:
3152 err = -ENOENT;
3153 goto out;
3154 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003155 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003156 goto out;
3157 case -NFS4ERR_WRONGSEC:
3158 err = -EPERM;
3159 if (client != *clnt)
3160 goto out;
Trond Myklebust41d058c2013-09-07 17:49:05 -04003161 /* No security negotiation if the user specified 'sec=' */
3162 if (NFS_SERVER(dir)->flags & NFS_MOUNT_SECFLAVOUR)
3163 goto out;
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003164 client = nfs4_create_sec_client(client, dir, name);
3165 if (IS_ERR(client))
3166 return PTR_ERR(client);
3167
3168 exception.retry = 1;
3169 break;
3170 default:
3171 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3172 }
3173 } while (exception.retry);
3174
3175out:
3176 if (err == 0)
3177 *clnt = client;
3178 else if (client != *clnt)
3179 rpc_shutdown_client(client);
3180
3181 return err;
3182}
3183
Bryan Schumaker80a16b22012-04-27 13:27:46 -04003184static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003185 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3186 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003188 int status;
3189 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003190
David Quigley1775fd32013-05-22 12:50:42 -04003191 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003192 if (client != NFS_CLIENT(dir)) {
3193 rpc_shutdown_client(client);
3194 nfs_fixup_secinfo_attributes(fattr);
3195 }
3196 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197}
3198
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003199struct rpc_clnt *
3200nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3201 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3202{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003203 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003204 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003205
David Quigley1775fd32013-05-22 12:50:42 -04003206 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003207 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003208 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003209 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003210}
3211
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3213{
Trond Myklebust76b32992007-08-10 17:45:11 -04003214 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 struct nfs4_accessargs args = {
3216 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003217 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003219 struct nfs4_accessres res = {
3220 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003221 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 struct rpc_message msg = {
3223 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3224 .rpc_argp = &args,
3225 .rpc_resp = &res,
3226 .rpc_cred = entry->cred,
3227 };
3228 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003229 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230
3231 /*
3232 * Determine which access bits we want to ask for...
3233 */
3234 if (mode & MAY_READ)
3235 args.access |= NFS4_ACCESS_READ;
3236 if (S_ISDIR(inode->i_mode)) {
3237 if (mode & MAY_WRITE)
3238 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3239 if (mode & MAY_EXEC)
3240 args.access |= NFS4_ACCESS_LOOKUP;
3241 } else {
3242 if (mode & MAY_WRITE)
3243 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3244 if (mode & MAY_EXEC)
3245 args.access |= NFS4_ACCESS_EXECUTE;
3246 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003247
3248 res.fattr = nfs_alloc_fattr();
3249 if (res.fattr == NULL)
3250 return -ENOMEM;
3251
Bryan Schumaker7c513052011-03-24 17:12:24 +00003252 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003254 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003255 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003257 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 return status;
3259}
3260
3261static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3262{
3263 struct nfs4_exception exception = { };
3264 int err;
3265 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003266 err = _nfs4_proc_access(inode, entry);
3267 trace_nfs4_access(inode, err);
3268 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 &exception);
3270 } while (exception.retry);
3271 return err;
3272}
3273
3274/*
3275 * TODO: For the time being, we don't try to get any attributes
3276 * along with any of the zero-copy operations READ, READDIR,
3277 * READLINK, WRITE.
3278 *
3279 * In the case of the first three, we want to put the GETATTR
3280 * after the read-type operation -- this is because it is hard
3281 * to predict the length of a GETATTR response in v4, and thus
3282 * align the READ data correctly. This means that the GETATTR
3283 * may end up partially falling into the page cache, and we should
3284 * shift it into the 'tail' of the xdr_buf before processing.
3285 * To do this efficiently, we need to know the total length
3286 * of data received, which doesn't seem to be available outside
3287 * of the RPC layer.
3288 *
3289 * In the case of WRITE, we also want to put the GETATTR after
3290 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003291 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 *
3293 * Both of these changes to the XDR layer would in fact be quite
3294 * minor, but I decided to leave them for a subsequent patch.
3295 */
3296static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3297 unsigned int pgbase, unsigned int pglen)
3298{
3299 struct nfs4_readlink args = {
3300 .fh = NFS_FH(inode),
3301 .pgbase = pgbase,
3302 .pglen = pglen,
3303 .pages = &page,
3304 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003305 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 struct rpc_message msg = {
3307 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3308 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003309 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 };
3311
Bryan Schumaker7c513052011-03-24 17:12:24 +00003312 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 -07003313}
3314
3315static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3316 unsigned int pgbase, unsigned int pglen)
3317{
3318 struct nfs4_exception exception = { };
3319 int err;
3320 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003321 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3322 trace_nfs4_readlink(inode, err);
3323 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 &exception);
3325 } while (exception.retry);
3326 return err;
3327}
3328
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003330 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332static int
3333nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003334 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335{
David Quigleyaa9c2662013-05-22 12:50:44 -04003336 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003337 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 struct nfs4_state *state;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003339 int opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 int status = 0;
3341
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003342 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3343 if (IS_ERR(ctx))
3344 return PTR_ERR(ctx);
3345
David Quigleyaa9c2662013-05-22 12:50:44 -04003346 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3347
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003348 sattr->ia_mode &= ~current_umask();
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003349 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, &opened);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 if (IS_ERR(state)) {
3351 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003352 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003355 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003356 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 return status;
3358}
3359
3360static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3361{
Trond Myklebust16e42952005-10-27 22:12:44 -04003362 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003363 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003365 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003367 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003368 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003369 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003371 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3372 .rpc_argp = &args,
3373 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003375 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003376
Bryan Schumaker7c513052011-03-24 17:12:24 +00003377 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003378 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003379 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 return status;
3381}
3382
3383static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3384{
3385 struct nfs4_exception exception = { };
3386 int err;
3387 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003388 err = _nfs4_proc_remove(dir, name);
3389 trace_nfs4_remove(dir, name, err);
3390 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 &exception);
3392 } while (exception.retry);
3393 return err;
3394}
3395
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003396static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003398 struct nfs_server *server = NFS_SERVER(dir);
3399 struct nfs_removeargs *args = msg->rpc_argp;
3400 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003402 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04003404 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003405
3406 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407}
3408
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003409static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3410{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003411 nfs4_setup_sequence(NFS_SERVER(data->dir),
3412 &data->args.seq_args,
3413 &data->res.seq_res,
3414 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415}
3416
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003417static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003419 struct nfs_unlinkdata *data = task->tk_calldata;
3420 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003421
Trond Myklebust14516c32010-07-31 14:29:06 -04003422 if (!nfs4_sequence_done(task, &res->seq_res))
3423 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003424 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003425 return 0;
3426 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003427 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428}
3429
Jeff Laytond3d41522010-09-17 17:31:57 -04003430static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3431{
3432 struct nfs_server *server = NFS_SERVER(dir);
3433 struct nfs_renameargs *arg = msg->rpc_argp;
3434 struct nfs_renameres *res = msg->rpc_resp;
3435
3436 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003437 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04003438 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003439}
3440
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003441static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3442{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003443 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3444 &data->args.seq_args,
3445 &data->res.seq_res,
3446 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003447}
3448
3449static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3450 struct inode *new_dir)
3451{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003452 struct nfs_renamedata *data = task->tk_calldata;
3453 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04003454
3455 if (!nfs4_sequence_done(task, &res->seq_res))
3456 return 0;
3457 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3458 return 0;
3459
3460 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003461 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003462 return 1;
3463}
3464
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3466 struct inode *new_dir, struct qstr *new_name)
3467{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003468 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003469 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 .old_dir = NFS_FH(old_dir),
3471 .new_dir = NFS_FH(new_dir),
3472 .old_name = old_name,
3473 .new_name = new_name,
3474 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003475 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003476 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003477 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 struct rpc_message msg = {
3479 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3480 .rpc_argp = &arg,
3481 .rpc_resp = &res,
3482 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003483 int status = -ENOMEM;
David Quigley14c43f72013-05-22 12:50:43 -04003484
Bryan Schumaker7c513052011-03-24 17:12:24 +00003485 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 if (!status) {
3487 update_changeattr(old_dir, &res.old_cinfo);
3488 update_changeattr(new_dir, &res.new_cinfo);
3489 }
3490 return status;
3491}
3492
3493static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3494 struct inode *new_dir, struct qstr *new_name)
3495{
3496 struct nfs4_exception exception = { };
3497 int err;
3498 do {
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003499 err = _nfs4_proc_rename(old_dir, old_name,
3500 new_dir, new_name);
3501 trace_nfs4_rename(old_dir, old_name, new_dir, new_name, err);
3502 err = nfs4_handle_exception(NFS_SERVER(old_dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 &exception);
3504 } while (exception.retry);
3505 return err;
3506}
3507
3508static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3509{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003510 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 struct nfs4_link_arg arg = {
3512 .fh = NFS_FH(inode),
3513 .dir_fh = NFS_FH(dir),
3514 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003515 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003517 struct nfs4_link_res res = {
3518 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003519 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003520 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 struct rpc_message msg = {
3522 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3523 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003524 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003526 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527
Trond Myklebust136f2622010-04-16 16:22:49 -04003528 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003529 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003530 goto out;
3531
David Quigley14c43f72013-05-22 12:50:43 -04003532 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3533 if (IS_ERR(res.label)) {
3534 status = PTR_ERR(res.label);
3535 goto out;
3536 }
David Quigleyaa9c2662013-05-22 12:50:44 -04003537 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04003538
Bryan Schumaker7c513052011-03-24 17:12:24 +00003539 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003540 if (!status) {
3541 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04003542 status = nfs_post_op_update_inode(inode, res.fattr);
3543 if (!status)
3544 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003545 }
David Quigley14c43f72013-05-22 12:50:43 -04003546
3547
3548 nfs4_label_free(res.label);
3549
Trond Myklebust136f2622010-04-16 16:22:49 -04003550out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003551 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 return status;
3553}
3554
3555static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3556{
3557 struct nfs4_exception exception = { };
3558 int err;
3559 do {
3560 err = nfs4_handle_exception(NFS_SERVER(inode),
3561 _nfs4_proc_link(inode, dir, name),
3562 &exception);
3563 } while (exception.retry);
3564 return err;
3565}
3566
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003567struct nfs4_createdata {
3568 struct rpc_message msg;
3569 struct nfs4_create_arg arg;
3570 struct nfs4_create_res res;
3571 struct nfs_fh fh;
3572 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003573 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003574};
3575
3576static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3577 struct qstr *name, struct iattr *sattr, u32 ftype)
3578{
3579 struct nfs4_createdata *data;
3580
3581 data = kzalloc(sizeof(*data), GFP_KERNEL);
3582 if (data != NULL) {
3583 struct nfs_server *server = NFS_SERVER(dir);
3584
David Quigley14c43f72013-05-22 12:50:43 -04003585 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3586 if (IS_ERR(data->label))
3587 goto out_free;
3588
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003589 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3590 data->msg.rpc_argp = &data->arg;
3591 data->msg.rpc_resp = &data->res;
3592 data->arg.dir_fh = NFS_FH(dir);
3593 data->arg.server = server;
3594 data->arg.name = name;
3595 data->arg.attrs = sattr;
3596 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04003597 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003598 data->res.server = server;
3599 data->res.fh = &data->fh;
3600 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003601 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003602 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003603 }
3604 return data;
David Quigley14c43f72013-05-22 12:50:43 -04003605out_free:
3606 kfree(data);
3607 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003608}
3609
3610static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3611{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003612 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003613 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003614 if (status == 0) {
3615 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04003616 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003617 }
3618 return status;
3619}
3620
3621static void nfs4_free_createdata(struct nfs4_createdata *data)
3622{
David Quigley14c43f72013-05-22 12:50:43 -04003623 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003624 kfree(data);
3625}
3626
Chuck Lever4f390c12006-08-22 20:06:22 -04003627static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003628 struct page *page, unsigned int len, struct iattr *sattr,
3629 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003631 struct nfs4_createdata *data;
3632 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633
Chuck Lever94a6d752006-08-22 20:06:23 -04003634 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003635 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003636
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003637 status = -ENOMEM;
3638 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3639 if (data == NULL)
3640 goto out;
3641
3642 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3643 data->arg.u.symlink.pages = &page;
3644 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04003645 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003647 status = nfs4_do_create(dir, dentry, data);
3648
3649 nfs4_free_createdata(data);
3650out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 return status;
3652}
3653
Chuck Lever4f390c12006-08-22 20:06:22 -04003654static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003655 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656{
3657 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003658 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04003660
3661 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3662
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003664 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3665 trace_nfs4_symlink(dir, &dentry->d_name, err);
3666 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 &exception);
3668 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003669
3670 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 return err;
3672}
3673
3674static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003675 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003677 struct nfs4_createdata *data;
3678 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003680 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3681 if (data == NULL)
3682 goto out;
3683
David Quigley1775fd32013-05-22 12:50:42 -04003684 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003685 status = nfs4_do_create(dir, dentry, data);
3686
3687 nfs4_free_createdata(data);
3688out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 return status;
3690}
3691
3692static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3693 struct iattr *sattr)
3694{
3695 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003696 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003698
David Quigleyaa9c2662013-05-22 12:50:44 -04003699 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3700
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003701 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003703 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3704 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3705 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 &exception);
3707 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003708 nfs4_label_release_security(label);
3709
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 return err;
3711}
3712
3713static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003714 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715{
3716 struct inode *dir = dentry->d_inode;
3717 struct nfs4_readdir_arg args = {
3718 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003719 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 .pgbase = 0,
3721 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003722 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003723 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 };
3725 struct nfs4_readdir_res res;
3726 struct rpc_message msg = {
3727 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3728 .rpc_argp = &args,
3729 .rpc_resp = &res,
3730 .rpc_cred = cred,
3731 };
3732 int status;
3733
Harvey Harrison3110ff82008-05-02 13:42:44 -07003734 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003735 dentry->d_parent->d_name.name,
3736 dentry->d_name.name,
3737 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003738 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003740 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 -05003741 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003742 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003743 status += args.pgbase;
3744 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003745
3746 nfs_invalidate_atime(dir);
3747
Harvey Harrison3110ff82008-05-02 13:42:44 -07003748 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 return status;
3750}
3751
3752static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003753 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754{
3755 struct nfs4_exception exception = { };
3756 int err;
3757 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003758 err = _nfs4_proc_readdir(dentry, cred, cookie,
3759 pages, count, plus);
3760 trace_nfs4_readdir(dentry->d_inode, err);
3761 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 &exception);
3763 } while (exception.retry);
3764 return err;
3765}
3766
3767static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04003768 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003770 struct nfs4_createdata *data;
3771 int mode = sattr->ia_mode;
3772 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003774 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3775 if (data == NULL)
3776 goto out;
3777
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003779 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003781 data->arg.ftype = NF4BLK;
3782 data->arg.u.device.specdata1 = MAJOR(rdev);
3783 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 }
3785 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003786 data->arg.ftype = NF4CHR;
3787 data->arg.u.device.specdata1 = MAJOR(rdev);
3788 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003789 } else if (!S_ISSOCK(mode)) {
3790 status = -EINVAL;
3791 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 }
David Quigley1775fd32013-05-22 12:50:42 -04003793
David Quigleyaa9c2662013-05-22 12:50:44 -04003794 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003795 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003796out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003797 nfs4_free_createdata(data);
3798out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 return status;
3800}
3801
3802static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3803 struct iattr *sattr, dev_t rdev)
3804{
3805 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003806 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003808
David Quigleyaa9c2662013-05-22 12:50:44 -04003809 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3810
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003811 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003813 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
3814 trace_nfs4_mknod(dir, &dentry->d_name, err);
3815 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 &exception);
3817 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003818
3819 nfs4_label_release_security(label);
3820
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 return err;
3822}
3823
3824static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3825 struct nfs_fsstat *fsstat)
3826{
3827 struct nfs4_statfs_arg args = {
3828 .fh = fhandle,
3829 .bitmask = server->attr_bitmask,
3830 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003831 struct nfs4_statfs_res res = {
3832 .fsstat = fsstat,
3833 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 struct rpc_message msg = {
3835 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3836 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003837 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 };
3839
Trond Myklebust0e574af2005-10-27 22:12:38 -04003840 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003841 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842}
3843
3844static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3845{
3846 struct nfs4_exception exception = { };
3847 int err;
3848 do {
3849 err = nfs4_handle_exception(server,
3850 _nfs4_proc_statfs(server, fhandle, fsstat),
3851 &exception);
3852 } while (exception.retry);
3853 return err;
3854}
3855
3856static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3857 struct nfs_fsinfo *fsinfo)
3858{
3859 struct nfs4_fsinfo_arg args = {
3860 .fh = fhandle,
3861 .bitmask = server->attr_bitmask,
3862 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003863 struct nfs4_fsinfo_res res = {
3864 .fsinfo = fsinfo,
3865 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 struct rpc_message msg = {
3867 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3868 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003869 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 };
3871
Bryan Schumaker7c513052011-03-24 17:12:24 +00003872 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873}
3874
3875static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3876{
3877 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04003878 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 int err;
3880
3881 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04003882 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003883 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04003884 if (err == 0) {
3885 struct nfs_client *clp = server->nfs_client;
3886
3887 spin_lock(&clp->cl_lock);
3888 clp->cl_lease_time = fsinfo->lease_time * HZ;
3889 clp->cl_last_renewal = now;
3890 spin_unlock(&clp->cl_lock);
3891 break;
3892 }
3893 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894 } while (exception.retry);
3895 return err;
3896}
3897
3898static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3899{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003900 int error;
3901
Trond Myklebust0e574af2005-10-27 22:12:38 -04003902 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003903 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003904 if (error == 0) {
3905 /* block layout checks this! */
3906 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003907 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003908 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003909
3910 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911}
3912
3913static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3914 struct nfs_pathconf *pathconf)
3915{
3916 struct nfs4_pathconf_arg args = {
3917 .fh = fhandle,
3918 .bitmask = server->attr_bitmask,
3919 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003920 struct nfs4_pathconf_res res = {
3921 .pathconf = pathconf,
3922 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 struct rpc_message msg = {
3924 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3925 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003926 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 };
3928
3929 /* None of the pathconf attributes are mandatory to implement */
3930 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3931 memset(pathconf, 0, sizeof(*pathconf));
3932 return 0;
3933 }
3934
Trond Myklebust0e574af2005-10-27 22:12:38 -04003935 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003936 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937}
3938
3939static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3940 struct nfs_pathconf *pathconf)
3941{
3942 struct nfs4_exception exception = { };
3943 int err;
3944
3945 do {
3946 err = nfs4_handle_exception(server,
3947 _nfs4_proc_pathconf(server, fhandle, pathconf),
3948 &exception);
3949 } while (exception.retry);
3950 return err;
3951}
3952
Trond Myklebust5521abf2013-03-16 20:54:34 -04003953int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04003954 const struct nfs_open_context *ctx,
3955 const struct nfs_lock_context *l_ctx,
3956 fmode_t fmode)
3957{
3958 const struct nfs_lockowner *lockowner = NULL;
3959
3960 if (l_ctx != NULL)
3961 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003962 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04003963}
3964EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
3965
Trond Myklebust5521abf2013-03-16 20:54:34 -04003966static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
3967 const struct nfs_open_context *ctx,
3968 const struct nfs_lock_context *l_ctx,
3969 fmode_t fmode)
3970{
3971 nfs4_stateid current_stateid;
3972
3973 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode))
3974 return false;
3975 return nfs4_stateid_match(stateid, &current_stateid);
3976}
3977
3978static bool nfs4_error_stateid_expired(int err)
3979{
3980 switch (err) {
3981 case -NFS4ERR_DELEG_REVOKED:
3982 case -NFS4ERR_ADMIN_REVOKED:
3983 case -NFS4ERR_BAD_STATEID:
3984 case -NFS4ERR_STALE_STATEID:
3985 case -NFS4ERR_OLD_STATEID:
3986 case -NFS4ERR_OPENMODE:
3987 case -NFS4ERR_EXPIRED:
3988 return true;
3989 }
3990 return false;
3991}
3992
Benny Halevyd20581a2011-05-22 19:52:03 +03003993void __nfs4_read_done_cb(struct nfs_read_data *data)
3994{
Fred Isamancd841602012-04-20 14:47:44 -04003995 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003996}
3997
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003998static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999{
Fred Isamancd841602012-04-20 14:47:44 -04004000 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004002 trace_nfs4_read(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004003 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004004 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05004005 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004007
Benny Halevyd20581a2011-05-22 19:52:03 +03004008 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05004010 renew_lease(server, data->timestamp);
4011 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012}
4013
Trond Myklebust5521abf2013-03-16 20:54:34 -04004014static bool nfs4_read_stateid_changed(struct rpc_task *task,
4015 struct nfs_readargs *args)
4016{
4017
4018 if (!nfs4_error_stateid_expired(task->tk_status) ||
4019 nfs4_stateid_is_current(&args->stateid,
4020 args->context,
4021 args->lock_context,
4022 FMODE_READ))
4023 return false;
4024 rpc_restart_call_prepare(task);
4025 return true;
4026}
4027
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004028static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
4029{
4030
4031 dprintk("--> %s\n", __func__);
4032
4033 if (!nfs4_sequence_done(task, &data->res.seq_res))
4034 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004035 if (nfs4_read_stateid_changed(task, &data->args))
4036 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03004037 return data->read_done_cb ? data->read_done_cb(task, data) :
4038 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004039}
4040
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004041static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004044 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004045 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Chuck Levera9c92d62013-08-09 12:48:18 -04004046 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047}
4048
NeilBrownef1820f2013-09-04 17:04:49 +10004049static int nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004050{
Trond Myklebust9b206142013-03-17 15:52:00 -04004051 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004052 &data->args.seq_args,
4053 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004054 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004055 return 0;
4056 if (nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4057 data->args.lock_context, FMODE_READ) == -EIO)
4058 return -EIO;
4059 if (unlikely(test_bit(NFS_CONTEXT_BAD, &data->args.context->flags)))
4060 return -EIO;
4061 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062}
4063
Fred Isamanb029bc92011-03-03 15:13:42 +00004064static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065{
Fred Isamancd841602012-04-20 14:47:44 -04004066 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004068 trace_nfs4_write(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004069 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004070 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004071 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004073 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04004075 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004076 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004077 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078}
4079
Trond Myklebust5521abf2013-03-16 20:54:34 -04004080static bool nfs4_write_stateid_changed(struct rpc_task *task,
4081 struct nfs_writeargs *args)
4082{
4083
4084 if (!nfs4_error_stateid_expired(task->tk_status) ||
4085 nfs4_stateid_is_current(&args->stateid,
4086 args->context,
4087 args->lock_context,
4088 FMODE_WRITE))
4089 return false;
4090 rpc_restart_call_prepare(task);
4091 return true;
4092}
4093
Fred Isamanb029bc92011-03-03 15:13:42 +00004094static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
4095{
4096 if (!nfs4_sequence_done(task, &data->res.seq_res))
4097 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004098 if (nfs4_write_stateid_changed(task, &data->args))
4099 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03004100 return data->write_done_cb ? data->write_done_cb(task, data) :
4101 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00004102}
4103
Trond Myklebust5a37f852012-04-28 14:55:16 -04004104static
4105bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00004106{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004107 const struct nfs_pgio_header *hdr = data->header;
4108
4109 /* Don't request attributes for pNFS or O_DIRECT writes */
4110 if (data->ds_clp != NULL || hdr->dreq != NULL)
4111 return false;
4112 /* Otherwise, request attributes if and only if we don't hold
4113 * a delegation
4114 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004115 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004116}
Fred Isamana69aef12011-03-03 15:13:47 +00004117
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004118static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119{
Fred Isamancd841602012-04-20 14:47:44 -04004120 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004121
Trond Myklebust5a37f852012-04-28 14:55:16 -04004122 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00004123 data->args.bitmask = NULL;
4124 data->res.fattr = NULL;
4125 } else
4126 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004127
Fred Isamanb029bc92011-03-03 15:13:42 +00004128 if (!data->write_done_cb)
4129 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004130 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 data->timestamp = jiffies;
4132
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004133 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004134 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135}
4136
NeilBrownef1820f2013-09-04 17:04:49 +10004137static int nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04004138{
Trond Myklebust9b206142013-03-17 15:52:00 -04004139 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004140 &data->args.seq_args,
4141 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004142 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004143 return 0;
4144 if (nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4145 data->args.lock_context, FMODE_WRITE) == -EIO)
4146 return -EIO;
4147 if (unlikely(test_bit(NFS_CONTEXT_BAD, &data->args.context->flags)))
4148 return -EIO;
4149 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150}
4151
Fred Isaman0b7c0152012-04-20 14:47:39 -04004152static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4153{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004154 nfs4_setup_sequence(NFS_SERVER(data->inode),
4155 &data->args.seq_args,
4156 &data->res.seq_res,
4157 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158}
4159
Fred Isaman0b7c0152012-04-20 14:47:39 -04004160static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004163
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004164 trace_nfs4_commit(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004165 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004166 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004167 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004169 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170}
4171
Fred Isaman0b7c0152012-04-20 14:47:39 -04004172static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004173{
4174 if (!nfs4_sequence_done(task, &data->res.seq_res))
4175 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004176 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004177}
4178
Fred Isaman0b7c0152012-04-20 14:47:39 -04004179static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004181 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004182
Fred Isaman0b7c0152012-04-20 14:47:39 -04004183 if (data->commit_done_cb == NULL)
4184 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004185 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004186 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004187 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188}
4189
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004190struct nfs4_renewdata {
4191 struct nfs_client *client;
4192 unsigned long timestamp;
4193};
4194
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195/*
4196 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4197 * standalone procedure for queueing an asynchronous RENEW.
4198 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004199static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004200{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004201 struct nfs4_renewdata *data = calldata;
4202 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004203
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004204 if (atomic_read(&clp->cl_count) > 1)
4205 nfs4_schedule_state_renewal(clp);
4206 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004207 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004208}
4209
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004210static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004212 struct nfs4_renewdata *data = calldata;
4213 struct nfs_client *clp = data->client;
4214 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004216 trace_nfs4_renew_async(clp, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04004218 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004219 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4220 return;
4221 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004222 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004223 return;
4224 }
4225 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004227 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228}
4229
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004230static const struct rpc_call_ops nfs4_renew_ops = {
4231 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004232 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004233};
4234
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004235static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236{
4237 struct rpc_message msg = {
4238 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4239 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004240 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004242 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004244 if (renew_flags == 0)
4245 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004246 if (!atomic_inc_not_zero(&clp->cl_count))
4247 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004248 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004249 if (data == NULL)
4250 return -ENOMEM;
4251 data->client = clp;
4252 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004253 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004254 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255}
4256
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004257static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258{
4259 struct rpc_message msg = {
4260 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4261 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004262 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 };
4264 unsigned long now = jiffies;
4265 int status;
4266
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004267 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 if (status < 0)
4269 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004270 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 return 0;
4272}
4273
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004274static inline int nfs4_server_supports_acls(struct nfs_server *server)
4275{
4276 return (server->caps & NFS_CAP_ACLS)
4277 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
4278 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
4279}
4280
Trond Myklebust21f498c2012-08-24 10:59:25 -04004281/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4282 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004283 * the stack.
4284 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004285#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004286
Neil Hormane9e3d722011-03-04 19:26:03 -05004287static int buf_to_pages_noslab(const void *buf, size_t buflen,
4288 struct page **pages, unsigned int *pgbase)
4289{
4290 struct page *newpage, **spages;
4291 int rc = 0;
4292 size_t len;
4293 spages = pages;
4294
4295 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004296 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004297 newpage = alloc_page(GFP_KERNEL);
4298
4299 if (newpage == NULL)
4300 goto unwind;
4301 memcpy(page_address(newpage), buf, len);
4302 buf += len;
4303 buflen -= len;
4304 *pages++ = newpage;
4305 rc++;
4306 } while (buflen != 0);
4307
4308 return rc;
4309
4310unwind:
4311 for(; rc > 0; rc--)
4312 __free_page(spages[rc-1]);
4313 return -ENOMEM;
4314}
4315
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004316struct nfs4_cached_acl {
4317 int cached;
4318 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004319 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004320};
4321
4322static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004323{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004324 struct nfs_inode *nfsi = NFS_I(inode);
4325
4326 spin_lock(&inode->i_lock);
4327 kfree(nfsi->nfs4_acl);
4328 nfsi->nfs4_acl = acl;
4329 spin_unlock(&inode->i_lock);
4330}
4331
4332static void nfs4_zap_acl_attr(struct inode *inode)
4333{
4334 nfs4_set_cached_acl(inode, NULL);
4335}
4336
4337static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4338{
4339 struct nfs_inode *nfsi = NFS_I(inode);
4340 struct nfs4_cached_acl *acl;
4341 int ret = -ENOENT;
4342
4343 spin_lock(&inode->i_lock);
4344 acl = nfsi->nfs4_acl;
4345 if (acl == NULL)
4346 goto out;
4347 if (buf == NULL) /* user is just asking for length */
4348 goto out_len;
4349 if (acl->cached == 0)
4350 goto out;
4351 ret = -ERANGE; /* see getxattr(2) man page */
4352 if (acl->len > buflen)
4353 goto out;
4354 memcpy(buf, acl->data, acl->len);
4355out_len:
4356 ret = acl->len;
4357out:
4358 spin_unlock(&inode->i_lock);
4359 return ret;
4360}
4361
Sachin Prabhu5794d212012-04-17 14:36:40 +01004362static 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 +00004363{
4364 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004365 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004366
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004367 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004368 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004369 if (acl == NULL)
4370 goto out;
4371 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004372 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004373 } else {
4374 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4375 if (acl == NULL)
4376 goto out;
4377 acl->cached = 0;
4378 }
4379 acl->len = acl_len;
4380out:
4381 nfs4_set_cached_acl(inode, acl);
4382}
4383
Andy Adamsonbf118a32011-12-07 11:55:27 -05004384/*
4385 * The getxattr API returns the required buffer length when called with a
4386 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4387 * the required buf. On a NULL buf, we send a page of data to the server
4388 * guessing that the ACL request can be serviced by a page. If so, we cache
4389 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4390 * the cache. If not so, we throw away the page, and cache the required
4391 * length. The next getxattr call will then produce another round trip to
4392 * the server, this time with the input buf of the required size.
4393 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004394static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004395{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004396 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004397 struct nfs_getaclargs args = {
4398 .fh = NFS_FH(inode),
4399 .acl_pages = pages,
4400 .acl_len = buflen,
4401 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004402 struct nfs_getaclres res = {
4403 .acl_len = buflen,
4404 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004405 struct rpc_message msg = {
4406 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4407 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004408 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004409 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004410 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4411 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004412
Andy Adamsonbf118a32011-12-07 11:55:27 -05004413 /* As long as we're doing a round trip to the server anyway,
4414 * let's be prepared for a page of acl data. */
4415 if (npages == 0)
4416 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004417 if (npages > ARRAY_SIZE(pages))
4418 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004419
Andy Adamsonbf118a32011-12-07 11:55:27 -05004420 for (i = 0; i < npages; i++) {
4421 pages[i] = alloc_page(GFP_KERNEL);
4422 if (!pages[i])
4423 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004424 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004425
4426 /* for decoding across pages */
4427 res.acl_scratch = alloc_page(GFP_KERNEL);
4428 if (!res.acl_scratch)
4429 goto out_free;
4430
Andy Adamsonbf118a32011-12-07 11:55:27 -05004431 args.acl_len = npages * PAGE_SIZE;
4432 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004433
Peng Taode040be2012-01-10 22:42:47 +08004434 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004435 __func__, buf, buflen, npages, args.acl_len);
4436 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4437 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004438 if (ret)
4439 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004440
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004441 /* Handle the case where the passed-in buffer is too short */
4442 if (res.acl_flags & NFS4_ACL_TRUNC) {
4443 /* Did the user only issue a request for the acl length? */
4444 if (buf == NULL)
4445 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004446 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004447 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004448 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004449 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004450 if (buf) {
4451 if (res.acl_len > buflen) {
4452 ret = -ERANGE;
4453 goto out_free;
4454 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004455 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004456 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004457out_ok:
4458 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004459out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004460 for (i = 0; i < npages; i++)
4461 if (pages[i])
4462 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004463 if (res.acl_scratch)
4464 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004465 return ret;
4466}
4467
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004468static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4469{
4470 struct nfs4_exception exception = { };
4471 ssize_t ret;
4472 do {
4473 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04004474 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004475 if (ret >= 0)
4476 break;
4477 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4478 } while (exception.retry);
4479 return ret;
4480}
4481
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004482static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4483{
4484 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004485 int ret;
4486
4487 if (!nfs4_server_supports_acls(server))
4488 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004489 ret = nfs_revalidate_inode(server, inode);
4490 if (ret < 0)
4491 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004492 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4493 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004494 ret = nfs4_read_cached_acl(inode, buf, buflen);
4495 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004496 /* -ENOENT is returned if there is no ACL or if there is an ACL
4497 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004498 return ret;
4499 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004500}
4501
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004502static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004503{
4504 struct nfs_server *server = NFS_SERVER(inode);
4505 struct page *pages[NFS4ACL_MAXPAGES];
4506 struct nfs_setaclargs arg = {
4507 .fh = NFS_FH(inode),
4508 .acl_pages = pages,
4509 .acl_len = buflen,
4510 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004511 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004512 struct rpc_message msg = {
4513 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4514 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004515 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004516 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004517 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004518 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004519
4520 if (!nfs4_server_supports_acls(server))
4521 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004522 if (npages > ARRAY_SIZE(pages))
4523 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004524 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4525 if (i < 0)
4526 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004527 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004528 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004529
4530 /*
4531 * Free each page after tx, so the only ref left is
4532 * held by the network stack
4533 */
4534 for (; i > 0; i--)
4535 put_page(pages[i-1]);
4536
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004537 /*
4538 * Acl update can result in inode attribute update.
4539 * so mark the attribute cache invalid.
4540 */
4541 spin_lock(&inode->i_lock);
4542 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4543 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004544 nfs_access_zap_cache(inode);
4545 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004546 return ret;
4547}
4548
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004549static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4550{
4551 struct nfs4_exception exception = { };
4552 int err;
4553 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004554 err = __nfs4_proc_set_acl(inode, buf, buflen);
4555 trace_nfs4_set_acl(inode, err);
4556 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004557 &exception);
4558 } while (exception.retry);
4559 return err;
4560}
4561
David Quigleyaa9c2662013-05-22 12:50:44 -04004562#ifdef CONFIG_NFS_V4_SECURITY_LABEL
4563static int _nfs4_get_security_label(struct inode *inode, void *buf,
4564 size_t buflen)
4565{
4566 struct nfs_server *server = NFS_SERVER(inode);
4567 struct nfs_fattr fattr;
4568 struct nfs4_label label = {0, 0, buflen, buf};
4569
4570 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4571 struct nfs4_getattr_arg args = {
4572 .fh = NFS_FH(inode),
4573 .bitmask = bitmask,
4574 };
4575 struct nfs4_getattr_res res = {
4576 .fattr = &fattr,
4577 .label = &label,
4578 .server = server,
4579 };
4580 struct rpc_message msg = {
4581 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4582 .rpc_argp = &args,
4583 .rpc_resp = &res,
4584 };
4585 int ret;
4586
4587 nfs_fattr_init(&fattr);
4588
4589 ret = rpc_call_sync(server->client, &msg, 0);
4590 if (ret)
4591 return ret;
4592 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4593 return -ENOENT;
4594 if (buflen < label.len)
4595 return -ERANGE;
4596 return 0;
4597}
4598
4599static int nfs4_get_security_label(struct inode *inode, void *buf,
4600 size_t buflen)
4601{
4602 struct nfs4_exception exception = { };
4603 int err;
4604
4605 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4606 return -EOPNOTSUPP;
4607
4608 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004609 err = _nfs4_get_security_label(inode, buf, buflen);
4610 trace_nfs4_get_security_label(inode, err);
4611 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004612 &exception);
4613 } while (exception.retry);
4614 return err;
4615}
4616
4617static int _nfs4_do_set_security_label(struct inode *inode,
4618 struct nfs4_label *ilabel,
4619 struct nfs_fattr *fattr,
4620 struct nfs4_label *olabel)
4621{
4622
4623 struct iattr sattr = {0};
4624 struct nfs_server *server = NFS_SERVER(inode);
4625 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4626 struct nfs_setattrargs args = {
4627 .fh = NFS_FH(inode),
4628 .iap = &sattr,
4629 .server = server,
4630 .bitmask = bitmask,
4631 .label = ilabel,
4632 };
4633 struct nfs_setattrres res = {
4634 .fattr = fattr,
4635 .label = olabel,
4636 .server = server,
4637 };
4638 struct rpc_message msg = {
4639 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
4640 .rpc_argp = &args,
4641 .rpc_resp = &res,
4642 };
4643 int status;
4644
4645 nfs4_stateid_copy(&args.stateid, &zero_stateid);
4646
4647 status = rpc_call_sync(server->client, &msg, 0);
4648 if (status)
4649 dprintk("%s failed: %d\n", __func__, status);
4650
4651 return status;
4652}
4653
4654static int nfs4_do_set_security_label(struct inode *inode,
4655 struct nfs4_label *ilabel,
4656 struct nfs_fattr *fattr,
4657 struct nfs4_label *olabel)
4658{
4659 struct nfs4_exception exception = { };
4660 int err;
4661
4662 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004663 err = _nfs4_do_set_security_label(inode, ilabel,
4664 fattr, olabel);
4665 trace_nfs4_set_security_label(inode, err);
4666 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004667 &exception);
4668 } while (exception.retry);
4669 return err;
4670}
4671
4672static int
4673nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4674{
4675 struct nfs4_label ilabel, *olabel = NULL;
4676 struct nfs_fattr fattr;
4677 struct rpc_cred *cred;
4678 struct inode *inode = dentry->d_inode;
4679 int status;
4680
4681 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4682 return -EOPNOTSUPP;
4683
4684 nfs_fattr_init(&fattr);
4685
4686 ilabel.pi = 0;
4687 ilabel.lfs = 0;
4688 ilabel.label = (char *)buf;
4689 ilabel.len = buflen;
4690
4691 cred = rpc_lookup_cred();
4692 if (IS_ERR(cred))
4693 return PTR_ERR(cred);
4694
4695 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4696 if (IS_ERR(olabel)) {
4697 status = -PTR_ERR(olabel);
4698 goto out;
4699 }
4700
4701 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4702 if (status == 0)
4703 nfs_setsecurity(inode, &fattr, olabel);
4704
4705 nfs4_label_free(olabel);
4706out:
4707 put_rpccred(cred);
4708 return status;
4709}
4710#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4711
4712
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004714nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004716 struct nfs_client *clp = server->nfs_client;
4717
4718 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 return 0;
4720 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004721 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004722 case -NFS4ERR_ADMIN_REVOKED:
4723 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004724 if (state == NULL)
4725 break;
4726 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004727 case -NFS4ERR_OPENMODE:
4728 if (state == NULL)
4729 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004730 if (nfs4_schedule_stateid_recovery(server, state) < 0)
Chuck Lever9f51a782013-10-17 14:13:13 -04004731 goto recovery_failed;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004732 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004733 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004734 if (state != NULL) {
4735 if (nfs4_schedule_stateid_recovery(server, state) < 0)
Chuck Lever9f51a782013-10-17 14:13:13 -04004736 goto recovery_failed;
Trond Myklebust5d422302013-03-14 16:57:48 -04004737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004739 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004740 nfs4_schedule_lease_recovery(clp);
4741 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004742#if defined(CONFIG_NFS_V4_1)
4743 case -NFS4ERR_BADSESSION:
4744 case -NFS4ERR_BADSLOT:
4745 case -NFS4ERR_BAD_HIGH_SLOT:
4746 case -NFS4ERR_DEADSESSION:
4747 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4748 case -NFS4ERR_SEQ_FALSE_RETRY:
4749 case -NFS4ERR_SEQ_MISORDERED:
4750 dprintk("%s ERROR %d, Reset session\n", __func__,
4751 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004752 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Chuck Leverf1478c12013-10-17 14:13:07 -04004753 goto restart_call;
Andy Adamson4745e312009-04-01 09:22:42 -04004754#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004756 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004757 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004759 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 case -NFS4ERR_OLD_STATEID:
Chuck Leverf1478c12013-10-17 14:13:07 -04004761 goto restart_call;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 }
4763 task->tk_status = nfs4_map_errors(task->tk_status);
4764 return 0;
Chuck Lever9f51a782013-10-17 14:13:13 -04004765recovery_failed:
Trond Myklebust5d422302013-03-14 16:57:48 -04004766 task->tk_status = -EIO;
4767 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004768wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004769 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004770 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4771 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
Chuck Leverf1478c12013-10-17 14:13:07 -04004772restart_call:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004773 task->tk_status = 0;
4774 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775}
4776
Chuck Leverf0920752012-05-21 22:45:41 -04004777static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4778 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004779{
4780 __be32 verf[2];
4781
Chuck Lever2c820d92012-05-21 22:45:33 -04004782 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4783 /* An impossible timestamp guarantees this value
4784 * will never match a generated boot time. */
4785 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04004786 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04004787 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004788 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04004789 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
4790 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04004791 }
Chuck Levercd937102012-03-02 17:14:31 -05004792 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4793}
4794
Chuck Levere984a552012-09-14 17:24:21 -04004795static unsigned int
4796nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4797 char *buf, size_t len)
4798{
4799 unsigned int result;
4800
4801 rcu_read_lock();
4802 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4803 clp->cl_ipaddr,
4804 rpc_peeraddr2str(clp->cl_rpcclient,
4805 RPC_DISPLAY_ADDR),
4806 rpc_peeraddr2str(clp->cl_rpcclient,
4807 RPC_DISPLAY_PROTO));
4808 rcu_read_unlock();
4809 return result;
4810}
4811
4812static unsigned int
4813nfs4_init_uniform_client_string(const struct nfs_client *clp,
4814 char *buf, size_t len)
4815{
Trond Myklebust55b59292013-07-24 16:36:35 -04004816 const char *nodename = clp->cl_rpcclient->cl_nodename;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004817
4818 if (nfs4_client_id_uniquifier[0] != '\0')
Trond Myklebust55b59292013-07-24 16:36:35 -04004819 return scnprintf(buf, len, "Linux NFSv%u.%u %s/%s",
4820 clp->rpc_ops->version,
4821 clp->cl_minorversion,
4822 nfs4_client_id_uniquifier,
4823 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004824 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4825 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004826 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004827}
4828
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004829/**
4830 * nfs4_proc_setclientid - Negotiate client ID
4831 * @clp: state data structure
4832 * @program: RPC program for NFSv4 callback service
4833 * @port: IP port number for NFS4 callback service
4834 * @cred: RPC credential to use for this call
4835 * @res: where to place the result
4836 *
4837 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4838 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004839int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4840 unsigned short port, struct rpc_cred *cred,
4841 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842{
4843 nfs4_verifier sc_verifier;
4844 struct nfs4_setclientid setclientid = {
4845 .sc_verifier = &sc_verifier,
4846 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004847 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 };
4849 struct rpc_message msg = {
4850 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4851 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004852 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004853 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004855 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856
Chuck Leverde734832012-07-11 16:30:50 -04004857 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004858 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004859 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4860 setclientid.sc_name_len =
4861 nfs4_init_uniform_client_string(clp,
4862 setclientid.sc_name,
4863 sizeof(setclientid.sc_name));
4864 else
4865 setclientid.sc_name_len =
4866 nfs4_init_nonuniform_client_string(clp,
4867 setclientid.sc_name,
4868 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004869 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004870 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004871 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Djalal Harounife2d5392013-06-18 17:58:12 +01004872 sizeof(setclientid.sc_netid), "%s",
Chuck Leverd4d3c502007-12-10 14:57:09 -05004873 rpc_peeraddr2str(clp->cl_rpcclient,
4874 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004875 rcu_read_unlock();
4876 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004877 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 clp->cl_ipaddr, port >> 8, port & 255);
4879
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004880 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4881 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4882 setclientid.sc_name_len, setclientid.sc_name);
4883 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004884 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004885 dprintk("NFS reply setclientid: %d\n", status);
4886 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887}
4888
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004889/**
4890 * nfs4_proc_setclientid_confirm - Confirm client ID
4891 * @clp: state data structure
4892 * @res: result of a previous SETCLIENTID
4893 * @cred: RPC credential to use for this call
4894 *
4895 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4896 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004897int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004898 struct nfs4_setclientid_res *arg,
4899 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901 struct rpc_message msg = {
4902 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004903 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004904 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 int status;
4907
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004908 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4909 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4910 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004911 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004912 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004913 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 return status;
4915}
4916
Trond Myklebustfe650402006-01-03 09:55:18 +01004917struct nfs4_delegreturndata {
4918 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004919 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004920 struct nfs_fh fh;
4921 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004922 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004923 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004924 int rpc_status;
4925};
4926
Trond Myklebustfe650402006-01-03 09:55:18 +01004927static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4928{
4929 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004930
Trond Myklebust14516c32010-07-31 14:29:06 -04004931 if (!nfs4_sequence_done(task, &data->res.seq_res))
4932 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004933
Trond Myklebustca8acf82013-08-13 10:36:56 -04004934 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004935 switch (task->tk_status) {
4936 case -NFS4ERR_STALE_STATEID:
4937 case -NFS4ERR_EXPIRED:
4938 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004939 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004940 break;
4941 default:
4942 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4943 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004944 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004945 return;
4946 }
4947 }
4948 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004949}
4950
4951static void nfs4_delegreturn_release(void *calldata)
4952{
Trond Myklebustfe650402006-01-03 09:55:18 +01004953 kfree(calldata);
4954}
4955
Andy Adamson938e1012009-04-01 09:22:28 -04004956static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4957{
4958 struct nfs4_delegreturndata *d_data;
4959
4960 d_data = (struct nfs4_delegreturndata *)data;
4961
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004962 nfs4_setup_sequence(d_data->res.server,
4963 &d_data->args.seq_args,
4964 &d_data->res.seq_res,
4965 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004966}
Andy Adamson938e1012009-04-01 09:22:28 -04004967
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004968static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004969 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01004970 .rpc_call_done = nfs4_delegreturn_done,
4971 .rpc_release = nfs4_delegreturn_release,
4972};
4973
Trond Myklebuste6f81072008-01-24 18:14:34 -05004974static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004975{
4976 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004977 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004978 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004979 struct rpc_message msg = {
4980 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4981 .rpc_cred = cred,
4982 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004983 struct rpc_task_setup task_setup_data = {
4984 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004985 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004986 .callback_ops = &nfs4_delegreturn_ops,
4987 .flags = RPC_TASK_ASYNC,
4988 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004989 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004990
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004991 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004992 if (data == NULL)
4993 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04004994 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004995 data->args.fhandle = &data->fh;
4996 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004997 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004998 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004999 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005000 data->res.fattr = &data->fattr;
5001 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005002 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005003 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005004 data->rpc_status = 0;
5005
Trond Myklebustc970aa82007-07-14 15:39:59 -04005006 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005007 msg.rpc_argp = &data->args;
5008 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005009 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005010 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005011 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005012 if (!issync)
5013 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005014 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005015 if (status != 0)
5016 goto out;
5017 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005018 if (status == 0)
5019 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5020 else
5021 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005022out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005023 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005024 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025}
5026
Trond Myklebuste6f81072008-01-24 18:14:34 -05005027int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028{
5029 struct nfs_server *server = NFS_SERVER(inode);
5030 struct nfs4_exception exception = { };
5031 int err;
5032 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005033 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Trond Myklebustca8acf82013-08-13 10:36:56 -04005034 trace_nfs4_delegreturn(inode, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 switch (err) {
5036 case -NFS4ERR_STALE_STATEID:
5037 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 case 0:
5039 return 0;
5040 }
5041 err = nfs4_handle_exception(server, err, &exception);
5042 } while (exception.retry);
5043 return err;
5044}
5045
5046#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5047#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5048
5049/*
5050 * sleep, with exponential backoff, and retry the LOCK operation.
5051 */
5052static unsigned long
5053nfs4_set_lock_task_retry(unsigned long timeout)
5054{
Colin Cross416ad3c2013-05-06 23:50:06 +00005055 freezable_schedule_timeout_killable_unsafe(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 timeout <<= 1;
5057 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5058 return NFS4_LOCK_MAXTIMEOUT;
5059 return timeout;
5060}
5061
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5063{
5064 struct inode *inode = state->inode;
5065 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005066 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005067 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005069 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005071 struct nfs_lockt_res res = {
5072 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073 };
5074 struct rpc_message msg = {
5075 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5076 .rpc_argp = &arg,
5077 .rpc_resp = &res,
5078 .rpc_cred = state->owner->so_cred,
5079 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080 struct nfs4_lock_state *lsp;
5081 int status;
5082
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005083 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005084 status = nfs4_set_lock_state(state, request);
5085 if (status != 0)
5086 goto out;
5087 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005088 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005089 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005090 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005091 switch (status) {
5092 case 0:
5093 request->fl_type = F_UNLCK;
5094 break;
5095 case -NFS4ERR_DENIED:
5096 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005098 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005099 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005100out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 return status;
5102}
5103
5104static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5105{
5106 struct nfs4_exception exception = { };
5107 int err;
5108
5109 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005110 err = _nfs4_proc_getlk(state, cmd, request);
5111 trace_nfs4_get_lock(request, state, cmd, err);
5112 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113 &exception);
5114 } while (exception.retry);
5115 return err;
5116}
5117
5118static int do_vfs_lock(struct file *file, struct file_lock *fl)
5119{
5120 int res = 0;
5121 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5122 case FL_POSIX:
5123 res = posix_lock_file_wait(file, fl);
5124 break;
5125 case FL_FLOCK:
5126 res = flock_lock_file_wait(file, fl);
5127 break;
5128 default:
5129 BUG();
5130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 return res;
5132}
5133
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005134struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005135 struct nfs_locku_args arg;
5136 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005137 struct nfs4_lock_state *lsp;
5138 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005139 struct file_lock fl;
5140 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005141 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005142};
5143
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005144static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5145 struct nfs_open_context *ctx,
5146 struct nfs4_lock_state *lsp,
5147 struct nfs_seqid *seqid)
5148{
5149 struct nfs4_unlockdata *p;
5150 struct inode *inode = lsp->ls_state->inode;
5151
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005152 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005153 if (p == NULL)
5154 return NULL;
5155 p->arg.fh = NFS_FH(inode);
5156 p->arg.fl = &p->fl;
5157 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005158 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005159 p->arg.stateid = &lsp->ls_stateid;
5160 p->lsp = lsp;
5161 atomic_inc(&lsp->ls_count);
5162 /* Ensure we don't close file until we're done freeing locks! */
5163 p->ctx = get_nfs_open_context(ctx);
5164 memcpy(&p->fl, fl, sizeof(p->fl));
5165 p->server = NFS_SERVER(inode);
5166 return p;
5167}
5168
Trond Myklebust06f814a2006-01-03 09:55:07 +01005169static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005170{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005171 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005172 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005173 nfs4_put_lock_state(calldata->lsp);
5174 put_nfs_open_context(calldata->ctx);
5175 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005176}
5177
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005178static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005179{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005180 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005181
Trond Myklebust14516c32010-07-31 14:29:06 -04005182 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5183 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005184 switch (task->tk_status) {
5185 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05005186 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
5187 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005188 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005189 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005190 case -NFS4ERR_BAD_STATEID:
5191 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005192 case -NFS4ERR_STALE_STATEID:
5193 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005194 break;
5195 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005196 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005197 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005198 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005199 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005200}
5201
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005202static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005203{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005204 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005206 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005207 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04005208 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005209 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005210 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005211 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005212 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005213 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005214 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005215 &calldata->res.seq_res,
5216 task) != 0)
5217 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005218 return;
5219out_no_action:
5220 task->tk_action = NULL;
5221out_wait:
5222 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223}
5224
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005225static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005226 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005227 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005228 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005229};
5230
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005231static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5232 struct nfs_open_context *ctx,
5233 struct nfs4_lock_state *lsp,
5234 struct nfs_seqid *seqid)
5235{
5236 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005237 struct rpc_message msg = {
5238 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5239 .rpc_cred = ctx->cred,
5240 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005241 struct rpc_task_setup task_setup_data = {
5242 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005243 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005244 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005245 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005246 .flags = RPC_TASK_ASYNC,
5247 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005248
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005249 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5250 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5251
Frank Filz137d6ac2007-07-09 15:32:29 -07005252 /* Ensure this is an unlock - when canceling a lock, the
5253 * canceled lock is passed in, and it won't be an unlock.
5254 */
5255 fl->fl_type = F_UNLCK;
5256
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005257 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5258 if (data == NULL) {
5259 nfs_free_seqid(seqid);
5260 return ERR_PTR(-ENOMEM);
5261 }
5262
Chuck Levera9c92d62013-08-09 12:48:18 -04005263 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005264 msg.rpc_argp = &data->arg;
5265 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005266 task_setup_data.callback_data = data;
5267 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005268}
5269
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5271{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005272 struct inode *inode = state->inode;
5273 struct nfs4_state_owner *sp = state->owner;
5274 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005275 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005276 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005277 struct rpc_task *task;
5278 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005279 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280
Trond Myklebust9b073572006-06-29 16:38:34 -04005281 status = nfs4_set_lock_state(state, request);
5282 /* Unlock _before_ we do the RPC call */
5283 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005284 /* Exclude nfs_delegation_claim_locks() */
5285 mutex_lock(&sp->so_delegreturn_mutex);
5286 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005287 down_read(&nfsi->rwsem);
5288 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
5289 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005290 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005291 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005292 }
5293 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005294 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005295 if (status != 0)
5296 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005297 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005298 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005299 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5300 goto out;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005301 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005302 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005303 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04005304 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005305 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005306 status = PTR_ERR(task);
5307 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005308 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005309 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005310 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005311out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005312 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005313 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005314 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315}
5316
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005317struct nfs4_lockdata {
5318 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005319 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005320 struct nfs4_lock_state *lsp;
5321 struct nfs_open_context *ctx;
5322 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005323 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005324 int rpc_status;
5325 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005326 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005327};
5328
5329static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005330 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5331 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005332{
5333 struct nfs4_lockdata *p;
5334 struct inode *inode = lsp->ls_state->inode;
5335 struct nfs_server *server = NFS_SERVER(inode);
5336
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005337 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005338 if (p == NULL)
5339 return NULL;
5340
5341 p->arg.fh = NFS_FH(inode);
5342 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005343 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005344 if (p->arg.open_seqid == NULL)
5345 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005346 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005347 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005348 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005349 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04005350 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005351 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005352 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005353 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005354 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005355 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005356 atomic_inc(&lsp->ls_count);
5357 p->ctx = get_nfs_open_context(ctx);
5358 memcpy(&p->fl, fl, sizeof(p->fl));
5359 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005360out_free_seqid:
5361 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005362out_free:
5363 kfree(p);
5364 return NULL;
5365}
5366
5367static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5368{
5369 struct nfs4_lockdata *data = calldata;
5370 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371
Harvey Harrison3110ff82008-05-02 13:42:44 -07005372 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005373 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005374 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005375 /* Do we need to do an open_to_lock_owner? */
5376 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005377 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005378 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005379 }
Trond Myklebust8e472f32013-04-20 01:30:53 -04005380 data->arg.open_stateid = &state->open_stateid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005381 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005382 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005383 } else
5384 data->arg.new_lock_owner = 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04005385 if (!nfs4_valid_open_stateid(state)) {
5386 data->rpc_status = -EBADF;
5387 task->tk_action = NULL;
5388 goto out_release_open_seqid;
5389 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005390 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005391 if (nfs4_setup_sequence(data->server,
5392 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005393 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005394 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04005395 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04005396out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005397 nfs_release_seqid(data->arg.open_seqid);
5398out_release_lock_seqid:
5399 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005400out_wait:
5401 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005402 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005403}
5404
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005405static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5406{
5407 struct nfs4_lockdata *data = calldata;
5408
Harvey Harrison3110ff82008-05-02 13:42:44 -07005409 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005410
Trond Myklebust14516c32010-07-31 14:29:06 -04005411 if (!nfs4_sequence_done(task, &data->res.seq_res))
5412 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04005413
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005414 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005415 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005416 if (data->rpc_status == 0)
5417 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
5418 else
5419 goto out;
5420 }
5421 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05005422 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005423 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04005424 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005425 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005426out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07005427 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005428}
5429
5430static void nfs4_lock_release(void *calldata)
5431{
5432 struct nfs4_lockdata *data = calldata;
5433
Harvey Harrison3110ff82008-05-02 13:42:44 -07005434 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005435 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005436 if (data->cancelled != 0) {
5437 struct rpc_task *task;
5438 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5439 data->arg.lock_seqid);
5440 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08005441 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005442 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005443 } else
5444 nfs_free_seqid(data->arg.lock_seqid);
5445 nfs4_put_lock_state(data->lsp);
5446 put_nfs_open_context(data->ctx);
5447 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005448 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005449}
5450
5451static const struct rpc_call_ops nfs4_lock_ops = {
5452 .rpc_call_prepare = nfs4_lock_prepare,
5453 .rpc_call_done = nfs4_lock_done,
5454 .rpc_release = nfs4_lock_release,
5455};
5456
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005457static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5458{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005459 switch (error) {
5460 case -NFS4ERR_ADMIN_REVOKED:
5461 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05005462 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005463 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04005464 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05005465 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005466 break;
5467 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005468 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05005469 case -NFS4ERR_EXPIRED:
5470 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005471 };
5472}
5473
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005474static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005475{
5476 struct nfs4_lockdata *data;
5477 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005478 struct rpc_message msg = {
5479 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5480 .rpc_cred = state->owner->so_cred,
5481 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005482 struct rpc_task_setup task_setup_data = {
5483 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005484 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005485 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005486 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005487 .flags = RPC_TASK_ASYNC,
5488 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005489 int ret;
5490
Harvey Harrison3110ff82008-05-02 13:42:44 -07005491 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005492 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005493 fl->fl_u.nfs4_fl.owner,
5494 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005495 if (data == NULL)
5496 return -ENOMEM;
5497 if (IS_SETLKW(cmd))
5498 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04005499 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005500 msg.rpc_argp = &data->arg;
5501 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005502 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005503 if (recovery_type > NFS_LOCK_NEW) {
5504 if (recovery_type == NFS_LOCK_RECLAIM)
5505 data->arg.reclaim = NFS_LOCK_RECLAIM;
5506 nfs4_set_sequence_privileged(&data->arg.seq_args);
5507 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04005508 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005509 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005510 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005511 ret = nfs4_wait_for_completion_rpc_task(task);
5512 if (ret == 0) {
5513 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005514 if (ret)
5515 nfs4_handle_setlk_error(data->server, data->lsp,
5516 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005517 } else
5518 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005519 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005520 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005521 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522}
5523
5524static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5525{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005526 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005527 struct nfs4_exception exception = {
5528 .inode = state->inode,
5529 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005530 int err;
5531
5532 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005533 /* Cache the lock if possible... */
5534 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5535 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005536 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005537 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
Trond Myklebust168667c2010-10-19 19:47:49 -04005538 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00005539 break;
5540 nfs4_handle_exception(server, err, &exception);
5541 } while (exception.retry);
5542 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543}
5544
5545static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5546{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005547 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005548 struct nfs4_exception exception = {
5549 .inode = state->inode,
5550 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005551 int err;
5552
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005553 err = nfs4_set_lock_state(state, request);
5554 if (err != 0)
5555 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04005556 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10005557 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
5558 return 0;
5559 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005560 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005561 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5562 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005563 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005564 trace_nfs4_lock_expired(request, state, F_SETLK, err);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005565 switch (err) {
5566 default:
5567 goto out;
5568 case -NFS4ERR_GRACE:
5569 case -NFS4ERR_DELAY:
5570 nfs4_handle_exception(server, err, &exception);
5571 err = 0;
5572 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005573 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005574out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00005575 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576}
5577
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005578#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005579/**
5580 * nfs41_check_expired_locks - possibly free a lock stateid
5581 *
5582 * @state: NFSv4 state for an inode
5583 *
5584 * Returns NFS_OK if recovery for this stateid is now finished.
5585 * Otherwise a negative NFS4ERR value is returned.
5586 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005587static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005588{
Chuck Levereb64cf92012-07-11 16:30:05 -04005589 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005590 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005591 struct nfs_server *server = NFS_SERVER(state->inode);
5592
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005593 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04005594 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005595 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
5596
5597 status = nfs41_test_stateid(server,
5598 &lsp->ls_stateid,
5599 cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04005600 trace_nfs4_test_lock_stateid(state, lsp, status);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005601 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005602 /* Free the stateid unless the server
5603 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04005604 if (status != -NFS4ERR_BAD_STATEID)
5605 nfs41_free_stateid(server,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005606 &lsp->ls_stateid,
5607 cred);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005608 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005609 ret = status;
5610 }
5611 }
5612 };
5613
5614 return ret;
5615}
5616
5617static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5618{
5619 int status = NFS_OK;
5620
5621 if (test_bit(LK_STATE_IN_USE, &state->flags))
5622 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005623 if (status != NFS_OK)
5624 status = nfs4_lock_expired(state, request);
5625 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005626}
5627#endif
5628
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5630{
Trond Myklebust9a99af492013-02-04 20:17:49 -05005631 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005632 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005633 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005634 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005635 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005637 if ((fl_flags & FL_POSIX) &&
5638 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5639 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005640 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005641 status = nfs4_set_lock_state(state, request);
5642 if (status != 0)
5643 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005644 request->fl_flags |= FL_ACCESS;
5645 status = do_vfs_lock(request->fl_file, request);
5646 if (status < 0)
5647 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005648 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005649 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005650 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005651 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005652 request->fl_flags = fl_flags & ~FL_SLEEP;
5653 status = do_vfs_lock(request->fl_file, request);
5654 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005655 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05005656 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5657 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005658 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005659 if (status != 0)
Trond Myklebust9a99af492013-02-04 20:17:49 -05005660 goto out;
5661 down_read(&nfsi->rwsem);
5662 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5663 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005664 goto out_unlock;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005665 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005666 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005667 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005668 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005669 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5670 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005671out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005672 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005673out:
5674 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675 return status;
5676}
5677
5678static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5679{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005680 struct nfs4_exception exception = {
5681 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005682 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005683 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684 int err;
5685
5686 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005687 err = _nfs4_proc_setlk(state, cmd, request);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005688 trace_nfs4_set_lock(request, state, cmd, err);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005689 if (err == -NFS4ERR_DENIED)
5690 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005692 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693 } while (exception.retry);
5694 return err;
5695}
5696
5697static int
5698nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5699{
5700 struct nfs_open_context *ctx;
5701 struct nfs4_state *state;
5702 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5703 int status;
5704
5705 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005706 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707 state = ctx->state;
5708
5709 if (request->fl_start < 0 || request->fl_end < 0)
5710 return -EINVAL;
5711
Trond Myklebustd9531262009-07-21 19:22:38 -04005712 if (IS_GETLK(cmd)) {
5713 if (state != NULL)
5714 return nfs4_proc_getlk(state, F_GETLK, request);
5715 return 0;
5716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717
5718 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5719 return -EINVAL;
5720
Trond Myklebustd9531262009-07-21 19:22:38 -04005721 if (request->fl_type == F_UNLCK) {
5722 if (state != NULL)
5723 return nfs4_proc_unlck(state, cmd, request);
5724 return 0;
5725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726
Trond Myklebustd9531262009-07-21 19:22:38 -04005727 if (state == NULL)
5728 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005729 /*
5730 * Don't rely on the VFS having checked the file open mode,
5731 * since it won't do this for flock() locks.
5732 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005733 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005734 case F_RDLCK:
5735 if (!(filp->f_mode & FMODE_READ))
5736 return -EBADF;
5737 break;
5738 case F_WRLCK:
5739 if (!(filp->f_mode & FMODE_WRITE))
5740 return -EBADF;
5741 }
5742
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743 do {
5744 status = nfs4_proc_setlk(state, cmd, request);
5745 if ((status != -EAGAIN) || IS_SETLK(cmd))
5746 break;
5747 timeout = nfs4_set_lock_task_retry(timeout);
5748 status = -ERESTARTSYS;
5749 if (signalled())
5750 break;
5751 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752 return status;
5753}
5754
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005755int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05005756{
5757 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05005758 int err;
5759
5760 err = nfs4_set_lock_state(state, fl);
5761 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005762 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04005763 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005764 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05005765}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005766
Trond Myklebustcf470c32012-03-07 13:49:12 -05005767struct nfs_release_lockowner_data {
5768 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005769 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005770 struct nfs_release_lockowner_args args;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005771 struct nfs4_sequence_args seq_args;
5772 struct nfs4_sequence_res seq_res;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005773};
5774
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005775static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
5776{
5777 struct nfs_release_lockowner_data *data = calldata;
5778 nfs40_setup_sequence(data->server,
5779 &data->seq_args, &data->seq_res, task);
5780}
5781
5782static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
5783{
5784 struct nfs_release_lockowner_data *data = calldata;
5785 nfs40_sequence_done(task, &data->seq_res);
5786}
5787
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005788static void nfs4_release_lockowner_release(void *calldata)
5789{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005790 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005791 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005792 kfree(calldata);
5793}
5794
Trond Myklebust17280172012-03-11 13:11:00 -04005795static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005796 .rpc_call_prepare = nfs4_release_lockowner_prepare,
5797 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005798 .rpc_release = nfs4_release_lockowner_release,
5799};
5800
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04005801static int nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005802{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005803 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005804 struct rpc_message msg = {
5805 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5806 };
5807
5808 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005809 return -EINVAL;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005810
Trond Myklebustcf470c32012-03-07 13:49:12 -05005811 data = kmalloc(sizeof(*data), GFP_NOFS);
5812 if (!data)
5813 return -ENOMEM;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005814 nfs4_init_sequence(&data->seq_args, &data->seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05005815 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005816 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005817 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5818 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5819 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005820
Trond Myklebustcf470c32012-03-07 13:49:12 -05005821 msg.rpc_argp = &data->args;
5822 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5823 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005824}
5825
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005826#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5827
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005828static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5829 const void *buf, size_t buflen,
5830 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005831{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005832 if (strcmp(key, "") != 0)
5833 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005834
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005835 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005836}
5837
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005838static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5839 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005840{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005841 if (strcmp(key, "") != 0)
5842 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005843
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005844 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005845}
5846
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005847static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5848 size_t list_len, const char *name,
5849 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005850{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005851 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005852
J. Bruce Fields096455a2006-03-20 23:23:42 -05005853 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5854 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005855
5856 if (list && len <= list_len)
5857 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005858 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005859}
5860
David Quigleyc9bccef2013-05-22 12:50:45 -04005861#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5862static inline int nfs4_server_supports_labels(struct nfs_server *server)
5863{
5864 return server->caps & NFS_CAP_SECURITY_LABEL;
5865}
5866
5867static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
5868 const void *buf, size_t buflen,
5869 int flags, int type)
5870{
5871 if (security_ismaclabel(key))
5872 return nfs4_set_security_label(dentry, buf, buflen);
5873
5874 return -EOPNOTSUPP;
5875}
5876
5877static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
5878 void *buf, size_t buflen, int type)
5879{
5880 if (security_ismaclabel(key))
5881 return nfs4_get_security_label(dentry->d_inode, buf, buflen);
5882 return -EOPNOTSUPP;
5883}
5884
5885static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
5886 size_t list_len, const char *name,
5887 size_t name_len, int type)
5888{
5889 size_t len = 0;
5890
5891 if (nfs_server_capable(dentry->d_inode, NFS_CAP_SECURITY_LABEL)) {
5892 len = security_inode_listsecurity(dentry->d_inode, NULL, 0);
5893 if (list && len <= list_len)
5894 security_inode_listsecurity(dentry->d_inode, list, len);
5895 }
5896 return len;
5897}
5898
5899static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
5900 .prefix = XATTR_SECURITY_PREFIX,
5901 .list = nfs4_xattr_list_nfs4_label,
5902 .get = nfs4_xattr_get_nfs4_label,
5903 .set = nfs4_xattr_set_nfs4_label,
5904};
5905#endif
5906
5907
Andy Adamson533eb462011-06-13 18:25:56 -04005908/*
5909 * nfs_fhget will use either the mounted_on_fileid or the fileid
5910 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005911static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5912{
Andy Adamson533eb462011-06-13 18:25:56 -04005913 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5914 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5915 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005916 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005917 return;
5918
5919 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005920 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005921 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5922 fattr->nlink = 2;
5923}
5924
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005925static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5926 const struct qstr *name,
5927 struct nfs4_fs_locations *fs_locations,
5928 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005929{
5930 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04005931 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005932 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005933 };
5934 struct nfs4_fs_locations_arg args = {
5935 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005936 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005937 .page = page,
5938 .bitmask = bitmask,
5939 };
Benny Halevy22958462009-04-01 09:22:02 -04005940 struct nfs4_fs_locations_res res = {
5941 .fs_locations = fs_locations,
5942 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005943 struct rpc_message msg = {
5944 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5945 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005946 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005947 };
5948 int status;
5949
Harvey Harrison3110ff82008-05-02 13:42:44 -07005950 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005951
5952 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5953 * is not supported */
5954 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5955 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5956 else
5957 bitmask[0] |= FATTR4_WORD0_FILEID;
5958
Trond Myklebustc228fd32007-01-13 02:28:11 -05005959 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005960 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005961 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005962 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005963 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005964 return status;
5965}
5966
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005967int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5968 const struct qstr *name,
5969 struct nfs4_fs_locations *fs_locations,
5970 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005971{
5972 struct nfs4_exception exception = { };
5973 int err;
5974 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04005975 err = _nfs4_proc_fs_locations(client, dir, name,
5976 fs_locations, page);
5977 trace_nfs4_get_fs_locations(dir, name, err);
5978 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005979 &exception);
5980 } while (exception.retry);
5981 return err;
5982}
5983
Chuck Leverb03d7352013-10-17 14:12:50 -04005984/*
5985 * This operation also signals the server that this client is
5986 * performing migration recovery. The server can stop returning
5987 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
5988 * appended to this compound to identify the client ID which is
5989 * performing recovery.
5990 */
5991static int _nfs40_proc_get_locations(struct inode *inode,
5992 struct nfs4_fs_locations *locations,
5993 struct page *page, struct rpc_cred *cred)
5994{
5995 struct nfs_server *server = NFS_SERVER(inode);
5996 struct rpc_clnt *clnt = server->client;
5997 u32 bitmask[2] = {
5998 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
5999 };
6000 struct nfs4_fs_locations_arg args = {
6001 .clientid = server->nfs_client->cl_clientid,
6002 .fh = NFS_FH(inode),
6003 .page = page,
6004 .bitmask = bitmask,
6005 .migration = 1, /* skip LOOKUP */
6006 .renew = 1, /* append RENEW */
6007 };
6008 struct nfs4_fs_locations_res res = {
6009 .fs_locations = locations,
6010 .migration = 1,
6011 .renew = 1,
6012 };
6013 struct rpc_message msg = {
6014 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6015 .rpc_argp = &args,
6016 .rpc_resp = &res,
6017 .rpc_cred = cred,
6018 };
6019 unsigned long now = jiffies;
6020 int status;
6021
6022 nfs_fattr_init(&locations->fattr);
6023 locations->server = server;
6024 locations->nlocations = 0;
6025
6026 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6027 nfs4_set_sequence_privileged(&args.seq_args);
6028 status = nfs4_call_sync_sequence(clnt, server, &msg,
6029 &args.seq_args, &res.seq_res);
6030 if (status)
6031 return status;
6032
6033 renew_lease(server, now);
6034 return 0;
6035}
6036
6037#ifdef CONFIG_NFS_V4_1
6038
6039/*
6040 * This operation also signals the server that this client is
6041 * performing migration recovery. The server can stop asserting
6042 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6043 * performing this operation is identified in the SEQUENCE
6044 * operation in this compound.
6045 *
6046 * When the client supports GETATTR(fs_locations_info), it can
6047 * be plumbed in here.
6048 */
6049static int _nfs41_proc_get_locations(struct inode *inode,
6050 struct nfs4_fs_locations *locations,
6051 struct page *page, struct rpc_cred *cred)
6052{
6053 struct nfs_server *server = NFS_SERVER(inode);
6054 struct rpc_clnt *clnt = server->client;
6055 u32 bitmask[2] = {
6056 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6057 };
6058 struct nfs4_fs_locations_arg args = {
6059 .fh = NFS_FH(inode),
6060 .page = page,
6061 .bitmask = bitmask,
6062 .migration = 1, /* skip LOOKUP */
6063 };
6064 struct nfs4_fs_locations_res res = {
6065 .fs_locations = locations,
6066 .migration = 1,
6067 };
6068 struct rpc_message msg = {
6069 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6070 .rpc_argp = &args,
6071 .rpc_resp = &res,
6072 .rpc_cred = cred,
6073 };
6074 int status;
6075
6076 nfs_fattr_init(&locations->fattr);
6077 locations->server = server;
6078 locations->nlocations = 0;
6079
6080 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6081 nfs4_set_sequence_privileged(&args.seq_args);
6082 status = nfs4_call_sync_sequence(clnt, server, &msg,
6083 &args.seq_args, &res.seq_res);
6084 if (status == NFS4_OK &&
6085 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6086 status = -NFS4ERR_LEASE_MOVED;
6087 return status;
6088}
6089
6090#endif /* CONFIG_NFS_V4_1 */
6091
6092/**
6093 * nfs4_proc_get_locations - discover locations for a migrated FSID
6094 * @inode: inode on FSID that is migrating
6095 * @locations: result of query
6096 * @page: buffer
6097 * @cred: credential to use for this operation
6098 *
6099 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6100 * operation failed, or a negative errno if a local error occurred.
6101 *
6102 * On success, "locations" is filled in, but if the server has
6103 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6104 * asserted.
6105 *
6106 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6107 * from this client that require migration recovery.
6108 */
6109int nfs4_proc_get_locations(struct inode *inode,
6110 struct nfs4_fs_locations *locations,
6111 struct page *page, struct rpc_cred *cred)
6112{
6113 struct nfs_server *server = NFS_SERVER(inode);
6114 struct nfs_client *clp = server->nfs_client;
6115 const struct nfs4_mig_recovery_ops *ops =
6116 clp->cl_mvops->mig_recovery_ops;
6117 struct nfs4_exception exception = { };
6118 int status;
6119
6120 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6121 (unsigned long long)server->fsid.major,
6122 (unsigned long long)server->fsid.minor,
6123 clp->cl_hostname);
6124 nfs_display_fhandle(NFS_FH(inode), __func__);
6125
6126 do {
6127 status = ops->get_locations(inode, locations, page, cred);
6128 if (status != -NFS4ERR_DELAY)
6129 break;
6130 nfs4_handle_exception(server, status, &exception);
6131 } while (exception.retry);
6132 return status;
6133}
6134
Andy Adamson5ec16a82013-08-08 10:57:55 -04006135/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006136 * If 'use_integrity' is true and the state managment nfs_client
6137 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6138 * and the machine credential as per RFC3530bis and RFC5661 Security
6139 * Considerations sections. Otherwise, just use the user cred with the
6140 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04006141 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006142static 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 +00006143{
6144 int status;
6145 struct nfs4_secinfo_arg args = {
6146 .dir_fh = NFS_FH(dir),
6147 .name = name,
6148 };
6149 struct nfs4_secinfo_res res = {
6150 .flavors = flavors,
6151 };
6152 struct rpc_message msg = {
6153 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6154 .rpc_argp = &args,
6155 .rpc_resp = &res,
6156 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006157 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006158 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006159
6160 if (use_integrity) {
6161 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006162 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6163 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006164 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006165
6166 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006167
6168 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6169 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6170
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006171 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6172 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006173 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006174
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006175 if (cred)
6176 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006177
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006178 return status;
6179}
6180
Bryan Schumaker72de53e2012-04-27 13:27:40 -04006181int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6182 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006183{
6184 struct nfs4_exception exception = { };
6185 int err;
6186 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006187 err = -NFS4ERR_WRONGSEC;
6188
6189 /* try to use integrity protection with machine cred */
6190 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6191 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6192
6193 /*
6194 * if unable to use integrity protection, or SECINFO with
6195 * integrity protection returns NFS4ERR_WRONGSEC (which is
6196 * disallowed by spec, but exists in deployed servers) use
6197 * the current filesystem's rpc_client and the user cred.
6198 */
6199 if (err == -NFS4ERR_WRONGSEC)
6200 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6201
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006202 trace_nfs4_secinfo(dir, name, err);
6203 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006204 &exception);
6205 } while (exception.retry);
6206 return err;
6207}
6208
Andy Adamson557134a2009-04-01 09:21:53 -04006209#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04006210/*
Andy Adamson357f54d2010-12-14 10:11:57 -05006211 * Check the exchange flags returned by the server for invalid flags, having
6212 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6213 * DS flags set.
6214 */
6215static int nfs4_check_cl_exchange_flags(u32 flags)
6216{
6217 if (flags & ~EXCHGID4_FLAG_MASK_R)
6218 goto out_inval;
6219 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6220 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6221 goto out_inval;
6222 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6223 goto out_inval;
6224 return NFS_OK;
6225out_inval:
6226 return -NFS4ERR_INVAL;
6227}
6228
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006229static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006230nfs41_same_server_scope(struct nfs41_server_scope *a,
6231 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006232{
6233 if (a->server_scope_sz == b->server_scope_sz &&
6234 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6235 return true;
6236
6237 return false;
6238}
6239
Andy Adamson357f54d2010-12-14 10:11:57 -05006240/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006241 * nfs4_proc_bind_conn_to_session()
6242 *
6243 * The 4.1 client currently uses the same TCP connection for the
6244 * fore and backchannel.
6245 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006246int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006247{
6248 int status;
6249 struct nfs41_bind_conn_to_session_res res;
6250 struct rpc_message msg = {
6251 .rpc_proc =
6252 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
6253 .rpc_argp = clp,
6254 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006255 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006256 };
6257
6258 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006259
6260 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
6261 if (unlikely(res.session == NULL)) {
6262 status = -ENOMEM;
6263 goto out;
6264 }
6265
6266 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006267 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006268 if (status == 0) {
6269 if (memcmp(res.session->sess_id.data,
6270 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6271 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6272 status = -EIO;
6273 goto out_session;
6274 }
6275 if (res.dir != NFS4_CDFS4_BOTH) {
6276 dprintk("NFS: %s: Unexpected direction from server\n",
6277 __func__);
6278 status = -EIO;
6279 goto out_session;
6280 }
6281 if (res.use_conn_in_rdma_mode) {
6282 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6283 __func__);
6284 status = -EIO;
6285 goto out_session;
6286 }
6287 }
6288out_session:
6289 kfree(res.session);
6290out:
6291 dprintk("<-- %s status= %d\n", __func__, status);
6292 return status;
6293}
6294
6295/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006296 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6297 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04006298 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006299static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
6300 .how = SP4_MACH_CRED,
6301 .enforce.u.words = {
6302 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6303 1 << (OP_EXCHANGE_ID - 32) |
6304 1 << (OP_CREATE_SESSION - 32) |
6305 1 << (OP_DESTROY_SESSION - 32) |
6306 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006307 },
6308 .allow.u.words = {
6309 [0] = 1 << (OP_CLOSE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006310 1 << (OP_LOCKU) |
6311 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006312 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006313 1 << (OP_SECINFO_NO_NAME - 32) |
6314 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006315 1 << (OP_FREE_STATEID - 32) |
6316 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006317 }
6318};
6319
6320/*
6321 * Select the state protection mode for client `clp' given the server results
6322 * from exchange_id in `sp'.
6323 *
6324 * Returns 0 on success, negative errno otherwise.
6325 */
6326static int nfs4_sp4_select_mode(struct nfs_client *clp,
6327 struct nfs41_state_protection *sp)
6328{
6329 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
6330 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6331 1 << (OP_EXCHANGE_ID - 32) |
6332 1 << (OP_CREATE_SESSION - 32) |
6333 1 << (OP_DESTROY_SESSION - 32) |
6334 1 << (OP_DESTROY_CLIENTID - 32)
6335 };
6336 unsigned int i;
6337
6338 if (sp->how == SP4_MACH_CRED) {
6339 /* Print state protect result */
6340 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
6341 for (i = 0; i <= LAST_NFS4_OP; i++) {
6342 if (test_bit(i, sp->enforce.u.longs))
6343 dfprintk(MOUNT, " enforce op %d\n", i);
6344 if (test_bit(i, sp->allow.u.longs))
6345 dfprintk(MOUNT, " allow op %d\n", i);
6346 }
6347
6348 /* make sure nothing is on enforce list that isn't supported */
6349 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
6350 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
6351 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6352 return -EINVAL;
6353 }
6354 }
6355
6356 /*
6357 * Minimal mode - state operations are allowed to use machine
6358 * credential. Note this already happens by default, so the
6359 * client doesn't have to do anything more than the negotiation.
6360 *
6361 * NOTE: we don't care if EXCHANGE_ID is in the list -
6362 * we're already using the machine cred for exchange_id
6363 * and will never use a different cred.
6364 */
6365 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
6366 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
6367 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
6368 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
6369 dfprintk(MOUNT, "sp4_mach_cred:\n");
6370 dfprintk(MOUNT, " minimal mode enabled\n");
6371 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
6372 } else {
6373 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6374 return -EINVAL;
6375 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006376
6377 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
6378 test_bit(OP_LOCKU, sp->allow.u.longs)) {
6379 dfprintk(MOUNT, " cleanup mode enabled\n");
6380 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
6381 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006382
6383 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
6384 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
6385 dfprintk(MOUNT, " secinfo mode enabled\n");
6386 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
6387 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006388
6389 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
6390 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
6391 dfprintk(MOUNT, " stateid mode enabled\n");
6392 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
6393 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04006394
6395 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
6396 dfprintk(MOUNT, " write mode enabled\n");
6397 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
6398 }
6399
6400 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
6401 dfprintk(MOUNT, " commit mode enabled\n");
6402 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
6403 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006404 }
6405
6406 return 0;
6407}
6408
6409/*
6410 * _nfs4_proc_exchange_id()
6411 *
6412 * Wrapper for EXCHANGE_ID operation.
6413 */
6414static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
6415 u32 sp4_how)
Benny Halevy99fe60d2009-04-01 09:22:29 -04006416{
6417 nfs4_verifier verifier;
6418 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05006419 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04006420 .client = clp,
Trond Myklebust4f0b4292013-05-20 12:24:03 -04006421 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6422 EXCHGID4_FLAG_BIND_PRINC_STATEID,
Benny Halevy99fe60d2009-04-01 09:22:29 -04006423 };
6424 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04006425 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04006426 };
6427 int status;
6428 struct rpc_message msg = {
6429 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
6430 .rpc_argp = &args,
6431 .rpc_resp = &res,
6432 .rpc_cred = cred,
6433 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04006434
Chuck Leverf0920752012-05-21 22:45:41 -04006435 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04006436 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
6437 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006438 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
6439 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6440 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006441
Chuck Leveracdeb692012-05-21 22:46:16 -04006442 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006443 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04006444 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006445 status = -ENOMEM;
6446 goto out;
6447 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006448
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006449 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006450 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006451 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04006452 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04006453 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04006454 }
6455
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006456 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006457 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006458 status = -ENOMEM;
6459 goto out_server_scope;
6460 }
6461
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006462 switch (sp4_how) {
6463 case SP4_NONE:
6464 args.state_protect.how = SP4_NONE;
6465 break;
6466
6467 case SP4_MACH_CRED:
6468 args.state_protect = nfs4_sp4_mach_cred_request;
6469 break;
6470
6471 default:
6472 /* unsupported! */
6473 WARN_ON_ONCE(1);
6474 status = -EINVAL;
6475 goto out_server_scope;
6476 }
6477
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006478 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006479 trace_nfs4_exchange_id(clp, status);
Chuck Lever177313f2012-05-21 22:44:58 -04006480 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04006481 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006482
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006483 if (status == 0)
6484 status = nfs4_sp4_select_mode(clp, &res.state_protect);
6485
Chuck Lever177313f2012-05-21 22:44:58 -04006486 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04006487 clp->cl_clientid = res.clientid;
6488 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
6489 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
6490 clp->cl_seqid = res.seqid;
6491
Chuck Leveracdeb692012-05-21 22:46:16 -04006492 kfree(clp->cl_serverowner);
6493 clp->cl_serverowner = res.server_owner;
6494 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04006495
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006496 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04006497 kfree(clp->cl_implid);
6498 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006499
Chuck Lever177313f2012-05-21 22:44:58 -04006500 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006501 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006502 res.server_scope)) {
6503 dprintk("%s: server_scope mismatch detected\n",
6504 __func__);
6505 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006506 kfree(clp->cl_serverscope);
6507 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006508 }
6509
Chuck Lever177313f2012-05-21 22:44:58 -04006510 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006511 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006512 goto out;
6513 }
Trond Myklebust32b01312012-05-26 13:41:04 -04006514 } else
6515 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006516
Chuck Leveracdeb692012-05-21 22:46:16 -04006517out_server_owner:
6518 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006519out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006520 kfree(res.server_scope);
6521out:
Chuck Lever177313f2012-05-21 22:44:58 -04006522 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006523 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006524 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006525 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04006526 clp->cl_implid->date.seconds,
6527 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006528 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006529 return status;
6530}
6531
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006532/*
6533 * nfs4_proc_exchange_id()
6534 *
6535 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6536 *
6537 * Since the clientid has expired, all compounds using sessions
6538 * associated with the stale clientid will be returning
6539 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
6540 * be in some phase of session reset.
6541 *
6542 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
6543 */
6544int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
6545{
6546 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
6547 int status;
6548
6549 /* try SP4_MACH_CRED if krb5i/p */
6550 if (authflavor == RPC_AUTH_GSS_KRB5I ||
6551 authflavor == RPC_AUTH_GSS_KRB5P) {
6552 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
6553 if (!status)
6554 return 0;
6555 }
6556
6557 /* try SP4_NONE */
6558 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
6559}
6560
Trond Myklebust66245532012-05-25 17:18:09 -04006561static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
6562 struct rpc_cred *cred)
6563{
6564 struct rpc_message msg = {
6565 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
6566 .rpc_argp = clp,
6567 .rpc_cred = cred,
6568 };
6569 int status;
6570
6571 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006572 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04006573 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04006574 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04006575 "DESTROY_CLIENTID.", status, clp->cl_hostname);
6576 return status;
6577}
6578
6579static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
6580 struct rpc_cred *cred)
6581{
6582 unsigned int loop;
6583 int ret;
6584
6585 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
6586 ret = _nfs4_proc_destroy_clientid(clp, cred);
6587 switch (ret) {
6588 case -NFS4ERR_DELAY:
6589 case -NFS4ERR_CLIENTID_BUSY:
6590 ssleep(1);
6591 break;
6592 default:
6593 return ret;
6594 }
6595 }
6596 return 0;
6597}
6598
6599int nfs4_destroy_clientid(struct nfs_client *clp)
6600{
6601 struct rpc_cred *cred;
6602 int ret = 0;
6603
6604 if (clp->cl_mvops->minor_version < 1)
6605 goto out;
6606 if (clp->cl_exchange_flags == 0)
6607 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04006608 if (clp->cl_preserve_clid)
6609 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04006610 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04006611 ret = nfs4_proc_destroy_clientid(clp, cred);
6612 if (cred)
6613 put_rpccred(cred);
6614 switch (ret) {
6615 case 0:
6616 case -NFS4ERR_STALE_CLIENTID:
6617 clp->cl_exchange_flags = 0;
6618 }
6619out:
6620 return ret;
6621}
6622
Andy Adamson2050f0c2009-04-01 09:22:30 -04006623struct nfs4_get_lease_time_data {
6624 struct nfs4_get_lease_time_args *args;
6625 struct nfs4_get_lease_time_res *res;
6626 struct nfs_client *clp;
6627};
6628
6629static void nfs4_get_lease_time_prepare(struct rpc_task *task,
6630 void *calldata)
6631{
Andy Adamson2050f0c2009-04-01 09:22:30 -04006632 struct nfs4_get_lease_time_data *data =
6633 (struct nfs4_get_lease_time_data *)calldata;
6634
6635 dprintk("--> %s\n", __func__);
6636 /* just setup sequence, do not trigger session recovery
6637 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006638 nfs41_setup_sequence(data->clp->cl_session,
6639 &data->args->la_seq_args,
6640 &data->res->lr_seq_res,
6641 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006642 dprintk("<-- %s\n", __func__);
6643}
6644
6645/*
6646 * Called from nfs4_state_manager thread for session setup, so don't recover
6647 * from sequence operation or clientid errors.
6648 */
6649static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
6650{
6651 struct nfs4_get_lease_time_data *data =
6652 (struct nfs4_get_lease_time_data *)calldata;
6653
6654 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006655 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
6656 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04006657 switch (task->tk_status) {
6658 case -NFS4ERR_DELAY:
6659 case -NFS4ERR_GRACE:
6660 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
6661 rpc_delay(task, NFS4_POLL_RETRY_MIN);
6662 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006663 /* fall through */
6664 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006665 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006666 return;
6667 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04006668 dprintk("<-- %s\n", __func__);
6669}
6670
Trond Myklebust17280172012-03-11 13:11:00 -04006671static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04006672 .rpc_call_prepare = nfs4_get_lease_time_prepare,
6673 .rpc_call_done = nfs4_get_lease_time_done,
6674};
6675
6676int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
6677{
6678 struct rpc_task *task;
6679 struct nfs4_get_lease_time_args args;
6680 struct nfs4_get_lease_time_res res = {
6681 .lr_fsinfo = fsinfo,
6682 };
6683 struct nfs4_get_lease_time_data data = {
6684 .args = &args,
6685 .res = &res,
6686 .clp = clp,
6687 };
6688 struct rpc_message msg = {
6689 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
6690 .rpc_argp = &args,
6691 .rpc_resp = &res,
6692 };
6693 struct rpc_task_setup task_setup = {
6694 .rpc_client = clp->cl_rpcclient,
6695 .rpc_message = &msg,
6696 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006697 .callback_data = &data,
6698 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04006699 };
6700 int status;
6701
Chuck Levera9c92d62013-08-09 12:48:18 -04006702 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006703 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006704 dprintk("--> %s\n", __func__);
6705 task = rpc_run_task(&task_setup);
6706
6707 if (IS_ERR(task))
6708 status = PTR_ERR(task);
6709 else {
6710 status = task->tk_status;
6711 rpc_put_task(task);
6712 }
6713 dprintk("<-- %s return %d\n", __func__, status);
6714
6715 return status;
6716}
6717
Andy Adamsonfc931582009-04-01 09:22:31 -04006718/*
6719 * Initialize the values to be used by the client in CREATE_SESSION
6720 * If nfs4_init_session set the fore channel request and response sizes,
6721 * use them.
6722 *
6723 * Set the back channel max_resp_sz_cached to zero to force the client to
6724 * always set csa_cachethis to FALSE because the current implementation
6725 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
6726 */
6727static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
6728{
Andy Adamson18aad3d2013-06-26 12:21:49 -04006729 unsigned int max_rqst_sz, max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006730
Andy Adamson18aad3d2013-06-26 12:21:49 -04006731 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
6732 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
6733
Andy Adamsonfc931582009-04-01 09:22:31 -04006734 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04006735 args->fc_attrs.max_rqst_sz = max_rqst_sz;
6736 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006737 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05006738 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04006739
6740 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05006741 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04006742 __func__,
6743 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05006744 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04006745
6746 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04006747 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
6748 args->bc_attrs.max_resp_sz = PAGE_SIZE;
6749 args->bc_attrs.max_resp_sz_cached = 0;
6750 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
6751 args->bc_attrs.max_reqs = 1;
6752
6753 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
6754 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
6755 __func__,
6756 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
6757 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
6758 args->bc_attrs.max_reqs);
6759}
6760
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006761static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04006762{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006763 struct nfs4_channel_attrs *sent = &args->fc_attrs;
6764 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
6765
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006766 if (rcvd->max_resp_sz > sent->max_resp_sz)
6767 return -EINVAL;
6768 /*
6769 * Our requested max_ops is the minimum we need; we're not
6770 * prepared to break up compounds into smaller pieces than that.
6771 * So, no point even trying to continue if the server won't
6772 * cooperate:
6773 */
6774 if (rcvd->max_ops < sent->max_ops)
6775 return -EINVAL;
6776 if (rcvd->max_reqs == 0)
6777 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006778 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
6779 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006780 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04006781}
6782
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006783static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
6784{
6785 struct nfs4_channel_attrs *sent = &args->bc_attrs;
6786 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04006787
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006788 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
6789 return -EINVAL;
6790 if (rcvd->max_resp_sz < sent->max_resp_sz)
6791 return -EINVAL;
6792 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
6793 return -EINVAL;
6794 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006795 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006796 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006797 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006798 return -EINVAL;
6799 return 0;
6800}
Andy Adamson8d353012009-04-01 09:22:32 -04006801
Andy Adamson8d353012009-04-01 09:22:32 -04006802static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
6803 struct nfs4_session *session)
6804{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006805 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04006806
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006807 ret = nfs4_verify_fore_channel_attrs(args, session);
6808 if (ret)
6809 return ret;
6810 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04006811}
6812
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006813static int _nfs4_proc_create_session(struct nfs_client *clp,
6814 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04006815{
6816 struct nfs4_session *session = clp->cl_session;
6817 struct nfs41_create_session_args args = {
6818 .client = clp,
6819 .cb_program = NFS4_CALLBACK,
6820 };
6821 struct nfs41_create_session_res res = {
6822 .client = clp,
6823 };
6824 struct rpc_message msg = {
6825 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
6826 .rpc_argp = &args,
6827 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006828 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04006829 };
6830 int status;
6831
6832 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04006833 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04006834
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006835 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006836 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04006837
Trond Myklebust43095d32012-11-20 11:13:12 -05006838 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04006839 /* Verify the session's negotiated channel_attrs values */
6840 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04006841 /* Increment the clientid slot sequence id */
6842 clp->cl_seqid++;
6843 }
6844
6845 return status;
6846}
6847
6848/*
6849 * Issues a CREATE_SESSION operation to the server.
6850 * It is the responsibility of the caller to verify the session is
6851 * expired before calling this routine.
6852 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006853int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04006854{
6855 int status;
6856 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04006857 struct nfs4_session *session = clp->cl_session;
6858
6859 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
6860
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006861 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04006862 if (status)
6863 goto out;
6864
Andy Adamsonaacd5532011-11-09 13:58:21 -05006865 /* Init or reset the session slot tables */
6866 status = nfs4_setup_session_slot_tables(session);
6867 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04006868 if (status)
6869 goto out;
6870
6871 ptr = (unsigned *)&session->sess_id.data[0];
6872 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
6873 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04006874out:
6875 dprintk("<-- %s\n", __func__);
6876 return status;
6877}
6878
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006879/*
6880 * Issue the over-the-wire RPC DESTROY_SESSION.
6881 * The caller must serialize access to this routine.
6882 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006883int nfs4_proc_destroy_session(struct nfs4_session *session,
6884 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006885{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006886 struct rpc_message msg = {
6887 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
6888 .rpc_argp = session,
6889 .rpc_cred = cred,
6890 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006891 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006892
6893 dprintk("--> nfs4_proc_destroy_session\n");
6894
6895 /* session is still being setup */
6896 if (session->clp->cl_cons_state != NFS_CS_READY)
6897 return status;
6898
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006899 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006900 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006901
6902 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04006903 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006904 "Session has been destroyed regardless...\n", status);
6905
6906 dprintk("<-- nfs4_proc_destroy_session\n");
6907 return status;
6908}
6909
Trond Myklebust7b38c362012-05-23 13:23:31 -04006910/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006911 * Renew the cl_session lease.
6912 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006913struct nfs4_sequence_data {
6914 struct nfs_client *clp;
6915 struct nfs4_sequence_args args;
6916 struct nfs4_sequence_res res;
6917};
6918
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006919static void nfs41_sequence_release(void *data)
6920{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006921 struct nfs4_sequence_data *calldata = data;
6922 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006923
Alexandros Batsakis71358402010-02-05 03:45:05 -08006924 if (atomic_read(&clp->cl_count) > 1)
6925 nfs4_schedule_state_renewal(clp);
6926 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006927 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006928}
6929
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006930static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6931{
6932 switch(task->tk_status) {
6933 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006934 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6935 return -EAGAIN;
6936 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006937 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006938 }
6939 return 0;
6940}
6941
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006942static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006943{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006944 struct nfs4_sequence_data *calldata = data;
6945 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006946
Trond Myklebust14516c32010-07-31 14:29:06 -04006947 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6948 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006949
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006950 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006951 if (task->tk_status < 0) {
6952 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006953 if (atomic_read(&clp->cl_count) == 1)
6954 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006955
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006956 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6957 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006958 return;
6959 }
6960 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006961 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006962out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006963 dprintk("<-- %s\n", __func__);
6964}
6965
6966static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6967{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006968 struct nfs4_sequence_data *calldata = data;
6969 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006970 struct nfs4_sequence_args *args;
6971 struct nfs4_sequence_res *res;
6972
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006973 args = task->tk_msg.rpc_argp;
6974 res = task->tk_msg.rpc_resp;
6975
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006976 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006977}
6978
6979static const struct rpc_call_ops nfs41_sequence_ops = {
6980 .rpc_call_done = nfs41_sequence_call_done,
6981 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006982 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006983};
6984
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006985static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
6986 struct rpc_cred *cred,
6987 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006988{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006989 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006990 struct rpc_message msg = {
6991 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6992 .rpc_cred = cred,
6993 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006994 struct rpc_task_setup task_setup_data = {
6995 .rpc_client = clp->cl_rpcclient,
6996 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006997 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04006998 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006999 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007000
Alexandros Batsakis71358402010-02-05 03:45:05 -08007001 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007002 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04007003 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007004 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08007005 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007006 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007007 }
Chuck Levera9c92d62013-08-09 12:48:18 -04007008 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007009 if (is_privileged)
7010 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007011 msg.rpc_argp = &calldata->args;
7012 msg.rpc_resp = &calldata->res;
7013 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007014 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007015
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007016 return rpc_run_task(&task_setup_data);
7017}
7018
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007019static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007020{
7021 struct rpc_task *task;
7022 int ret = 0;
7023
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007024 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
7025 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007026 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007027 if (IS_ERR(task))
7028 ret = PTR_ERR(task);
7029 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08007030 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007031 dprintk("<-- %s status=%d\n", __func__, ret);
7032 return ret;
7033}
7034
7035static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
7036{
7037 struct rpc_task *task;
7038 int ret;
7039
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007040 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007041 if (IS_ERR(task)) {
7042 ret = PTR_ERR(task);
7043 goto out;
7044 }
7045 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05007046 if (!ret) {
7047 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
7048
7049 if (task->tk_status == 0)
7050 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007051 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05007052 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007053 rpc_put_task(task);
7054out:
7055 dprintk("<-- %s status=%d\n", __func__, ret);
7056 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007057}
7058
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007059struct nfs4_reclaim_complete_data {
7060 struct nfs_client *clp;
7061 struct nfs41_reclaim_complete_args arg;
7062 struct nfs41_reclaim_complete_res res;
7063};
7064
7065static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
7066{
7067 struct nfs4_reclaim_complete_data *calldata = data;
7068
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007069 nfs41_setup_sequence(calldata->clp->cl_session,
7070 &calldata->arg.seq_args,
7071 &calldata->res.seq_res,
7072 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007073}
7074
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007075static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7076{
7077 switch(task->tk_status) {
7078 case 0:
7079 case -NFS4ERR_COMPLETE_ALREADY:
7080 case -NFS4ERR_WRONG_CRED: /* What to do here? */
7081 break;
7082 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007083 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007084 /* fall through */
7085 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007086 return -EAGAIN;
7087 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007088 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007089 }
7090 return 0;
7091}
7092
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007093static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
7094{
7095 struct nfs4_reclaim_complete_data *calldata = data;
7096 struct nfs_client *clp = calldata->clp;
7097 struct nfs4_sequence_res *res = &calldata->res.seq_res;
7098
7099 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007100 if (!nfs41_sequence_done(task, res))
7101 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007102
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007103 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007104 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
7105 rpc_restart_call_prepare(task);
7106 return;
7107 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007108 dprintk("<-- %s\n", __func__);
7109}
7110
7111static void nfs4_free_reclaim_complete_data(void *data)
7112{
7113 struct nfs4_reclaim_complete_data *calldata = data;
7114
7115 kfree(calldata);
7116}
7117
7118static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
7119 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
7120 .rpc_call_done = nfs4_reclaim_complete_done,
7121 .rpc_release = nfs4_free_reclaim_complete_data,
7122};
7123
7124/*
7125 * Issue a global reclaim complete.
7126 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04007127static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
7128 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007129{
7130 struct nfs4_reclaim_complete_data *calldata;
7131 struct rpc_task *task;
7132 struct rpc_message msg = {
7133 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04007134 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007135 };
7136 struct rpc_task_setup task_setup_data = {
7137 .rpc_client = clp->cl_rpcclient,
7138 .rpc_message = &msg,
7139 .callback_ops = &nfs4_reclaim_complete_call_ops,
7140 .flags = RPC_TASK_ASYNC,
7141 };
7142 int status = -ENOMEM;
7143
7144 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04007145 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007146 if (calldata == NULL)
7147 goto out;
7148 calldata->clp = clp;
7149 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007150
Chuck Levera9c92d62013-08-09 12:48:18 -04007151 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007152 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007153 msg.rpc_argp = &calldata->arg;
7154 msg.rpc_resp = &calldata->res;
7155 task_setup_data.callback_data = calldata;
7156 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007157 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007158 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007159 goto out;
7160 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05007161 status = nfs4_wait_for_completion_rpc_task(task);
7162 if (status == 0)
7163 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007164 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007165 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007166out:
7167 dprintk("<-- %s status=%d\n", __func__, status);
7168 return status;
7169}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007170
7171static void
7172nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
7173{
7174 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00007175 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007176 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007177
7178 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00007179 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
7180 * right now covering the LAYOUTGET we are about to send.
7181 * However, that is not so catastrophic, and there seems
7182 * to be no way to prevent it completely.
7183 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007184 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05007185 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007186 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00007187 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
7188 NFS_I(lgp->args.inode)->layout,
7189 lgp->args.ctx->state)) {
7190 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00007191 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007192}
7193
7194static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
7195{
7196 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07007197 struct inode *inode = lgp->args.inode;
7198 struct nfs_server *server = NFS_SERVER(inode);
7199 struct pnfs_layout_hdr *lo;
7200 struct nfs4_state *state = NULL;
Weston Andros Adamson30005122013-02-28 20:30:10 -05007201 unsigned long timeo, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007202
7203 dprintk("--> %s\n", __func__);
7204
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007205 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07007206 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007207
7208 switch (task->tk_status) {
7209 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07007210 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007211 case -NFS4ERR_LAYOUTTRYLATER:
7212 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05007213 timeo = rpc_get_timeout(task->tk_client);
7214 giveup = lgp->args.timestamp + timeo;
7215 if (time_after(giveup, jiffies))
7216 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07007217 break;
7218 case -NFS4ERR_EXPIRED:
7219 case -NFS4ERR_BAD_STATEID:
7220 spin_lock(&inode->i_lock);
7221 lo = NFS_I(inode)->layout;
7222 if (!lo || list_empty(&lo->plh_segs)) {
7223 spin_unlock(&inode->i_lock);
7224 /* If the open stateid was bad, then recover it. */
7225 state = lgp->args.ctx->state;
7226 } else {
7227 LIST_HEAD(head);
7228
7229 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
7230 spin_unlock(&inode->i_lock);
7231 /* Mark the bad layout state as invalid, then
7232 * retry using the open stateid. */
7233 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007234 }
7235 }
Trond Myklebustee314c22012-10-01 17:25:48 -07007236 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
7237 rpc_restart_call_prepare(task);
7238out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007239 dprintk("<-- %s\n", __func__);
7240}
7241
Idan Kedar85541162012-08-02 11:47:10 +03007242static size_t max_response_pages(struct nfs_server *server)
7243{
7244 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
7245 return nfs_page_array_len(0, max_resp_sz);
7246}
7247
7248static void nfs4_free_pages(struct page **pages, size_t size)
7249{
7250 int i;
7251
7252 if (!pages)
7253 return;
7254
7255 for (i = 0; i < size; i++) {
7256 if (!pages[i])
7257 break;
7258 __free_page(pages[i]);
7259 }
7260 kfree(pages);
7261}
7262
7263static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
7264{
7265 struct page **pages;
7266 int i;
7267
7268 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
7269 if (!pages) {
7270 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
7271 return NULL;
7272 }
7273
7274 for (i = 0; i < size; i++) {
7275 pages[i] = alloc_page(gfp_flags);
7276 if (!pages[i]) {
7277 dprintk("%s: failed to allocate page\n", __func__);
7278 nfs4_free_pages(pages, size);
7279 return NULL;
7280 }
7281 }
7282
7283 return pages;
7284}
7285
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007286static void nfs4_layoutget_release(void *calldata)
7287{
7288 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007289 struct inode *inode = lgp->args.inode;
7290 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007291 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007292
7293 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03007294 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007295 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007296 put_nfs_open_context(lgp->args.ctx);
7297 kfree(calldata);
7298 dprintk("<-- %s\n", __func__);
7299}
7300
7301static const struct rpc_call_ops nfs4_layoutget_call_ops = {
7302 .rpc_call_prepare = nfs4_layoutget_prepare,
7303 .rpc_call_done = nfs4_layoutget_done,
7304 .rpc_release = nfs4_layoutget_release,
7305};
7306
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007307struct pnfs_layout_segment *
7308nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007309{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007310 struct inode *inode = lgp->args.inode;
7311 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007312 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007313 struct rpc_task *task;
7314 struct rpc_message msg = {
7315 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
7316 .rpc_argp = &lgp->args,
7317 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04007318 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007319 };
7320 struct rpc_task_setup task_setup_data = {
7321 .rpc_client = server->client,
7322 .rpc_message = &msg,
7323 .callback_ops = &nfs4_layoutget_call_ops,
7324 .callback_data = lgp,
7325 .flags = RPC_TASK_ASYNC,
7326 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007327 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007328 int status = 0;
7329
7330 dprintk("--> %s\n", __func__);
7331
Idan Kedar85541162012-08-02 11:47:10 +03007332 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
7333 if (!lgp->args.layout.pages) {
7334 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007335 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03007336 }
7337 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05007338 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03007339
Weston Andros Adamson35124a02011-03-24 16:48:21 -04007340 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007341 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04007342 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007343
7344 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
7345 pnfs_get_layout_hdr(NFS_I(inode)->layout);
7346
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007347 task = rpc_run_task(&task_setup_data);
7348 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007349 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007350 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00007351 if (status == 0)
7352 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007353 trace_nfs4_layoutget(lgp->args.ctx,
7354 &lgp->args.range,
7355 &lgp->res.range,
7356 status);
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05007357 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
7358 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007359 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007360 rpc_put_task(task);
7361 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007362 if (status)
7363 return ERR_PTR(status);
7364 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007365}
7366
Benny Halevycbe82602011-05-22 19:52:37 +03007367static void
7368nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
7369{
7370 struct nfs4_layoutreturn *lrp = calldata;
7371
7372 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007373 nfs41_setup_sequence(lrp->clp->cl_session,
7374 &lrp->args.seq_args,
7375 &lrp->res.seq_res,
7376 task);
Benny Halevycbe82602011-05-22 19:52:37 +03007377}
7378
7379static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
7380{
7381 struct nfs4_layoutreturn *lrp = calldata;
7382 struct nfs_server *server;
7383
7384 dprintk("--> %s\n", __func__);
7385
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007386 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03007387 return;
7388
7389 server = NFS_SERVER(lrp->args.inode);
7390 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007391 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03007392 return;
7393 }
Benny Halevycbe82602011-05-22 19:52:37 +03007394 dprintk("<-- %s\n", __func__);
7395}
7396
7397static void nfs4_layoutreturn_release(void *calldata)
7398{
7399 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04007400 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03007401
7402 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04007403 spin_lock(&lo->plh_inode->i_lock);
7404 if (lrp->res.lrs_present)
7405 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
7406 lo->plh_block_lgets--;
7407 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04007408 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03007409 kfree(calldata);
7410 dprintk("<-- %s\n", __func__);
7411}
7412
7413static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
7414 .rpc_call_prepare = nfs4_layoutreturn_prepare,
7415 .rpc_call_done = nfs4_layoutreturn_done,
7416 .rpc_release = nfs4_layoutreturn_release,
7417};
7418
7419int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
7420{
7421 struct rpc_task *task;
7422 struct rpc_message msg = {
7423 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
7424 .rpc_argp = &lrp->args,
7425 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04007426 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03007427 };
7428 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04007429 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03007430 .rpc_message = &msg,
7431 .callback_ops = &nfs4_layoutreturn_call_ops,
7432 .callback_data = lrp,
7433 };
7434 int status;
7435
7436 dprintk("--> %s\n", __func__);
Chuck Levera9c92d62013-08-09 12:48:18 -04007437 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03007438 task = rpc_run_task(&task_setup_data);
7439 if (IS_ERR(task))
7440 return PTR_ERR(task);
7441 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007442 trace_nfs4_layoutreturn(lrp->args.inode, status);
Benny Halevycbe82602011-05-22 19:52:37 +03007443 dprintk("<-- %s status=%d\n", __func__, status);
7444 rpc_put_task(task);
7445 return status;
7446}
7447
Andy Adamson7f11d8d2011-07-30 20:52:35 -04007448/*
7449 * Retrieve the list of Data Server devices from the MDS.
7450 */
7451static int _nfs4_getdevicelist(struct nfs_server *server,
7452 const struct nfs_fh *fh,
7453 struct pnfs_devicelist *devlist)
7454{
7455 struct nfs4_getdevicelist_args args = {
7456 .fh = fh,
7457 .layoutclass = server->pnfs_curr_ld->id,
7458 };
7459 struct nfs4_getdevicelist_res res = {
7460 .devlist = devlist,
7461 };
7462 struct rpc_message msg = {
7463 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
7464 .rpc_argp = &args,
7465 .rpc_resp = &res,
7466 };
7467 int status;
7468
7469 dprintk("--> %s\n", __func__);
7470 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
7471 &res.seq_res, 0);
7472 dprintk("<-- %s status=%d\n", __func__, status);
7473 return status;
7474}
7475
7476int nfs4_proc_getdevicelist(struct nfs_server *server,
7477 const struct nfs_fh *fh,
7478 struct pnfs_devicelist *devlist)
7479{
7480 struct nfs4_exception exception = { };
7481 int err;
7482
7483 do {
7484 err = nfs4_handle_exception(server,
7485 _nfs4_getdevicelist(server, fh, devlist),
7486 &exception);
7487 } while (exception.retry);
7488
7489 dprintk("%s: err=%d, num_devs=%u\n", __func__,
7490 err, devlist->num_devs);
7491
7492 return err;
7493}
7494EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
7495
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007496static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007497_nfs4_proc_getdeviceinfo(struct nfs_server *server,
7498 struct pnfs_device *pdev,
7499 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007500{
7501 struct nfs4_getdeviceinfo_args args = {
7502 .pdev = pdev,
7503 };
7504 struct nfs4_getdeviceinfo_res res = {
7505 .pdev = pdev,
7506 };
7507 struct rpc_message msg = {
7508 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
7509 .rpc_argp = &args,
7510 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007511 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007512 };
7513 int status;
7514
7515 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00007516 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007517 dprintk("<-- %s status=%d\n", __func__, status);
7518
7519 return status;
7520}
7521
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007522int nfs4_proc_getdeviceinfo(struct nfs_server *server,
7523 struct pnfs_device *pdev,
7524 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007525{
7526 struct nfs4_exception exception = { };
7527 int err;
7528
7529 do {
7530 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007531 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007532 &exception);
7533 } while (exception.retry);
7534 return err;
7535}
7536EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
7537
Andy Adamson863a3c62011-03-23 13:27:54 +00007538static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
7539{
7540 struct nfs4_layoutcommit_data *data = calldata;
7541 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007542 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00007543
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007544 nfs41_setup_sequence(session,
7545 &data->args.seq_args,
7546 &data->res.seq_res,
7547 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00007548}
7549
7550static void
7551nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
7552{
7553 struct nfs4_layoutcommit_data *data = calldata;
7554 struct nfs_server *server = NFS_SERVER(data->args.inode);
7555
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007556 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00007557 return;
7558
7559 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007560 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
7561 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
7562 case -NFS4ERR_BADLAYOUT: /* no layout */
7563 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00007564 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007565 break;
7566 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00007567 nfs_post_op_update_inode_force_wcc(data->args.inode,
7568 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007569 break;
7570 default:
7571 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
7572 rpc_restart_call_prepare(task);
7573 return;
7574 }
7575 }
Andy Adamson863a3c62011-03-23 13:27:54 +00007576}
7577
7578static void nfs4_layoutcommit_release(void *calldata)
7579{
7580 struct nfs4_layoutcommit_data *data = calldata;
7581
Andy Adamsondb29c082011-07-30 20:52:38 -04007582 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00007583 put_rpccred(data->cred);
7584 kfree(data);
7585}
7586
7587static const struct rpc_call_ops nfs4_layoutcommit_ops = {
7588 .rpc_call_prepare = nfs4_layoutcommit_prepare,
7589 .rpc_call_done = nfs4_layoutcommit_done,
7590 .rpc_release = nfs4_layoutcommit_release,
7591};
7592
7593int
Andy Adamsonef311532011-03-12 02:58:10 -05007594nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00007595{
7596 struct rpc_message msg = {
7597 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
7598 .rpc_argp = &data->args,
7599 .rpc_resp = &data->res,
7600 .rpc_cred = data->cred,
7601 };
7602 struct rpc_task_setup task_setup_data = {
7603 .task = &data->task,
7604 .rpc_client = NFS_CLIENT(data->args.inode),
7605 .rpc_message = &msg,
7606 .callback_ops = &nfs4_layoutcommit_ops,
7607 .callback_data = data,
7608 .flags = RPC_TASK_ASYNC,
7609 };
7610 struct rpc_task *task;
7611 int status = 0;
7612
7613 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
7614 "lbw: %llu inode %lu\n",
7615 data->task.tk_pid, sync,
7616 data->args.lastbytewritten,
7617 data->args.inode->i_ino);
7618
Chuck Levera9c92d62013-08-09 12:48:18 -04007619 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00007620 task = rpc_run_task(&task_setup_data);
7621 if (IS_ERR(task))
7622 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05007623 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00007624 goto out;
7625 status = nfs4_wait_for_completion_rpc_task(task);
7626 if (status != 0)
7627 goto out;
7628 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007629 trace_nfs4_layoutcommit(data->args.inode, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00007630out:
7631 dprintk("%s: status %d\n", __func__, status);
7632 rpc_put_task(task);
7633 return status;
7634}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007635
Andy Adamson97431202013-08-08 10:57:56 -04007636/**
7637 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
7638 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
7639 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007640static int
7641_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007642 struct nfs_fsinfo *info,
7643 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007644{
7645 struct nfs41_secinfo_no_name_args args = {
7646 .style = SECINFO_STYLE_CURRENT_FH,
7647 };
7648 struct nfs4_secinfo_res res = {
7649 .flavors = flavors,
7650 };
7651 struct rpc_message msg = {
7652 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
7653 .rpc_argp = &args,
7654 .rpc_resp = &res,
7655 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007656 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007657 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007658 int status;
7659
7660 if (use_integrity) {
7661 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007662 cred = nfs4_get_clid_cred(server->nfs_client);
7663 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007664 }
7665
7666 dprintk("--> %s\n", __func__);
7667 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
7668 &res.seq_res, 0);
7669 dprintk("<-- %s status=%d\n", __func__, status);
7670
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007671 if (cred)
7672 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007673
7674 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007675}
7676
7677static int
7678nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7679 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
7680{
7681 struct nfs4_exception exception = { };
7682 int err;
7683 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007684 /* first try using integrity protection */
7685 err = -NFS4ERR_WRONGSEC;
7686
7687 /* try to use integrity protection with machine cred */
7688 if (_nfs4_is_integrity_protected(server->nfs_client))
7689 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
7690 flavors, true);
7691
7692 /*
7693 * if unable to use integrity protection, or SECINFO with
7694 * integrity protection returns NFS4ERR_WRONGSEC (which is
7695 * disallowed by spec, but exists in deployed servers) use
7696 * the current filesystem's rpc_client and the user cred.
7697 */
7698 if (err == -NFS4ERR_WRONGSEC)
7699 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
7700 flavors, false);
7701
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007702 switch (err) {
7703 case 0:
7704 case -NFS4ERR_WRONGSEC:
7705 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007706 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007707 default:
7708 err = nfs4_handle_exception(server, err, &exception);
7709 }
7710 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007711out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007712 return err;
7713}
7714
7715static int
7716nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
7717 struct nfs_fsinfo *info)
7718{
7719 int err;
7720 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04007721 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007722 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04007723 struct nfs4_secinfo4 *secinfo;
7724 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007725
7726 page = alloc_page(GFP_KERNEL);
7727 if (!page) {
7728 err = -ENOMEM;
7729 goto out;
7730 }
7731
7732 flavors = page_address(page);
7733 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
7734
7735 /*
7736 * Fall back on "guess and check" method if
7737 * the server doesn't support SECINFO_NO_NAME
7738 */
7739 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
7740 err = nfs4_find_root_sec(server, fhandle, info);
7741 goto out_freepage;
7742 }
7743 if (err)
7744 goto out_freepage;
7745
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04007746 for (i = 0; i < flavors->num_flavors; i++) {
7747 secinfo = &flavors->flavors[i];
7748
7749 switch (secinfo->flavor) {
7750 case RPC_AUTH_NULL:
7751 case RPC_AUTH_UNIX:
7752 case RPC_AUTH_GSS:
7753 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
7754 &secinfo->flavor_info);
7755 break;
7756 default:
7757 flavor = RPC_AUTH_MAXFLAVOR;
7758 break;
7759 }
7760
7761 if (flavor != RPC_AUTH_MAXFLAVOR) {
7762 err = nfs4_lookup_root_sec(server, fhandle,
7763 info, flavor);
7764 if (!err)
7765 break;
7766 }
7767 }
7768
7769 if (flavor == RPC_AUTH_MAXFLAVOR)
7770 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007771
7772out_freepage:
7773 put_page(page);
7774 if (err == -EACCES)
7775 return -EPERM;
7776out:
7777 return err;
7778}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007779
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007780static int _nfs41_test_stateid(struct nfs_server *server,
7781 nfs4_stateid *stateid,
7782 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04007783{
7784 int status;
7785 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007786 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04007787 };
7788 struct nfs41_test_stateid_res res;
7789 struct rpc_message msg = {
7790 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
7791 .rpc_argp = &args,
7792 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007793 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04007794 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007795 struct rpc_clnt *rpc_client = server->client;
7796
7797 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
7798 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007799
Chuck Lever38527b12012-07-11 16:30:23 -04007800 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04007801 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007802 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007803 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007804 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04007805 if (status != NFS_OK) {
7806 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04007807 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04007808 }
7809 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04007810 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04007811}
7812
Chuck Lever38527b12012-07-11 16:30:23 -04007813/**
7814 * nfs41_test_stateid - perform a TEST_STATEID operation
7815 *
7816 * @server: server / transport on which to perform the operation
7817 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007818 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04007819 *
7820 * Returns NFS_OK if the server recognizes that "stateid" is valid.
7821 * Otherwise a negative NFS4ERR value is returned if the operation
7822 * failed or the state ID is not currently valid.
7823 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007824static int nfs41_test_stateid(struct nfs_server *server,
7825 nfs4_stateid *stateid,
7826 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04007827{
7828 struct nfs4_exception exception = { };
7829 int err;
7830 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007831 err = _nfs41_test_stateid(server, stateid, cred);
Chuck Lever377e5072012-07-11 16:29:45 -04007832 if (err != -NFS4ERR_DELAY)
7833 break;
7834 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04007835 } while (exception.retry);
7836 return err;
7837}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007838
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007839struct nfs_free_stateid_data {
7840 struct nfs_server *server;
7841 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007842 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007843};
7844
7845static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
7846{
7847 struct nfs_free_stateid_data *data = calldata;
7848 nfs41_setup_sequence(nfs4_get_session(data->server),
7849 &data->args.seq_args,
7850 &data->res.seq_res,
7851 task);
7852}
7853
7854static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
7855{
7856 struct nfs_free_stateid_data *data = calldata;
7857
7858 nfs41_sequence_done(task, &data->res.seq_res);
7859
7860 switch (task->tk_status) {
7861 case -NFS4ERR_DELAY:
7862 if (nfs4_async_handle_error(task, data->server, NULL) == -EAGAIN)
7863 rpc_restart_call_prepare(task);
7864 }
7865}
7866
7867static void nfs41_free_stateid_release(void *calldata)
7868{
7869 kfree(calldata);
7870}
7871
Trond Myklebust17f26b12013-08-21 15:48:42 -04007872static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007873 .rpc_call_prepare = nfs41_free_stateid_prepare,
7874 .rpc_call_done = nfs41_free_stateid_done,
7875 .rpc_release = nfs41_free_stateid_release,
7876};
7877
7878static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
7879 nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007880 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007881 bool privileged)
7882{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007883 struct rpc_message msg = {
7884 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007885 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007886 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007887 struct rpc_task_setup task_setup = {
7888 .rpc_client = server->client,
7889 .rpc_message = &msg,
7890 .callback_ops = &nfs41_free_stateid_ops,
7891 .flags = RPC_TASK_ASYNC,
7892 };
7893 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007894
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007895 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
7896 &task_setup.rpc_client, &msg);
7897
Chuck Lever38527b12012-07-11 16:30:23 -04007898 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007899 data = kmalloc(sizeof(*data), GFP_NOFS);
7900 if (!data)
7901 return ERR_PTR(-ENOMEM);
7902 data->server = server;
7903 nfs4_stateid_copy(&data->args.stateid, stateid);
7904
7905 task_setup.callback_data = data;
7906
7907 msg.rpc_argp = &data->args;
7908 msg.rpc_resp = &data->res;
Chuck Levera9c92d62013-08-09 12:48:18 -04007909 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007910 if (privileged)
7911 nfs4_set_sequence_privileged(&data->args.seq_args);
7912
7913 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007914}
7915
Chuck Lever38527b12012-07-11 16:30:23 -04007916/**
7917 * nfs41_free_stateid - perform a FREE_STATEID operation
7918 *
7919 * @server: server / transport on which to perform the operation
7920 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007921 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04007922 *
7923 * Returns NFS_OK if the server freed "stateid". Otherwise a
7924 * negative NFS4ERR value is returned.
7925 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007926static int nfs41_free_stateid(struct nfs_server *server,
7927 nfs4_stateid *stateid,
7928 struct rpc_cred *cred)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007929{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007930 struct rpc_task *task;
7931 int ret;
7932
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007933 task = _nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007934 if (IS_ERR(task))
7935 return PTR_ERR(task);
7936 ret = rpc_wait_for_completion_task(task);
7937 if (!ret)
7938 ret = task->tk_status;
7939 rpc_put_task(task);
7940 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007941}
Trond Myklebust36281ca2012-03-04 18:13:56 -05007942
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007943static int nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
7944{
7945 struct rpc_task *task;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007946 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007947
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007948 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007949 nfs4_free_lock_state(server, lsp);
7950 if (IS_ERR(task))
7951 return PTR_ERR(task);
7952 rpc_put_task(task);
7953 return 0;
7954}
7955
Trond Myklebust36281ca2012-03-04 18:13:56 -05007956static bool nfs41_match_stateid(const nfs4_stateid *s1,
7957 const nfs4_stateid *s2)
7958{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007959 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007960 return false;
7961
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007962 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007963 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007964 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007965 return true;
7966
7967 return false;
7968}
7969
Andy Adamson557134a2009-04-01 09:21:53 -04007970#endif /* CONFIG_NFS_V4_1 */
7971
Trond Myklebust36281ca2012-03-04 18:13:56 -05007972static bool nfs4_match_stateid(const nfs4_stateid *s1,
7973 const nfs4_stateid *s2)
7974{
Trond Myklebustf597c532012-03-04 18:13:56 -05007975 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05007976}
7977
7978
Trond Myklebust17280172012-03-11 13:11:00 -04007979static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007980 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007981 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007982 .recover_open = nfs4_open_reclaim,
7983 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04007984 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04007985 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007986};
7987
Andy Adamson591d71c2009-04-01 09:22:47 -04007988#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007989static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007990 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
7991 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
7992 .recover_open = nfs4_open_reclaim,
7993 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05007994 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007995 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04007996 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04007997};
7998#endif /* CONFIG_NFS_V4_1 */
7999
Trond Myklebust17280172012-03-11 13:11:00 -04008000static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008001 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008002 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008003 .recover_open = nfs4_open_expired,
8004 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04008005 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008006};
8007
Andy Adamson591d71c2009-04-01 09:22:47 -04008008#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008009static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008010 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8011 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04008012 .recover_open = nfs41_open_expired,
8013 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05008014 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04008015};
8016#endif /* CONFIG_NFS_V4_1 */
8017
Trond Myklebust17280172012-03-11 13:11:00 -04008018static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008019 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04008020 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008021 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04008022};
8023
8024#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008025static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008026 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04008027 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008028 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04008029};
8030#endif
8031
Chuck Leverec011fe2013-10-17 14:12:39 -04008032static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008033 .get_locations = _nfs40_proc_get_locations,
Chuck Leverec011fe2013-10-17 14:12:39 -04008034};
8035
8036#if defined(CONFIG_NFS_V4_1)
8037static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008038 .get_locations = _nfs41_proc_get_locations,
Chuck Leverec011fe2013-10-17 14:12:39 -04008039};
8040#endif /* CONFIG_NFS_V4_1 */
8041
Trond Myklebust97dc1352010-06-16 09:52:26 -04008042static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
8043 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008044 .init_caps = NFS_CAP_READDIRPLUS
8045 | NFS_CAP_ATOMIC_OPEN
8046 | NFS_CAP_CHANGE_ATTR
8047 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008048 .init_client = nfs40_init_client,
8049 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008050 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008051 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008052 .free_lock_state = nfs4_release_lockowner,
Chuck Lever9915ea72013-08-09 12:48:27 -04008053 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008054 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
8055 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
8056 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008057 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008058};
8059
8060#if defined(CONFIG_NFS_V4_1)
8061static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
8062 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008063 .init_caps = NFS_CAP_READDIRPLUS
8064 | NFS_CAP_ATOMIC_OPEN
8065 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04008066 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04008067 | NFS_CAP_STATEID_NFSV41
8068 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008069 .init_client = nfs41_init_client,
8070 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008071 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008072 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008073 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04008074 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008075 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8076 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8077 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008078 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008079};
8080#endif
8081
Steve Dickson42c2c422013-05-22 12:50:38 -04008082#if defined(CONFIG_NFS_V4_2)
8083static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
8084 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04008085 .init_caps = NFS_CAP_READDIRPLUS
8086 | NFS_CAP_ATOMIC_OPEN
8087 | NFS_CAP_CHANGE_ATTR
8088 | NFS_CAP_POSIX_LOCK
8089 | NFS_CAP_STATEID_NFSV41
8090 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008091 .init_client = nfs41_init_client,
8092 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04008093 .match_stateid = nfs41_match_stateid,
8094 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04008095 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04008096 .call_sync_ops = &nfs41_call_sync_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04008097 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8098 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8099 .state_renewal_ops = &nfs41_state_renewal_ops,
8100};
8101#endif
8102
Trond Myklebust97dc1352010-06-16 09:52:26 -04008103const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
8104 [0] = &nfs_v4_0_minor_ops,
8105#if defined(CONFIG_NFS_V4_1)
8106 [1] = &nfs_v4_1_minor_ops,
8107#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04008108#if defined(CONFIG_NFS_V4_2)
8109 [2] = &nfs_v4_2_minor_ops,
8110#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04008111};
8112
Trond Myklebust17f26b12013-08-21 15:48:42 -04008113static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04008114 .create = nfs_create,
8115 .lookup = nfs_lookup,
8116 .atomic_open = nfs_atomic_open,
8117 .link = nfs_link,
8118 .unlink = nfs_unlink,
8119 .symlink = nfs_symlink,
8120 .mkdir = nfs_mkdir,
8121 .rmdir = nfs_rmdir,
8122 .mknod = nfs_mknod,
8123 .rename = nfs_rename,
8124 .permission = nfs_permission,
8125 .getattr = nfs_getattr,
8126 .setattr = nfs_setattr,
8127 .getxattr = generic_getxattr,
8128 .setxattr = generic_setxattr,
8129 .listxattr = generic_listxattr,
8130 .removexattr = generic_removexattr,
8131};
8132
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08008133static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008134 .permission = nfs_permission,
8135 .getattr = nfs_getattr,
8136 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008137 .getxattr = generic_getxattr,
8138 .setxattr = generic_setxattr,
8139 .listxattr = generic_listxattr,
8140 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008141};
8142
David Howells509de812006-08-22 20:06:11 -04008143const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008144 .version = 4, /* protocol version */
8145 .dentry_ops = &nfs4_dentry_operations,
8146 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008147 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04008148 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008149 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04008150 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04008151 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008152 .getattr = nfs4_proc_getattr,
8153 .setattr = nfs4_proc_setattr,
8154 .lookup = nfs4_proc_lookup,
8155 .access = nfs4_proc_access,
8156 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008157 .create = nfs4_proc_create,
8158 .remove = nfs4_proc_remove,
8159 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04008160 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008161 .unlink_done = nfs4_proc_unlink_done,
8162 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04008163 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04008164 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04008165 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008166 .link = nfs4_proc_link,
8167 .symlink = nfs4_proc_symlink,
8168 .mkdir = nfs4_proc_mkdir,
8169 .rmdir = nfs4_proc_remove,
8170 .readdir = nfs4_proc_readdir,
8171 .mknod = nfs4_proc_mknod,
8172 .statfs = nfs4_proc_statfs,
8173 .fsinfo = nfs4_proc_fsinfo,
8174 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04008175 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008176 .decode_dirent = nfs4_decode_dirent,
8177 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04008178 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04008179 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05008180 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008181 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04008182 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04008183 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008184 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008185 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04008186 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008187 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008188 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00008189 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04008190 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04008191 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04008192 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04008193 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04008194 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00008195 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04008196 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04008197 .create_server = nfs4_create_server,
8198 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008199};
8200
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008201static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
8202 .prefix = XATTR_NAME_NFSV4_ACL,
8203 .list = nfs4_xattr_list_nfs4_acl,
8204 .get = nfs4_xattr_get_nfs4_acl,
8205 .set = nfs4_xattr_set_nfs4_acl,
8206};
8207
8208const struct xattr_handler *nfs4_xattr_handlers[] = {
8209 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04008210#ifdef CONFIG_NFS_V4_SECURITY_LABEL
8211 &nfs4_xattr_nfs4_label_handler,
8212#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008213 NULL
8214};
8215
Linus Torvalds1da177e2005-04-16 15:20:36 -07008216/*
8217 * Local variables:
8218 * c-basic-offset: 8
8219 * End:
8220 */